aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/audionode/connect/index.html
blob: a0b5a763cb1a41bc24279db123b6ff433bec626c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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>