OLD | NEW |
---|---|
(Empty) | |
1 <!DOCTYPE html> | |
2 <html> | |
3 | |
4 <head> | |
5 <script src="../resources/js-test.js"></script> | |
6 <script src="resources/compatibility.js"></script> | |
7 <script src="resources/audio-testing.js"></script> | |
8 </head> | |
9 | |
10 <body> | |
11 <script> | |
12 description('Test disconnect() method on AudioParam destination.'); | |
13 window.jsTestIsAsync = true; | |
14 | |
15 var audit = Audit.createTaskRunner(); | |
16 | |
17 // Task 1: test disconnect(AudioParam) method. | |
18 audit.defineTask('disconnect(AudioParam)', function (done) { | |
19 | |
20 // Creates a buffer source with value [1] and then connect it to two gain | |
21 // nodes in series. The output of the buffer source is lowered by half | |
22 // (* 0.5) and then connected to two |.gain| AudioParams in each gain node . | |
23 // (1) bufferSource => gain1 => gain2 | |
24 // (2) bufferSource => half => gain1.gain | |
25 // (3) half => gain2.gain | |
26 // This graph should produce the output of 2.25 (= 1 * 1.5 * 1.5). After | |
27 // disconnecting (3), it should produce 1.5. | |
28 var context = new OfflineAudioContext(1, 441000, 44100); | |
29 var source = context.createBufferSource(); | |
30 var buffer1ch = createTestingAudioBuffer(context, 1, 128); | |
31 var half = context.createGain(); | |
32 var gain1 = context.createGain(); | |
33 var gain2 = context.createGain(); | |
34 | |
35 source.buffer = buffer1ch; | |
36 source.loop = true; | |
37 half.gain.value = 0.5; | |
38 | |
39 source.connect(gain1); | |
40 gain1.connect(gain2); | |
41 gain2.connect(context.destination); | |
42 source.connect(half); | |
43 | |
44 // Connecting |half| to both |gain1.gain| and |gain2.gain| amplifies the | |
45 // signal by 2.25 (= 1.5 * 1.5) because each gain node amplifies the signa l | |
46 // by 1.5 (= 1.0 + 0.5). | |
47 half.connect(gain1.gain); | |
48 half.connect(gain2.gain); | |
49 | |
50 source.start(); | |
51 | |
52 context.startRendering().then(function (buffer) { | |
53 | |
54 // Note that this test depends on the disconnection below to happen | |
55 // sometime during rendering. | |
56 var channelData = buffer.getChannelData(0); | |
57 var expectedValues = [2.25, 1.5]; // 1 * 1.5 * 1.5 -> 1 * 1.5 | |
58 var passed = compareElements(expectedValues, channelData); | |
59 | |
60 if (passed) { | |
61 testPassed('The rendered buffer matches expected values: [' + | |
62 expectedValues.toString() + ']' | |
63 ); | |
64 } else { | |
65 testFailed('The rendered buffer does not match expected values.'); | |
66 } | |
67 }).then(done); | |
68 | |
69 // Disconnects after the rendering starts. | |
70 // | |
71 // FIXME: This does not guarantee that the disconnection happens after the | |
72 // rendering starts. Reconsider this if there can be a way to change the | |
73 // graph in the middle of rendering. | |
74 // | |
75 // The 10ms delay is 1/1000 of the total render length (10,000ms). Because | |
Raymond Toy
2015/02/13 20:47:26
Isn't the render length 44100 frames = 1 sec?
| |
76 // OfflineAudioContext runs faster than real time, the disconnection might | |
77 // happen after the rendering finishes. Then lower the delay and increase | |
78 // the render length to avoid the test failure. | |
79 setTimeout(function () { | |
80 half.disconnect(gain2.gain); | |
81 }, 10); | |
82 }); | |
83 | |
84 // Task 2: test disconnect(AudioParam, output) method. | |
85 audit.defineTask('disconnect(AudioParam, output)', function (done) { | |
86 | |
87 // Create a 2-channel buffer source with [1, 2] in each channel and | |
88 // make a serial connection through gain1 and gain 2. The make the buffer | |
89 // source half with a gain node and connect it to a 2-output splitter. | |
90 // Connect each output to 2 gain AudioParams respectively. | |
91 // (1) bufferSource => gain1 => gain2 | |
92 // (2) bufferSource => half => splitter(2) | |
93 // (3) splitter#0 => gain1.gain | |
94 // (4) splitter#1 => gain2.gain | |
95 // This graph should produce 3 (= 1 * 1.5 * 2) and 6 (= 2 * 1.5 * 2) for | |
96 // each channel. After disconnecting (4), it should output 1.5 and 3. | |
97 var context = new OfflineAudioContext(2, 441000, 44100); | |
98 var source = context.createBufferSource(); | |
99 var buffer2ch = createTestingAudioBuffer(context, 2, 128); | |
100 var splitter = context.createChannelSplitter(2); | |
101 var half = context.createGain(); | |
102 var gain1 = context.createGain(); | |
103 var gain2 = context.createGain(); | |
104 | |
105 source.buffer = buffer2ch; | |
106 source.loop = true; | |
107 half.gain.value = 0.5; | |
108 | |
109 source.connect(gain1); | |
110 gain1.connect(gain2); | |
111 gain2.connect(context.destination); | |
112 | |
113 // |source| originally is [1, 2] but it becomes [0.5, 1] after 0.5 gain. | |
114 // Each splitter's output will be applied to |gain1.gain| and |gain2.gain| | |
115 // respectively in an additive fashion. | |
116 source.connect(half); | |
117 half.connect(splitter); | |
118 | |
119 // This amplifies the signal by 1.5. (= 1.0 + 0.5) | |
120 splitter.connect(gain1.gain, 0); | |
121 | |
122 // This amplifies the signal by 2. (= 1.0 + 1.0) | |
123 splitter.connect(gain2.gain, 1); | |
124 | |
125 source.start(); | |
126 | |
127 context.startRendering().then(function (buffer) { | |
128 var channelData0 = buffer.getChannelData(0); | |
129 var channelData1 = buffer.getChannelData(1); | |
130 var expectedValuesCh0 = [3, 1.5]; // 1 * 1.5 * 2 -> 1 * 1.5 | |
131 var expectedValuesCh1 = [6, 3]; // 2 * 1.5 * 2 -> 2 * 1.5 | |
132 var passedCh0 = compareElements(expectedValuesCh0, channelData0); | |
133 var passedCh1 = compareElements(expectedValuesCh1, channelData1); | |
134 | |
135 if (passedCh0 && passedCh1) { | |
136 testPassed('The rendered buffer matches expected values: Ch0 = [' + | |
137 expectedValuesCh0.toString() + '] Ch1 = [' + expectedValuesCh1.toStr ing() + | |
138 ']' | |
139 ); | |
140 } else { | |
141 testFailed('Expected values do not match.'); | |
142 } | |
143 }).then(done); | |
144 | |
145 // Disconnect after the rendering starts. See the comment in the previous | |
146 // test task. | |
147 setTimeout(function () { | |
148 splitter.disconnect(gain2.gain, 1); | |
149 }, 10); | |
150 | |
151 }); | |
152 | |
153 // Task 3: exception checks. | |
154 audit.defineTask('exceptions', function (done) { | |
155 var context = new AudioContext(); | |
156 var gain1 = context.createGain(); | |
157 var splitter = context.createChannelSplitter(2); | |
158 var gain2 = context.createGain(); | |
159 var gain3 = context.createGain(); | |
160 | |
161 // Build an audiograph to test the invalid disconnection. | |
162 gain1.connect(splitter); | |
163 splitter.connect(gain2.gain, 0); | |
164 splitter.connect(gain3.gain, 1); | |
165 gain2.connect(gain3); | |
166 gain3.connect(context.destination); | |
167 | |
168 // gain1 is not connected to gain3.gain. Exception should be thrown. | |
169 Should.throwWithType('InvalidAccessError', function () { | |
170 gain1.disconnect(gain3.gain); | |
171 }); | |
172 | |
173 // When the output index is good but the destination is invalid. | |
174 Should.throwWithType('InvalidAccessError', function () { | |
175 splitter.disconnect(gain1.gain, 1); | |
176 }); | |
177 | |
178 // When both arguments are wrong, throw IndexSizeError first. | |
179 Should.throwWithType('IndexSizeError', function () { | |
180 splitter.disconnect(gain1.gain, 2); | |
181 }); | |
182 | |
183 done(); | |
184 }); | |
185 | |
186 audit.defineTask('finish', function (done) { | |
187 finishJSTest(); | |
188 done(); | |
189 }); | |
190 | |
191 audit.runTasks( | |
192 'disconnect(AudioParam)', | |
193 'disconnect(AudioParam, output)', | |
194 'exceptions', | |
195 'finish' | |
196 ); | |
197 | |
198 // The value-wise comparison of two arrays with different length. It | |
199 // checks the value of element regardless of its occurrence. For example, | |
200 // (1) [2.25, 2.25, 2.25, 1.5, 1.5, 1.5] === [2.25, 1.5] | |
201 // (2) [2.25, 2.25, 1.5, 1.5, 0, 7, 7, 7, 9, 1] !== [2.25, 1.5] | |
202 // (3) [-1, 2, 3, 2.25, 2.25, 1.5, 1.5] !== [2.25, 1.5] | |
203 // (4) [3, 3, 3, 7, 7, 7] !== [2.25, 1.5] | |
204 function compareElements(expected, actual) { | |
205 var indexExpected = 0, indexActual = 0; | |
206 while (indexExpected < expected.length && indexActual < actual.length) { | |
207 if (expected[indexExpected] === actual[indexActual]) { | |
208 indexActual++; | |
209 } else { | |
210 indexExpected++; | |
211 } | |
212 } | |
213 | |
214 if (indexExpected < expected.length - 1 || indexActual < actual.length - 1) { | |
215 // This is for the debugging purpose. Printed out only when failed. | |
216 console.log('The value ' + actual[indexActual] + ' at index ' + | |
217 indexActual + ' was not found in expected values'); | |
218 return false; | |
219 } else { | |
220 return true; | |
221 } | |
222 } | |
223 | |
224 successfullyParsed = true; | |
225 </script> | |
226 </body> | |
227 | |
228 </html> | |
OLD | NEW |