5 <script src=
"../resources/js-test.js"></script>
6 <script src=
"resources/compatibility.js"></script>
7 <script src=
"resources/audio-testing.js"></script>
12 description('Test disconnect() method on AudioNode destination.');
13 window
.jsTestIsAsync
= true;
15 var audit
= Audit
.createTaskRunner();
17 // Task 1: test disconnect() method.
18 audit
.defineTask('disconnect()', function (done
) {
20 // Connect a source to multiple gain nodes, each connected to the
21 // destination. Then disconnect the source. The expected output should be
22 // all zeros since the source was disconnected.
23 var context
= new OfflineAudioContext(1, 128, 44100);
24 var source
= context
.createBufferSource();
25 var buffer1ch
= createTestingAudioBuffer(context
, 1, 128);
26 var gain1
= context
.createGain();
27 var gain2
= context
.createGain();
28 var gain3
= context
.createGain();
30 source
.buffer
= buffer1ch
;
32 source
.connect(gain1
);
33 source
.connect(gain2
);
34 source
.connect(gain3
);
35 gain1
.connect(context
.destination
);
36 gain2
.connect(context
.destination
);
37 gain3
.connect(context
.destination
);
40 // This disconnects everything.
43 context
.startRendering().then(function (buffer
) {
45 // With everything disconnected, the result should be zero.
46 Should('Channel #0', buffer
.getChannelData(0)).beConstantValueOf(0);
51 // Task 2: test disconnect(output) method.
52 audit
.defineTask('disconnect(output)', function (done
) {
54 // Create multiple connections from each output of a ChannelSplitter
55 // to a gain node. Then test if disconnecting a single output of splitter
56 // is actually disconnected.
57 var context
= new OfflineAudioContext(1, 128, 44100);
58 var source
= context
.createBufferSource();
59 var buffer3ch
= createTestingAudioBuffer(context
, 3, 128);
60 var splitter
= context
.createChannelSplitter(3);
61 var sum
= context
.createGain();
63 source
.buffer
= buffer3ch
;
65 source
.connect(splitter
);
66 splitter
.connect(sum
, 0);
67 splitter
.connect(sum
, 1);
68 splitter
.connect(sum
, 2);
69 sum
.connect(context
.destination
);
72 // This disconnects the second output.
73 splitter
.disconnect(1);
75 context
.startRendering().then(function (buffer
) {
77 // The rendered channel should contain 4. (= 1 + 0 + 3)
78 Should('Channel #0', buffer
.getChannelData(0)).beConstantValueOf(4);
83 // Task 3: test disconnect(AudioNode) method.
84 audit
.defineTask('disconnect(AudioNode)', function (done
) {
86 // Connect a source to multiple gain nodes. Then test if disconnecting a
87 // single destination selectively works correctly.
88 var context
= new OfflineAudioContext(1, 128, 44100);
89 var source
= context
.createBufferSource();
90 var buffer1ch
= createTestingAudioBuffer(context
, 1, 128);
91 var gain1
= context
.createGain();
92 var gain2
= context
.createGain();
93 var gain3
= context
.createGain();
94 var orphan
= context
.createGain();
96 source
.buffer
= buffer1ch
;
98 source
.connect(gain1
);
99 source
.connect(gain2
);
100 source
.connect(gain3
);
101 gain1
.connect(context
.destination
);
102 gain2
.connect(context
.destination
);
103 gain3
.connect(context
.destination
);
106 source
.disconnect(gain2
);
108 context
.startRendering().then(function (buffer
) {
110 // The |sum| gain node should produce value 2. (1 + 0 + 1 = 2)
111 Should('Channel #0', buffer
.getChannelData(0)).beConstantValueOf(2);
116 // Task 4: test disconnect(AudioNode, output) method.
117 audit
.defineTask('disconnect(AudioNode, output)', function (done
) {
119 // Connect a buffer with 2 channels with each containing 1 and 2
120 // respectively to a ChannelSplitter, then connect the splitter to 2 gain
121 // nodes as shown below:
122 // (1) splitter#0 => gain1
123 // (2) splitter#0 => gain2
124 // (3) splitter#1 => gain2
125 // Then disconnect (2) and verify if the selective disconnection on a
126 // specified output of the destination node works correctly.
127 var context
= new OfflineAudioContext(1, 128, 44100);
128 var source
= context
.createBufferSource();
129 var buffer2ch
= createTestingAudioBuffer(context
, 2, 128);
130 var splitter
= context
.createChannelSplitter(2);
131 var gain1
= context
.createGain();
132 var gain2
= context
.createGain();
134 source
.buffer
= buffer2ch
;
136 source
.connect(splitter
);
137 splitter
.connect(gain1
, 0); // gain1 gets channel 0.
138 splitter
.connect(gain2
, 0); // gain2 sums channel 0 and 1.
139 splitter
.connect(gain2
, 1);
140 gain1
.connect(context
.destination
);
141 gain2
.connect(context
.destination
);
144 splitter
.disconnect(gain2
, 0); // Now gain2 gets [2]
146 context
.startRendering().then(function (buffer
) {
148 // The sum of gain1 and gain2 should produce value 3. (= 1 + 2)
149 Should('Channel #0', buffer
.getChannelData(0)).beConstantValueOf(3);
154 // Task 5: test disconnect(AudioNode, output, input) method.
155 audit
.defineTask('disconnect(AudioNode, output, input)', function (done
) {
157 // Create a 3-channel buffer with [1, 2, 3] in each channel and then pass
158 // it through a splitter and a merger. Each input/output of the splitter
159 // and the merger is connected in a sequential order as shown below.
160 // (1) splitter#0 => merger#0
161 // (2) splitter#1 => merger#1
162 // (3) splitter#2 => merger#2
163 // Then disconnect (3) and verify if each channel contains [1] and [2]
165 var context
= new OfflineAudioContext(3, 128, 44100);
166 var source
= context
.createBufferSource();
167 var buffer3ch
= createTestingAudioBuffer(context
, 3, 128);
168 var splitter
= context
.createChannelSplitter(3);
169 var merger
= context
.createChannelMerger(3);
171 source
.buffer
= buffer3ch
;
173 source
.connect(splitter
);
174 splitter
.connect(merger
, 0, 0);
175 splitter
.connect(merger
, 1, 1);
176 splitter
.connect(merger
, 2, 2);
177 merger
.connect(context
.destination
);
180 splitter
.disconnect(merger
, 2, 2);
182 context
.startRendering().then(function (buffer
) {
184 // Each channel should have 1, 2, and 0 respectively.
185 Should('Channel #0', buffer
.getChannelData(0)).beConstantValueOf(1);
186 Should('Channel #1', buffer
.getChannelData(1)).beConstantValueOf(2);
187 Should('Channel #2', buffer
.getChannelData(2)).beConstantValueOf(0);
192 // Task 6: exception checks.
193 audit
.defineTask('exceptions', function (done
) {
194 var context
= new OfflineAudioContext(2, 128, 44100);
195 var gain1
= context
.createGain();
196 var splitter
= context
.createChannelSplitter(2);
197 var merger
= context
.createChannelMerger(2);
198 var gain2
= context
.createGain();
199 var gain3
= context
.createGain();
201 // Connect a splitter to gain nodes and merger so we can test the possible
202 // ways of disconnecting the nodes to verify that appropriate exceptions
204 gain1
.connect(splitter
);
205 splitter
.connect(gain2
, 0);
206 splitter
.connect(gain3
, 1);
207 splitter
.connect(merger
, 0, 0);
208 splitter
.connect(merger
, 1, 1);
209 gain2
.connect(gain3
);
210 gain3
.connect(context
.destination
);
211 merger
.connect(context
.destination
);
213 // There is no output #2. An exception should be thrown.
214 Should('splitter.disconnect(2)', function () {
215 splitter
.disconnect(2);
216 }).throw('IndexSizeError');
218 // Disconnecting the output already disconnected should not throw.
219 Should('Disconnecting a connection twice', function () {
220 splitter
.disconnect(1);
221 splitter
.disconnect(1);
224 // gain1 is not connected gain2. An exception should be thrown.
225 Should('gain1.disconnect(gain2)', function () {
226 gain1
.disconnect(gain2
);
227 }).throw('InvalidAccessError');
229 // gain1 and gain3 are not connected. An exception should be thrown.
230 Should('gain1.disconnect(gain3)', function () {
231 gain1
.disconnect(gain3
);
232 }).throw('InvalidAccessError');
234 // There is no output #2 in the splitter. An exception should be thrown.
235 Should('splitter.disconnect(gain2, 2)', function () {
236 splitter
.disconnect(gain2
, 2);
237 }).throw('IndexSizeError');
239 // The splitter and gain1 are not connected. An exception should be thrown.
240 Should('splitter.disconnect(gain1, 0)', function () {
241 splitter
.disconnect(gain1
, 0);
242 }).throw('InvalidAccessError');
244 // The splitter output #0 and the gain3 output #0 are not connected. An
245 // exception should be thrown.
246 Should('splitter.disconnect(gain3, 0, 0)', function () {
247 splitter
.disconnect(gain3
, 0, 0);
248 }).throw('InvalidAccessError');
250 // The output index is out of bound. An exception should be thrown.
251 Should('splitter.disconnect(merger, 3, 0)', function () {
252 splitter
.disconnect(merger
, 3, 0);
253 }).throw('IndexSizeError');
258 audit
.defineTask('finish', function (done
) {
265 'disconnect(output)',
266 'disconnect(AudioNode)',
267 'disconnect(AudioNode, output)',
268 'disconnect(AudioNode, output, input)',
273 successfullyParsed
= true;