5 <script src=
"../resources/js-test.js"></script>
6 <script src=
"resources/compatibility.js"></script>
7 <script src=
"resources/audio-testing.js"></script>
12 description('AudioBufferSourceNode: DC-driven playbackRate modulation.');
13 window
.jsTestIsAsync
= true;
15 var sampleRate
= 44100;
17 // To get an observable change on playbackRate modulation, the minimum
18 // rendering length should greater than the rendering quantum.
19 var renderLength
= 256;
20 var half
= renderLength
/ 2;
22 // With the playbackRate of 1, the duration of impulse buffer should be 4
23 // samples (which means the interval between impulses is 4). Doubling
24 // playback speed decrease the interval to 2 samples.
25 var impulseLength
= 4;
27 var context
= new OfflineAudioContext(1, renderLength
, sampleRate
);
28 var impulseBuffer
, dcOffsetBuffer
;
30 var audit
= Audit
.createTaskRunner();
33 // Task: build an impulse and DC-offset buffers for testing.
34 audit
.defineTask('build-buffers', function (done
) {
35 // 4-sample impulse sample.
36 impulseBuffer
= createImpulseBuffer(context
, impulseLength
);
38 // Create a DC offset buffer with 2 values [0, 1] for modulating
39 // playbackRate. The first half of buffer is 0 and the rest is 1.
40 dcOffsetBuffer
= context
.createBuffer(1, renderLength
, sampleRate
);
41 var dcOffsetArray
= dcOffsetBuffer
.getChannelData(0);
42 for (i
= 0; i
< dcOffsetArray
.length
; i
++) {
44 // Note that these values will be added to the playbackRate AudioParam
45 // value. For example, 0 DC offset value will result playbackRate of 1
46 // because the default playbackRate value is 1.
47 dcOffsetArray
[i
] = i
< half
? 0 : 1;
54 // Task: Render the actual buffer and compare with the reference.
55 audit
.defineTask('synthesize-verify', function (done
) {
56 var impulse
= context
.createBufferSource();
57 var dcOffset
= context
.createBufferSource();
59 impulse
.buffer
= impulseBuffer
;
60 dcOffset
.buffer
= dcOffsetBuffer
;
63 impulse
.connect(context
.destination
);
64 dcOffset
.connect(impulse
.playbackRate
);
69 context
.startRendering().then(function (renderedBuffer
) {
70 var data
= renderedBuffer
.getChannelData(0);
71 var passed
= true, i
= 0;
72 var nextImpulseIndex
= 0;
74 while (i
< renderLength
) {
75 if (i
=== nextImpulseIndex
&& data
[i
] === 1) {
76 // From 0 to 127th element, the interval between impulses is 4. On the other
77 // hand, the interval is 2 between 128th and 255th element.
78 nextImpulseIndex
+= (i
< half
) ? impulseLength
: impulseLength
/ 2;
79 } else if (data
[i
] !== 0) {
80 // If a value is neither 0 or 1, break the loop and fail the test.
89 testPassed('Doubling playbackRate decreased the interval between impulses to half.');
91 testFailed('Doubling playbackRate produced the incorrect result' +
92 'at the index ' + i
+ '.');
97 audit
.defineTask('finish', function (done
) {
108 successfullyParsed
= true;