title: Play PCM with javascript
body: <p>I got some problems playing PCM Audio on the browser. The PCM audio comes from an android device with udp-protocol and is saved on the server as *.raw</p>
  
  <p>I unsuccessfully trying to play this saved file with the help of webaudioapi. Using following code, plays me some creepy sound with white noise:</p>
  
  <pre><code>var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
  audioCtx.sampleRate = 16000;
  
  
  // Stereo
  var channels = 1;
  // Create an empty two second stereo buffer at the
  // sample rate of the AudioContext
  var frameCount = audioCtx.sampleRate * 10.0;
  
  var myAudioBuffer = audioCtx.createBuffer(channels, frameCount, audioCtx.sampleRate);
  
  
  var req = new XMLHttpRequest();
  req.open('GET', "example.raw", false);
  req.overrideMimeType('text\/plain; charset=x-user-defined');
  req.send(null);
  
  function play(){
      for (var channel = 0; channel &lt; channels; channel++) {
  
          var nowBuffering = myAudioBuffer.getChannelData(channel,16,16000);
          for (var i = 0; i &lt; frameCount; i++) {
              // audio needs to be in [-1.0; 1.0]
              // for this reason I also tried to divide it by 32767
              // as my pcm sample is in 16-Bit. It plays still the
              // same creepy sound less noisy.
              nowBuffering[i] = (req.responseText.charCodeAt(i) &amp; 0xff;
  
          }
      }
      // Get an AudioBufferSourceNode.
      // This is the AudioNode to use when we want to play an AudioBuffer
      var source = audioCtx.createBufferSource();
      // set the buffer in the AudioBufferSourceNode
      source.buffer = myAudioBuffer;
      // connect the AudioBufferSourceNode to the
      // destination so we can hear the sound
      source.connect(audioCtx.destination);
      // start the source playing
      source.start();
  }
  </code></pre>
  
  <p>It's playing such an unidentifiable sound that I'm not sure if it's playing the pcm file which I supposed it has to do.</p>
  
  <p>I'm supposing it has to do something with the pcm file. The PCM file has 16 kHz sample rate, 16 bits per sample and only one channel or rather mono-channel.</p>
  
  <p>Anybody with the same problem here or did anybody have suggestions to fix my problem?</p>
  
  <p>I am looking since some days for a solution and appreciate any help.</p>