diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/audionode/connect | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/audionode/connect')
-rw-r--r-- | files/zh-cn/web/api/audionode/connect/index.html | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/audionode/connect/index.html b/files/zh-cn/web/api/audionode/connect/index.html new file mode 100644 index 0000000000..a0b5a763cb --- /dev/null +++ b/files/zh-cn/web/api/audionode/connect/index.html @@ -0,0 +1,145 @@ +--- +title: AudioNode.connect() +slug: Web/API/AudioNode/connect +translation_of: Web/API/AudioNode/connect +--- +<p>{{ APIRef("Web Audio API") }}</p> + +<div> +<p>{{ domxref("AudioNode") }} 接口的 <code>connect()</code> 方法使你能将一个节点的输出连接到一个指定目标,这个指定的目标可能是另一个 <code>AudioNode</code>(从而将音频数据引导到下一个指定节点)或一个{{domxref("AudioParam")}}, 以便上一个节点的输出数据随着时间流逝能自动地对下一个参数值进行改变。</p> +</div> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">var <em>destinationNode</em> = <em>AudioNode</em>.connect(<em>destination</em>, <em>outputIndex</em>, <em>inputIndex</em>); + +<em>AudioNode</em>.connect(<em>destination</em>, <em>outputIndex</em>); +</pre> + +<h3 id="属性">属性</h3> + +<dl> + <dt><code>destination</code></dt> + <dd>需要连接的 {{domxref("AudioNode")}} 或 {{domxref("AudioParam")}}.</dd> + <dt><code>outputIndex</code> {{optional_inline}}</dt> + <dd>一个索引,用于描述当前 <code>AudioNode</code> 的哪个输出会连接到destination。索引数字是由输出频道(详见 <a href="/en-US/docs/Web/API/Web_Audio_API/Basic_concepts_behind_Web_Audio_API#Audio_channels">Audio channels</a>)的数量来确定的。当你只能将给定的输出连接到给定的输入一次(重复的尝试会被忽略)时,可以通过多次调用 <code>connect()</code> 将一个输出连接到多个输入。可以通过这样来实现扇出。这个参数的默认值为0。</dd> + <dt><code>inputIndex</code> {{optional_inline}}</dt> + <dd>一个索引,用于描述当前 <code>AudioNode</code> 会连接到destination的哪个输入,它的默认值是0。索引数字是由输入频道(详见 <a href="/en-US/docs/Web/API/Web_Audio_API/Basic_concepts_behind_Web_Audio_API#Audio_channels">Audio channels</a>)的数量来确定的。将一个 <code>AudioNode</code> 连接回之前的 <code>AudioNode</code>,以此形成一个圈是可行的。 不过只在这个圈里有至少一个 {{domxref("DelayNode")}} 才可行。否则会抛出一个 <code>NotSupportedError</code> 异常。此参数在destination是 {{domxref("AudioParam")}}时不可用。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>如果destination是一个节点, <code>connect()</code> 返回destination所表示的 {{domxref("AudioNode")}} 对象的引用,允许你链式地调用数个 <code>connect()</code> 。某些浏览器关于该接口的旧实现会返回 {{jsxref("undefined")}}。</p> + +<p>如果destination是一个 <code>AudioParam</code>,<code>connect()</code> 返回 <code>undefined</code>.</p> + +<h3 id="异常">异常</h3> + +<dl> + <dt><code>IndexSizeError</code></dt> + <dd>这个异常表明 <code>outputIndex</code> 或 <code>inputIndex</code> 与当前输入或输出不符。</dd> + <dt><code>InvalidAccessError</code></dt> + <dd>目标节点与原节点不在同一个音频上下文。</dd> + <dt><code>NotSupportedError</code></dt> + <dd>该链接会形成一个闭环(音频在这个环里不断重复经过同一个节点)并且这个闭环里没有 {{domxref("DelayNode")}} 来防止产生的波形被卡住,不停地构建相同的音频帧。</dd> +</dl> + +<h2 id="示例">示例</h2> + +<h3 id="Connecting_to_an_audio_input">Connecting to an audio input</h3> + +<p>The most obvious use of the <code>connect()</code> 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 {{domxref("MediaElementAudioSourceNode")}}—that is, the audio from an HTML5 media element such as {{HTMLElement("audio")}}—through a band pass filter implemented using a {{domxref("BiquadFilterNode")}} to reduce noise before then sending the audio along to the speakers.</p> + +<p>This example creates an oscillator, then links it to a gain node, so that the gain node controls the volume of the oscillator node.</p> + +<pre class="brush: js" style="font-size: 14px;">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); +</pre> + +<h3 id="AudioParam_example">AudioParam example</h3> + +<p>In this example, we will be altering the gain value of a {{domxref("GainNode")}} using an {{domxref("OscillatorNode")}} with a slow frequency value. This technique is know as an <em>LFO</em>-controlled parameter.</p> + +<pre class="brush: js;highlight[8,9]">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();</pre> + +<h4 id="AudioParam_notes">AudioParam notes</h4> + +<p>It is possible to connect an <code>AudioNode</code> output to more than one {{ domxref("AudioParam") }}, and more than one AudioNode output to a single {{ domxref("AudioParam") }}, with multiple calls to <code>connect()</code>. <a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API/Basic_concepts_behind_Web_Audio_API#Fan-in_and_Fan-out">Fan-in and fan-out</a> are therefore supported.</p> + +<p>An {{ domxref("AudioParam") }} will take the rendered audio data from any <code>AudioNode</code> output connected to it and convert it to mono by <a href="/en-US/docs/Web/API/Web_Audio_API/Basic_concepts_behind_Web_Audio_API#Up-mixing_and_down-mixing">down-mixing</a> (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 {{ domxref("AudioParam") }} would normally have without any audio connections), including any timeline changes scheduled for the parameter.</p> + +<p>Therefore, it is possible to choose the range in which an {{domxref("AudioParam")}} will change by setting the value of the {{domxref("AudioParam")}} to the central frequency, and to use a {{domxref("GainNode")}} between the audio source and the {{domxref("AudioParam")}} to adjust the range of the {{domxref("AudioParam")}} changes.</p> + +<h2 id="Specifications">Specifications</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('Web Audio API', '#widl-AudioNode-connect-AudioNode-AudioNode-destination-unsigned-long-output-unsigned-long-input', 'connect() to an AudioNode')}}</td> + <td>{{Spec2('Web Audio API')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('Web Audio API', '#widl-AudioNode-connect-void-AudioParam-destination-unsigned-long-output', 'connect() to an AudioParam')}}</td> + <td>{{Spec2('Web Audio API')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + +<div> + + +<p>{{Compat("api.AudioNode.connect")}}</p> +</div> + +<h2 id="See_also">See also</h2> + +<ul> + <li><a href="/en-US/docs/Web/API/Web_Audio_API/Using_Web_Audio_API">Using the Web Audio API</a></li> +</ul> |