OLD | NEW |
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <html> | 2 <html> |
3 | 3 |
4 <head> | 4 <head> |
5 <script src="../resources/js-test.js"></script> | 5 <script src="../resources/js-test.js"></script> |
6 <script src="resources/compatibility.js"></script> | 6 <script src="resources/compatibility.js"></script> |
7 <script src="resources/audio-testing.js"></script> | 7 <script src="resources/audio-testing.js"></script> |
8 </head> | 8 </head> |
9 | 9 |
10 <body> | 10 <body> |
11 <script> | 11 <script> |
12 description('Test ChannelMergerNode behavior on dynamic input change.'); | 12 description('Test ChannelMergerNode behavior on dynamic input change.'); |
13 window.jsTestIsAsync = true; | 13 window.jsTestIsAsync = true; |
14 | 14 |
| 15 var renderQuantum = 128; |
| 16 |
| 17 var numberOfChannels = 2; |
15 var sampleRate = 44100; | 18 var sampleRate = 44100; |
16 var numberOfChannels = 2; | 19 var renderDuration = 0.5; |
17 | 20 var disconnectTime = 0.5 * renderDuration; |
18 // The test needs the long render length (20 seconds) to capture the | |
19 // disconnection which happens after starting the rendering. | |
20 var renderLength = sampleRate * 20; | |
21 | 21 |
22 var audit = Audit.createTaskRunner(); | 22 var audit = Audit.createTaskRunner(); |
23 | 23 |
24 // Task: Check if the merger outputs a silent channel when an input is | 24 // Task: Check if the merger outputs a silent channel when an input is |
25 // disconnected. | 25 // disconnected. |
26 audit.defineTask('silent-disconnect', function (done) { | 26 audit.defineTask('silent-disconnect', function (done) { |
27 var context = new OfflineAudioContext( | 27 var context = new OfflineAudioContext(numberOfChannels, renderDuration * s
ampleRate, sampleRate); |
28 numberOfChannels, renderLength, sampleRate | |
29 ); | |
30 var merger = context.createChannelMerger(); | 28 var merger = context.createChannelMerger(); |
31 var source1 = context.createBufferSource(); | 29 var source1 = context.createBufferSource(); |
32 var source2 = context.createBufferSource(); | 30 var source2 = context.createBufferSource(); |
33 | 31 |
34 // Create and assign a mono testing buffer. | 32 // Create and assign a constant buffer. |
35 var bufferDCOffset = createTestingAudioBuffer(context, 1, renderLength); | 33 var bufferDCOffset = createConstantBuffer(context, 1, 1); |
36 source1.buffer = bufferDCOffset; | 34 source1.buffer = source2.buffer = bufferDCOffset; |
37 source2.buffer = bufferDCOffset; | 35 source1.loop = source2.loop = true; |
38 | 36 |
39 // Connect the output of source into the 4th input of merger. The merger | 37 // Connect the output of source into the 4th input of merger. The merger |
40 // should produce 6 channel output. | 38 // should produce 6 channel output. |
41 source1.connect(merger, 0, 0); | 39 source1.connect(merger, 0, 0); |
42 source2.connect(merger, 0, 1); | 40 source2.connect(merger, 0, 1); |
43 merger.connect(context.destination); | 41 merger.connect(context.destination); |
44 source1.start(); | 42 source1.start(); |
45 source2.start(); | 43 source2.start(); |
46 | 44 |
47 // When the rendering begins, disconnect |source2| as soon as possible. | 45 // Schedule the disconnection of |source2| at the half of render duration. |
48 context.onstatechange = function () { | 46 context.suspend(disconnectTime).then(function () { |
49 if (context.state === 'running') | 47 source2.disconnect(); |
50 source2.disconnect(); | 48 context.resume(); |
51 }; | 49 }); |
52 | 50 |
53 context.startRendering().then(function (buffer) { | 51 context.startRendering().then(function (buffer) { |
54 | |
55 // The entire first channel of the output should be 1. | 52 // The entire first channel of the output should be 1. |
56 Should('Channel #0', buffer.getChannelData(0)).beConstantValueOf(1); | 53 Should('Channel #0', buffer.getChannelData(0)).beConstantValueOf(1); |
57 | 54 |
| 55 // Calculate the first zero index in the second channel. |
| 56 var channel1 = buffer.getChannelData(1); |
| 57 var disconnectIndex = disconnectTime * sampleRate; |
| 58 disconnectIndex -= (disconnectIndex) % renderQuantum; |
| 59 var firstZeroIndex = channel1.findIndex(function (element, index) { |
| 60 if (element === 0) |
| 61 return index; |
| 62 }); |
| 63 |
58 // The second channel should contain 1, and 0 after the disconnection. | 64 // The second channel should contain 1, and 0 after the disconnection. |
59 Should('Channel #1', buffer.getChannelData(1)).containValues([1, 0]); | 65 Should('Channel #1', channel1).containValues([1, 0]); |
| 66 Should('The index of first zero in the channel #1', firstZeroIndex) |
| 67 .beEqualTo(disconnectIndex); |
60 | 68 |
61 done(); | 69 }).then(done); |
62 }); | |
63 }); | 70 }); |
64 | 71 |
65 audit.defineTask('finish', function (done) { | 72 audit.defineTask('finish', function (done) { |
66 finishJSTest(); | 73 finishJSTest(); |
67 done(); | 74 done(); |
68 }); | 75 }); |
69 | 76 |
70 audit.runTasks( | 77 audit.runTasks(); |
71 'silent-disconnect', | |
72 'finish' | |
73 ); | |
74 | 78 |
75 successfullyParsed = true; | 79 successfullyParsed = true; |
76 </script> | 80 </script> |
77 </body> | 81 </body> |
78 | 82 |
79 </html> | 83 </html> |
OLD | NEW |