Change sample rate of AudioContext (getUserMedia) Change sample rate of AudioContext (getUserMedia) javascript javascript

Change sample rate of AudioContext (getUserMedia)


As far as I know, there is no way to change the sample rate within an audio context. The sample rate will usually be the sample rate of your recording device and will stay that way. So you will not be able to write something like this:

var input = audio_context.createMediaStreamSource(stream);var resampler = new Resampler(44100, 48000);input.connect(resampler);resampler.connect(audio_context.destination);

However, if you want to take your audio stream, resample it and then send it to the backend (or do sth. else with it outside of the Web Audio API), you can use an external sample rate converter (e.g. https://github.com/taisel/XAudioJS/blob/master/resampler.js).

   var resampler = new Resampler(44100, 48000, 1, 2229);   function startUsermedia(stream) {        var input = audio_context.createMediaStreamSource(stream);        console.log('Media stream created.');        recorder = audio_context.createScriptProcessor(2048);        recorder.onaudioprocess = recorderProcess;        recorder.connect(audio_context.destination);    }    function recorderProcess(e) {        var buffer = e.inputBuffer.getChannelData(0);        var resampled = resampler.resampler(buffer);        //--> do sth with the resampled data for instance send to server    }


It looks like there is an open bug about the inability to set the sampling rate:

https://github.com/WebAudio/web-audio-api/issues/300

There's also a Chrome issue:

https://bugs.chromium.org/p/chromium/issues/detail?id=432248

I checked the latest Chromium code and there is nothing in there that lets you set the sampling rate.

Edit: Seems like it has been implemented in Chrome, but is broken currently - see the comments in the Chromium issue.


audioContext = new AudioContext({sampleRate: 48000})

Simply Set sample rate when created AudioContext object, This worked for me