title: Downsampling 48khz to 16khz - Javascript
body: <p>At the moment <code>getUserMedia</code> is getting audio (from a mic) at 48Khz. But my speech-recognition server could only use audio at 16Khz (could be 48Khz but it will do down-sampling). My objective is to save bandwidth doing the down-sampling on the client side. </p>
  
  <pre><code>recorder.onaudioprocess = function(e){
      if(!recording) return;
      console.log ('recording');
      left = e.inputBuffer.getChannelData(0);
      Stream.write(convertoFloat32ToInt16(left));//write to server
  
    }
  
  function convertoFloat32ToInt16(buffer) {
    var l = buffer.length;
    var buf = new Int16Array(l)
  
    while (l--) {
      if(l%3==0){
      buf[l/3] = buffer[l]*0xFFFF;
    }
    }
    return buf.buffer
  }
  </code></pre>
  
  <p>Any other implementations from you?
  EDIT:
  I put the nodejs server recording the same audio to a file and then I open it with matlab. The files have the same size. And shouldn't, right?</p>
  
  <p>Matlab plots - 16k</p>
  
  <p><a href="https://i.stack.imgur.com/rgwm0.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rgwm0.jpg" alt="enter image description here"></a></p>
  
  <p>Matlab plots - 48k</p>
  
  <p><a href="https://i.stack.imgur.com/4lEYt.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4lEYt.jpg" alt="48khz"></a></p>