I get alternating 16-bit PCM samples over a wire. Each sample is signed
I read it as an Int16bit array, called this ALL_DATA. Thus, each of the array entries is a 16-bit pattern.
Since it alternates, I extract it to 2 RLRL channels. I get 2 (16-bit) arrays, half the size of the ALL_DATA array.
After that, I go through each sample and normalize it to Float32Array, because that is what the web audio API is.
var normalizedSample = (sample> 0)? sample / 32768: sample / -32768;
This is the right way to do it.
I get distorted sounds. You can tell what is going on. So literally, if you listen to a classic guitar, it sounds like it's electric with distortions.
Regarding the arguments, I set the example code, but this code processes
MONO SOUND to make the example simpler, so we also do not need to alternate it
var startTime = 0; var fileReader = new FileReader(); fileReader.onload = function (e) { var data = new DataView(e.target.result); var audio = new Int16Array(data.byteLength / Int16Array.BYTES_PER_ELEMENT); var len = audio.length; for (var jj = 0; jj < len; ++jj) { audio[jj] = data.getInt16(jj * Int16Array.BYTES_PER_ELEMENT, true); } var right = new Float32Array(audio.length); var channleCounter = 0; for (var i = 0; i < audio.length; ) { var normalizedRight = (audio[i] > 0) ? audio[i] / 32768 : audio[i] / -32768; i = i + 1; right[channleCounter] = normalizedRight; channleCounter++; } var source = audioContext.createBufferSource(); var audioBuffer = audioContext.createBuffer(1, right.length, 44100); audioBuffer.getChannelData(0).set(right); source.buffer = audioBuffer; source.connect(audioContext.destination); source.noteOn(startTime); startTime += audioBuffer.duration; };
Any suggestions that may cause a distorted sound will help. I wrote pcm data before sending it to the server side and the file is good and the sound is excellent.
javascript pcm typed-arrays web-audio
Evren bingΓΈl
source share