Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 213
f.lorenzo Avatar asked May 04 '15 13:05

f.lorenzo


People also ask

How do you adjust a sample rate?

To change the sample rate from 44.1 to 48 kHz, you have to determine a rational number (ratio of integers), P/Q , such that P/Q times the original sample rate, 44100, is equal to 48000 within some specified tolerance.

How many AudioContext are there?

How many Audio Contexts should I have? A: Generally, you should include one AudioContext per page, and a single audio context can support many nodes connected to it. Though you may include multiple AudioContexts on a single page, this can lead to a performance hit.

What is window AudioContext?

The AudioContext interface represents an audio-processing graph built from audio modules linked together, each represented by an AudioNode . An audio context controls both the creation of the nodes it contains and the execution of the audio processing, or decoding.


2 Answers

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     } 
like image 81
basilikum Avatar answered Sep 18 '22 14:09

basilikum


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.

like image 39
Timmmm Avatar answered Sep 21 '22 14:09

Timmmm