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 |
瀏覽器相容性
載入中…