javascript - 网络音频 API : How to access time/sample rate in a AudioWorkletProcessor?

标签 javascript html web-audio-api

我想实现一个可以感知时间的 AudioWorkletProcessor。例如:如何将 DelayNode 重新实现为处理器?

MDN 文档说:

By specification, each block of audio your process() function receives contains 128 frames (that is, 128 samples for each channel), but it is planned that this value will change in the future, and may in fact vary depending on circumstances, so you should always check the array's length rather than assuming a particular size.

我可以通过输入的长度获得帧数,但如何获得使用的采样率?这样我就可以知道这个输入有多长时间(以秒为单位)。

我的最终目标是能够计算某个时间窗口内信号的平均能量。

class EnergyProcessor extends AudioWorkletProcessor {
  process(inputs, outputs, parameters) {
    if (inputs.length !== 1) {
      throw 'invalid inputs'
    }

    // how much time is covered by inputs?

    inputs[0].forEach((channel, channelID) => {
      let sum = 0
      let count = 0

      channel.forEach((value, i) => {
        sum += value * value
        count += 1

        for (let o = 0; o < outputs.length; o++) {
          // skip when writing x channels to x - 1
          if (channelID >= outputs[o].length) {
            continue
          }
          outputs[o][channelID][i] = sum / count
        }
      })
    })

    return true
  }
}

registerProcessor('EnergyProcessor', EnergyProcessor)

最佳答案

The MDN says那个

[...] lives in the AudioWorkletGlobalScope and runs on the Web Audio rendering thread.

AudioWorkletGlobalScope据说有对其上下文的引用

sampleRate: Read only
Returns a float that represents the sample rate of the associated BaseAudioContext.

所以你可能可以简单地、神奇地

console.log(sampleRate)

或者您需要做的任何事情。

关于javascript - 网络音频 API : How to access time/sample rate in a AudioWorkletProcessor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62651602/

相关文章:

javascript - 浏览器视口(viewport)外的模态弹出窗口

javascript - 通过 Redux 中的回调分派(dispatch)操作

javascript - 带有 Web 音频节点的 Jquery UI slider

ASP.NET 中的 Javascript 命名空间声明

javascript - 如何在javascript中委托(delegate)它?

javascript - IE9无法获取属性值 'documentElement'

javascript - jQuery 不改变元素背景颜色

Javascript 类名操作不起作用

javascript - PhantomJS分析mp3文件的FFT数据并保存

javascript - PhoneGap 构建服务 "Build in HTML5, CSS, JavaScript"声明