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 detune modulation.');
13 window
.jsTestIsAsync
= true;
15 var sampleRate
= 44100;
17 // To get an observable change on detune modulation, the minimum
18 // rendering length should greater than the rendering quantum.
19 var renderLength
= 256;
20 var half
= renderLength
/ 2;
22 // With the detune of 0, the duration of impulse buffer should be 4
23 // samples (which means the interval between impulses is 4). Increasing
24 // detune to 1200 (1 octave) 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, 1200] for modulating
39 // detune. The first half of buffer is 0 and the rest is 1200.
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 detune AudioParam
45 // value. For example, 1 DC offset value will result detune of 1200.
46 dcOffsetArray
[i
] = i
< half
? 0 : 1200;
53 // Task: Render the actual buffer and compare with the reference.
54 audit
.defineTask('synthesize-verify', function (done
) {
55 var impulse
= context
.createBufferSource();
56 var dcOffset
= context
.createBufferSource();
58 impulse
.buffer
= impulseBuffer
;
59 dcOffset
.buffer
= dcOffsetBuffer
;
62 impulse
.connect(context
.destination
);
63 dcOffset
.connect(impulse
.detune
);
68 context
.startRendering().then(function (renderedBuffer
) {
69 var data
= renderedBuffer
.getChannelData(0);
70 var passed
= true, i
= 0;
71 var nextImpulseIndex
= 0;
73 while (i
< renderLength
) {
74 if (i
=== nextImpulseIndex
&& data
[i
] === 1) {
75 // From 0 to 127th element, the interval between impulses is 4. On the other
76 // hand, the interval is 2 between 128th and 255th element.
77 nextImpulseIndex
+= (i
< half
) ? impulseLength
: impulseLength
/ 2;
78 } else if (data
[i
] !== 0) {
79 // If a value is neither 0 or 1, break the loop and fail the test.
88 testPassed('Increasing detune to 1200 decreased the interval between impulses to half.');
90 testFailed('Increasing detune produced the incorrect result' +
91 'at the index ' + i
+ '.');
96 audit
.defineTask('finish', function (done
) {
107 successfullyParsed
= true;