mirror of
https://github.com/scratchfoundation/scratch-audio.git
synced 2024-12-22 14:02:29 -05:00
Merge pull request #114 from LLK/bugfix/handle-broken-sounds
Handle empty and unusually formatted sounds that caused crashes
This commit is contained in:
commit
9a2332e56b
2 changed files with 15 additions and 1 deletions
|
@ -150,7 +150,16 @@ class ArrayBufferStream {
|
|||
* @return {number} the next 32 bit integer in the stream
|
||||
*/
|
||||
readInt32 () {
|
||||
const val = new Int32Array(this.arrayBuffer, this._position, 1)[0];
|
||||
let val;
|
||||
if (this._position % 4 === 0) {
|
||||
val = new Int32Array(this.arrayBuffer, this._position, 1)[0];
|
||||
} else {
|
||||
// Cannot read Int32 directly out because offset is not multiple of 4
|
||||
// Need to slice out the values first
|
||||
val = new Int32Array(
|
||||
this.arrayBuffer.slice(this._position, this._position + 4)
|
||||
)[0];
|
||||
}
|
||||
this._position += 4; // one 32 bit int is 4 bytes
|
||||
return val;
|
||||
}
|
||||
|
|
|
@ -154,6 +154,11 @@ class AudioEngine {
|
|||
// decoder If that fails, attempt to decode as ADPCM
|
||||
const decoding = decodeAudioData(this.audioContext, bufferCopy1)
|
||||
.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));
|
||||
}
|
||||
|
||||
// The audio context failed to parse the sound data
|
||||
// we gave it, so try to decode as 'adpcm'
|
||||
|
||||
|
|
Loading…
Reference in a new issue