AudioWorkletNode: AudioWorkletNode() constructor
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2021.
Secure context: This feature is available only insecure contexts (HTTPS), in some or allsupporting browsers.
TheAudioWorkletNode()constructor creates a newAudioWorkletNode object, which represents anAudioNode that uses a JavaScript function to perform custom audioprocessing.
In this article
Syntax
new AudioWorkletNode(context, name)new AudioWorkletNode(context, name, options)Parameters
contextThe
BaseAudioContextinstance this node will be associated with.nameA string, which represents the name of the
AudioWorkletProcessorthisnode will be based on. A processor with the provided name must first be registeredusing theAudioWorkletGlobalScope.registerProcessor()method.optionsOptionalAn object containing zero or more of the following optional properties to configure the new node:
Note:The result ofthe structured clone algorithmapplied to the object is also internally passed into the associated
AudioWorkletProcessor()constructor— this allows custom initialization of an underlying user-definedAudioWorkletProcessor.numberOfInputsOptionalThe value to initialize the
numberOfInputsproperty to. Defaults to 1.numberOfOutputsOptionalThe value to initialize the
numberOfOutputsproperty to. Defaults to 1.outputChannelCountOptionalAnarray defining the number of channels for each output. For example,outputChannelCount: [n, m] specifies the number of channels in the first output to ben and the second output to bem. The array length must match
numberOfOutputs.parameterDataOptionalAn object containing the initial values of custom
AudioParamobjects on this node (in itsparametersproperty), withkeybeing the name of a custom parameter andvaluebeing its initial value.processorOptionsOptionalAny additional data that can be used for custom initialization of the underlying
AudioWorkletProcessor.
Exceptions
NotSupportedErrorDOMExceptionThe specified
options.outputChannelCountis0or largerthan the current implementation supports.Both
options.numberOfInputsandoptions.numberOfOutputsare 0.IndexSizeErrorDOMExceptionThe length of
options.outputChannelCountarray does not matchoptions.numberOfOutputs.
Usage notes
Differentoptions parameter values can have the following effects.
If the number of inputs and number of outputs are both set to 0, aNotSupportedError will be thrown and the node construction process aborted. If the length of theoutputChannelCount array doesn't matchnumberOfOutputs, anIndexSizeErrorDOMException will be thrown.
IfoutputChannelCount isn't specified, andnumberOfInputs andnumberOfOutputs are both 1, theAudioWorkletNode's initial channel count is set to 1. This has the effect of changing the output channel count to dynamically change to the computed number of channels, based on the input's channel count and the current setting of theAudioNode propertychannelCountMode.
Otherwise, ifoutputChannelCount is providedand if the values ofnumberOfInputs andnumberOfOutputs are both 1, the audio worklet node's channel count is set to the value ofoutputChannelCount. Otherwise, the channel count of each channel in the set of output channels is set to match the corresponding value in theoutputChannelCount array.
Examples
For a complete example demonstrating user-defined audio processing, see theAudioWorkletNode page.
Specifications
| Specification |
|---|
| Web Audio API> # dom-audioworkletnode-audioworkletnode> |