My application is designed to stream PCM binary data from the server directly to the Web Audio API.
To achieve audio normalization, I utilize a DataView to convert the incoming data to Int16 format, where each sample is divided by 32768 before converting them back to Float32 for playback:
var data = new DataView(arrayBuffer);
var tempArray = new Int16Array(data.byteLength / Int16Array.BYTES_PER_ELEMENT);
var length = tempArray.length;
for (var index = 0; index < length; ++index) {
tempArray[index] = data.getInt16(index * Int16Array.BYTES_PER_ELEMENT, true);
}
var bufferToPlay = new Float32Array(tempArray.length);
var channelCounter = 0;
for (var i = 0; i < tempArray.length;) {
var normalizedAudio = tempArray[i] / 32768;
i = i + 1;
bufferToPlay[channelCounter] = normalizedAudio;
channelCounter++;
}
An interesting observation:
When running this process on my 64-bit Windows machine, the audio output is smooth. However, when using my older 32-bit Windows XP machine, the audio contains noticeable artifacts resembling resampling or bit conversion issues.
I've been researching endianness extensively, but considering both machines are Pentium-based, they should both be little-endian systems. So, how could there be such discrepancies?