web audio api - How to get the sample rate of a microphoneinput device using Javascript? - Stack Overflow

When you create a new AudioContext it sets the sample rate to the default output device. This is expect

When you create a new AudioContext it sets the sample rate to the default output device. This is expected default behavior. Does anyone know if there is there any way to get the sample rate of the input device in Javascript?

We can see in the docs for AudioContext it says this about 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 mon. 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.

Example of how I'm using it:

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

I've been scouring the docs and the internet for a way to do this but have not found anything useful. Appreciate any help.

When you create a new AudioContext it sets the sample rate to the default output device. This is expected default behavior. Does anyone know if there is there any way to get the sample rate of the input device in Javascript?

We can see in the docs for AudioContext it says this about 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 mon. 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.

Example of how I'm using it:

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

I've been scouring the docs and the internet for a way to do this but have not found anything useful. Appreciate any help.

Share Improve this question asked Mar 17, 2022 at 2:02 avolivaavoliva 3,3765 gold badges25 silver badges37 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

You can get the sampleRate of the audio track within the stream like this:

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

You can then use that to create the AudioContext.

const context = new AudioContext({ sampleRate });

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745322204a4622493.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信