Skip to content Skip to sidebar Skip to footer

Change Sample Rate Of Audiocontext (getusermedia)

Im trying to record a 48000Hz recording via getUserMedia. But without luck. The returned audio MediaStream returns 44100Hz. How can i set this to 48000Hz? Here are snippets of my c

Solution 1:

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 = newResampler(44100, 48000, 1, 2229);

   functionstartUsermedia(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);
    }

    functionrecorderProcess(e) {
        var buffer = e.inputBuffer.getChannelData(0);
        var resampled = resampler.resampler(buffer);
        //--> do sth with the resampled data for instance send to server
    }

Solution 2:

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.

Solution 3:

audioContext = new AudioContext({sampleRate: 48000})

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

Solution 4:

it's been added to chrome:

var ctx = new (window.AudioContext || window.webkitAudioContext)({ sampleRate:16000});

https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/AudioContext

Solution 5:

NOTE: This answer is outdated.


You can't. The sample rate of the AudioContext is set by the browser/device and there is nothing you can do to change it. In fact, you will find that 44.1kHz on your machine might be 48kHz on mine. It varies to whatever the OS picks by default.

Also remember that not all hardware is capable of all sample rates.

Post a Comment for "Change Sample Rate Of Audiocontext (getusermedia)"