AudioNode: connect() 方法
AudioNode 接口的 connect() 方法允许您将该节点的一个输出连接到一个目标。目标可以是另一个 AudioNode(从而将声音数据定向到指定的节点),也可以是 AudioParam,这样节点输出的数据就会自动用于随时间改变该参数的值。
语法
connect(destination)
connect(destination, outputIndex)
connect(destination, outputIndex, inputIndex)
参数
destination-
要连接到的
AudioNode或AudioParam。 outputIndex可选-
一个指定当前
AudioNode的哪个输出连接到目标的索引。索引号根据输出通道数定义(参见 音频通道)。虽然您只能将给定的输出连接到给定的输入一次(重复的尝试将被忽略),但您可以通过重复调用connect()将一个输出连接到多个输入。这使得 扇出 成为可能。默认值为 0。 inputIndex可选-
一个描述您希望当前
AudioNode连接到目标哪个输入的索引;默认值为 0。索引号根据输入通道数定义(参见 音频通道)。可以将一个AudioNode连接到另一个AudioNode,而后者又连接回第一个AudioNode,从而形成一个循环。
返回值
如果目标是一个节点,connect() 会返回对目标 AudioNode 对象的引用,允许您链式调用多个 connect() 调用。在某些浏览器中,此接口的旧实现会返回 undefined。
如果目标是 AudioParam,connect() 会返回 undefined。
异常
IndexSizeErrorDOMException-
如果指定的
outputIndex或inputIndex与现有输入或输出不匹配,则抛出此异常。 InvalidAccessErrorDOMException-
如果目标节点与源节点不在同一个音频上下文中,则抛出此异常。
NotSupportedErrorDOMException-
如果指定的连接会创建一个循环(音频会通过相同的节点反复循环)且循环中没有
DelayNode对象来防止生成的波形无限地卡在构建相同的音频帧,则抛出此异常。如果目标是AudioParam时使用了inputIndex参数,也会抛出此异常。
示例
连接到音频输入
connect() 方法最明显的用途是将一个节点的音频输出定向到另一个节点的音频输入以进行进一步处理。例如,您可能想将来自 MediaElementAudioSourceNode 的音频(即来自 HTML 媒体元素的音频,如 <audio>)通过使用 BiquadFilterNode 实现的带通滤波器进行处理,以在将音频发送到扬声器之前降低噪音。
此示例创建一个振荡器,然后将其链接到一个增益节点,以便增益节点控制振荡器节点的音量。
const audioCtx = new AudioContext();
const oscillator = audioCtx.createOscillator();
const gainNode = audioCtx.createGain();
oscillator.connect(gainNode);
gainNode.connect(audioCtx.destination);
AudioParam 示例
在此示例中,我们将使用一个低频值(频率很低)的 OscillatorNode 来改变 GainNode 的增益值。这种技术被称为LFO 控制的参数。
const audioCtx = new AudioContext();
// create an normal oscillator to make sound
const oscillator = audioCtx.createOscillator();
// create a second oscillator that will be used as an LFO (Low-frequency
// oscillator), and will control a parameter
const lfo = audioCtx.createOscillator();
// set the frequency of the second oscillator to a low number
lfo.frequency.value = 2.0; // 2Hz: two oscillations per second
// create a gain whose gain AudioParam will be controlled by the LFO
const 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 说明
通过多次调用 connect(),可以将一个 AudioNode 输出连接到一个以上的 AudioParam,也可以将一个以上的 AudioNode 输出连接到单个 AudioParam。因此,支持 扇入和扇出。
AudioParam 将接收连接到它的任何 AudioNode 输出的渲染音频数据,并通过 降采样(如果它不是单声道的话)将其转换为单声道。接下来,它会将这些数据与任何其他此类输出以及参数的固有值(即 AudioParam 在没有任何音频连接时的正常值),包括为参数安排的任何时间线更改混合在一起。
因此,您可以通过将 AudioParam 的值设置为中心频率来选择 AudioParam 将发生变化的范围,并且可以使用音频源和 AudioParam 之间的 GainNode 来调整 AudioParam 变化的范围。
规范
| 规范 |
|---|
| Web Audio API # dom-audionode-connect |
| Web Audio API # dom-audionode-connect-destinationparam-output |
浏览器兼容性
加载中…