Web音频分析器节点-定期运行 [英] Web audio analyser node - run at regular interval

查看:104
本文介绍了Web音频分析器节点-定期运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用在特定频率上检测音频信号(摩尔斯电码)网络音频.我使用分析器节点的getFloatFrequencyData函数检索频率数据.

I want to detect an audio signal (morse code) on specific frequencies using web audio. I retrieve the frequency data using an analyser node's getFloatFrequencyData function.

现在的问题是:使用setInterval()定期采样频率数据还不够规则:回调比预期的要早或晚执行几毫秒.

Now the problem: Using setInterval() to regularly sample the frequency data is not regular enough: the callback gets executed a few milliseconds earlier or later than expected.

我如何才能每隔几毫秒定期准确地获取分析仪的频率数据?我更喜欢使用内置分析仪节点的FFT功能,而不是借助例如手动处理音频数据的方法. Goertzel 算法.

How can I retrieve the analyser's frequency data regularly exactly every few miliseconds? I would prefer using the built-in analyser node's FFT functionality instead of resorting to manually processing the audio data via e.g. Goertzel algorithm.

有问题的setInterval()的代码示例:

Code sample with the problematic setInterval():

// Analyse microphone audio frequencies: 
function onStream(stream) {
  let audioCtx = new(window.AudioContext || window.webkitAudioContext)(),
    source = audioCtx.createMediaStreamSource(stream),
    analyser = audioCtx.createAnalyser(),
    fft = new Float32Array(analyser.frequencyBinCount);

  source.connect(analyser);

  // Doesn't execute exactly every 100ms as needed - what to do?
  setInterval(() => {
    analyser.getFloatFrequencyData(fft);
    console.log(performance.now(), fft[0]);
  }, 100);
}

navigator.mediaDevices.getUserMedia({audio: true}).then(onStream);

推荐答案

是的,您不能真正使用分析仪.它什么时候开始运行存在太多不确定性,您不能保证它何时才能准确运行.您最好暂时使用ScriptProcessor(最终使用AudioWorklet),然后自己进行FFT(或其他识别代码).

Yeah, you can't really use an analyzer. There's too much uncertainty in when it will get run, and you can't guarantee precisely when it will run. You're better off using a ScriptProcessor for now (AudioWorklet eventually), and doing the FFT (or other recognition code) yourself.

这篇关于Web音频分析器节点-定期运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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