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
* @param {Object} sounds - an array of objects containing metadata for sound files of a sprite
* Decode a sound, decompressing it into audio samples.
* 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;
for (var i=0; i<sounds.length; i++) {
var md5 = sounds[i].md5;
var buffer = new Tone.Buffer();
this.audioBuffers[md5] = buffer;
// 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);
}
);
}
// Squeak sound format (not implemented yet)
if (sounds[i].format == 'squeak') {
log.warn('unable to load sound in squeak format');
continue;
}
// most sounds decode natively, but for adpcm sounds we use our own decoder
if (sounds[i].format == 'adpcm') {
log.warn('loading sound in adpcm format');
// create a closure to store the sound md5, to use when the
// 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);
}
// 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);
}
);
}
};