files/en-us/web/api/audioworkletnode/index.md
{{APIRef("Web Audio API")}}{{SecureContext_Header}}
[!NOTE] Although the interface is available outside secure contexts, the {{domxref("BaseAudioContext.audioWorklet")}} property is not, thus custom {{domxref("AudioWorkletProcessor")}}s cannot be defined outside them.
The AudioWorkletNode interface of the Web Audio API represents a base class for a user-defined {{domxref("AudioNode")}}, which can be connected to an audio routing graph along with other nodes. It has an associated {{domxref("AudioWorkletProcessor")}}, which does the actual audio processing in a Web Audio rendering thread.
{{InheritanceDiagram}}
AudioWorkletNode object.Also Inherits properties from its parent, {{domxref("AudioNode")}}.
AudioWorkletProcessor. If the AudioWorkletProcessor has a static {{domxref("AudioWorkletProcessor.parameterDescriptors", "parameterDescriptors")}} getter, the {{domxref("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.Also inherits methods from its parent, {{domxref("AudioNode")}}.
The AudioWorkletNode interface does not define any methods of its own.
In this example we create a custom AudioWorkletNode that outputs random noise.
First, we need to define a custom {{domxref("AudioWorkletProcessor")}}, which will output random noise, and register it. Note that this should be done in a separate file.
// 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.
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}}
{{Compat}}