mirror of
https://github.com/scratchfoundation/scratch-audio.git
synced 2024-12-22 14:02:29 -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) {
|
||||
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue