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. Then connect the half of buffer source to two gain | |
Raymond Toy
2015/02/12 19:45:52
What does "half of buffer source" mean?
hongchan
2015/02/13 01:16:56
It means the output of buffer source will be lower
| |
22 // 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, 256, 44100); | |
29 var source = context.createBufferSource(); | |
30 var buffer1ch = createTestingBuffer(context, 1, 128); | |
31 var half = context.createGain(); | |
32 var gain1 = context.createGain(); | |
33 var gain2 = context.createGain(); | |
34 var orphan = context.createGain(); | |
35 | |
36 source.buffer = buffer1ch; | |
37 source.loop = true; | |
38 half.gain.value = 0.5; | |
Raymond Toy
2015/02/12 19:45:52
I think it's clearer to move this line just after
hongchan
2015/02/13 01:16:56
Hmm. I logically separated the configuration part
| |
39 | |
40 source.connect(gain1); | |
41 gain1.connect(gain2); | |
42 gain2.connect(context.destination); | |
43 source.connect(half); | |
44 half.connect(gain1.gain); // This amplifies by 1.5. (= 1.0 + 0.5) | |
45 half.connect(gain2.gain); // This amplifies by 1.5. (= 1.0 + 0.5) | |
46 | |
47 source.start(); | |
48 context.startRendering(); | |
49 | |
50 // This happens right after the rendering started. The closest moment when | |
51 // this operation can happen is the beginning of the 2nd rendering quantum . | |
52 // So the value change in the buffer will be observed at 128th sample. | |
Raymond Toy
2015/02/12 19:45:52
As we discussed, this may not be true in general.
hongchan
2015/02/13 01:16:56
Done.
| |
53 half.disconnect(gain2.gain); | |
54 | |
55 Should.throwWithType('InvalidAccessError', function () { | |
56 half.disconnect(orphan.gain); | |
57 }); | |
Raymond Toy
2015/02/12 19:45:52
Move this test somewhere else.
| |
58 | |
59 context.oncomplete = function (event) { | |
60 var channelData = event.renderedBuffer.getChannelData(0); | |
61 var valueIndex = 0; | |
62 var expectedValues = [2.25, 1.5]; // 1 * 1.5 * 1.5 -> 1 * 1.5 | |
63 | |
64 for (var i = 0; i < channelData.length; i++) { | |
65 // console.log(i + ' : ' + channelData[i]); | |
66 if (expectedValues[valueIndex] === channelData[i]) | |
67 valueIndex++; | |
68 } | |
Raymond Toy
2015/02/12 19:45:52
What happens if something is totally messed up and
hongchan
2015/02/13 01:16:56
That's a good point. Will fix to catch other wrong
| |
69 | |
70 if (valueIndex === expectedValues.length) { | |
71 testPassed('Expected values are verified.'); | |
72 } else { | |
73 testFailed('Expected values do not match.'); | |
74 } | |
75 | |
76 done(); | |
77 }; | |
78 }); | |
79 | |
80 // Task 2: test disconnect(AudioParam, output) method. | |
81 audit.defineTask('disconnect(AudioParam, output)', function (done) { | |
82 | |
83 // Create a 2-channel buffer source with [1, 2] and make a serial | |
84 // connection through gain1 and gain 2. The make the buffer source half | |
85 // with a gain node and connect it to a 2-output splitter. Connect each | |
86 // output to 2 gain AudioParams respectively. | |
87 // (1) bufferSource => gain1 => gain2 | |
88 // (2) bufferSource => half => splitter(2) | |
89 // (3) splitter#0 => gain1.gain | |
90 // (4) splitter#1 => gain2.gain | |
91 // This graph should produce 3 (= 1 * 1.5 * 2) and 6 (= 2 * 1.5 * 2) for | |
92 // each channel. After disconnecting (4), it should output 1.5 and 3. | |
93 var context = new OfflineAudioContext(2, 256, 44100); | |
94 var source = context.createBufferSource(); | |
95 var buffer2ch = createTestingBuffer(context, 2, 128); | |
96 var splitter = context.createChannelSplitter(2); | |
97 var half = context.createGain(); | |
98 var gain1 = context.createGain(); | |
99 var gain2 = context.createGain(); | |
100 | |
101 source.buffer = buffer2ch; | |
102 source.loop = true; | |
103 half.gain.value = 0.5; | |
104 | |
105 source.connect(gain1); | |
106 gain1.connect(gain2); | |
107 gain2.connect(context.destination); | |
108 | |
109 // |source| is [1, 2] but it becomes [0.5, 1]. Each splitter's output will | |
110 // be applied to |gain1.gain| and |gain2.gain| respectively in an additive | |
111 // fashion. | |
112 source.connect(half); | |
113 half.connect(splitter); | |
114 splitter.connect(gain1.gain, 0); // This amplifies by 1.5. (= 1.0 + 0.5) | |
115 splitter.connect(gain2.gain, 1); // This amplifies by 2. (= 1.0 + 1.0) | |
116 | |
117 source.start(); | |
118 context.startRendering(); | |
119 | |
120 // So the value change in the buffer will be observed at 128th sample. | |
121 // See the comment above. | |
122 splitter.disconnect(gain2.gain, 1); | |
123 | |
124 Should.throwWithType('InvalidAccessError', function () { | |
125 splitter.disconnect(gain1.gain, 1); | |
126 }); | |
127 | |
128 Should.throwWithType('IndexSizeError', function () { | |
129 splitter.disconnect(gain1.gain, 2); | |
130 }); | |
131 | |
132 context.oncomplete = function (event) { | |
133 var channelData0 = event.renderedBuffer.getChannelData(0); | |
134 var channelData1 = event.renderedBuffer.getChannelData(1); | |
135 var valueIndex = 0; | |
136 var expectedValuesCh0 = [3, 1.5]; // 1 * 1.5 * 2 -> 1 * 1.5 | |
137 var expectedValuesCh1 = [6, 3]; // 2 * 1.5 * 2 -> 2 * 1.5 | |
138 | |
139 for (var i = 0; i < channelData0.length; i++) { | |
140 // console.log(i + ' : ' + channelData0[i] + ' ' + channelData1[i]); | |
141 if (expectedValuesCh0[valueIndex] === channelData0[i] && | |
142 expectedValuesCh1[valueIndex] === channelData1[i]) { | |
143 valueIndex++; | |
144 } | |
145 } | |
146 | |
147 if (valueIndex === expectedValuesCh0.length) { | |
148 testPassed('Expected values are verified.'); | |
149 } else { | |
150 testFailed('Expected values do not match.'); | |
151 } | |
152 | |
153 done(); | |
154 }; | |
155 }); | |
156 | |
157 audit.defineTask('finish', function (done) { | |
158 finishJSTest(); | |
159 done(); | |
160 }); | |
161 | |
162 audit.runTasks( | |
163 'disconnect(AudioParam)', | |
164 'disconnect(AudioParam, output)', | |
165 'finish' | |
166 ); | |
167 | |
168 successfullyParsed = true; | |
169 </script> | |
170 </body> | |
171 | |
172 </html> | |
OLD | NEW |