AudioWorkletNode

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

AudioWorkletNode interface of the Web 音频 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.

构造函数

AudioWorkletNode()
Creates a new instance of an AudioWorkletNode 对象。

特性

Also Inherits properties from its parent, AudioNode .

AudioWorkletNode.port 只读
返回 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 只读
Returns an AudioParamMap — a collection of AudioParam objects. They are instantiated during the creation of the underlying AudioWorkletProcessor 。若 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 .

事件处理程序

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.

方法

Also inherits methods from its parent, AudioNode .

AudioWorkletNode interface does not define any methods of its own.

范例

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)
					

规范

规范 状态 Comment
Web 音频 API
The definition of 'AudioWorkletNode' in that specification.
工作草案

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
AudioWorkletNode Chrome 66 Edge 79 Firefox 76 IE 不支持 No Opera Yes Safari 不支持 No WebView Android 66 Chrome Android 66 Firefox Android 不支持 No Opera Android Yes Safari iOS 不支持 No Samsung Internet Android 9.0
AudioWorkletNode() 构造函数 Chrome 66 Edge 79 Firefox 76 IE 不支持 No Opera ? Safari 不支持 No WebView Android 66 Chrome Android 66 Firefox Android 不支持 No Opera Android ? Safari iOS 不支持 No Samsung Internet Android 9.0
onprocessorerror Chrome 66 Edge 79 Firefox 76 IE 不支持 No Opera ? Safari 不支持 No WebView Android 66 Chrome Android 66 Firefox Android 不支持 No Opera Android ? Safari iOS 不支持 No Samsung Internet Android 9.0
parameters Chrome 66 Edge 79 Firefox 76 IE 不支持 No Opera Yes Safari 不支持 No WebView Android 66 Chrome Android 66 Firefox Android 不支持 No Opera Android Yes Safari iOS 不支持 No Samsung Internet Android 9.0
port Chrome 66 Edge 79 Firefox 76 IE 不支持 No Opera Yes Safari 不支持 No WebView Android 66 Chrome Android 66 Firefox Android 不支持 No Opera Android Yes Safari iOS 不支持 No Samsung Internet Android 9.0
processorerror event Chrome 66 Edge 79 Firefox 76 IE 不支持 No Opera ? Safari 不支持 No WebView Android 66 Chrome Android 66 Firefox Android 不支持 No Opera Android ? Safari iOS 不支持 No Samsung Internet Android 9.0

图例

完整支持
完整支持
不支持
不支持
兼容性未知
兼容性未知

另请参阅