mirror of
https://github.com/scratchfoundation/scratch-audio.git
synced 2024-12-22 22:12:48 -05:00
Catch ADPCM decode error, and use an empty sound
This commit is contained in:
parent
9a2332e56b
commit
29b2e7ebc7
1 changed files with 11 additions and 2 deletions
|
@ -156,7 +156,7 @@ class AudioEngine {
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
// If the file is empty, create an empty sound
|
// If the file is empty, create an empty sound
|
||||||
if (sound.data.length === 0) {
|
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
|
// 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
|
// First we need to create another copy of our original data
|
||||||
const bufferCopy2 = sound.data.buffer.slice(0);
|
const bufferCopy2 = sound.data.buffer.slice(0);
|
||||||
// Try decoding as adpcm
|
// Try decoding as adpcm
|
||||||
return new ADPCMSoundDecoder(this.audioContext).decode(bufferCopy2);
|
return new ADPCMSoundDecoder(this.audioContext).decode(bufferCopy2)
|
||||||
|
.catch(() => Promise.resolve(this._emptySound()));
|
||||||
})
|
})
|
||||||
.then(
|
.then(
|
||||||
buffer => ([soundId, buffer]),
|
buffer => ([soundId, buffer]),
|
||||||
|
@ -177,6 +178,14 @@ class AudioEngine {
|
||||||
return decoding;
|
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.
|
* Decode a sound, decompressing it into audio samples.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in a new issue