diff --git a/src/SoundPlayer.js b/src/SoundPlayer.js new file mode 100644 index 0000000..ac24e5c --- /dev/null +++ b/src/SoundPlayer.js @@ -0,0 +1,37 @@ +var Tone = require('tone'); + +function SoundPlayer (outputNode) { + this.outputNode = outputNode; + this.buffer; // a Tone.Buffer + this.bufferSource; + this.playbackRate = 1; +} + +SoundPlayer.prototype.setBuffer = function (buffer) { + this.buffer = buffer; +}; + +SoundPlayer.prototype.setPlaybackRate = function (playbackRate) { + this.playbackRate = playbackRate; +}; + +SoundPlayer.prototype.stop = function () { + if (this.bufferSource){ + this.bufferSource.stop(); + } +}; + +SoundPlayer.prototype.start = function () { + this.stop(); + + this.bufferSource = new Tone.BufferSource(this.buffer.get()); + this.bufferSource.playbackRate.value = this.playbackRate; + this.bufferSource.connect(this.outputNode); + this.bufferSource.start(); +}; + +SoundPlayer.prototype.onEnded = function (callback) { + this.bufferSource.onended = function () {callback();}; +}; + +module.exports = SoundPlayer; diff --git a/src/index.js b/src/index.js index c272246..c3820aa 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,6 @@ var log = require('./log'); var Tone = require('tone'); +var SoundPlayer = require('./SoundPlayer'); // var Soundfont = require('soundfont-player'); var Vocoder = require('./vocoder'); var ADPCMSoundLoader = require('./ADPCMSoundLoader'); @@ -27,7 +28,7 @@ function AudioEngine (sounds) { this.wobble.effectSend.chain(wobbleGain, this.wobble.effectReturn); // telephone effect - simulating the 'tinny' sound coming over a phone line - // basically, a lowpass filter and a highpass filter + // using a lowpass filter and a highpass filter this.telephone = new Tone.Effect(); var telephoneLP = new Tone.Filter(1200, 'lowpass', -24); var telephoneHP = new Tone.Filter(800, 'highpass', -24); @@ -86,10 +87,7 @@ AudioEngine.prototype.loadSounds = function (sounds) { // create a set of empty sound player objects // the sound buffers will be added asynchronously as they load for (var i=0; i