W3cubDocs

/Web APIs

AudioWorkletNode

Note: 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.

EventTarget AudioNode AudioWorkletNode

Constructor

AudioWorkletNode()

Creates a new instance of an AudioWorkletNode object.

Instance 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.

Events

processorerror

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

Instance 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 random noise.

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

js

// random-noise-processor.js
class RandomNoiseProcessor 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("random-noise-processor", RandomNoiseProcessor);

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.

js

const audioContext = new AudioContext();
await audioContext.audioWorklet.addModule("random-noise-processor.js");
const randomNoiseNode = new AudioWorkletNode(
  audioContext,
  "random-noise-processor",
);
randomNoiseNode.connect(audioContext.destination);

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
AudioWorkletNode 66 79 76 No 53 14.1 66 66 79 47 14.5 9.0
AudioWorkletNode 66 79 76 No 53 14.1 66 66 79 47 14.5 9.0
parameters 66 79 76 No 53 14.1 66 66 79 47 14.5 9.0
port 66 79 76 No 53 14.1 66 66 79 47 14.5 9.0
processorerror_event 66 79 76 No 53 14.1 66 66 79 47 14.5 9.0

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/AudioWorkletNode