Change sample rate of AudioContext (getUserMedia)

后端 未结 7 2258
粉色の甜心
粉色の甜心 2020-12-08 10:07

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 s

7条回答
  •  忘掉有多难
    2020-12-08 10:55

    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
        }
    

提交回复
热议问题