I'm looking to convert a Float32Array into an Int16Array.
Here's what I have (i'm not providing data
).
var data = ...; /*new Float32Array();*/
var dataAsInt16Array = new Int16Array(data.length);
for(var i=0; i<data.length; i++){
dataAsInt16Array[i] = parseInt(data[i]*32767,10);
}
I'm not convinced that I'm doing it correctly and looking for some direction.
You can do it directly from the ArrayBuffer
var dataAsInt16Array = new Int16Array(data.buffer);
var f32 = new Float32Array(4);
f32[0] = 0.1, f32[1] = 0.2, f32[2] = 0.3, f32[3] = 0.4;
// [0.10000000149011612, 0.20000000298023224, 0.30000001192092896, 0.4000000059604645]
var i16 = new Int16Array(f32.buffer);
// [-13107, 15820, -13107, 15948, -26214, 16025, -13107, 16076]
// and back again
new Float32Array(i16.buffer);
// [0.10000000149011612, 0.20000000298023224, 0.30000001192092896, 0.4000000059604645]