The web audio API down is 44.1kHz in Javascript

I am using RecorderJS to record a microphone stream from a user. The default export is a 44.1 kHz, 16 bit WAV file. Is there anyway I can reduce this to 11 kHz or 16 kHz if it's not weird? Is there anyway I can get a 16bit 16x WAV file from the getUserMedia Web Audio API stream using only javascript?

I am trying to reduce the file size thus saving a lot of bandwidth for the users. Thank.

+3


source to share


1 answer


Edit: one more thing, you can also only send one feed instead of ...

I'm not sure if this is the right way, but I did by doing interpolation of the data received from the microphone.I assume you are grabbing your data from the microphone something like this,

 this.node.onaudioprocess = function(e){
      if (!recording) return;
      worker.postMessage({
           command: 'record',
           buffer: [
                e.inputBuffer.getChannelData(0),
                e.inputBuffer.getChannelData(1)
                ]
      });
  }

      



now change it to

var oldSampleRate = 44100, newSampleRate = 16000;
this.node.onaudioprocess = function(e){

    var leftData = e.inputBuffer.getChannelData(0);
    var rightData = e.inputBuffer.getChannelData(1);
    leftData = interpolateArray(leftData, leftData.length * (newSampleRate/oldSampleRate)  );
    rightData = interpolateArray(rightData, rightData.length * (newSampleRate/oldSampleRate) );
    if (!recording) return;
    worker.postMessage({
         command: 'record',
         buffer: [
              leftData,
              rightData
              ]
    });
}

function interpolateArray(data, fitCount) {
    var linearInterpolate = function (before, after, atPoint) {
        return before + (after - before) * atPoint;
    };

    var newData = new Array();
    var springFactor = new Number((data.length - 1) / (fitCount - 1));
    newData[0] = data[0]; // for new allocation
    for ( var i = 1; i < fitCount - 1; i++) {
        var tmp = i * springFactor;
        var before = new Number(Math.floor(tmp)).toFixed();
        var after = new Number(Math.ceil(tmp)).toFixed();
        var atPoint = tmp - before;
        newData[i] = linearInterpolate(data[before], data[after], atPoint);
    }
    newData[fitCount - 1] = data[data.length - 1]; // for new allocation
    return newData;
};

      

+1


source







All Articles