I'm trying to put a WAV file in an AudioBuffer so that I can manipulate it. I've created WAV files from an AudioBuffer before, and that required converting a Float32Array to a DataView containing Int16 values. I used this handy function I picked up:
function floatTo16BitPCM(output, offset, input){
for (var i = 0; i < input.length; i++, offset+=2){
var s = Math.max(-1, Math.min(1, input[i]));
output.setInt16(offset, s < 0 ? s * 0x8000 : s * 0x7FFF, true);
}
}
Well, all I need to do now is reverse this (the WAV files are loaded from the server, so I don't have the original data anymore). I can't figure out what is actually happening in that function or how the data is transformed.
Here's what seems to work. I load the data in an ajax call with the response type "arraybuffer". Otherwise, the response ends up being a string and it is a mess to work with. Then I convert to a 16-bit array. Then I convert that to a Float32 array in the way that works with the WAV encoding. I also needed to throw away the WAV's header, and some metadata at the end of it.