title: How to play wav audio byte array via javascript/html5?
body: <p>I'm using the following method to play a byte array containing wav data.  The function is being called from a GWT project.</p>
  
  <p>This function plays back sound, but it sounds like some kind of Hellish monster. The sample rate is definitely correct (the sound is being generated by neospeech) and I've tried all kinds of values for numberOfSamples, which just seems to represent how long the audio data is.</p>
  
  <p>A value greater than 30000 for numberOfSamples will play the audio file's full length but it is garbled and horrible. </p>
  
  <p>So, what am I doing wrong?</p>
  
  <pre><code>function playByteArray(byteArray, numberOfSamples) {
      sampleRate = 8000;
  
      if (!window.AudioContext) {
          if (!window.webkitAudioContext) {
              alert("Your browser does not support any AudioContext and cannot play back this audio.");
              return;
          }
          window.AudioContext = window.webkitAudioContext;
      }
  
      var audioContext = new AudioContext();
  
      var buffer = audioContext.createBuffer(1, numberOfSamples, sampleRate);
      var buf = buffer.getChannelData(0);
      for (i = 0; i &lt; byteArray.length; ++i) {
          buf[i] = byteArray[i];
      }
  
      var source = audioContext.createBufferSource();
      source.buffer = buffer;
      source.connect(audioContext.destination);
      source.start(0);
  }
  </code></pre>