AudioWorkletProcessor: process() method
Theprocess()
method of anAudioWorkletProcessor
-derived class implements the audioprocessing algorithm for the audio processor worklet.
Although the method isnot a part of theAudioWorkletProcessor
interface, any implementationofAudioWorkletProcessor
must provide aprocess()
method.
The method is called synchronously from the audio rendering thread, once for each blockof audio (also known as a rendering quantum) being directed through the processor'scorrespondingAudioWorkletNode
. In other words, every time a new block ofaudio is ready for your processor to manipulate, yourprocess()
function isinvoked to do so.
Note:Currently, audio data blocks are always 128 frameslong—that is, they contain 128 32-bit floating-point samples for each of the inputs'channels. However, plans are already in place to revise the specification to allow thesize of the audio blocks to be changed depending on circumstances (for example, if theaudio hardware or CPU utilization is more efficient with larger block sizes).Therefore, youmust always check the size of the sample array rather thanassuming a particular size.
This size may even be allowed to change over time, so you mustn't look at just thefirst block and assume the sample buffers will always be the same size.
In this article
Syntax
process(inputs, outputs, parameters)
Parameters
inputs
An array ofinputs connected to the node, each item of which is, in turn,an array ofchannels. Eachchannel is a
Float32Array
containing 128 samples. For example,inputs[n][m][i]
will accessn-th input,m-th channel of that input, andi-th sampleof that channel.Each sample value is in range of
[-1 .. 1]
.The number ofinputs and thus the length of that array is fixed at theconstruction of the node (see
AudioWorkletNode
). If there isno active node connected to then-th input of the node,inputs[n]
will be an empty array (zero input channels available).The number ofchannels in each input may vary, depending on
channelCount
andchannelCountMode
properties.outputs
An array ofoutputs that is similar to the
inputs
parameter instructure. It is intended to be filled during the execution of theprocess()
method. Each of the output channels is filled with zeros bydefault — the processor will output silence unless the output arrays are modified.parameters
An object containing string keys and
Float32Array
values. For eachcustomAudioParam
defined using theparameterDescriptors
getter, the key in the object is aname
of thatAudioParam
, and the value is aFloat32Array
. The valuesof the array are calculated by taking scheduled automation events intoconsideration.If the automation rate of the parameter is
"a-rate"
, the arraywill contain 128 values — one for each frame in the current audio block. If there'sno automation happening during the time represented by the current block, the arraymay contain a single value that is constant for the entire block, instead of 128identical values.If the automation rate is
"k-rate"
, the arraywill contain a single value, which is to be used for each of 128 frames.
Return value
A Boolean value indicating whether or not to force theAudioWorkletNode
to remain active even if theuser agent's internal logicwould otherwise decide that it's safe to shut down the node.
The returned value lets your processor have influence over the lifetime policy oftheAudioWorkletProcessor
and the node that owns it. If the combinationof the return value and the state of the node causes the browser to decide to stop thenode,process()
will not be called again.
Returningtrue
forces the Web Audio API to keep the node alive,while returningfalse
allows the browser to terminate the node if it isneither generating new audio data nor receiving data through its inputs that it isprocessing.
The 3 most common types of audio node are:
- A source of output. An
AudioWorkletProcessor
implementing such a nodeshould returntrue
from theprocess
method as long as itproduces an output. The method should returnfalse
as soon as it's knownthat it will no longer produce an output. For example, take theAudioBufferSourceNode
— the processor behind such a node should returntrue
from theprocess
method while the buffer is playing,and start returningfalse
when the buffer playing has ended (there's noway to callplay
on the sameAudioBufferSourceNode
again). - A node that transforms its input. A processor implementing such a node should return
false
from theprocess
method to allow the presence ofactive input nodes and references to the node to determine whether it can begarbage-collected. An example of a node with this behavior is theGainNode
. As soon as there are no inputs connected and referencesretained, gain can no longer be applied to anything, so it can be safelygarbage-collected. - A node that transforms its input, but has a so-calledtail-time — thismeans that it will produce an output for some time even after its inputs aredisconnected or are inactive (producing zero-channels). A processor implementing sucha node should return
true
from theprocess
method for theperiod of thetail-time, beginning as soon as inputs are found that containzero-channels. An example of such a node is theDelayNode
— it has atail-time equal to itsdelayTime
property.
Note:An absence of thereturn
statement means that the method returnsundefined
, and as this is a falsy value, it is like returningfalse
.Omitting an explicitreturn
statement may cause hard-to-detect problems for your nodes.
Exceptions
As theprocess()
method is implemented by the user, it can throw anything.If an uncaught error is thrown, the node will emit anprocessorerror
event and willoutput silence for the rest of its lifetime.
Examples
In this example we create anAudioWorkletProcessor
that outputs whitenoise to its first output. The gain can be controlled by thecustomGain
parameter.
class WhiteNoiseProcessor extends AudioWorkletProcessor { process(inputs, outputs, parameters) { // take the first output const output = outputs[0]; // fill each channel with random values multiplied by gain output.forEach((channel) => { for (let i = 0; i < channel.length; i++) { // generate random value for each sample // Math.random range is [0; 1); we need [-1; 1] // this won't include exact 1 but is fine for now for simplicity channel[i] = (Math.random() * 2 - 1) * // the array can contain 1 or 128 values // depending on if the automation is present // and if the automation rate is k-rate or a-rate (parameters["customGain"].length > 1 ? parameters["customGain"][i] : parameters["customGain"][0]); } }); // as this is a source node which generates its own output, // we return true so it won't accidentally get garbage-collected // if we don't have any references to it in the main thread return true; } // define the customGain parameter used in process method static get parameterDescriptors() { return [ { name: "customGain", defaultValue: 1, minValue: 0, maxValue: 1, automationRate: "a-rate", }, ]; }}
Specifications
Specification |
---|
Web Audio API> # process> |
Browser compatibility
This is not a method provided by browsers, but a callback method that must be written in client code.