Decode sounds in audio engine

Remove logic for loading sounds, since that is handled by Scratch Storage
This commit is contained in:
Eric Rosenbaum 2017-03-22 18:09:42 -04:00
parent c5daccef7c
commit 3608a5947f

View file

@ -56,37 +56,37 @@ function AudioEngine () {
} }
/** /**
* Load all sounds for a sprite and store them in the audioBuffers dictionary, indexed by md5 * Decode a sound, decompressing it into audio samples.
* @param {Object} sounds - an array of objects containing metadata for sound files of a sprite * Store a reference to it the sound in the audioBuffers dictionary, indexed by md5
* @param {Object} sound - an object containing audio data and metadata for a sound
*/ */
AudioEngine.prototype.loadSounds = function (sounds) { AudioEngine.prototype.decodeSound = function (sound) {
var storedContext = this; var storedContext = this;
for (var i=0; i<sounds.length; i++) {
var md5 = sounds[i].md5; // if the format string is empty, assume the sound is a wav file and use the native decoder
var buffer = new Tone.Buffer(); if (sound.format === '') {
this.audioBuffers[md5] = buffer; 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);
}
);
}
// Squeak sound format (not implemented yet) // if the format is adpcm, we use a custom decoder
if (sounds[i].format == 'squeak') { if (sound.format === 'adpcm') {
log.warn('unable to load sound in squeak format'); var loader = new ADPCMSoundDecoder();
continue; loader.decode(sound.data.buffer).then(
} function (decodedAudio) {
// most sounds decode natively, but for adpcm sounds we use our own decoder storedContext.audioBuffers[sound.md5] = new Tone.Buffer(decodedAudio);
if (sounds[i].format == 'adpcm') { },
log.warn('loading sound in adpcm format'); function (error) {
// create a closure to store the sound md5, to use when the log.warn('adpcm audio data could not be decoded', error);
// decoder completes and resolves the promise
(function () {
var storedMd5 = sounds[i].md5;
var loader = new ADPCMSoundLoader();
loader.load(sounds[i].fileUrl).then(function (audioBuffer) {
storedContext.audioBuffers[storedMd5] = new Tone.Buffer(audioBuffer);
});
}());
} else {
this.audioBuffers[md5] = new Tone.Buffer(sounds[i].fileUrl);
} }
);
} }
}; };