Skip to content
This repository has been archived by the owner on May 21, 2019. It is now read-only.

Commit

Permalink
Play back sound effect cues #10, #12
Browse files Browse the repository at this point in the history
  • Loading branch information
brianchirls committed Mar 8, 2017
1 parent bedebba commit 0862f33
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
25 changes: 22 additions & 3 deletions src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ const puppetShow = new PuppetShow({
audioContext
});

const sfx = [];
const sfx = new Map();
[
'sounds/bark.wav',
'sounds/laugh.wav'
Expand All @@ -284,7 +284,7 @@ const sfx = [];
context: audioContext,
name
});
sfx.push(effect);
sfx.set(name, effect);
});


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -553,7 +572,7 @@ function animate(timestamp) {
}
});

if (!isRecording) {
if (!isRecording || puppetShow.playing) {
puppetShow.update();
}

Expand Down
4 changes: 2 additions & 2 deletions src/js/sound-effect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
Expand Down

0 comments on commit 0862f33

Please sign in to comment.