html5 audio - Javascript: Converting from Int16 to Float32 -
i'm trying put wav file in audiobuffer can manipulate it. i've created wav files audiobuffer before, , required converting float32array dataview containing int16 values. used handy function picked up:
function floatto16bitpcm(output, offset, input){ (var = 0; < 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, need reverse (the wav files loaded server, don't have original data anymore). can't figure out happening in function or how data transformed.
each real data of array input reduced interval [-1, 1]
- math.min(1, x) gives x if x<=1 , 1 otherwise
- math.max(-1, y) gives y if y>=-1 , -1 otherwise
then real number between -1 , 1 converted signed 16-bit integer.
whether positive or negative multiplied 32767 or -32768 retains integer part cast. equivalent keep 16 significant bits after decimal point in binary representation.
16 bits integers stored in little endian on two-byte in buffer, 1 after other (according offset advances 2 two)
for reverse operation, place 2 consecutive bytes in int16. convert real distinguishing 2 cases following sign , dividing 32768 or 32767. operation done data loss.
Comments
Post a Comment