28

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 code:

var startUsermedia = this.startUsermedia;

            navigator.getUserMedia({ 
                audio: true, 
                //sampleRate: 48000 
            }, startUsermedia, function (e) {
                console.log('No live audio input: ' + e);
            });

The startUsermedia function:

startUsermedia: function (stream) {
            var input = audio_context.createMediaStreamSource(stream);
            console.log('Media stream created.');
            // Uncomment if you want the audio to feedback directly
            //input.connect(audio_context.destination);
            //__log('Input connected to audio context destination.');

            recorder = new Recorder(input);
            console.log('Recorder initialised.');
        },

I tried changing the property sampleRate of the AudioContext, but no luck.

How can i change the sampleRate to 48000Hz?

EDIT : We are also now okay with a flash solution that can record and export wav files at 48000Hz

f.lorenzo
  • 886
  • 2
  • 9
  • 22

7 Answers7

16

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
    }
basilikum
  • 9,714
  • 4
  • 38
  • 55
  • Thank you but sadly a resampled version of the recording wouldn't work. But this is a good answer for other people looking for a way to convert sampleRates – f.lorenzo May 04 '15 at 14:05
  • Ok, then just out of curiosity: what do you want to do with your audio stream? Do you want to save it somehow or what is your goal? – basilikum May 04 '15 at 14:08
  • Sadly i can't tell you the purpose of it because of company policy. But a correct sampleRate is a must. – f.lorenzo May 04 '15 at 14:10
  • 1
    Ok, I understand. Well if you have control over the environment (browser, OS), where your application is used, then maybe you can change the default sample rate of your audio device directly there. – basilikum May 04 '15 at 14:21
  • 1
    The audio context adopts the sample rate of the output device rather than the input device. [source](https://bugs.chromium.org/p/chromium/issues/detail?id=432248#c12). – Octavian Naicu Jul 12 '18 at 22:35
  • @basilikum good point. I have a similar need. In my case I send an audio recording to a server (the application is a voice-bot). In my tests I'm experiencing input audio device "supply" 32 bit / 48KHz, and I would like to reduce to 8/16 8/16Khz (enough for a voice message) to reduce useless networking bandwidth in client/server audio data exchanges (via socketio). The WEIRD behaiviour I experiencing is that getUserMedia seems not accepting any audio bit/rate constraints: https://stackoverflow.com/questions/62392352/mediadevices-getusermedia-how-can-i-set-audio-constraints-sampling-rate-bit-d – Giorgio Robino Jun 17 '20 at 13:44
5

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.

Timmmm
  • 68,359
  • 51
  • 283
  • 367
5
audioContext = new AudioContext({sampleRate: 48000})

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

Hassam
  • 133
  • 2
  • 5
3

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.

Brad
  • 146,404
  • 44
  • 300
  • 476
  • I see, thank you. Is there maybe a flash solution that does support 48kHz? (It doesn't matter if some hardware don't support it) – f.lorenzo May 04 '15 at 14:00
  • @f.lorenzo Maybe. You should post a different question for that, since a Flash answer is completely different than the Web Audio API. – Brad May 04 '15 at 14:20
3

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

Sean
  • 1,761
  • 1
  • 21
  • 26
2

You can use an OfflineAudioContext to essentially render your audio buffer to a different sample rate (but this is batch operation).

So you would record your recording using the normal audio context, and then use an OfflineAudioContext with a different sample rate to render your buffer. There is an example on the Mozilla page.

Guillaume Belrose
  • 2,408
  • 3
  • 21
  • 24
0

It is now in the spec but not yet implemented in Chromium. Also in bugs.chromium.org, "Status: Available" does not mean it is implemented. It just means that nobody is working on it and that it is available for anyone who wants to work on it. So "Available" means "Not assigned".

Julien
  • 101
  • 2