OLD | NEW |
| (Empty) |
1 <!doctype html> | |
2 <html> | |
3 <head> | |
4 <script src="../resources/js-test.js"></script> | |
5 <script src="resources/compatibility.js"></script> | |
6 <script src="resources/audit-util.js"></script> | |
7 <script src="resources/audio-testing.js"></script> | |
8 <script src="resources/audioparam-testing.js"></script> | |
9 <title>Test Sampling for SetTargetAtTime</title> | |
10 </head> | |
11 | |
12 <body> | |
13 <script> | |
14 description("Test Sampling of SetTargetAtTime"); | |
15 | |
16 window.jsTestIsAsync = true; | |
17 // Some slow sample rate, but otherwise arbitrary. | |
18 var sampleRate = 12800; | |
19 | |
20 // Time constant for setTargetValue. Make it short, but is otherwise arbit
rary. | |
21 var timeConstant = 10 / sampleRate; | |
22 | |
23 // Defaut initial gain for test. Arbitrary, but we want it large so the c
hanges look large, | |
24 // even if the relative change isn't. | |
25 var initialGain = 10000; | |
26 | |
27 var audit = Audit.createTaskRunner(); | |
28 | |
29 // Test sampling of setTargetAtTime that starts at |startFrame|. A gain n
ode is used for | |
30 // testing. |initializeGainFunction| initializes the gain value. | |
31 function doTest(message, startFrame, threshold, initializeGainFunction) { | |
32 var context = new OfflineAudioContext(1, 256, sampleRate); | |
33 var source = context.createBufferSource(); | |
34 var b = context.createBuffer(1, 1, sampleRate); | |
35 b.getChannelData(0)[0] = 1; | |
36 source.buffer = b; | |
37 source.loop = true; | |
38 | |
39 var gain = context.createGain(); | |
40 // Initialize the value of the gain node appropriately. | |
41 initializeGainFunction(gain); | |
42 gain.gain.setTargetAtTime(0, startFrame / sampleRate, timeConstant); | |
43 | |
44 source.connect(gain); | |
45 gain.connect(context.destination); | |
46 | |
47 source.start(); | |
48 | |
49 return context.startRendering().then(function (resultBuffer) { | |
50 // Verify that the sampling of the setTargetAtTime automation was done
correctly. We just | |
51 // look at the sample just past the start of the automation. | |
52 var resultData = resultBuffer.getChannelData(0); | |
53 // Compute the true result at the frame just past startFrame and verif
y that the actual | |
54 // rendered result is within |threshold| of the expected value. | |
55 var frame = Math.ceil(startFrame); | |
56 var v = 10000 * Math.exp(-(frame / sampleRate - startFrame / sampleRat
e) / timeConstant); | |
57 Should(message + ": Target value at frame " + frame, resultData[frame]
).beCloseTo(v, threshold); | |
58 }); | |
59 } | |
60 | |
61 function initializeGainBySetter (g) { | |
62 g.gain.value = initialGain; | |
63 } | |
64 | |
65 function initializeGainBySetValue (g) { | |
66 g.gain.setValueAtTime(initialGain, 0); | |
67 } | |
68 | |
69 audit.defineTask("setValue;128.1", function (done) { | |
70 doTest("Initialize by setValueAtTime", 128.1, 3.6029e-8, initializeGainB
ySetValue).then(done); | |
71 }); | |
72 | |
73 audit.defineTask("setValue;0.1", function (done) { | |
74 doTest("Initialize by setValueAtTime", 0.1, 3.6029e-8, initializeGainByS
etValue).then(done); | |
75 }); | |
76 | |
77 audit.defineTask("setValue;0.0", function (done) { | |
78 doTest("Initialize by setValueAtTime", 0, 3.6029e-8, initializeGainBySet
Value).then(done); | |
79 }); | |
80 | |
81 audit.defineTask("setter;128.1", function (done) { | |
82 doTest("Initialize by setter", 128.1, 3.6029e-8, initializeGainBySetter)
.then(done); | |
83 }); | |
84 | |
85 audit.defineTask("setter;0.1", function (done) { | |
86 doTest("Initialize by setter", 0.1, 3.6029e-8, initializeGainBySetter).t
hen(done); | |
87 }); | |
88 | |
89 audit.defineTask("setter;0.0", function (done) { | |
90 doTest("Initialize by setter", 0, 0, initializeGainBySetter).then(done); | |
91 }); | |
92 | |
93 | |
94 audit.defineTask("finish", function (done) { | |
95 finishJSTest(); | |
96 done(); | |
97 }); | |
98 | |
99 audit.runTasks(); | |
100 successfullyParsed = true; | |
101 </script> | |
102 </body> | |
103 </html> | |
OLD | NEW |