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