目前,我使用的是Web。我设法“读”了一个麦克风,并把它播放给我的扬声器,这是非常无缝的工作。
使用Web,我现在希望重采样一个传入的音频流(又名。麦克风)从44.1千赫到16千赫。16千赫,因为我正在使用一些工具,需要16千赫。由于44.1kHz除以16 not不是整数,我相信我不能简单地使用低通滤波器和“跳过样本”,对吗?
我还看到一些人建议使用.createScriptProcessor()
,但是由于不推荐使用它,我觉得使用它有点糟糕,所以我现在正在寻找一种不同的方法。而且,我不一定需要audioContext.Destination
来听到它!如果我得到了重放输出的“原始”数据,那还是很好的。
到目前为止我的方法
AudioContext({sampleRate: 16000})
->引发一个错误:“当前不支持用不同的采样率从AudioContexts连接AudioNodes。”OfflineAudioContext
AudioWorkletProcessor
重采样。在这种情况下,我认为,我可以使用处理器实际重采样输入和输出“重放”源。但我真的想不出怎么重新整理它。main.js
...
microphoneGranted: async function(stream){
audioContext = new AudioContext();
var microphone = audioContext.createMediaStreamSource(stream);
await audioContext.audioWorklet.addModule('resample_proc.js');
const resampleNode = new AudioWorkletNode(audioContext, 'resample_proc');
microphone.connect(resampleNode).connect(audioContext.destination);
}
...
resample_proc.js (假设只有一个输入和输出通道)
class ResampleProcesscor extends AudioWorkletProcessor {
...
process(inputs, outputs, parameters) {
const input = inputs[0];
const output = outputs[0];
if(input.length > 0){
const inputChannel0 = input[0];
const outputChannel0 = output[0];
for (let i = 0; i < inputChannel0.length; ++i) {
//do something with resample here?
}
return true;
}
}
}
registerProcessor('resample_proc', ResampleProcesscor);
谢谢!
发布于 2020-08-12 15:15:18
你的想法看上去不错。虽然我不能提供代码来进行重采样,但我可以指出,您可能希望从采样率转换开始。方法1适用于L/M = 160/441。设计过滤器需要做一些工作,但只需要完成一次。您还可以搜索多阶段筛选,以获得有关如何有效执行此操作的提示。
铬在不同的部分中所做的是使用一个窗口-sinc函数在任意一组速率之间重新采样。这是维基百科链接中的方法2。
发布于 2022-06-05 20:35:46
WebAudio API现在允许通过在构造函数中传递示例速率来重采样。此代码适用于Chrome和Safari:
const audioStream = await navigator.mediaDevices.getUserMedia({ audio: true, video: false })
const audioContext = new AudioContext({ sampleRate: 16000 })
const audioStreamSource = audioContext.createMediaStreamSource(audioStream);
audioStreamSource.connect(audioContext.destination)
但在抛出带有NotSupportedError
的AudioContext.createMediaStreamSource: Connecting AudioNodes from AudioContexts with different sample-rate is currently not supported.
异常的火狐中失败。
在下面的示例中,我将来自麦克风的音频降到8 8kHz,并增加了1秒钟的延迟,这样我们就可以清楚地听到下采样的效果:https://codesandbox.io/s/magical-rain-xr4g80
https://stackoverflow.com/questions/63348386
复制相似问题