jdscardoso
jdscardoso

Reputation: 291

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

Upvotes: 2

Views: 2675

Answers (2)

jdscardoso
jdscardoso

Reputation: 291

The solution (thanks @OskarEriksson, this is your tip and now I have no problems):

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/3); //<-----Only change here

  while (l--) {
    if(l%3==0){
    buf[l/3] = buffer[l]*0xFFFF;
  }
  }
  return buf.buffer
}

The Matlab Plots: 48k enter image description here

16k

enter image description here

Note: The word that I spell is exactly the same, but you can see some delay because of browser switching, and press Start\Stop button.

Upvotes: 2

Oskar Eriksson
Oskar Eriksson

Reputation: 2641

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.

Upvotes: 2

Related Questions