title: PCM Web Audio Api Javascript - I am getting distorted sounds
body: <p>I am receiving an interleaved 16 bit PCM samples over the wire. Each sample is signed</p>
  
  <p>I read it as Int16bit array, lets call this ALL_DATA. So each of the array entries is a a 16 bit sample. </p>
  
  <p>Because it is interleaved I extract it into 2 channels R-L-R-L 
  I end up with 2  (16 bit) arrays half the size of ALL_DATA array.</p>
  
  <p>After that I go over each sample and  normalize it to Float32Array because that is what 
  web audio API uses.</p>
  
  <p>var normalizedSample= (sample > 0) ? sample / 32768 : sample / -32768;</p>
  
  <p>Is this the right way to do it. </p>
  
  <p>I am getting distorted sounds. You can tell what is going on. So literally if you are listening to classic guitar it sounds like it is electric with distortion. </p>
  
  <p>For Arguments sake I am putting down the example code but This code handles</p>
  
  <p>MONO SOUND to make the example simpler, so we don't have to interleave it as well</p>
  
  <pre><code>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 &lt; 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 &lt; audio.length; ) {
                          var normalizedRight = (audio[i] &gt; 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;
  
                  };
  </code></pre>
  
  <p>Any suggestions that might be causing the distorted sound will help. I have written the pcm data before I send it on the server side to a file is good and sound is perfect. </p>