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 AudioNode destination.'); |
| 13 window.jsTestIsAsync = true; |
| 14 |
| 15 var audit = Audit.createTaskRunner(); |
| 16 |
| 17 // Task 1: test disconnect() method. |
| 18 audit.defineTask('disconnect()', function (done) { |
| 19 |
| 20 // Connect a source to multiple gain nodes, each connected to the |
| 21 // destination. Then disconnect the source. The expected output should be |
| 22 // all zeros since the source was disconnected. |
| 23 var context = new OfflineAudioContext(1, 128, 44100); |
| 24 var source = context.createBufferSource(); |
| 25 var buffer1ch = createTestingAudioBuffer(context, 1, 128); |
| 26 var gain1 = context.createGain(); |
| 27 var gain2 = context.createGain(); |
| 28 var gain3 = context.createGain(); |
| 29 |
| 30 source.buffer = buffer1ch; |
| 31 |
| 32 source.connect(gain1); |
| 33 source.connect(gain2); |
| 34 source.connect(gain3); |
| 35 gain1.connect(context.destination); |
| 36 gain2.connect(context.destination); |
| 37 gain3.connect(context.destination); |
| 38 source.start(); |
| 39 |
| 40 // This disconnects everything. |
| 41 source.disconnect(); |
| 42 |
| 43 context.startRendering().then(function (buffer) { |
| 44 var channelData = buffer.getChannelData(0); |
| 45 |
| 46 // With everything disconnected, the result should be zero. |
| 47 Should.haveValueInChannel(0, channelData); |
| 48 |
| 49 }).then(done); |
| 50 }); |
| 51 |
| 52 // Task 2: test disconnect(output) method. |
| 53 audit.defineTask('disconnect(output)', function (done) { |
| 54 |
| 55 // Create multiple connections from each output of a ChannelSplitter |
| 56 // to a gain node. Then test if disconnecting a single output of splitter |
| 57 // is actually disconnected. |
| 58 var context = new OfflineAudioContext(1, 128, 44100); |
| 59 var source = context.createBufferSource(); |
| 60 var buffer3ch = createTestingAudioBuffer(context, 3, 128); |
| 61 var splitter = context.createChannelSplitter(3); |
| 62 var sum = context.createGain(); |
| 63 |
| 64 source.buffer = buffer3ch; |
| 65 |
| 66 source.connect(splitter); |
| 67 splitter.connect(sum, 0); |
| 68 splitter.connect(sum, 1); |
| 69 splitter.connect(sum, 2); |
| 70 sum.connect(context.destination); |
| 71 source.start(); |
| 72 |
| 73 // This disconnects the second output. |
| 74 splitter.disconnect(1); |
| 75 |
| 76 context.startRendering().then(function (buffer) { |
| 77 var channelData = buffer.getChannelData(0); |
| 78 |
| 79 // The rendered channel should contain 4. (= 1 + 0 + 3) |
| 80 Should.haveValueInChannel(4, channelData); |
| 81 |
| 82 }).then(done); |
| 83 }); |
| 84 |
| 85 // Task 3: test disconnect(AudioNode) method. |
| 86 audit.defineTask('disconnect(AudioNode)', function (done) { |
| 87 |
| 88 // Connect a source to multiple gain nodes. Then test if disconnecting a |
| 89 // single destination selectively works correctly. |
| 90 var context = new OfflineAudioContext(1, 128, 44100); |
| 91 var source = context.createBufferSource(); |
| 92 var buffer1ch = createTestingAudioBuffer(context, 1, 128); |
| 93 var gain1 = context.createGain(); |
| 94 var gain2 = context.createGain(); |
| 95 var gain3 = context.createGain(); |
| 96 var orphan = context.createGain(); |
| 97 |
| 98 source.buffer = buffer1ch; |
| 99 |
| 100 source.connect(gain1); |
| 101 source.connect(gain2); |
| 102 source.connect(gain3); |
| 103 gain1.connect(context.destination); |
| 104 gain2.connect(context.destination); |
| 105 gain3.connect(context.destination); |
| 106 source.start(); |
| 107 |
| 108 source.disconnect(gain2); |
| 109 |
| 110 context.startRendering().then(function (buffer) { |
| 111 var channelData = buffer.getChannelData(0); |
| 112 |
| 113 // The |sum| gain node should produce value 2. (1 + 0 + 1 = 2) |
| 114 Should.haveValueInChannel(2, channelData); |
| 115 |
| 116 }).then(done); |
| 117 }); |
| 118 |
| 119 // Task 4: test disconnect(AudioNode, output) method. |
| 120 audit.defineTask('disconnect(AudioNode, output)', function (done) { |
| 121 |
| 122 // Connect a buffer with 2 channels with each containing 1 and 2 |
| 123 // respectively to a ChannelSplitter, then connect the splitter to 2 gain |
| 124 // nodes as shown below: |
| 125 // (1) splitter#0 => gain1 |
| 126 // (2) splitter#0 => gain2 |
| 127 // (3) splitter#1 => gain2 |
| 128 // Then disconnect (2) and verify if the selective disconnection on a |
| 129 // specified output of the destination node works correctly. |
| 130 var context = new OfflineAudioContext(1, 128, 44100); |
| 131 var source = context.createBufferSource(); |
| 132 var buffer2ch = createTestingAudioBuffer(context, 2, 128); |
| 133 var splitter = context.createChannelSplitter(2); |
| 134 var gain1 = context.createGain(); |
| 135 var gain2 = context.createGain(); |
| 136 |
| 137 source.buffer = buffer2ch; |
| 138 |
| 139 source.connect(splitter); |
| 140 splitter.connect(gain1, 0); // gain1 gets channel 0. |
| 141 splitter.connect(gain2, 0); // gain2 sums channel 0 and 1. |
| 142 splitter.connect(gain2, 1); |
| 143 gain1.connect(context.destination); |
| 144 gain2.connect(context.destination); |
| 145 source.start(); |
| 146 |
| 147 splitter.disconnect(gain2, 0); // Now gain2 gets [2] |
| 148 |
| 149 context.startRendering().then(function (buffer) { |
| 150 var channelData0 = buffer.getChannelData(0); |
| 151 |
| 152 // The sum of gain1 and gain2 should produce value 3. (= 1 + 2) |
| 153 Should.haveValueInChannel(3, channelData0); |
| 154 |
| 155 }).then(done); |
| 156 }); |
| 157 |
| 158 // Task 5: test disconnect(AudioNode, output, input) method. |
| 159 audit.defineTask('disconnect(AudioNode, output, input)', function (done) { |
| 160 |
| 161 // Create a 3-channel buffer with [1, 2, 3] in each channel and then pass |
| 162 // it through a splitter and a merger. Each input/output of the splitter |
| 163 // and the merger is connected in a sequential order as shown below. |
| 164 // (1) splitter#0 => merger#0 |
| 165 // (2) splitter#1 => merger#1 |
| 166 // (3) splitter#2 => merger#2 |
| 167 // Then disconnect (3) and verify if each channel contains [1] and [2] |
| 168 // respectively. |
| 169 var context = new OfflineAudioContext(3, 128, 44100); |
| 170 var source = context.createBufferSource(); |
| 171 var buffer3ch = createTestingAudioBuffer(context, 3, 128); |
| 172 var splitter = context.createChannelSplitter(3); |
| 173 var merger = context.createChannelMerger(3); |
| 174 |
| 175 source.buffer = buffer3ch; |
| 176 |
| 177 source.connect(splitter); |
| 178 splitter.connect(merger, 0, 0); |
| 179 splitter.connect(merger, 1, 1); |
| 180 splitter.connect(merger, 2, 2); |
| 181 merger.connect(context.destination); |
| 182 source.start(); |
| 183 |
| 184 splitter.disconnect(merger, 2, 2); |
| 185 |
| 186 context.startRendering().then(function (buffer) { |
| 187 var channelData0 = buffer.getChannelData(0); |
| 188 var channelData1 = buffer.getChannelData(1); |
| 189 var channelData2 = buffer.getChannelData(2); |
| 190 |
| 191 // Each channel should have 1, 2, and 0 respectively. |
| 192 Should.haveValueInChannel(1, channelData0); |
| 193 Should.haveValueInChannel(2, channelData1); |
| 194 Should.haveValueInChannel(0, channelData2); |
| 195 |
| 196 }).then(done); |
| 197 }); |
| 198 |
| 199 // Task 6: exception checks. |
| 200 audit.defineTask('exceptions', function (done) { |
| 201 var context = new OfflineAudioContext(2, 128, 44100); |
| 202 var gain1 = context.createGain(); |
| 203 var splitter = context.createChannelSplitter(2); |
| 204 var merger = context.createChannelMerger(2); |
| 205 var gain2 = context.createGain(); |
| 206 var gain3 = context.createGain(); |
| 207 |
| 208 // Connect a splitter to gain nodes and merger so we can test the possible
|
| 209 // ways of disconnecting the nodes to verify that appropriate exceptions |
| 210 // are thrown. |
| 211 gain1.connect(splitter); |
| 212 splitter.connect(gain2, 0); |
| 213 splitter.connect(gain3, 1); |
| 214 splitter.connect(merger, 0, 0); |
| 215 splitter.connect(merger, 1, 1); |
| 216 gain2.connect(gain3); |
| 217 gain3.connect(context.destination); |
| 218 merger.connect(context.destination); |
| 219 |
| 220 // There is no output #2. An exception should be thrown. |
| 221 Should.throwWithType('IndexSizeError', function () { |
| 222 splitter.disconnect(2); |
| 223 }); |
| 224 |
| 225 // Disconnecting the output already disconnected should not throw. |
| 226 Should.notThrow(function () { |
| 227 splitter.disconnect(1); |
| 228 splitter.disconnect(1); |
| 229 }); |
| 230 |
| 231 // gain1 is not connected gain2. An exception should be thrown. |
| 232 Should.throwWithType('InvalidAccessError', function () { |
| 233 gain1.disconnect(gain2); |
| 234 }); |
| 235 |
| 236 // gain1 and gain3 are not connected. An exception should be thrown. |
| 237 Should.throwWithType('InvalidAccessError', function () { |
| 238 gain1.disconnect(gain3); |
| 239 }); |
| 240 |
| 241 // There is no output #2 in the splitter. An exception should be thrown. |
| 242 Should.throwWithType('IndexSizeError', function () { |
| 243 splitter.disconnect(gain2, 2); |
| 244 }); |
| 245 |
| 246 // The splitter and gain1 are not connected. An exception should be thrown
. |
| 247 Should.throwWithType('InvalidAccessError', function () { |
| 248 splitter.disconnect(gain1, 0); |
| 249 }); |
| 250 |
| 251 // The splitter output #0 and the gain3 output #0 are not connected. An |
| 252 // exception should be thrown. |
| 253 Should.throwWithType('InvalidAccessError', function () { |
| 254 splitter.disconnect(gain3, 0, 0); |
| 255 }); |
| 256 |
| 257 // The output index is out of bound. An exception should be thrown. |
| 258 Should.throwWithType('IndexSizeError', function () { |
| 259 splitter.disconnect(merger, 3, 0); |
| 260 }); |
| 261 |
| 262 done(); |
| 263 }); |
| 264 |
| 265 audit.defineTask('finish', function (done) { |
| 266 finishJSTest(); |
| 267 done(); |
| 268 }); |
| 269 |
| 270 audit.runTasks( |
| 271 'disconnect()', |
| 272 'disconnect(output)', |
| 273 'disconnect(AudioNode)', |
| 274 'disconnect(AudioNode, output)', |
| 275 'disconnect(AudioNode, output, input)', |
| 276 'exceptions', |
| 277 'finish' |
| 278 ); |
| 279 |
| 280 successfullyParsed = true; |
| 281 </script> |
| 282 </body> |
| 283 |
| 284 </html> |
OLD | NEW |