OLD | NEW |
1 <!doctype html> | 1 <!doctype html> |
2 <html> | 2 <html> |
3 <head> | 3 <head> |
4 <script src="../../resources/js-test.js"></script> | 4 <script src="../../resources/testharness.js"></script> |
| 5 <script src="../../resources/testharnessreport.js"></script> |
5 <script src="../resources/audit-util.js"></script> | 6 <script src="../resources/audit-util.js"></script> |
6 <script src="../resources/audio-testing.js"></script> | 7 <script src="../resources/audit.js"></script> |
7 </head> | 8 </head> |
8 | 9 |
9 <body> | 10 <body> |
10 <script> | 11 <script> |
11 description('Test synchronous graph manipulation with OfflineAudioContext.
suspend() and OfflineAudioContext.resume().'); | 12 var audit = Audit.createTaskRunner(); |
12 window.jsTestIsAsync = true; | |
13 | 13 |
14 var context; | 14 var context; |
15 var renderQuantum = 128; | 15 var renderQuantum = 128; |
16 var renderDuration = 3; | 16 var renderDuration = 3; |
17 | 17 |
18 // The sample rate is multiple of the rendering quantum, so suspension | 18 // The sample rate is multiple of the rendering quantum, so suspension |
19 // times fall in to the render quantum boundary. | 19 // times fall in to the render quantum boundary. |
20 var sampleRate = renderQuantum * 100; | 20 var sampleRate = renderQuantum * 100; |
21 | 21 |
22 context = new OfflineAudioContext(1, sampleRate * renderDuration, sampleRa
te); | |
23 | |
24 // Create a constant buffer of 1.0. | |
25 var constantBuffer = createConstantBuffer(context, 1, 1.0); | |
26 var constantSource = context.createBufferSource(); | |
27 constantSource.buffer = constantBuffer; | |
28 constantSource.loop = true; | |
29 | |
30 // The audio output from the beginning (0.0 second) to the first suspend | |
31 // time should be 0.0 because there is no connection to the destination. | |
32 | |
33 // Suspend at 1 second and activate the source node. The audio output | 22 // Suspend at 1 second and activate the source node. The audio output |
34 // should be 1.0 from |suspendTime1| to the next suspension. | 23 // should be 1.0 from |suspendTime1| to the next suspension. |
35 var suspendTime1 = 1; | 24 var suspendTime1 = 1; |
36 context.suspend(suspendTime1).then(function () { | |
37 if (context.currentTime === suspendTime1) | |
38 testPassed('Context is suspended at ' + suspendTime1 * sampleRate + '
frame as expected.'); | |
39 | |
40 constantSource.connect(context.destination); | |
41 constantSource.start(); | |
42 testPassed('A constant buffer is connected to destination and started at
' + | |
43 suspendTime1 * sampleRate + ' frame.'); | |
44 | |
45 context.resume(); | |
46 }); | |
47 | 25 |
48 // Suspend at 2 seconds and disconnect the node. The audio output should | 26 // Suspend at 2 seconds and disconnect the node. The audio output should |
49 // be 0.0 from |suspendTime2| to the end. | 27 // be 0.0 from |suspendTime2| to the end. |
50 var suspendTime2 = 2; | 28 var suspendTime2 = 2; |
51 context.suspend(suspendTime2).then(function () { | |
52 if (context.currentTime === suspendTime2) | |
53 testPassed('Context is suspended at ' + suspendTime2 * sampleRate + '
frame as expected.'); | |
54 | 29 |
55 constantSource.disconnect(); | 30 audit.define('test', (task, should) => { |
56 testPassed('A constant buffer is disconnected at ' + suspendTime2 * samp
leRate + ' frame.'); | 31 task.describe('Synchronous graph manipulation with suspend() and resume(
)'); |
| 32 context = new OfflineAudioContext(1, sampleRate * renderDuration, |
| 33 sampleRate); |
57 | 34 |
58 context.resume(); | 35 // Create a constant buffer of 1.0. |
| 36 var constantBuffer = createConstantBuffer(context, 1, 1.0); |
| 37 var constantSource = context.createBufferSource(); |
| 38 constantSource.buffer = constantBuffer; |
| 39 constantSource.loop = true; |
| 40 |
| 41 // The audio output from the beginning (0.0 second) to the first suspend |
| 42 // time should be 0.0 because there is no connection to the destination. |
| 43 |
| 44 context.suspend(suspendTime1).then(function () { |
| 45 if (context.currentTime === suspendTime1) { |
| 46 should(context.currentTime * sampleRate, |
| 47 'Frame at which context is suspended') |
| 48 .beEqualTo(suspendTime1 * sampleRate) |
| 49 } |
| 50 should(() => { |
| 51 constantSource.connect(context.destination); |
| 52 constantSource.start() |
| 53 }, |
| 54 'Connecting a constant buffer to destination and starting at ' + |
| 55 suspendTime1 * sampleRate + ' frame') |
| 56 .notThrow();; |
| 57 context.resume(); |
| 58 }); |
| 59 |
| 60 context.suspend(suspendTime2).then(function () { |
| 61 if (context.currentTime === suspendTime2) { |
| 62 should(context.currentTime * sampleRate, |
| 63 'Context is suspended') |
| 64 .beEqualTo(suspendTime2 * sampleRate); |
| 65 } |
| 66 should(() => constantSource.disconnect(), |
| 67 'Disconnecting a constant buffer at ' + suspendTime2 * |
| 68 sampleRate + ' frame') |
| 69 .notThrow(); |
| 70 |
| 71 context.resume(); |
| 72 }); |
| 73 |
| 74 context.startRendering().then(function (buffer) { |
| 75 verifyResult(should, buffer); |
| 76 }).then(() => task.done()); |
59 }); | 77 }); |
60 | 78 |
61 context.startRendering().then(function (buffer) { | 79 function verifyResult(should, buffer) { |
62 verifyResult(buffer); | |
63 finishJSTest(); | |
64 }); | |
65 | |
66 function verifyResult(buffer) { | |
67 var data = buffer.getChannelData(0); | 80 var data = buffer.getChannelData(0); |
68 | 81 |
69 var suspendIndex1 = suspendTime1 * sampleRate; | 82 var suspendIndex1 = suspendTime1 * sampleRate; |
70 var suspendIndex2 = suspendTime2 * sampleRate; | 83 var suspendIndex2 = suspendTime2 * sampleRate; |
71 var endIndex = renderDuration * sampleRate; | 84 var endIndex = renderDuration * sampleRate; |
72 | 85 |
73 // Split the rendered buffer into 3 segments: | 86 // Split the rendered buffer into 3 segments: |
74 // [0, suspendIndex1), [suspendIndex1, suspendIndex2), [suspendIndex2, | 87 // [0, suspendIndex1), [suspendIndex1, suspendIndex2), [suspendIndex2, |
75 // endIndex). | 88 // endIndex). |
76 var subarray0 = data.subarray(0, suspendIndex1); | 89 var subarray0 = data.subarray(0, suspendIndex1); |
77 var subarray1 = data.subarray(suspendIndex1, suspendIndex2); | 90 var subarray1 = data.subarray(suspendIndex1, suspendIndex2); |
78 var subarray2 = data.subarray(suspendIndex2, endIndex); | 91 var subarray2 = data.subarray(suspendIndex2, endIndex); |
79 | 92 |
80 // Each segment should contain a constant value of 0, 1 and 0 | 93 // Each segment should contain a constant value of 0, 1 and 0 |
81 // respectively. | 94 // respectively. |
82 Should('Buffer frame [0, ' + suspendIndex1 + ')', subarray0).beConstant
ValueOf(0); | 95 should(subarray0, 'Buffer frame [0, ' + suspendIndex1 + ')') |
83 Should('Buffer frame [' + suspendIndex1 + ', ' + suspendIndex2 + ')', su
barray1) | 96 .beConstantValueOf(0); |
| 97 should(subarray1, 'Buffer frame [' + suspendIndex1 + ', ' + |
| 98 suspendIndex2 + ')') |
84 .beConstantValueOf(1); | 99 .beConstantValueOf(1); |
85 Should('Buffer frame [' + suspendIndex2 + ', ' + endIndex + ')', subarra
y2) | 100 should(subarray2, 'Buffer frame [' + suspendIndex2 + ', ' + endIndex + |
| 101 ')') |
86 .beConstantValueOf(0); | 102 .beConstantValueOf(0); |
87 } | 103 } |
88 | 104 |
89 successfullyParsed = true; | 105 audit.run(); |
90 </script> | 106 </script> |
91 | 107 |
92 </body> | 108 </body> |
93 </html> | 109 </html> |
OLD | NEW |