如何使用 Javascript 获取 microphone/input 设备的采样率?

How to get the sample rate of a microphone/input device using Javascript?

当您创建一个新的 AudioContext 时,它会将采样率设置为默认的 output 设备。这是预期的默认行为。有谁知道在Javascript中有没有办法得到输入设备的采样率?

我们可以在 AudioContext 的文档中看到关于 sampleRate

的说明

The value will typically be between 8,000 Hz and 96,000 Hz; the default will vary depending on the output device, but the sample rate 44,100 Hz is the most common. If the sampleRate property is not included in the options, or the options are not specified when creating the audio context, the new context's output device's preferred sample rate is used by default.

我如何使用它的示例:

const stream = await navigator.mediaDevices.getUserMedia({audio: true, video: false});
const context = new AudioContext();
context.sampleRate // This is the default output device's sample rate. I need the default input sampleRate

我一直在搜索文档和互联网寻找执行此操作的方法,但没有找到任何有用的方法。感谢任何帮助。

您可以像这样在流中获取音轨的 sampleRate

const sampleRate = stream.getAudioTracks()[0].getSettings().sampleRate;

然后您可以使用它来创建 AudioContext

const context = new AudioContext({ sampleRate });