From d2eb771eea8bb4e5ff775f23e3c28168fb768c9b Mon Sep 17 00:00:00 2001 From: Eric Rosenbaum Date: Sat, 19 Nov 2016 14:17:43 -0500 Subject: [PATCH] cleanup and return a promise --- src/ADPCMSoundLoader.js | 138 ++++++++++++++++++++++------------------ 1 file changed, 75 insertions(+), 63 deletions(-) diff --git a/src/ADPCMSoundLoader.js b/src/ADPCMSoundLoader.js index b4ea9ba..198b7bb 100644 --- a/src/ADPCMSoundLoader.js +++ b/src/ADPCMSoundLoader.js @@ -5,77 +5,89 @@ ADPCMSoundLoader loads wav files that have been compressed with the ADPCM format based on code from Scratch-Flash: https://github.com/LLK/scratch-flash/blob/master/src/sound/WAVFile.as +to do: I think this will ultimately need to run in a web worker + */ var ArrayBufferStream = require('./ArrayBufferStream'); var Tone = require('tone'); var log = require('./log'); -function ADPCMSoundLoader (url) { - var request = new XMLHttpRequest(); - request.open('GET', url, true); - request.responseType = 'arraybuffer'; - - request.onload = function () { - var audioData = request.response; - var stream = new ArrayBufferStream(audioData); - - var riffStr = stream.readUint8String(4); - if (riffStr != 'RIFF') { - log.warn('incorrect adpcm wav header'); - } - - var lengthInHeader = stream.readInt32(); - if ((lengthInHeader + 8) != audioData.byteLength) { - log.warn('adpcm wav length in header: ' + length + 'is incorrect'); - } - - var wavStr = stream.readUint8String(4); - if (wavStr != 'WAVE') { - log.warn('incorrect adpcm wav header'); - } - - var formatChunk = this.extractChunk('fmt ', stream); - this.encoding = formatChunk.readUint16(); - this.channels = formatChunk.readUint16(); - this.samplesPerSecond = formatChunk.readUint32(); - this.bytesPerSecond = formatChunk.readUint32(); - this.blockAlignment = formatChunk.readUint16(); - this.bitsPerSample = formatChunk.readUint16(); - formatChunk.position += 2; // skip extra header byte count - this.samplesPerBlock = formatChunk.readUint16(); - this.adpcmBlockSize = ((this.samplesPerBlock - 1) / 2) + 4; // block size in bytes - - var samples = this.imaDecompress(this.extractChunk('data', stream), this.adpcmBlockSize); - var buffer = Tone.context.createBuffer(1, samples.length, this.samplesPerSecond); - - // todo: optimize this? - for (var i=0; i