From 83afeb1916f38287d14a373d6ee1d7f76617b9fd Mon Sep 17 00:00:00 2001 From: Eric Rosenbaum Date: Mon, 27 Mar 2017 11:51:01 -0400 Subject: [PATCH] Tidy up sound decoding --- src/index.js | 47 +++++++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/src/index.js b/src/index.js index d3c2cb7..68450a5 100644 --- a/src/index.js +++ b/src/index.js @@ -62,34 +62,33 @@ function AudioEngine () { */ AudioEngine.prototype.decodeSound = function (sound) { + var loaderPromise = null; + + switch (sound.format) { + case '': + loaderPromise = Tone.context.decodeAudioData(sound.data.buffer); + break; + case 'adpcm': + loaderPromise = (new ADPCMSoundDecoder()).decode(sound.data.buffer); + break; + default: + return log.warn('unknown sound format', sound.format); + } + var storedContext = this; - // if the format string is empty, assume the sound is a wav file and use the native decoder - if (sound.format === '') { - Tone.context.decodeAudioData(sound.data.buffer).then( - function (decodedAudio) { - storedContext.audioBuffers[sound.md5] = new Tone.Buffer(decodedAudio); - }, - function (error) { - log.warn('audio data could not be decoded', error); - } - ); - } - - // if the format is adpcm, we use a custom decoder - if (sound.format === 'adpcm') { - var loader = new ADPCMSoundDecoder(); - loader.decode(sound.data.buffer).then( - function (decodedAudio) { - storedContext.audioBuffers[sound.md5] = new Tone.Buffer(decodedAudio); - }, - function (error) { - log.warn('adpcm audio data could not be decoded', error); - } - ); - } + loaderPromise.then( + function (decodedAudio) { + storedContext.audioBuffers[sound.md5] = new Tone.Buffer(decodedAudio); + }, + function (error) { + log.warn('audio data could not be decoded', error); + } + ); }; + + /** * Play a note for a duration on an instrument with a volume * @param {number} note - a MIDI note number