Chromium Code Reviews| 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 2 AudioParams amplify the signal by 1.5 (= 1.0 + 0.5 ) | |
|
Raymond Toy
2015/02/13 17:08:15
half -> |half|, maybe?
"AudioParams amplify" -> "
hongchan
2015/02/13 20:19:26
Rephrased.
| |
| 45 // for each gain node, which is x2.25 (= 1.5 * 1.5) amplification in total . | |
| 46 half.connect(gain1.gain); | |
| 47 half.connect(gain2.gain); | |
| 48 | |
| 49 source.start(); | |
| 50 | |
| 51 context.startRendering().then(function (buffer) { | |
| 52 var channelData = buffer.getChannelData(0); | |
|
Raymond Toy
2015/02/13 17:08:15
I think you need a comment here that says you're d
| |
| 53 var expectedValues = [2.25, 1.5]; // 1 * 1.5 * 1.5 -> 1 * 1.5 | |
| 54 var passed = compareElements(expectedValues, channelData); | |
| 55 | |
| 56 if (passed) { | |
| 57 testPassed('Expected values match.'); | |
| 58 } else { | |
| 59 testFailed('Expected values do not match.'); | |
| 60 } | |
| 61 }).then(done); | |
| 62 | |
| 63 // Disconnects after the rendering starts. | |
| 64 // FIXME: This does not guarantee that the disconnection happens after the | |
| 65 // rendering starts. Reconsider this when we implements a way to change th e | |
|
Raymond Toy
2015/02/13 17:08:15
We aren't actually considering implementing sample
hongchan
2015/02/13 20:19:26
Fixed.
| |
| 66 // graph in a sample-accurately way in the middle of rendering. | |
| 67 setTimeout(function () { | |
| 68 half.disconnect(gain2.gain); | |
| 69 }, 10); | |
|
Raymond Toy
2015/02/13 17:08:15
I think there are some additional comments needed
hongchan
2015/02/13 20:19:26
Done.
| |
| 70 }); | |
| 71 | |
| 72 // Task 2: test disconnect(AudioParam, output) method. | |
| 73 audit.defineTask('disconnect(AudioParam, output)', function (done) { | |
| 74 | |
| 75 // Create a 2-channel buffer source with [1, 2] in each channel and | |
| 76 // make a serial connection through gain1 and gain 2. The make the buffer | |
| 77 // source half with a gain node and connect it to a 2-output splitter. | |
| 78 // Connect each output to 2 gain AudioParams respectively. | |
| 79 // (1) bufferSource => gain1 => gain2 | |
| 80 // (2) bufferSource => half => splitter(2) | |
| 81 // (3) splitter#0 => gain1.gain | |
| 82 // (4) splitter#1 => gain2.gain | |
| 83 // This graph should produce 3 (= 1 * 1.5 * 2) and 6 (= 2 * 1.5 * 2) for | |
| 84 // each channel. After disconnecting (4), it should output 1.5 and 3. | |
| 85 var context = new OfflineAudioContext(2, 441000, 44100); | |
| 86 var source = context.createBufferSource(); | |
| 87 var buffer2ch = createTestingAudioBuffer(context, 2, 128); | |
| 88 var splitter = context.createChannelSplitter(2); | |
| 89 var half = context.createGain(); | |
| 90 var gain1 = context.createGain(); | |
| 91 var gain2 = context.createGain(); | |
| 92 | |
| 93 source.buffer = buffer2ch; | |
| 94 source.loop = true; | |
| 95 half.gain.value = 0.5; | |
| 96 | |
| 97 source.connect(gain1); | |
| 98 gain1.connect(gain2); | |
| 99 gain2.connect(context.destination); | |
| 100 | |
| 101 // |source| originally is [1, 2] but it becomes [0.5, 1] after 0.5 gain. | |
| 102 // Each splitter's output will be applied to |gain1.gain| and |gain2.gain| | |
| 103 // respectively in an additive fashion. | |
| 104 source.connect(half); | |
| 105 half.connect(splitter); | |
| 106 | |
| 107 // This amplifies the signal by 1.5. (= 1.0 + 0.5) | |
| 108 splitter.connect(gain1.gain, 0); | |
| 109 | |
| 110 // This amplifies the signal by 2. (= 1.0 + 1.0) | |
| 111 splitter.connect(gain2.gain, 1); | |
| 112 | |
| 113 source.start(); | |
| 114 context.startRendering().then(function (buffer) { | |
| 115 var channelData0 = buffer.getChannelData(0); | |
|
Raymond Toy
2015/02/13 17:08:15
Same comments here and for setTimeout as for the p
hongchan
2015/02/13 20:19:26
The comment is down there at setTimeout().
| |
| 116 var channelData1 = buffer.getChannelData(1); | |
| 117 var expectedValuesCh0 = [3, 1.5]; // 1 * 1.5 * 2 -> 1 * 1.5 | |
| 118 var expectedValuesCh1 = [6, 3]; // 2 * 1.5 * 2 -> 2 * 1.5 | |
| 119 var passedCh0 = compareElements(expectedValuesCh0, channelData0); | |
| 120 var passedCh1 = compareElements(expectedValuesCh1, channelData1); | |
| 121 | |
| 122 if (passedCh0 && passedCh1) { | |
| 123 testPassed('Expected values match.'); | |
| 124 } else { | |
| 125 testFailed('Expected values do not match.'); | |
| 126 } | |
| 127 }).then(done); | |
| 128 | |
| 129 // Disconnect after the rendering starts. See the comment above. | |
| 130 setTimeout(function () { | |
| 131 splitter.disconnect(gain2.gain, 1); | |
| 132 }, 10); | |
| 133 | |
| 134 }); | |
| 135 | |
| 136 // Task 3: exception checks. | |
| 137 audit.defineTask('exceptions', function (done) { | |
| 138 var context = new AudioContext(); | |
| 139 var gain1 = context.createGain(); | |
| 140 var splitter = context.createChannelSplitter(2); | |
| 141 var gain2 = context.createGain(); | |
| 142 var gain3 = context.createGain(); | |
| 143 | |
| 144 gain1.connect(splitter); | |
| 145 splitter.connect(gain2.gain, 0); | |
| 146 splitter.connect(gain3.gain, 1); | |
| 147 gain2.connect(gain3); | |
| 148 gain3.connect(context.destination); | |
| 149 | |
| 150 // gain1 is not connected to gain3.gain. Exception should be thrown. | |
| 151 Should.throwWithType('InvalidAccessError', function () { | |
| 152 gain1.disconnect(gain3.gain); | |
| 153 }); | |
| 154 | |
| 155 // When the output index is good but the destination is invalid. | |
| 156 Should.throwWithType('InvalidAccessError', function () { | |
| 157 splitter.disconnect(gain1.gain, 1); | |
| 158 }); | |
| 159 | |
| 160 // When both arguments are wrong, throw IndexSizeError first. | |
|
Raymond Toy
2015/02/13 17:08:15
This seems wrong. Since the splitter is not connec
hongchan
2015/02/13 20:19:26
I thought about it and decided to make it this way
| |
| 161 Should.throwWithType('IndexSizeError', function () { | |
| 162 splitter.disconnect(gain1.gain, 2); | |
| 163 }); | |
| 164 | |
| 165 done(); | |
| 166 }); | |
| 167 | |
| 168 audit.defineTask('finish', function (done) { | |
| 169 finishJSTest(); | |
| 170 done(); | |
| 171 }); | |
| 172 | |
| 173 audit.runTasks( | |
| 174 'disconnect(AudioParam)', | |
| 175 'disconnect(AudioParam, output)', | |
| 176 'exceptions', | |
| 177 'finish' | |
| 178 ); | |
| 179 | |
| 180 // The value-wise comparison of two arrays with different length. It | |
| 181 // checks the value of element regardless of its occurrence. For example, | |
| 182 // (1) [2.25, 2.25, 2.25, 1.5, 1.5, 1.5] === [2.25, 1.5] | |
| 183 // (2) [2.25, 2.25, 1.5, 1.5, 0, 7, 7, 7, 9, 1] !== [2.25, 1.5] | |
| 184 // (3) [-1, 2, 3, 2.25, 2.25, 1.5, 1.5] !== [2.25, 1.5] | |
| 185 // (4) [3, 3, 3, 7, 7, 7] !== [2.25, 1.5] | |
| 186 function compareElements(expected, actual) { | |
| 187 var indexExpected = 0, indexActual = 0; | |
| 188 while (indexExpected < expected.length && indexActual < actual.length) { | |
| 189 if (expected[indexExpected] === actual[indexActual]) { | |
| 190 indexActual++; | |
| 191 } else { | |
| 192 indexExpected++; | |
| 193 } | |
| 194 } | |
| 195 | |
| 196 if (indexExpected < expected.length - 1 || indexActual < actual.length - 1) { | |
| 197 console.log('The value ' + actual[indexActual] + ' at index ' + | |
|
Raymond Toy
2015/02/13 17:08:15
console.log or testFailed?
hongchan
2015/02/13 20:19:26
This method is called by the other test task. Decl
Raymond Toy
2015/02/13 20:47:25
compareElements could be modified to print out the
Raymond Toy
2015/02/13 20:52:33
I wasn't clear, but you don't need to make this mo
| |
| 198 indexActual + ' was not found in expected values'); | |
| 199 return false; | |
| 200 } else { | |
| 201 return true; | |
| 202 } | |
| 203 } | |
| 204 | |
| 205 successfullyParsed = true; | |
| 206 </script> | |
| 207 </body> | |
| 208 | |
| 209 </html> | |
| OLD | NEW |