diff --git a/src/js/index.js b/src/js/index.js index 8a815ed..f203a38 100644 --- a/src/js/index.js +++ b/src/js/index.js @@ -273,7 +273,7 @@ const puppetShow = new PuppetShow({ audioContext }); -const sfx = []; +const sfx = new Map(); [ 'sounds/bark.wav', 'sounds/laugh.wav' @@ -284,7 +284,7 @@ const sfx = []; context: audioContext, name }); - sfx.push(effect); + sfx.set(name, effect); }); @@ -491,6 +491,25 @@ puppetShow puppet.visible = true; return; } + + if (event.type === 'sound') { + const name = event.params.name; + const time = event.time; + const duration = event.duration; + const currentTime = puppetShow.currentTime; + + const timeLeft = Math.max(0, time + duration - currentTime); + if (timeLeft > 0) { + const effect = sfx.get(name); + if (!effect) { + console.warn('Unknown sound effect', name, event); + return; + } + + effect.play(Math.max(0, currentTime - time)); + } + return; + } }); // Request animation frame loop function @@ -553,7 +572,7 @@ function animate(timestamp) { } }); - if (!isRecording) { + if (!isRecording || puppetShow.playing) { puppetShow.update(); } diff --git a/src/js/sound-effect.js b/src/js/sound-effect.js index 25c62cc..5b3e47c 100644 --- a/src/js/sound-effect.js +++ b/src/js/sound-effect.js @@ -43,13 +43,13 @@ function SoundEffect(options) { } }; - this.play = () => { + this.play = offset => { if (buffer) { const source = context.createBufferSource(); source.buffer = buffer; source.connect(context.destination); source.addEventListener('ended', stopEvent); - source.start(0); + source.start(0, offset || 0); sources.push(source); } };