AudioWorkletNode - Web APIs 编辑

Although the interface is available outside secure contexts, the BaseAudioContext.audioWorklet property is not, thus custom AudioWorkletProcessors cannot be defined outside them.

The AudioWorkletNode interface of the Web Audio API represents a base class for a user-defined AudioNode, which can be connected to an audio routing graph along with other nodes. It has an associated AudioWorkletProcessor, which does the actual audio processing in a Web Audio rendering thread.

Constructor

AudioWorkletNode()
Creates a new instance of an AudioWorkletNode object.

Properties

Also Inherits properties from its parent, AudioNode.

AudioWorkletNode.port Read only
Returns a MessagePort used for bidirectional communication between the node and its associated AudioWorkletProcessor. The other end is available under the port property of the processor.
AudioWorkletNode.parameters Read only
Returns an AudioParamMap — a collection of AudioParam objects. They are instantiated during the creation of the underlying AudioWorkletProcessor. If the AudioWorkletProcessor has a static parameterDescriptors getter, the AudioParamDescriptor array returned from it is used to create AudioParam objects on the AudioWorkletNode. With this mechanism it is possible to make your own AudioParam objects accessible from your AudioWorkletNode. You can then use their values in the associated AudioWorkletProcessor.

Event handlers

AudioWorkletNode.onprocessorerror
Fired when an error is thrown in associated AudioWorkletProcessor. Once fired, the processor and consequently the node will output silence throughout its lifetime.

Methods

Also inherits methods from its parent, AudioNode.

The AudioWorkletNode interface does not define any methods of its own.

Examples

In this example we create a custom AudioWorkletNode that outputs white noise.

First, we need to define a custom AudioWorkletProcessor, which will output white noise, and register it. Note that this should be done in a separate file.

// white-noise-processor.js
class WhiteNoiseProcessor extends AudioWorkletProcessor {
  process (inputs, outputs, parameters) {
    const output = outputs[0]
    output.forEach(channel => {
      for (let i = 0; i < channel.length; i++) {
        channel[i] = Math.random() * 2 - 1
      }
    })
    return true
  }
}

registerProcessor('white-noise-processor', WhiteNoiseProcessor)

Next, in our main script file we'll load the processor, create an instance of AudioWorkletNode passing it the name of the processor, and connect the node to an audio graph.

const audioContext = new AudioContext()
await audioContext.audioWorklet.addModule('white-noise-processor.js')
const whiteNoiseNode = new AudioWorkletNode(audioContext, 'white-noise-processor')
whiteNoiseNode.connect(audioContext.destination)

Specifications

SpecificationStatusComment
Web Audio API
The definition of 'AudioWorkletNode' in that specification.
Working Draft

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:197 次

字数:6248

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文