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 of LinearRampToValueAtTime</title> | |
10 </head> | |
11 | |
12 <body> | |
13 <script> | |
14 description("Test Sampling of LinearRampToValueAtTime"); | |
15 | |
16 window.jsTestIsAsync = true; | |
17 var sampleRate = 12800; | |
18 var context; | |
19 | |
20 var audit = Audit.createTaskRunner(); | |
21 | |
22 function runTest(config) { | |
23 // Create a short context with a constant signal source connected to a g
ain node that will | |
24 // be automated. | |
25 context = new OfflineAudioContext(1, 256, sampleRate); | |
26 | |
27 // Create a constant unit amplitude source. | |
28 var source = context.createBufferSource(); | |
29 var b = createConstantBuffer(context, 1, 1); | |
30 source.buffer = b; | |
31 source.loop = true; | |
32 | |
33 // Gain node that is to be automated. | |
34 var gain = context.createGain(); | |
35 gain.gain.value = 0; | |
36 gain.gain.setValueAtTime(config.startValue, config.startTime); | |
37 config.automationFunction(gain); | |
38 | |
39 source.connect(gain); | |
40 gain.connect(context.destination); | |
41 | |
42 source.start(); | |
43 | |
44 return context.startRendering().then(function (resultBuffer) { | |
45 // Check that the automation has the correct sampling. | |
46 var resultData = resultBuffer.getChannelData(0); | |
47 | |
48 // The automation has starts at config.startTime, so the frame just af
ter this should have | |
49 // the automation applied. | |
50 var startFrame = Math.ceil(config.startTime * sampleRate); | |
51 | |
52 // The automation ends at config.endTime so the frame just before this
should have the | |
53 // automation applied. | |
54 var endFrame = Math.floor(config.endTime * sampleRate); | |
55 | |
56 // Use the true automation to find the expected values. | |
57 var expectedStart = config.expectedFunction(startFrame / sampleRate); | |
58 var expectedEnd = config.expectedFunction(endFrame / sampleRate); | |
59 | |
60 var success = Should(config.desc + ": Sample " + startFrame, | |
61 resultData[startFrame], { | |
62 precision: 7 | |
63 }).beCloseTo(expectedStart, config.startValueThreshold); | |
64 success = Should(config.desc + ": Sample " + endFrame, | |
65 resultData[endFrame], { | |
66 precision: 7 | |
67 }).beCloseTo(expectedEnd, config.endValueThreshold) && success; | |
68 | |
69 if (success) | |
70 testPassed(config.desc + " passed.\n"); | |
71 else | |
72 testFailed(config.desc + " failed.\n"); | |
73 }); | |
74 } | |
75 | |
76 function expectedLinear(t) { | |
77 var slope = (this.endValue - this.startValue) / (this.endTime - this.sta
rtTime); | |
78 return this.startValue + slope * (t - this.startTime); | |
79 }; | |
80 | |
81 function expectedExponential(t) { | |
82 var ratio = this.endValue / this.startValue; | |
83 var exponent = (t - this.startTime) / (this.endTime - this.startTime); | |
84 return this.startValue * Math.pow(ratio, exponent); | |
85 }; | |
86 | |
87 function linearAutomation(g) { | |
88 g.gain.linearRampToValueAtTime(this.endValue, this.endTime); | |
89 } | |
90 | |
91 function exponentialAutomation(g) { | |
92 g.gain.exponentialRampToValueAtTime(this.endValue, this.endTime); | |
93 } | |
94 | |
95 // Basically want to test that if neither the start time nor end time is o
n a frame boundary | |
96 // that we sample the automation curve correctly. The start times and end
times are mostly | |
97 // arbitrary, except that they cannot be on a frame boundary. | |
98 var testConfigs = [{ | |
99 desc: "linearRamp", | |
100 startTime: .1 / sampleRate, | |
101 endTime: 128.1 / sampleRate, | |
102 startValue: 1, | |
103 endValue: 0, | |
104 startValueThreshold: 1.201e-8, | |
105 endValueThreshold: 1.526e-5, | |
106 automationFunction: linearAutomation, | |
107 expectedFunction: expectedLinear | |
108 }, { | |
109 desc: "linearRamp:short", | |
110 startTime: .1 / sampleRate, | |
111 endTime: 5.1 / sampleRate, | |
112 startValue: 1, | |
113 endValue: 0, | |
114 startValueThreshold: 8.723e-9, | |
115 endValueThreshold: 9.537e-7, | |
116 automationFunction: linearAutomation, | |
117 expectedFunction: expectedLinear | |
118 }, { | |
119 desc: "linearRamp:long", | |
120 startTime: .1 / sampleRate, | |
121 endTime: 200.1 / sampleRate, | |
122 startValue: 1, | |
123 endValue: 0, | |
124 startValueThreshold: 2.827e-8, | |
125 endValueThreshold: 4.674e-5, | |
126 automationFunction: linearAutomation, | |
127 expectedFunction: expectedLinear | |
128 }, { | |
129 desc: "exponentialRamp", | |
130 startTime: .1 / sampleRate, | |
131 endTime: 128.1 / sampleRate, | |
132 startValue: 1, | |
133 endValue: 1e-5, | |
134 startValueThreshold: 2.505e-8, | |
135 endValueThreshold: 1.484e-7, | |
136 automationFunction: exponentialAutomation, | |
137 expectedFunction: expectedExponential | |
138 }, { | |
139 desc: "exponentialRamp:short", | |
140 startTime: .1 / sampleRate, | |
141 endTime: 5.1 / sampleRate, | |
142 startValue: 1, | |
143 endValue: 1e-5, | |
144 startValueThreshold: 5.027e-8, | |
145 endValueThreshold: 3.821e-7, | |
146 automationFunction: exponentialAutomation, | |
147 expectedFunction: expectedExponential | |
148 }, { | |
149 desc: "exponentialRamp:long", | |
150 startTime: .1 / sampleRate, | |
151 endTime: 200.1 / sampleRate, | |
152 startValue: 1, | |
153 endValue: 1e-5, | |
154 startValueThreshold: 8.035e-9, | |
155 endValueThreshold: 1.337e-6, | |
156 automationFunction: exponentialAutomation, | |
157 expectedFunction: expectedExponential | |
158 }, | |
159 ]; | |
160 | |
161 function createTaskFunction(config) { | |
162 return function (done) { | |
163 runTest(config).then(done); | |
164 }; | |
165 } | |
166 | |
167 // Create all of the tasks from the test configs | |
168 for (var k = 0; k < testConfigs.length; ++k) { | |
169 var config = testConfigs[k]; | |
170 var taskName = config.desc + ":task" + k; | |
171 audit.defineTask(taskName, createTaskFunction(config)); | |
172 } | |
173 | |
174 audit.defineTask("finish", function (done) { | |
175 finishJSTest(); | |
176 done(); | |
177 }); | |
178 | |
179 audit.runTasks(); | |
180 successfullyParsed = true; | |
181 </script> | |
182 </body> | |
183 </html> | |
OLD | NEW |