From 29b2e7ebc74044835f36afdc41465a894621ec59 Mon Sep 17 00:00:00 2001 From: Eric Rosenbaum Date: Thu, 10 Jan 2019 16:52:43 -0500 Subject: [PATCH 1/2] Catch ADPCM decode error, and use an empty sound --- src/AudioEngine.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/AudioEngine.js b/src/AudioEngine.js index c4e5805..0747b2e 100644 --- a/src/AudioEngine.js +++ b/src/AudioEngine.js @@ -156,7 +156,7 @@ class AudioEngine { .catch(() => { // If the file is empty, create an empty sound if (sound.data.length === 0) { - return Promise.resolve(this.audioContext.createBuffer(1, 1, this.audioContext.sampleRate)); + return Promise.resolve(this._emptySound()); } // The audio context failed to parse the sound data @@ -165,7 +165,8 @@ class AudioEngine { // First we need to create another copy of our original data const bufferCopy2 = sound.data.buffer.slice(0); // Try decoding as adpcm - return new ADPCMSoundDecoder(this.audioContext).decode(bufferCopy2); + return new ADPCMSoundDecoder(this.audioContext).decode(bufferCopy2) + .catch(() => Promise.resolve(this._emptySound())); }) .then( buffer => ([soundId, buffer]), @@ -177,6 +178,14 @@ class AudioEngine { return decoding; } + /** + * An empty sound buffer, for use when we are unable to decode a sound file. + * @returns {AudioBuffer} - an empty audio buffer. + */ + _emptySound () { + return this.audioContext.createBuffer(1, 1, this.audioContext.sampleRate); + } + /** * Decode a sound, decompressing it into audio samples. * From aee73d97a56f21de828f5984584b8ebbbc041e35 Mon Sep 17 00:00:00 2001 From: Eric Rosenbaum Date: Mon, 14 Jan 2019 15:37:02 -0500 Subject: [PATCH 2/2] Remove unnecessary promise wrappers --- src/AudioEngine.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/AudioEngine.js b/src/AudioEngine.js index 0747b2e..0340f50 100644 --- a/src/AudioEngine.js +++ b/src/AudioEngine.js @@ -156,7 +156,7 @@ class AudioEngine { .catch(() => { // If the file is empty, create an empty sound if (sound.data.length === 0) { - return Promise.resolve(this._emptySound()); + return this._emptySound(); } // The audio context failed to parse the sound data @@ -166,7 +166,7 @@ class AudioEngine { const bufferCopy2 = sound.data.buffer.slice(0); // Try decoding as adpcm return new ADPCMSoundDecoder(this.audioContext).decode(bufferCopy2) - .catch(() => Promise.resolve(this._emptySound())); + .catch(() => this._emptySound()); }) .then( buffer => ([soundId, buffer]),