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, 44100 * 10, 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 // Disconnects after the rendering starts. |
| 53 // |
| 54 // FIXME: Although this guarantees that the disconnection happens after |
| 55 // the rendering starts, still the actual disconnection might happen after |
| 56 // oncomplete event fired. |
| 57 // |
| 58 // The 10ms delay is 1/1000 of the total render length (10,000ms). Because
|
| 59 // OfflineAudioContext runs faster than real time, the disconnection might |
| 60 // happen after the rendering finishes. Then lower the delay and increase |
| 61 // the render length to avoid the test failure. |
| 62 var stateAlreadyChanged = false; |
| 63 context.onstatechange = function () { |
| 64 if (!stateAlreadyChanged) { |
| 65 half.disconnect(gain2.gain); |
| 66 stateAlreadyChanged = true; |
| 67 } |
| 68 }; |
| 69 |
| 70 context.startRendering().then(function (buffer) { |
| 71 |
| 72 // Note that this test depends on the disconnection below to happen |
| 73 // sometime during rendering. |
| 74 var channelData = buffer.getChannelData(0); |
| 75 var expectedValues = [2.25, 1.5]; // 1 * 1.5 * 1.5 -> 1 * 1.5 |
| 76 var passed = compareElements(expectedValues, channelData); |
| 77 |
| 78 if (passed) { |
| 79 testPassed('The rendered buffer matches expected values: [' + |
| 80 expectedValues.toString() + ']' |
| 81 ); |
| 82 } else { |
| 83 testFailed('The rendered buffer does not match expected values.'); |
| 84 } |
| 85 }).then(done); |
| 86 }); |
| 87 |
| 88 // Task 2: test disconnect(AudioParam, output) method. |
| 89 audit.defineTask('disconnect(AudioParam, output)', function (done) { |
| 90 |
| 91 // Create a 2-channel buffer source with [1, 2] in each channel and |
| 92 // make a serial connection through gain1 and gain 2. The make the buffer |
| 93 // source half with a gain node and connect it to a 2-output splitter. |
| 94 // Connect each output to 2 gain AudioParams respectively. |
| 95 // (1) bufferSource => gain1 => gain2 |
| 96 // (2) bufferSource => half => splitter(2) |
| 97 // (3) splitter#0 => gain1.gain |
| 98 // (4) splitter#1 => gain2.gain |
| 99 // This graph should produce 3 (= 1 * 1.5 * 2) and 6 (= 2 * 1.5 * 2) for |
| 100 // each channel. After disconnecting (4), it should output 1.5 and 3. |
| 101 var context = new OfflineAudioContext(2, 44100 * 10, 44100); |
| 102 var source = context.createBufferSource(); |
| 103 var buffer2ch = createTestingAudioBuffer(context, 2, 128); |
| 104 var splitter = context.createChannelSplitter(2); |
| 105 var half = context.createGain(); |
| 106 var gain1 = context.createGain(); |
| 107 var gain2 = context.createGain(); |
| 108 |
| 109 source.buffer = buffer2ch; |
| 110 source.loop = true; |
| 111 half.gain.value = 0.5; |
| 112 |
| 113 source.connect(gain1); |
| 114 gain1.connect(gain2); |
| 115 gain2.connect(context.destination); |
| 116 |
| 117 // |source| originally is [1, 2] but it becomes [0.5, 1] after 0.5 gain. |
| 118 // Each splitter's output will be applied to |gain1.gain| and |gain2.gain|
|
| 119 // respectively in an additive fashion. |
| 120 source.connect(half); |
| 121 half.connect(splitter); |
| 122 |
| 123 // This amplifies the signal by 1.5. (= 1.0 + 0.5) |
| 124 splitter.connect(gain1.gain, 0); |
| 125 |
| 126 // This amplifies the signal by 2. (= 1.0 + 1.0) |
| 127 splitter.connect(gain2.gain, 1); |
| 128 |
| 129 source.start(); |
| 130 |
| 131 // Disconnect after the rendering starts. See the comment in the previous |
| 132 // test task. |
| 133 var stateAlreadyChanged = false; |
| 134 context.onstatechange = function () { |
| 135 if (!stateAlreadyChanged) { |
| 136 splitter.disconnect(gain2.gain, 1); |
| 137 stateAlreadyChanged = true; |
| 138 } |
| 139 }; |
| 140 |
| 141 context.startRendering().then(function (buffer) { |
| 142 var channelData0 = buffer.getChannelData(0); |
| 143 var channelData1 = buffer.getChannelData(1); |
| 144 var expectedValuesCh0 = [3, 1.5]; // 1 * 1.5 * 2 -> 1 * 1.5 |
| 145 var expectedValuesCh1 = [6, 3]; // 2 * 1.5 * 2 -> 2 * 1.5 |
| 146 var passedCh0 = compareElements(expectedValuesCh0, channelData0); |
| 147 var passedCh1 = compareElements(expectedValuesCh1, channelData1); |
| 148 |
| 149 if (passedCh0 && passedCh1) { |
| 150 testPassed('The rendered buffer matches expected values: Ch0 = [' + |
| 151 expectedValuesCh0.toString() + '] Ch1 = [' + expectedValuesCh1.toStr
ing() + |
| 152 ']' |
| 153 ); |
| 154 } else { |
| 155 testFailed('Expected values do not match.'); |
| 156 } |
| 157 }).then(done); |
| 158 }); |
| 159 |
| 160 // Task 3: exception checks. |
| 161 audit.defineTask('exceptions', function (done) { |
| 162 var context = new AudioContext(); |
| 163 var gain1 = context.createGain(); |
| 164 var splitter = context.createChannelSplitter(2); |
| 165 var gain2 = context.createGain(); |
| 166 var gain3 = context.createGain(); |
| 167 |
| 168 // Connect a splitter to gain nodes and merger so we can test the possible
|
| 169 // ways of disconnecting the nodes to verify that appropriate exceptions |
| 170 // are thrown. |
| 171 gain1.connect(splitter); |
| 172 splitter.connect(gain2.gain, 0); |
| 173 splitter.connect(gain3.gain, 1); |
| 174 gain2.connect(gain3); |
| 175 gain3.connect(context.destination); |
| 176 |
| 177 // gain1 is not connected to gain3.gain. Exception should be thrown. |
| 178 Should.throwWithType('InvalidAccessError', function () { |
| 179 gain1.disconnect(gain3.gain); |
| 180 }); |
| 181 |
| 182 // When the output index is good but the destination is invalid. |
| 183 Should.throwWithType('InvalidAccessError', function () { |
| 184 splitter.disconnect(gain1.gain, 1); |
| 185 }); |
| 186 |
| 187 // When both arguments are wrong, throw IndexSizeError first. |
| 188 Should.throwWithType('IndexSizeError', function () { |
| 189 splitter.disconnect(gain1.gain, 2); |
| 190 }); |
| 191 |
| 192 done(); |
| 193 }); |
| 194 |
| 195 audit.defineTask('finish', function (done) { |
| 196 finishJSTest(); |
| 197 done(); |
| 198 }); |
| 199 |
| 200 audit.runTasks( |
| 201 'disconnect(AudioParam)', |
| 202 'disconnect(AudioParam, output)', |
| 203 'exceptions', |
| 204 'finish' |
| 205 ); |
| 206 |
| 207 // The value-wise comparison of two arrays with different length. It |
| 208 // checks the value of element regardless of its occurrence. For example, |
| 209 // (1) [2.25, 2.25, 2.25, 1.5, 1.5, 1.5] === [2.25, 1.5] |
| 210 // (2) [2.25, 2.25, 1.5, 1.5, 0, 7, 7, 7, 9, 1] !== [2.25, 1.5] |
| 211 // (3) [-1, 2, 3, 2.25, 2.25, 1.5, 1.5] !== [2.25, 1.5] |
| 212 // (4) [3, 3, 3, 7, 7, 7] !== [2.25, 1.5] |
| 213 function compareElements(expected, actual) { |
| 214 var indexExpected = 0, indexActual = 0; |
| 215 while (indexExpected < expected.length && indexActual < actual.length) { |
| 216 if (expected[indexExpected] === actual[indexActual]) { |
| 217 indexActual++; |
| 218 } else { |
| 219 indexExpected++; |
| 220 } |
| 221 } |
| 222 |
| 223 if (indexExpected < expected.length - 1 || indexActual < actual.length -
1) { |
| 224 // This is for the debugging purpose. Printed out only when failed. |
| 225 console.log('The value ' + actual[indexActual] + ' at index ' + |
| 226 indexActual + ' was not found in expected values'); |
| 227 return false; |
| 228 } else { |
| 229 return true; |
| 230 } |
| 231 } |
| 232 |
| 233 successfullyParsed = true; |
| 234 </script> |
| 235 </body> |
| 236 |
| 237 </html> |
OLD | NEW |