mirror of
https://github.com/scratchfoundation/scratch-audio.git
synced 2024-12-22 22:12:48 -05:00
Tidy up sound decoding
This commit is contained in:
parent
3608a5947f
commit
83afeb1916
1 changed files with 23 additions and 24 deletions
47
src/index.js
47
src/index.js
|
@ -62,34 +62,33 @@ function AudioEngine () {
|
||||||
*/
|
*/
|
||||||
AudioEngine.prototype.decodeSound = function (sound) {
|
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;
|
var storedContext = this;
|
||||||
|
|
||||||
// if the format string is empty, assume the sound is a wav file and use the native decoder
|
loaderPromise.then(
|
||||||
if (sound.format === '') {
|
function (decodedAudio) {
|
||||||
Tone.context.decodeAudioData(sound.data.buffer).then(
|
storedContext.audioBuffers[sound.md5] = new Tone.Buffer(decodedAudio);
|
||||||
function (decodedAudio) {
|
},
|
||||||
storedContext.audioBuffers[sound.md5] = new Tone.Buffer(decodedAudio);
|
function (error) {
|
||||||
},
|
log.warn('audio data could not be decoded', error);
|
||||||
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);
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Play a note for a duration on an instrument with a volume
|
* Play a note for a duration on an instrument with a volume
|
||||||
* @param {number} note - a MIDI note number
|
* @param {number} note - a MIDI note number
|
||||||
|
|
Loading…
Reference in a new issue