AudioNode.connect()
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.
AudioNode
接口的 connect()
方法使你能将一个节点的输出连接到一个指定目标,这个指定的目标可能是另一个 AudioNode
(从而将音频数据引导到下一个指定节点)或一个AudioParam
, 以便上一个节点的输出数据随着时间流逝能自动地对下一个参数值进行改变。
语法
connect(destination)
connect(destination, outputIndex)
connect(destination, outputIndex, inputIndex)
属性
destination
-
需要连接的
AudioNode
或AudioParam
. outputIndex
可选-
一个索引,用于描述当前
AudioNode
的哪个输出会连接到 destination。索引数字是由输出频道(详见 Audio channels)的数量来确定的。当你只能将给定的输出连接到给定的输入一次(重复的尝试会被忽略)时,可以通过多次调用connect()
将一个输出连接到多个输入。可以通过这样来实现扇出。这个参数的默认值为 0。 inputIndex
可选-
一个索引,用于描述当前
AudioNode
会连接到 destination 的哪个输入,它的默认值是 0。索引数字是由输入频道(详见 Audio channels)的数量来确定的。将一个AudioNode
连接回之前的AudioNode
,以此形成一个圈是可行的。不过只在这个圈里有至少一个DelayNode
才可行。否则会抛出一个NotSupportedError
异常。此参数在 destination 是AudioParam
时不可用。
返回值
异常
IndexSizeError
-
这个异常表明
outputIndex
或inputIndex
与当前输入或输出不符。 InvalidAccessError
-
目标节点与原节点不在同一个音频上下文。
NotSupportedError
-
该链接会形成一个闭环(音频在这个环里不断重复经过同一个节点)并且这个闭环里没有
DelayNode
来防止产生的波形被卡住,不停地构建相同的音频帧。
示例
Connecting to an audio input
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 HTML5 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.
var AudioContext = window.AudioContext || window.webkitAudioContext;
var audioCtx = new AudioContext();
var oscillator = audioCtx.createOscillator();
var 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.
var AudioContext = window.AudioContext || window.webkitAudioContext;
var audioCtx = new AudioContext();
// create an normal oscillator to make sound
var oscillator = audioCtx.createOscillator();
// create a second oscillator that will be used as an LFO (Low-frequency
// oscillator), and will control a parameter
var lfo = audioCtx.createOscillator();
// set the frequency of the second oscillator to a low number
lfo.frequency.value = 2.0; // 2Hz: two oscillations par second
// create a gain whose gain AudioParam will be controlled by the LFO
var gain = audioCtx.createGain();
// connect the LFO to the gain AudioParam. This means the value of the LFO
// will not produce any audio, but will change the value of the gain instead
lfo.connect(gain.gain);
// connect the oscillator that will produce audio to the gain
oscillator.connect(gain);
// connect the gain to the destination so we hear sound
gain.connect(audioCtx.destination);
// start the oscillator that will produce audio
oscillator.start();
// start the oscillator that will modify the gain value
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
Specification |
---|
Web Audio API # dom-audionode-connect |
Web Audio API # dom-audionode-connect-destinationparam-output |
Browser compatibility
BCD tables only load in the browser