files/en-us/web/api/audioworkletglobalscope/registerprocessor/index.md
{{ APIRef("Web Audio API") }}
The registerProcessor method of the
{{domxref("AudioWorkletGlobalScope")}} interface registers a class constructor derived
from {{domxref("AudioWorkletProcessor")}} interface under a specified name.
registerProcessor(name, processorCtor)
name
processorCtor
[!NOTE] A key-value pair
{ name: constructor }is saved internally in the {{domxref("AudioWorkletGlobalScope")}} once the processor is registered. The name is to be referred to when creating an {{domxref("AudioWorkletNode")}} based on the registered processor. A new processor by the given name is internally created and associated with the new node.
None ({{jsxref("undefined")}}).
NotSupportedError {{domxref("DOMException")}}
{{jsxref("TypeError")}}
In this example we create a custom AudioWorkletNode that outputs silence.
First, we need to define a custom {{domxref("AudioWorkletProcessor")}} and register it. Note that this should be done in a separate file.
// test-processor.js
class TestProcessor extends AudioWorkletProcessor {
process(inputs, outputs, parameters) {
return true;
}
}
registerProcessor("test-processor", TestProcessor);
Next, in our main script file we'll load the processor, create an instance of
AudioWorkletNode — passing it the processor name that we used when calling
registerProcessor — and connect it to an audio graph.
const audioContext = new AudioContext();
await audioContext.audioWorklet.addModule("test-processor.js");
const node = new AudioWorkletNode(audioContext, "test-processor");
node.connect(audioContext.destination);
{{Specifications}}
{{Compat}}