javascript - 如何使用 Javascript 获取麦克风/输入设备的采样率?

标签 javascript web-audio-api audiocontext sample-rate

当您创建新的 AudioContext 时,它将采样率设置为默认的输出设备。这是预期的默认行为。有谁知道是否有什么方法可以在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 });

关于javascript - 如何使用 Javascript 获取麦克风/输入设备的采样率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71506257/

相关文章:

Javascript setinterval 和 clearinterval

javascript - html2canvas 如何循环或重用多个 div/html dom 元素

回调中未调用 Javascript 警报

javascript - createMediaElementSource()性能

audio - 与FFT中的一系列频率相关的幅度

javascript - 网络音频 api 卷积器似乎没有输出零

javascript - Firefox Audiocontext 暂停

javascript - 使用 CSS/JS 在每一行的开头或结尾附加一些内容?

javascript - AudioContext 振荡器不会播放多次

javascript - 网络音频延迟节点指南