javascript 更改 AudioContext (getUserMedia) 的采样率

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30031561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 11:31:02  来源:igfitidea点击:

Change sample rate of AudioContext (getUserMedia)

javascriptgetusermediasample-rateaudiocontext

提问by f.lorenzo

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?

我试图通过 getUserMedia 录制 48000Hz 的录音。但没有运气。返回的音频 MediaStream 返回 44100Hz。我如何将其设置为 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 函数:

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.

我尝试更改 AudioContext 的属性 sampleRate,但没有成功。

How can i change the sampleRate to 48000Hz?

如何将采样率更改为 48000Hz?

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

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

回答by basilikum

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

但是,如果您想获取音频流,对其重新采样,然后将其发送到后端(或在 Web Audio API 之外使用它做其他事情),您可以使用外部采样率转换器(例如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
    }

回答by Timmmm

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

https://github.com/WebAudio/web-audio-api/issues/300

There's also a Chrome issue:

还有一个 Chrome 问题:

https://bugs.chromium.org/p/chromium/issues/detail?id=432248

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.

我检查了最新的 Chromium 代码,里面没有任何东西可以让你设置采样率。

Edit:Seems like it has been implemented in Chrome, but is broken currently - see the comments in the Chromium issue.

编辑:似乎它已在 Chrome 中实现,但目前已损坏 - 请参阅 Chromium 问题中的评论。

回答by Brad

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.

你不能。AudioContext 的采样率由浏览器/设备设置,您无法更改它。事实上,你会发现你机器上的 44.1kHz 可能是我机器上的 48kHz。它因操作系统默认选择的不同而异。

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

还请记住,并非所有硬件都具有所有采样率。

回答by Sean

it's been added to chrome:

它已添加到 chrome:

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

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

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

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

回答by Hassam

audioContext = new AudioContext({sampleRate: 48000})

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

只需在创建 AudioContext 对象时设置采样率,这对我有用

回答by Guillaume Belrose

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

您可以使用OfflineAudioContext将音频缓冲区呈现为不同的采样率(但这是批处理操作)。

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.

因此,您将使用普通音频上下文录制您的录音,然后使用具有不同采样率的 OfflineAudioContext 来呈现您的缓冲区。Mozilla 页面上有一个示例。

回答by Julien

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

它现在在规范中,但尚未在 Chromium 中实现。同样在 bugs.chromium.org 中,“状态:可用”并不意味着它已实施。这只是意味着没有人在从事它,并且它可供任何想要从事这项工作的人使用。所以“可用”意味着“未分配”。