AudioNode: connect() method
The connect()
method of the AudioNode
interface lets you connect one of the node's outputs to a target, which may be either another AudioNode
(thereby directing the sound data to the specified node) or an AudioParam
, so that the node's output data is automatically used to change the value of that parameter over time.
Syntax
connect(destination)
connect(destination, outputIndex)
connect(destination, outputIndex, inputIndex)
Parameters
destination
-
The AudioNode
or AudioParam
to which to connect.
-
outputIndex
Optional
-
An index specifying which output of the current AudioNode
to connect to the destination. The index numbers are defined according to the number of output channels (see Audio channels). While you can only connect a given output to a given input once (repeated attempts are ignored), you can connect an output to multiple inputs by calling connect()
repeatedly. This makes fan-out possible. The default value is 0.
-
inputIndex
Optional
-
An index describing which input of the destination you want to connect the current AudioNode
to; the default is 0. The index numbers are defined according to the number of input channels (see Audio channels). It is possible to connect an AudioNode
to another AudioNode
, which in turn connects back to the first AudioNode
, creating a cycle.
Return value
If the destination is a node, connect()
returns a reference to the destination AudioNode
object, allowing you to chain multiple connect()
calls. In some browsers, older implementations of this interface return undefined
.
If the destination is an AudioParam
, connect()
returns undefined
.
Exceptions
-
IndexSizeError
DOMException
-
Thrown if the value specified as outputIndex
or inputIndex
doesn't correspond to an existing input or output.
-
InvalidAccessError
DOMException
-
Thrown if the destination node is not part of the same audio context as the source node.
-
NotSupportedError
DOMException
-
Thrown if the specified connection would create a cycle (in which the audio loops back through the same nodes repeatedly) and there are no DelayNode
objects in the cycle to prevent the resulting waveform from getting stuck constructing the same audio frame indefinitely. Also thrown if the inputIndex
parameter is used while the destination is an AudioParam
.
Examples
The most obvious use of the connect()
method is to direct the audio output from one node into the audio input of another node for further processing. For example, you might send the audio from a MediaElementAudioSourceNode
—that is, the audio from an HTML media element such as <audio>
—through a band pass filter implemented using a BiquadFilterNode
to reduce noise before then sending the audio along to the speakers.
This example creates an oscillator, then links it to a gain node, so that the gain node controls the volume of the oscillator node.
const audioCtx = new AudioContext();
const oscillator = audioCtx.createOscillator();
const gainNode = audioCtx.createGain();
oscillator.connect(gainNode);
gainNode.connect(audioCtx.destination);
AudioParam example
In this example, we will be altering the gain value of a GainNode
using an OscillatorNode
with a slow frequency value. This technique is know as an LFO-controlled parameter.
const audioCtx = new AudioContext();
const oscillator = audioCtx.createOscillator();
const lfo = audioCtx.createOscillator();
lfo.frequency.value = 2.0;
const gain = audioCtx.createGain();
lfo.connect(gain.gain);
oscillator.connect(gain);
gain.connect(audioCtx.destination);
oscillator.start();
lfo.start();
AudioParam notes
It is possible to connect an AudioNode
output to more than one AudioParam
, and more than one AudioNode output to a single AudioParam
, with multiple calls to connect()
. Fan-in and fan-out are therefore supported.
An AudioParam
will take the rendered audio data from any AudioNode
output connected to it and convert it to mono by down-mixing (if it is not already mono). Next, it will mix it together with any other such outputs, and the intrinsic parameter value (the value the AudioParam
would normally have without any audio connections), including any timeline changes scheduled for the parameter.
Therefore, it is possible to choose the range in which an AudioParam
will change by setting the value of the AudioParam
to the central frequency, and to use a GainNode
between the audio source and the AudioParam
to adjust the range of the AudioParam
changes.
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 |
connect |
14 |
12 |
25 |
No |
15 |
6 |
4.4.3 |
18 |
25 |
14 |
6 |
1.0 |
See also