更改AudioContext的采样率

更改AudioContext的采样率

本文介绍了更改AudioContext的采样率(getUserMedia)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过getUserMedia录制48000Hz录音。但没有运气。返回的音频MediaStream返回44100Hz。如何设置为48000Hz?

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?

以下是我的代码片段:

var startUsermedia = this.startUsermedia;

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

startUsermedia函数:

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.');
        },

我尝试更改AudioContext的属性sampleRate,但没有运气。

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

如何将sampleRate更改为48000Hz?

How can i change the sampleRate to 48000Hz?

编辑:我们也是现在可以使用闪存解决方案,可以记录和导出48000Hz的wav文件

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

推荐答案

据我所知,没有办法更改音频上下文中的采样率。采样率通常是您的录音设备的采样率,并将保持这种方式。所以你将不能写这样的东西:

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

但是,如果您想拍摄音频流,请重新取样然后将其发送到后端(或者在Web Audio API之外用它做其他事情,你可以使用外部采样率转换器(例如)。

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
    }

这篇关于更改AudioContext的采样率(getUserMedia)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 14:51