mirror of
https://github.com/scratchfoundation/scratch-audio.git
synced 2025-01-08 13:51:58 -05:00
wip on decoding adpcm wav files
This commit is contained in:
parent
0c2548f6ba
commit
d400de1b4c
1 changed files with 64 additions and 4 deletions
68
src/index.js
68
src/index.js
|
@ -65,14 +65,18 @@ AudioEngine.prototype.loadSounds = function (sounds) {
|
||||||
var soundPlayers = [];
|
var soundPlayers = [];
|
||||||
|
|
||||||
for (var i=0; i<sounds.length; i++) {
|
for (var i=0; i<sounds.length; i++) {
|
||||||
// skip adpcm form sounds since we can't load them yet
|
|
||||||
|
var buffer;
|
||||||
|
|
||||||
if (sounds[i].format == 'adpcm') {
|
if (sounds[i].format == 'adpcm') {
|
||||||
log.warn('cannot load sound in adpcm format');
|
log.warn('attempting to load sound in adpcm format');
|
||||||
continue;
|
buffer = this._loadSoundADPCM(sounds[i].fileUrl);
|
||||||
|
} else {
|
||||||
|
buffer = new Tone.Buffer(sounds[i].fileUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
var player = {};
|
var player = {};
|
||||||
player.buffer = new Tone.Buffer(sounds[i].fileUrl);
|
player.buffer = buffer;
|
||||||
player.bufferSource = null;
|
player.bufferSource = null;
|
||||||
soundPlayers[i] = player;
|
soundPlayers[i] = player;
|
||||||
}
|
}
|
||||||
|
@ -80,6 +84,62 @@ AudioEngine.prototype.loadSounds = function (sounds) {
|
||||||
return soundPlayers;
|
return soundPlayers;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
AudioEngine.prototype._loadSoundADPCM = function (url) {
|
||||||
|
var request = new XMLHttpRequest();
|
||||||
|
request.open('GET', url, true);
|
||||||
|
request.responseType = 'arraybuffer';
|
||||||
|
|
||||||
|
request.onload = function () {
|
||||||
|
var audioData = request.response;
|
||||||
|
|
||||||
|
// convert a Uint8 array to a string
|
||||||
|
function Uint8ToStr (arr) {
|
||||||
|
var str = '';
|
||||||
|
for (var i=0; i<arr.length; i++) {
|
||||||
|
str += String.fromCharCode(arr[i]);
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
// RIFF
|
||||||
|
var riffData = new Uint8Array(audioData, 0, 4);
|
||||||
|
if (Uint8ToStr(riffData) != 'RIFF') {
|
||||||
|
log.warn('adpcm wav header missing RIFF');
|
||||||
|
}
|
||||||
|
// length
|
||||||
|
var length = new Int32Array(audioData, 4, 1)[0];
|
||||||
|
if ((length + 8) != audioData.byteLength) {
|
||||||
|
log.warn('adpcm wav length in header: ' + length + 'is incorrect');
|
||||||
|
}
|
||||||
|
// WAVE
|
||||||
|
var waveData = new Uint8Array(audioData, 8, 4);
|
||||||
|
if (Uint8ToStr(waveData) != 'WAVE') {
|
||||||
|
log.warn('adpcm wave header missing WAVE');
|
||||||
|
}
|
||||||
|
|
||||||
|
extractChunk('fmt', audioData);
|
||||||
|
|
||||||
|
function extractChunk (chunkType, arrayBuffer) {
|
||||||
|
var position = 12; // start at the 12th byte, after RIFF + length + WAVE
|
||||||
|
while (position < (arrayBuffer.byteLength - 8)) {
|
||||||
|
var type = new Uint8Array(arrayBuffer, position, 4);
|
||||||
|
var typeStr = Uint8ToStr(type);
|
||||||
|
position += 4;
|
||||||
|
var chunkSize = new Uint32Array(arrayBuffer, position, 1)[0];
|
||||||
|
position += 4;
|
||||||
|
position += chunkSize;
|
||||||
|
|
||||||
|
if (typeStr == chunkType) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
request.send();
|
||||||
|
};
|
||||||
|
|
||||||
// AudioEngine.prototype._soundsLoaded = function() {
|
// AudioEngine.prototype._soundsLoaded = function() {
|
||||||
// console.log('all sounds loaded');
|
// console.log('all sounds loaded');
|
||||||
// }
|
// }
|
||||||
|
|
Loading…
Reference in a new issue