The AudioWorkletProcessor() constructor creates a new AudioWorkletProcessor object, which represents an underlying audio processing mechanism of an AudioWorkletNode.
On this page
AudioWorkletProcessor: AudioWorkletProcessor() constructor
Syntax
Note: The AudioWorkletProcessor and classes that derive from it cannot be instantiated directly from a user-supplied code. Instead, they are created only internally by the creation of an associated AudioWorkletNodes.
js
new AudioWorkletProcessor(options)
Parameters
-
options -
An object that is passed as options parameter to the
AudioWorkletNode constructorand passed through the structured clone algorithm. Available properties are as follows:numberOfInputsOptional-
The value to initialize the
numberOfInputsproperty to. Defaults to 1. numberOfOutputsOptional-
The value to initialize the
numberOfOutputsproperty to. Defaults to 1. outputChannelCountOptional-
An array defining the number of channels for each output. For example, outputChannelCount: [n, m] specifies the number of channels in the first output to be n and the second output to be m. The array length must match
numberOfOutputs. parameterDataOptional-
An object containing the initial values of custom
AudioParamobjects on this node (in itsparametersproperty), withkeybeing the name of a custom parameter andvaluebeing its initial value. processorOptionsOptional-
Any additional data that can be used for custom initialization of the underlying
AudioWorkletProcessor.
Note that there are default values for the first two properties, so even if there are no options object passed to the
AudioWorkletNode constructor, the options object passed by the node to theAudioWorkletProcessorconstructor will exist and at minimum havenumberOfInputsandnumberOfOutputs.
Return value
The newly constructed AudioWorkletProcessor instance.
Examples
In this example we pass custom options to the AudioWorkletNode constructor and observe how a structured clone of them gets passed to our AudioWorkletProcessor constructor.
First, we need to define a custom AudioWorkletProcessor and register it. Note that this should be done in a separate file.
js
// test-processor.js
class TestProcessor extends AudioWorkletProcessor {
constructor(options) {
super();
console.log(options.numberOfInputs);
console.log(options.processorOptions.someUsefulVariable);
}
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 name of the processor and options object.
In the options object we pass processorOptions with a Map instance under someUsefulVariable key. We don't pass numberOfInputs and see how it gets its default value.
js
const audioContext = new AudioContext();
await audioContext.audioWorklet.addModule("test-processor.js");
const testNode = new AudioWorkletNode(audioContext, "test-processor", {
processorOptions: {
someUsefulVariable: new Map([
[1, "one"],
[2, "two"],
]),
},
});
The console output will be as follows:
> 1 // AudioWorkletNode options.numberOfInputs set to default
> Map(2) { 1 => "one", 2 => "two" } // A cloned map under someUsefulVariable
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 | |
AudioWorkletProcessor |
64 | 79 | 76 | No | 51 | 14.1 | 64 | 64 | 79 | 47 | 14.5 | 9.0 |
See also
AudioWorkletNodeinterface
© 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/AudioWorkletProcessor/AudioWorkletProcessor