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 detune modulation.'); | |
14 window.jsTestIsAsync = true; | |
15 | |
16 var sampleRate = 44100; | |
17 | |
18 // To get an observable change on detune 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 detune of 0, the duration of impulse buffer should be 4 | |
24 // samples (which means the interval between impulses is 4). Increasing | |
25 // detune to 1200 (1 octave) 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, 1200] for modulating | |
40 // detune. The first half of buffer is 0 and the rest is 1200. | |
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 detune AudioParam | |
46 // value. For example, 1 DC offset value will result detune of 1200. | |
47 dcOffsetArray[i] = i < half ? 0 : 1200; | |
48 } | |
49 | |
50 done(); | |
51 }); | |
52 | |
53 | |
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(); | |
58 | |
59 impulse.buffer = impulseBuffer; | |
60 dcOffset.buffer = dcOffsetBuffer; | |
61 impulse.loop = true; | |
62 | |
63 impulse.connect(context.destination); | |
64 dcOffset.connect(impulse.detune); | |
65 | |
66 impulse.start(); | |
67 dcOffset.start(); | |
68 | |
69 context.startRendering().then(function (renderedBuffer) { | |
70 var data = renderedBuffer.getChannelData(0); | |
71 var passed = true, i = 0; | |
72 var nextImpulseIndex = 0; | |
73 | |
74 while (i < renderLength) { | |
75 if (i === nextImpulseIndex && data[i] === 1) { | |
76 // From 0 to 127th element, the interval between impulses is 4. On t
he 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. | |
81 passed = false; | |
82 break; | |
83 } | |
84 | |
85 i++; | |
86 } | |
87 | |
88 if (passed) { | |
89 testPassed('Increasing detune to 1200 decreased the interval between i
mpulses to half.'); | |
90 } else { | |
91 testFailed('Increasing detune produced the incorrect result' + | |
92 'at the index ' + i + '.'); | |
93 } | |
94 }).then(done); | |
95 }); | |
96 | |
97 audit.defineTask('finish', function (done) { | |
98 finishJSTest(); | |
99 done(); | |
100 }); | |
101 | |
102 audit.runTasks( | |
103 'build-buffers', | |
104 'synthesize-verify', | |
105 'finish' | |
106 ); | |
107 | |
108 successfullyParsed = true; | |
109 </script> | |
110 </body> | |
111 | |
112 </html> | |
OLD | NEW |