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/audit-util.js"></script> | |
8 <script src="resources/audio-testing.js"></script> | |
9 </head> | |
10 | |
11 <body> | |
12 <script> | |
13 description('Test disconnect() method on AudioParam destination.'); | |
14 window.jsTestIsAsync = true; | |
15 | |
16 var renderQuantum = 128; | |
17 | |
18 var sampleRate = 44100; | |
19 var renderDuration = 0.5; | |
20 var disconnectTime = 0.5 * renderDuration; | |
21 | |
22 var audit = Audit.createTaskRunner(); | |
23 | |
24 // Calculate the index for disconnection. | |
25 function getDisconnectIndex(disconnectTime) { | |
26 var disconnectIndex = disconnectTime * sampleRate; | |
27 return disconnectIndex -= (disconnectIndex) % renderQuantum; | |
28 } | |
29 | |
30 // Get the index of value change. | |
31 function getValueChangeIndex(array, targetValue) { | |
32 return array.findIndex(function (element, index) { | |
33 if (element === targetValue) | |
34 return true; | |
35 }); | |
36 } | |
37 | |
38 // Task 1: test disconnect(AudioParam) method. | |
39 audit.defineTask('disconnect(AudioParam)', function (done) { | |
40 | |
41 // Creates a buffer source with value [1] and then connect it to two gain | |
42 // nodes in series. The output of the buffer source is lowered by half | |
43 // (* 0.5) and then connected to two |.gain| AudioParams in each gain node
. | |
44 // | |
45 // (1) bufferSource => gain1 => gain2 | |
46 // (2) bufferSource => half => gain1.gain | |
47 // (3) half => gain2.gain | |
48 // | |
49 // This graph should produce the output of 2.25 (= 1 * 1.5 * 1.5). After | |
50 // disconnecting (3), it should produce 1.5. | |
51 var context = new OfflineAudioContext(1, renderDuration * sampleRate, samp
leRate); | |
52 var source = context.createBufferSource(); | |
53 var buffer1ch = createConstantBuffer(context, 1, 1); | |
54 var half = context.createGain(); | |
55 var gain1 = context.createGain(); | |
56 var gain2 = context.createGain(); | |
57 | |
58 source.buffer = buffer1ch; | |
59 source.loop = true; | |
60 half.gain.value = 0.5; | |
61 | |
62 source.connect(gain1); | |
63 gain1.connect(gain2); | |
64 gain2.connect(context.destination); | |
65 source.connect(half); | |
66 | |
67 // Connecting |half| to both |gain1.gain| and |gain2.gain| amplifies the | |
68 // signal by 2.25 (= 1.5 * 1.5) because each gain node amplifies the signa
l | |
69 // by 1.5 (= 1.0 + 0.5). | |
70 half.connect(gain1.gain); | |
71 half.connect(gain2.gain); | |
72 | |
73 source.start(); | |
74 | |
75 // Schedule the disconnection at the half of render duration. | |
76 context.suspend(disconnectTime).then(function () { | |
77 half.disconnect(gain2.gain); | |
78 context.resume(); | |
79 }); | |
80 | |
81 context.startRendering().then(function (buffer) { | |
82 var channelData = buffer.getChannelData(0); | |
83 var disconnectIndex = getDisconnectIndex(disconnectTime); | |
84 var valueChangeIndex = getValueChangeIndex(channelData, 1.5); | |
85 | |
86 // Expected values are: 1 * 1.5 * 1.5 -> 1 * 1.5 = [2.25, 1.5] | |
87 Should('Channel #0', channelData).containValues([2.25, 1.5]); | |
88 Should('The index of value change', valueChangeIndex) | |
89 .beEqualTo(disconnectIndex); | |
90 | |
91 }).then(done); | |
92 }); | |
93 | |
94 // Task 2: test disconnect(AudioParam, output) method. | |
95 audit.defineTask('disconnect(AudioParam, output)', function (done) { | |
96 | |
97 // Create a 2-channel buffer source with [1, 2] in each channel and | |
98 // make a serial connection through gain1 and gain 2. The make the buffer | |
99 // source half with a gain node and connect it to a 2-output splitter. | |
100 // Connect each output to 2 gain AudioParams respectively. | |
101 // | |
102 // (1) bufferSource => gain1 => gain2 | |
103 // (2) bufferSource => half => splitter(2) | |
104 // (3) splitter#0 => gain1.gain | |
105 // (4) splitter#1 => gain2.gain | |
106 // | |
107 // This graph should produce 3 (= 1 * 1.5 * 2) and 6 (= 2 * 1.5 * 2) for | |
108 // each channel. After disconnecting (4), it should output 1.5 and 3. | |
109 var context = new OfflineAudioContext(2, renderDuration * sampleRate, samp
leRate); | |
110 var source = context.createBufferSource(); | |
111 var buffer2ch = createConstantBuffer(context, 1, [1, 2]); | |
112 var splitter = context.createChannelSplitter(2); | |
113 var half = context.createGain(); | |
114 var gain1 = context.createGain(); | |
115 var gain2 = context.createGain(); | |
116 | |
117 source.buffer = buffer2ch; | |
118 source.loop = true; | |
119 half.gain.value = 0.5; | |
120 | |
121 source.connect(gain1); | |
122 gain1.connect(gain2); | |
123 gain2.connect(context.destination); | |
124 | |
125 // |source| originally is [1, 2] but it becomes [0.5, 1] after 0.5 gain. | |
126 // Each splitter's output will be applied to |gain1.gain| and |gain2.gain| | |
127 // respectively in an additive fashion. | |
128 source.connect(half); | |
129 half.connect(splitter); | |
130 | |
131 // This amplifies the signal by 1.5. (= 1.0 + 0.5) | |
132 splitter.connect(gain1.gain, 0); | |
133 | |
134 // This amplifies the signal by 2. (= 1.0 + 1.0) | |
135 splitter.connect(gain2.gain, 1); | |
136 | |
137 source.start(); | |
138 | |
139 // Schedule the disconnection at the half of render duration. | |
140 context.suspend(disconnectTime).then(function () { | |
141 splitter.disconnect(gain2.gain, 1); | |
142 context.resume(); | |
143 }); | |
144 | |
145 context.startRendering().then(function (buffer) { | |
146 var channelData0 = buffer.getChannelData(0); | |
147 var channelData1 = buffer.getChannelData(1); | |
148 | |
149 var disconnectIndex = getDisconnectIndex(disconnectTime); | |
150 var valueChangeIndexCh0 = getValueChangeIndex(channelData0, 1.5); | |
151 var valueChangeIndexCh1 = getValueChangeIndex(channelData1, 3); | |
152 | |
153 // Expected values are: 1 * 1.5 * 2 -> 1 * 1.5 = [3, 1.5] | |
154 Should('Channel #0', channelData0).containValues([3, 1.5]); | |
155 Should('The index of value change in channel #0', valueChangeIndexCh0) | |
156 .beEqualTo(disconnectIndex); | |
157 | |
158 // Expected values are: 2 * 1.5 * 2 -> 2 * 1.5 = [6, 3] | |
159 Should('Channel #1', channelData1).containValues([6, 3]); | |
160 Should('The index of value change in channel #1', valueChangeIndexCh1) | |
161 .beEqualTo(disconnectIndex); | |
162 | |
163 }).then(done); | |
164 }); | |
165 | |
166 // Task 3: exception checks. | |
167 audit.defineTask('exceptions', function (done) { | |
168 var context = new AudioContext(); | |
169 var gain1 = context.createGain(); | |
170 var splitter = context.createChannelSplitter(2); | |
171 var gain2 = context.createGain(); | |
172 var gain3 = context.createGain(); | |
173 | |
174 // Connect a splitter to gain nodes and merger so we can test the possible | |
175 // ways of disconnecting the nodes to verify that appropriate exceptions | |
176 // are thrown. | |
177 gain1.connect(splitter); | |
178 splitter.connect(gain2.gain, 0); | |
179 splitter.connect(gain3.gain, 1); | |
180 gain2.connect(gain3); | |
181 gain3.connect(context.destination); | |
182 | |
183 // gain1 is not connected to gain3.gain. Exception should be thrown. | |
184 Should('gain1.disconnect(gain3.gain)', function () { | |
185 gain1.disconnect(gain3.gain); | |
186 }).throw('InvalidAccessError'); | |
187 | |
188 // When the output index is good but the destination is invalid. | |
189 Should('splitter.disconnect(gain1.gain, 1)', function () { | |
190 splitter.disconnect(gain1.gain, 1); | |
191 }).throw('InvalidAccessError'); | |
192 | |
193 // When both arguments are wrong, throw IndexSizeError first. | |
194 Should('splitter.disconnect(gain1.gain, 2)', function () { | |
195 splitter.disconnect(gain1.gain, 2); | |
196 }).throw('IndexSizeError'); | |
197 | |
198 done(); | |
199 }); | |
200 | |
201 audit.defineTask('finish', function (done) { | |
202 finishJSTest(); | |
203 done(); | |
204 }); | |
205 | |
206 audit.runTasks( | |
207 'disconnect(AudioParam)', | |
208 'disconnect(AudioParam, output)', | |
209 'exceptions', | |
210 'finish' | |
211 ); | |
212 | |
213 successfullyParsed = true; | |
214 </script> | |
215 </body> | |
216 | |
217 </html> | |
OLD | NEW |