summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webaudio/the-audio-api/the-audionode-interface/audionode-disconnect.html
blob: 65b93222d1002e227ab1ad2e29b6e140b65bd9e4 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<!DOCTYPE html>
<html>
  <head>
    <title>
      audionode-disconnect.html
    </title>
    <script src="/resources/testharness.js"></script>
    <script src="/resources/testharnessreport.js"></script>
    <script src="/webaudio/resources/audit-util.js"></script>
    <script src="/webaudio/resources/audit.js"></script>
  </head>
  <body>
    <script id="layout-test-code">
      let audit = Audit.createTaskRunner();

      // Task 1: test disconnect() method.
      audit.define('disconnect()', (task, should) => {

        // Connect a source to multiple gain nodes, each connected to the
        // destination. Then disconnect the source. The expected output should
        // be all zeros since the source was disconnected.
        let context = new OfflineAudioContext(1, 128, 44100);
        let source = context.createBufferSource();
        let buffer1ch = createConstantBuffer(context, 128, [1]);
        let gain1 = context.createGain();
        let gain2 = context.createGain();
        let gain3 = context.createGain();

        source.buffer = buffer1ch;

        source.connect(gain1);
        source.connect(gain2);
        source.connect(gain3);
        gain1.connect(context.destination);
        gain2.connect(context.destination);
        gain3.connect(context.destination);
        source.start();

        // This disconnects everything.
        source.disconnect();

        context.startRendering()
            .then(function(buffer) {

              // With everything disconnected, the result should be zero.
              should(buffer.getChannelData(0), 'Channel #0')
                  .beConstantValueOf(0);

            })
            .then(() => task.done());
      });

      // Task 2: test disconnect(output) method.
      audit.define('disconnect(output)', (task, should) => {

        // Create multiple connections from each output of a ChannelSplitter
        // to a gain node. Then test if disconnecting a single output of
        // splitter is actually disconnected.
        let context = new OfflineAudioContext(1, 128, 44100);
        let source = context.createBufferSource();
        let buffer3ch = createConstantBuffer(context, 128, [1, 2, 3]);
        let splitter = context.createChannelSplitter(3);
        let sum = context.createGain();

        source.buffer = buffer3ch;

        source.connect(splitter);
        splitter.connect(sum, 0);
        splitter.connect(sum, 1);
        splitter.connect(sum, 2);
        sum.connect(context.destination);
        source.start();

        // This disconnects the second output.
        splitter.disconnect(1);

        context.startRendering()
            .then(function(buffer) {

              // The rendered channel should contain 4. (= 1 + 0 + 3)
              should(buffer.getChannelData(0), 'Channel #0')
                  .beConstantValueOf(4);

            })
            .then(() => task.done());
      });

      // Task 3: test disconnect(AudioNode) method.
      audit.define('disconnect(AudioNode)', (task, should) => {

        // Connect a source to multiple gain nodes. Then test if disconnecting a
        // single destination selectively works correctly.
        let context = new OfflineAudioContext(1, 128, 44100);
        let source = context.createBufferSource();
        let buffer1ch = createConstantBuffer(context, 128, [1]);
        let gain1 = context.createGain();
        let gain2 = context.createGain();
        let gain3 = context.createGain();
        let orphan = context.createGain();

        source.buffer = buffer1ch;

        source.connect(gain1);
        source.connect(gain2);
        source.connect(gain3);
        gain1.connect(context.destination);
        gain2.connect(context.destination);
        gain3.connect(context.destination);
        source.start();

        source.disconnect(gain2);

        context.startRendering()
            .then(function(buffer) {

              // The |sum| gain node should produce value 2. (1 + 0 + 1 = 2)
              should(buffer.getChannelData(0), 'Channel #0')
                  .beConstantValueOf(2);

            })
            .then(() => task.done());
      });

      // Task 4: test disconnect(AudioNode, output) method.
      audit.define('disconnect(AudioNode, output)', (task, should) => {

        // Connect a buffer with 2 channels with each containing 1 and 2
        // respectively to a ChannelSplitter, then connect the splitter to 2
        // gain nodes as shown below:
        //   (1) splitter#0 => gain1
        //   (2) splitter#0 => gain2
        //   (3) splitter#1 => gain2
        // Then disconnect (2) and verify if the selective disconnection on a
        // specified output of the destination node works correctly.
        let context = new OfflineAudioContext(1, 128, 44100);
        let source = context.createBufferSource();
        let buffer2ch = createConstantBuffer(context, 128, [1, 2]);
        let splitter = context.createChannelSplitter(2);
        let gain1 = context.createGain();
        let gain2 = context.createGain();

        source.buffer = buffer2ch;

        source.connect(splitter);
        splitter.connect(gain1, 0);  // gain1 gets channel 0.
        splitter.connect(gain2, 0);  // gain2 sums channel 0 and 1.
        splitter.connect(gain2, 1);
        gain1.connect(context.destination);
        gain2.connect(context.destination);
        source.start();

        splitter.disconnect(gain2, 0);  // Now gain2 gets [2]

        context.startRendering()
            .then(function(buffer) {

              // The sum of gain1 and gain2 should produce value 3. (= 1 + 2)
              should(buffer.getChannelData(0), 'Channel #0')
                  .beConstantValueOf(3);

            })
            .then(() => task.done());
      });

      // Task 5: test disconnect(AudioNode, output, input) method.
      audit.define('disconnect(AudioNode, output, input)', (task, should) => {

        // Create a 3-channel buffer with [1, 2, 3] in each channel and then
        // pass it through a splitter and a merger. Each input/output of the
        // splitter and the merger is connected in a sequential order as shown
        // below.
        //   (1) splitter#0 => merger#0
        //   (2) splitter#1 => merger#1
        //   (3) splitter#2 => merger#2
        // Then disconnect (3) and verify if each channel contains [1] and [2]
        // respectively.
        let context = new OfflineAudioContext(3, 128, 44100);
        let source = context.createBufferSource();
        let buffer3ch = createConstantBuffer(context, 128, [1, 2, 3]);
        let splitter = context.createChannelSplitter(3);
        let merger = context.createChannelMerger(3);

        source.buffer = buffer3ch;

        source.connect(splitter);
        splitter.connect(merger, 0, 0);
        splitter.connect(merger, 1, 1);
        splitter.connect(merger, 2, 2);
        merger.connect(context.destination);
        source.start();

        splitter.disconnect(merger, 2, 2);

        context.startRendering()
            .then(function(buffer) {

              // Each channel should have 1, 2, and 0 respectively.
              should(buffer.getChannelData(0), 'Channel #0')
                  .beConstantValueOf(1);
              should(buffer.getChannelData(1), 'Channel #1')
                  .beConstantValueOf(2);
              should(buffer.getChannelData(2), 'Channel #2')
                  .beConstantValueOf(0);

            })
            .then(() => task.done());
      });

      // Task 6: exception checks.
      audit.define('exceptions', (task, should) => {
        let context = new OfflineAudioContext(2, 128, 44100);
        let gain1 = context.createGain();
        let splitter = context.createChannelSplitter(2);
        let merger = context.createChannelMerger(2);
        let gain2 = context.createGain();
        let gain3 = context.createGain();

        // Connect a splitter to gain nodes and merger so we can test the
        // possible ways of disconnecting the nodes to verify that appropriate
        // exceptions are thrown.
        gain1.connect(splitter);
        splitter.connect(gain2, 0);
        splitter.connect(gain3, 1);
        splitter.connect(merger, 0, 0);
        splitter.connect(merger, 1, 1);
        gain2.connect(gain3);
        gain3.connect(context.destination);
        merger.connect(context.destination);

        // There is no output #2. An exception should be thrown.
        should(function() {
          splitter.disconnect(2);
        }, 'splitter.disconnect(2)').throw(DOMException, 'IndexSizeError');

        // Disconnecting the output already disconnected should not throw.
        should(function() {
          splitter.disconnect(1);
          splitter.disconnect(1);
        }, 'Disconnecting a connection twice').notThrow();

        // gain1 is not connected gain2. An exception should be thrown.
        should(function() {
          gain1.disconnect(gain2);
        }, 'gain1.disconnect(gain2)').throw(DOMException, 'InvalidAccessError');

        // gain1 and gain3 are not connected. An exception should be thrown.
        should(function() {
          gain1.disconnect(gain3);
        }, 'gain1.disconnect(gain3)').throw(DOMException, 'InvalidAccessError');

        // There is no output #2 in the splitter. An exception should be thrown.
        should(function() {
          splitter.disconnect(gain2, 2);
        }, 'splitter.disconnect(gain2, 2)').throw(DOMException, 'IndexSizeError');

        // The splitter and gain1 are not connected. An exception should be
        // thrown.
        should(function() {
          splitter.disconnect(gain1, 0);
        }, 'splitter.disconnect(gain1, 0)').throw(DOMException, 'InvalidAccessError');

        // The splitter output #0 and the gain3 output #0 are not connected. An
        // exception should be thrown.
        should(function() {
          splitter.disconnect(gain3, 0, 0);
        }, 'splitter.disconnect(gain3, 0, 0)').throw(DOMException, 'InvalidAccessError');

        // The output index is out of bound. An exception should be thrown.
        should(function() {
          splitter.disconnect(merger, 3, 0);
        }, 'splitter.disconnect(merger, 3, 0)').throw(DOMException, 'IndexSizeError');

        task.done();
      });

      audit.define('disabled-outputs', (task, should) => {
        // See crbug.com/656652
        let context = new OfflineAudioContext(2, 1024, 44100);
        let g1 = context.createGain();
        let g2 = context.createGain();
        g1.connect(g2);
        g1.disconnect(g2);
        let g3 = context.createGain();
        g2.connect(g3);
        g1.connect(g2);
        context.startRendering()
            .then(function() {
              // If we make it here, we passed.
              should(true, 'Disabled outputs handled')
                  .message('correctly', 'inccorrectly');
            })
            .then(() => task.done());
      });

      audit.run();
    </script>
  </body>
</html>