Search code examples
javascriptnode.jsweb-audio-apigetusermedia

Downsampling 48khz to 16khz - Javascript


At the moment getUserMedia 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.

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
}

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?

Matlab plots - 16k

enter image description here

Matlab plots - 48k

48khz


Solution

  • It looks to me like the data is being written to a stereo file, right? That could explain the choppy waveform, since you only have the data for the left channel and the right channel is filled with zeroes.

    Also, you're creating your Int16Array() to be the length of the original buffer, but it should really only be a third of the length, since you only use every third sample. That'd probably explain why they're the same size once you've rendered it.

    I'm not sure how to interpret matlab plots, but that's what it looks like to me.