From c8d194b951403f3eed4e33ec28575925faeca841 Mon Sep 17 00:00:00 2001 From: Eric Rosenbaum Date: Mon, 24 Oct 2016 16:01:43 -0400 Subject: [PATCH] handle missing sounds --- src/index.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index 7219a68..1416800 100644 --- a/src/index.js +++ b/src/index.js @@ -23,7 +23,7 @@ function AudioEngine (sounds) { this.effectsNode.chain(this.delay, this.panner, this.reverb, Tone.Master); // reset effects to their default parameters - // this.clearEffects(); + this.clearEffects(); // load sounds @@ -66,19 +66,24 @@ AudioEngine.prototype.loadSounds = function (sounds) { AudioEngine.prototype.playSound = function (index) { var player = this.soundPlayers[index]; - if (player.buffer.loaded) { + if (player && player.buffer.loaded) { player.start(); } else { // if the sound has not yet loaded, wait and try again log.warn('sound ' + index + ' not loaded yet'); setTimeout(function () { this.playSound(index); - }, 100); + }.bind(this), 100); } }; AudioEngine.prototype.getSoundDuration = function (index) { - return this.soundPlayers[index].buffer.duration; + var player = this.soundPlayers[index]; + if (player && player.buffer.loaded) { + return player.buffer.duration; + } else { + return 0; + } }; AudioEngine.prototype.playNoteForBeats = function (note, beats) { @@ -172,6 +177,9 @@ AudioEngine.prototype.changeEffect = function (effect, value) { AudioEngine.prototype._setPitchShift = function (value) { this.pitchShiftRatio = value; + if (!this.soundPlayers) { + return; + } for (var i=0; i