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/audit-util.js"></script> | |
8 <script src="resources/audio-testing.js"></script> | |
9 </head> | |
10 | |
11 <body> | |
12 <script> | |
13 description('AudioBufferSourceNode: DC-driven playbackRate modulation.'); | |
14 window.jsTestIsAsync = true; | |
15 | |
16 var sampleRate = 44100; | |
17 | |
18 // To get an observable change on playbackRate modulation, the minimum | |
19 // rendering length should greater than the rendering quantum. | |
20 var renderLength = 256; | |
21 var half = renderLength / 2; | |
22 | |
23 // With the playbackRate of 1, the duration of impulse buffer should be 4 | |
24 // samples (which means the interval between impulses is 4). Doubling | |
25 // playback speed decrease the interval to 2 samples. | |
26 var impulseLength = 4; | |
27 | |
28 var context = new OfflineAudioContext(1, renderLength, sampleRate); | |
29 var impulseBuffer, dcOffsetBuffer; | |
30 | |
31 var audit = Audit.createTaskRunner(); | |
32 | |
33 | |
34 // Task: build an impulse and DC-offset buffers for testing. | |
35 audit.defineTask('build-buffers', function (done) { | |
36 // 4-sample impulse sample. | |
37 impulseBuffer = createImpulseBuffer(context, impulseLength); | |
38 | |
39 // Create a DC offset buffer with 2 values [0, 1] for modulating | |
40 // playbackRate. The first half of buffer is 0 and the rest is 1. | |
41 dcOffsetBuffer = context.createBuffer(1, renderLength, sampleRate); | |
42 var dcOffsetArray = dcOffsetBuffer.getChannelData(0); | |
43 for (i = 0; i < dcOffsetArray.length; i++) { | |
44 | |
45 // Note that these values will be added to the playbackRate AudioParam | |
46 // value. For example, 0 DC offset value will result playbackRate of 1 | |
47 // because the default playbackRate value is 1. | |
48 dcOffsetArray[i] = i < half ? 0 : 1; | |
49 } | |
50 | |
51 done(); | |
52 }); | |
53 | |
54 | |
55 // Task: Render the actual buffer and compare with the reference. | |
56 audit.defineTask('synthesize-verify', function (done) { | |
57 var impulse = context.createBufferSource(); | |
58 var dcOffset = context.createBufferSource(); | |
59 | |
60 impulse.buffer = impulseBuffer; | |
61 dcOffset.buffer = dcOffsetBuffer; | |
62 impulse.loop = true; | |
63 | |
64 impulse.connect(context.destination); | |
65 dcOffset.connect(impulse.playbackRate); | |
66 | |
67 impulse.start(); | |
68 dcOffset.start(); | |
69 | |
70 context.startRendering().then(function (renderedBuffer) { | |
71 var data = renderedBuffer.getChannelData(0); | |
72 var passed = true, i = 0; | |
73 var nextImpulseIndex = 0; | |
74 | |
75 while (i < renderLength) { | |
76 if (i === nextImpulseIndex && data[i] === 1) { | |
77 // From 0 to 127th element, the interval between impulses is 4. On t
he other | |
78 // hand, the interval is 2 between 128th and 255th element. | |
79 nextImpulseIndex += (i < half) ? impulseLength : impulseLength / 2; | |
80 } else if (data[i] !== 0) { | |
81 // If a value is neither 0 or 1, break the loop and fail the test. | |
82 passed = false; | |
83 break; | |
84 } | |
85 | |
86 i++; | |
87 } | |
88 | |
89 if (passed) { | |
90 testPassed('Doubling playbackRate decreased the interval between impul
ses to half.'); | |
91 } else { | |
92 testFailed('Doubling playbackRate produced the incorrect result' + | |
93 'at the index ' + i + '.'); | |
94 } | |
95 }).then(done); | |
96 }); | |
97 | |
98 audit.defineTask('finish', function (done) { | |
99 finishJSTest(); | |
100 done(); | |
101 }); | |
102 | |
103 audit.runTasks( | |
104 'build-buffers', | |
105 'synthesize-verify', | |
106 'finish' | |
107 ); | |
108 | |
109 successfullyParsed = true; | |
110 </script> | |
111 </body> | |
112 | |
113 </html> | |
OLD | NEW |