如何使用web audio api获取原始pcm音频? [英] How to use web audio api to get raw pcm audio?

查看:613
本文介绍了如何使用web audio api获取原始pcm音频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

usergetmedia如何在Chrome中使用麦克风然后流式传输以获取原始音频?我需要获得线性16的音频。

How usergetmedia to use the microphone in chrome and then stream to get raw audio? I need need to get the audio in linear 16.

推荐答案

不幸的是,MediaRecorder不支持原始PCM捕获。 (在我看来,这是一个悲伤的疏忽。)因此,您需要获取原始样本并自行缓冲/保存。

Unfortunately, the MediaRecorder doesn't support raw PCM capture. (A sad oversight, in my opinion.) Therefore, you'll need to get the raw samples and buffer/save them yourself.

您可以使用< a href =https://developer.mozilla.org/en-US/docs/Web/API/ScriptProcessorNode =nofollow noreferrer> ScriptProcessorNode 。通常,此节点用于以编程方式修改音频数据,用于自定义效果以及不支持的效果。但是,没有理由不能将它用作捕获点。未经测试,但尝试类似这样的代码:

You can do this with the ScriptProcessorNode. Normally, this Node is used to modify the audio data programmatically, for custom effects and what not. But, there's no reason you can't just use it as a capture point. Untested, but try something like this code:

const captureNode = audioContext.createScriptProcessor(8192, 1, 1);
captureNode.addEventListener('audioprocess', (e) => {
  const rawLeftChannelData = inputBuffer.getChannelData(0);
  // rawLeftChannelData is now a typed array with floating point samples
});

(您可以在 MDN 。)

这些浮点样本居中在零 0 上,理想情况下将绑定到 -1 1 。转换为整数范围时,您需要将值限制在此范围内,剪切除此范围之外的任何值。 (如果声音在浏览器中混合在一起,则值有时可能超过 -1 1 。理论上,浏览器还可以记录来自外部声音设备的float32样本,这些样本也可能超出该范围,但我不知道有任何浏览器/平台这样做。)

Those floating point samples are centered on zero 0 and will ideally be bound to -1 and 1. When converting to an integer range, you'll want to clamp values to this range, clipping anything beyond it. (The values can sometimes exceed -1 and 1 in the event loud sounds are mixed together in-browser. In theory, the browser can also record float32 samples from an external sound device which may also exceed that range, but I don't know of any browser/platform that does this.)

转换为整数时,值是有符号还是无符号值很重要。如果签名,对于16位,范围是 -32768 32767 。对于未签名的,它是 0 65535 。找出你想要使用的格式,并将 -1 缩放到 1 值,直到该范围。

When converting to integer, it matters if the values are signed or unsigned. If signed, for 16-bit, the range is -32768 to 32767. For unsigned, it's 0 to 65535. Figure out what format you want to use and scale the -1 to 1 values up to that range.

关于此转换的最后一个注释......字节序可能很重要。另请参阅: https://stackoverflow.com/a/7870190/362536

One final note on this conversion... endianness can matter. See also: https://stackoverflow.com/a/7870190/362536

这篇关于如何使用web audio api获取原始pcm音频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆