Web audio API downsample 44.1 khz in Javascript

2.3k views Asked by At

I'm using RecorderJS to record a microphone stream from the user. The default export is a WAV file at 44.1 kHz, 16bit. Is there anyway I can downsample this to 11kHz or 16kHz without it sounding weird? Is there anyway I can get a 16bit 16khz WAV file out of a Web Audio API getUserMedia stream, by using only javascript?

I'm trying to reduce the file size, thus saving a lot of bandwidth for the users. Thanks.

1

There are 1 answers

0
mido On

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

I am not sure if this is the right way, but I did by doing interpolation of the data received from microphone, I am guess, you are capturing your data from 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 modify it into

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;
};