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 <script src="resources/late-start-testing.js"></script> | |
9 </head> | 8 </head> |
10 | 9 |
11 <body> | 10 <body> |
12 <script> | 11 <script> |
13 description('Test the late call of start(0) of BufferSource.'); | 12 description('Test the late call of start(0) of BufferSource.'); |
14 window.jsTestIsAsync = true; | 13 window.jsTestIsAsync = true; |
15 | 14 |
| 15 var renderQuantum = 128; |
| 16 |
| 17 var sampleRate = 44100; |
| 18 var renderDuration = 0.25; |
| 19 var startTime = 0.5 * renderDuration; |
| 20 |
16 var audit = Audit.createTaskRunner(); | 21 var audit = Audit.createTaskRunner(); |
17 | 22 |
18 var sampleRate = 44100; | 23 // Calculate the index for actual start time. |
| 24 function getStartIndex(time) { |
| 25 var startIndex = time * sampleRate; |
| 26 return startIndex -= (startIndex) % renderQuantum; |
| 27 } |
19 | 28 |
20 // The long render length (30 seconds) is to make sure the |onstatechange| | 29 // Get the index of value change. |
21 // event gets fired to start the source, which can take quite a bit of time. | 30 function getValueChangeIndex(array, targetValue) { |
22 var renderLength = 30; | 31 return array.findIndex(function (element, index) { |
| 32 if (element === targetValue) |
| 33 return true; |
| 34 }); |
| 35 } |
23 | 36 |
24 var context = new OfflineAudioContext(1, sampleRate * renderLength, sampleRa
te); | 37 audit.defineTask('test-late-start', function (done) { |
25 var dcOffsetbuffer = createConstantBuffer(context, 1000, 1.0); | 38 var context = new OfflineAudioContext(1, renderDuration * sampleRate, samp
leRate); |
26 var source = context.createBufferSource(); | 39 var dcOffsetbuffer = createConstantBuffer(context, 1, 1.0); |
27 source.buffer = dcOffsetbuffer; | 40 var source = context.createBufferSource(); |
| 41 source.buffer = dcOffsetbuffer; |
| 42 source.loop = true; |
| 43 source.connect(context.destination); |
28 | 44 |
29 // Test the buffer node is rendered correctly when the start time of start()
| 45 // Schedule source.start(0) at 0.01 second. The specified timing of |
30 // call is in the past in terms of the context time. | 46 // start() call is already passed in terms of the context time. So the |
31 runLateStartTest(audit, context, source); | 47 // argument |0| will be clamped to the current context time. |
| 48 // |
| 49 // With the sample rate of 44100, 0.01 second is 441 samples. Rounding |
| 50 // it down to the render quantum gives 384 samples. This is clearly larger |
| 51 // than a single render quantum. |
| 52 // |
| 53 // See issue: crbug.com/462167 |
| 54 context.suspend(startTime).then(function () { |
| 55 source.start(0); |
| 56 context.resume(); |
| 57 }); |
| 58 |
| 59 // Start rendering and verify result: this verifies if 1) the rendered |
| 60 // buffer contains at least one non-zero value and 2) the non-zero value i
s |
| 61 // found later than the first output sample. |
| 62 context.startRendering().then(function (buffer) { |
| 63 |
| 64 var channelData = buffer.getChannelData(0); |
| 65 var startIndex = getStartIndex(startTime); |
| 66 var nonZeroValueIndex = getValueChangeIndex(channelData, 1.0); |
| 67 |
| 68 Should('The output', channelData).containValues([0, 1]); |
| 69 Should('The index of value change', nonZeroValueIndex) |
| 70 .beEqualTo(startIndex); |
| 71 |
| 72 if (nonZeroValueIndex === 0) |
| 73 testFailed('The first sample was non-zero value. It should be zero.'); |
| 74 else |
| 75 testPassed('The rendered buffer contains non-zero values after the fir
st sample.'); |
| 76 |
| 77 }).then(done); |
| 78 }); |
| 79 |
| 80 audit.defineTask('finish-test', function (done) { |
| 81 done(); |
| 82 finishJSTest(); |
| 83 }); |
| 84 |
| 85 audit.runTasks(); |
32 | 86 |
33 successfullyParsed = true; | 87 successfullyParsed = true; |
34 </script> | 88 </script> |
35 </body> | 89 </body> |
36 | 90 |
37 </html> | 91 </html> |
OLD | NEW |