From e9667130f37e2d51609f39a46f09d09ae99b53a3 Mon Sep 17 00:00:00 2001 From: JannisX11 Date: Sat, 20 Apr 2024 19:46:47 +0200 Subject: [PATCH] Fix #2065 Long sound keyframe can desync --- js/animations/timeline_animators.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/js/animations/timeline_animators.js b/js/animations/timeline_animators.js index 6ef0c26fa..53ea56585 100644 --- a/js/animations/timeline_animators.js +++ b/js/animations/timeline_animators.js @@ -745,10 +745,14 @@ class EffectAnimator extends GeneralAnimator { displayFrame(in_loop) { if (in_loop && !this.muted.sound) { this.sound.forEach(kf => { - var diff = kf.time - this.animation.time; - if (diff >= 0 && diff < (1/60) * (Timeline.playback_speed/100)) { + let diff = this.animation.time - kf.time; + if (diff < 0) return; + + let media = Timeline.playing_sounds.find(s => s.keyframe_id == kf.uuid); + if (diff >= 0 && diff < (1/60) * (Timeline.playback_speed/100) && !media) { if (kf.data_points[0].file && !kf.cooldown) { - var media = new Audio(kf.data_points[0].file); + media = new Audio(kf.data_points[0].file); + media.keyframe_id = kf.uuid; media.playbackRate = Math.clamp(Timeline.playback_speed/100, 0.1, 4.0); media.volume = Math.clamp(settings.volume.value/100, 0, 1); media.play().catch(() => {}); @@ -762,6 +766,14 @@ class EffectAnimator extends GeneralAnimator { delete kf.cooldown; }, 400) } + } else if (diff > 0) { + media = Timeline.playing_sounds.find(s => s.keyframe_id == kf.uuid); + if (Math.abs(media.currentTime - diff) > 0.08) { + console.log('Resyncing sound') + // Resync + media.currentTime = diff; + media.playbackRate = Math.clamp(Timeline.playback_speed/100, 0.1, 4.0); + } } }) } @@ -834,6 +846,7 @@ class EffectAnimator extends GeneralAnimator { media.playbackRate = Math.clamp(Timeline.playback_speed/100, 0.1, 4.0); media.volume = Math.clamp(settings.volume.value/100, 0, 1); media.currentTime = -diff; + media.keyframe_id = kf.uuid; media.play().catch(() => {}); Timeline.playing_sounds.push(media); media.onended = function() {