From 3900ac3f94b4e96ab524d7ada928c572fdb8d1cb Mon Sep 17 00:00:00 2001
From: JannisX11 <jannis4236@gmail.com>
Date: Sun, 27 Sep 2020 18:16:13 +0200
Subject: [PATCH] Fix keyframe waveforms after data point changes

---
 index.html                 |  4 ++--
 js/animations/animation.js | 34 +++++++++++++++++-----------------
 js/animations/keyframe.js  |  2 +-
 3 files changed, 20 insertions(+), 20 deletions(-)

diff --git a/index.html b/index.html
index 898f3dc6..1e8ffe8f 100644
--- a/index.html
+++ b/index.html
@@ -892,8 +892,8 @@
 									>
 										<i class="material-icons keyframe_icon_smaller" v-if="keyframe.interpolation == 'catmullrom'">lens</i>
 										<i class="material-icons" v-else>stop</i>
-										<svg class="keyframe_waveform" v-if="keyframe.channel == 'sound' && keyframe.file && waveforms[keyframe.file]" :style="{width: waveforms[keyframe.file].duration * size}">
-											<polygon :points="getWaveformPoints(waveforms[keyframe.file].samples, size)"></polygon>
+										<svg class="keyframe_waveform" v-if="keyframe.channel == 'sound' && keyframe.data_points[0].file && waveforms[keyframe.data_points[0].file]" :style="{width: waveforms[keyframe.data_points[0].file].duration * size}">
+											<polygon :points="getWaveformPoints(waveforms[keyframe.data_points[0].file].samples, size)"></polygon>
 										</svg>
 									</keyframe>
 								</div>
diff --git a/js/animations/animation.js b/js/animations/animation.js
index 25af2a45..a9f7b984 100644
--- a/js/animations/animation.js
+++ b/js/animations/animation.js
@@ -970,20 +970,20 @@ class EffectAnimator extends GeneralAnimator {
 			this.sound.forEach(kf => {
 				var diff = kf.time - Timeline.time;
 				if (diff >= 0 && diff < (1/60) * (Timeline.playback_speed/100)) {
-					if (kf.file && !kf.cooldown) {
-						 var media = new Audio(kf.file);
-						 window._media = media
-						 media.volume = Math.clamp(settings.volume.value/100, 0, 1);
-						 media.play();
-						 Timeline.playing_sounds.push(media);
-						 media.onended = function() {
-						 	Timeline.playing_sounds.remove(media);
-						 }
+					if (kf.data_points[0]?.file && !kf.cooldown) {
+						var media = new Audio(kf.data_points[0]?.file);
+						window._media = media
+						media.volume = Math.clamp(settings.volume.value/100, 0, 1);
+						media.play().catch(() => {});
+						Timeline.playing_sounds.push(media);
+						media.onended = function() {
+							Timeline.playing_sounds.remove(media);
+						}
 
-						 kf.cooldown = true;
-						 setTimeout(() => {
-						 	delete kf.cooldown;
-						 }, 400)
+						kf.cooldown = true;
+						setTimeout(() => {
+							delete kf.cooldown;
+						}, 400)
 					} 
 				}
 			})
@@ -992,14 +992,14 @@ class EffectAnimator extends GeneralAnimator {
 	startPreviousSounds() {
 		if (!this.muted.sound) {
 			this.sound.forEach(kf => {
-				if (kf.file && !kf.cooldown) {
+				if (kf.data_points[0]?.file && !kf.cooldown) {
 					var diff = kf.time - Timeline.time;
-					if (diff < 0 && Timeline.waveforms[kf.file] && Timeline.waveforms[kf.file].duration > -diff) {
-						var media = new Audio(kf.file);
+					if (diff < 0 && Timeline.waveforms[kf.data_points[0]?.file] && Timeline.waveforms[kf.data_points[0]?.file].duration > -diff) {
+						var media = new Audio(kf.data_points[0]?.file);
 						window._media = media
 						media.volume = Math.clamp(settings.volume.value/100, 0, 1);
 						media.currentTime = -diff;
-						media.play();
+						media.play().catch(() => {});
 						Timeline.playing_sounds.push(media);
 						media.onended = function() {
 							Timeline.playing_sounds.remove(media);
diff --git a/js/animations/keyframe.js b/js/animations/keyframe.js
index 7da6a24c..73b8a981 100644
--- a/js/animations/keyframe.js
+++ b/js/animations/keyframe.js
@@ -671,7 +671,7 @@ BARS.defineActions(function() {
 				resource_id: 'animation_audio',
 				extensions: ['ogg'],
 				type: 'Audio File',
-				startpath: Timeline.selected[0].file
+				startpath: Timeline.selected[0].data_points[0]?.file
 			}, function(files) {
 
 				// Todo: move to panel