Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(85)

Side by Side Diff: third_party/WebKit/LayoutTests/webaudio/resources/audioparam-testing.js

Issue 2886883002: Refactor audioparam-testing.js (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 var sampleRate = 44100; 1 (function(global) {
2 2
3
3 // Information about the starting/ending times and starting/ending values for 4 // Information about the starting/ending times and starting/ending values for
4 // each time interval. 5 // each time interval.
5 var timeValueInfo; 6 let timeValueInfo;
6 7
7 // The difference between starting values between each time interval. 8 // The difference between starting values between each time interval.
8 var startingValueDelta; 9 let startingValueDelta;
9 10
10 // For any automation function that has an end or target value, the end value is 11 // For any automation function that has an end or target value, the end value is
11 // based the starting value of the time interval. The starting value will be 12 // based the starting value of the time interval. The starting value will be
12 // increased or decreased by |startEndValueChange|. We choose half of 13 // increased or decreased by |startEndValueChange|. We choose half of
13 // |startingValueDelta| so that the ending value will be distinct from the 14 // |startingValueDelta| so that the ending value will be distinct from the
14 // starting value for next time interval. This allows us to detect where the 15 // starting value for next time interval. This allows us to detect where the
15 // ramp begins and ends. 16 // ramp begins and ends.
16 var startEndValueChange; 17 let startEndValueChange;
17 18
18 // Default threshold to use for detecting discontinuities that should appear at 19 // Default threshold to use for detecting discontinuities that should appear at
19 // each time interval. 20 // each time interval.
20 var discontinuityThreshold; 21 let discontinuityThreshold;
21 22
22 // Time interval between value changes. It is best if 1 / numberOfTests is not 23 let context;
23 // close to timeInterval.
24 var timeInterval = .03;
25
26 // Some suitable time constant so that we can see a significant change over a
27 // timeInterval. This is only needed by setTargetAtTime() which needs a time
28 // constant.
29 var timeConstant = timeInterval / 3;
30
31 var gainNode;
32
33 var context;
34 24
35 // Make sure we render long enough to capture all of our test data. 25 // Make sure we render long enough to capture all of our test data.
36 function renderLength(numberOfTests) { 26 function renderLength(numberOfTests) {
37 return timeToSampleFrame((numberOfTests + 1) * timeInterval, sampleRate); 27 return timeToSampleFrame((numberOfTests + 1) * timeInterval, sampleRate);
38 } 28 }
39 29
40 // Create a constant reference signal with the given |value|. Basically the 30 // Create a constant reference signal with the given |value|. Basically the
41 // same as |createConstantBuffer|, but with the parameters to match the other 31 // same as |createConstantBuffer|, but with the parameters to match the other
42 // create functions. The |endValue| is ignored. 32 // create functions. The |endValue| is ignored.
43 function createConstantArray(startTime, endTime, value, endValue, sampleRate) { 33 function createConstantArray(startTime, endTime, value, endValue, sampleRate) {
44 var startFrame = timeToSampleFrame(startTime, sampleRate); 34 let startFrame = timeToSampleFrame(startTime, sampleRate);
45 var endFrame = timeToSampleFrame(endTime, sampleRate); 35 let endFrame = timeToSampleFrame(endTime, sampleRate);
46 var length = endFrame - startFrame; 36 let length = endFrame - startFrame;
47 37
48 var buffer = createConstantBuffer(context, length, value); 38 let buffer = createConstantBuffer(context, length, value);
49 39
50 return buffer.getChannelData(0); 40 return buffer.getChannelData(0);
51 } 41 }
52 42
53 function getStartEndFrames(startTime, endTime, sampleRate) { 43 function getStartEndFrames(startTime, endTime, sampleRate) {
54 // Start frame is the ceiling of the start time because the ramp starts at or 44 // Start frame is the ceiling of the start time because the ramp starts at or
55 // after the sample frame. End frame is the ceiling because it's the 45 // after the sample frame. End frame is the ceiling because it's the
56 // exclusive ending frame of the automation. 46 // exclusive ending frame of the automation.
57 var startFrame = Math.ceil(startTime * sampleRate); 47 let startFrame = Math.ceil(startTime * sampleRate);
58 var endFrame = Math.ceil(endTime * sampleRate); 48 let endFrame = Math.ceil(endTime * sampleRate);
59 49
60 return {startFrame: startFrame, endFrame: endFrame}; 50 return {startFrame: startFrame, endFrame: endFrame};
61 } 51 }
62 52
63 // Create a linear ramp starting at |startValue| and ending at |endValue|. The 53 // Create a linear ramp starting at |startValue| and ending at |endValue|. The
64 // ramp starts at time |startTime| and ends at |endTime|. (The start and end 54 // ramp starts at time |startTime| and ends at |endTime|. (The start and end
65 // times are only used to compute how many samples to return.) 55 // times are only used to compute how many samples to return.)
66 function createLinearRampArray( 56 function createLinearRampArray(
67 startTime, endTime, startValue, endValue, sampleRate) { 57 startTime, endTime, startValue, endValue, sampleRate) {
68 var frameInfo = getStartEndFrames(startTime, endTime, sampleRate); 58 let frameInfo = getStartEndFrames(startTime, endTime, sampleRate);
69 var startFrame = frameInfo.startFrame; 59 let startFrame = frameInfo.startFrame;
70 var endFrame = frameInfo.endFrame; 60 let endFrame = frameInfo.endFrame;
71 var length = endFrame - startFrame; 61 let length = endFrame - startFrame;
72 var array = new Array(length); 62 let array = new Array(length);
73 63
74 var step = 64 let step =
75 Math.fround((endValue - startValue) / (endTime - startTime) / sampleRate); 65 Math.fround((endValue - startValue) / (endTime - startTime) / sampleRate);
76 var start = Math.fround( 66 let start = Math.fround(
77 startValue + 67 startValue +
78 (endValue - startValue) * (startFrame / sampleRate - startTime) / 68 (endValue - startValue) * (startFrame / sampleRate - startTime) /
79 (endTime - startTime)); 69 (endTime - startTime));
80 70
81 var slope = (endValue - startValue) / (endTime - startTime); 71 let slope = (endValue - startValue) / (endTime - startTime);
82 72
83 // v(t) = v0 + (v1 - v0)*(t-t0)/(t1-t0) 73 // v(t) = v0 + (v1 - v0)*(t-t0)/(t1-t0)
84 for (k = 0; k < length; ++k) { 74 for (k = 0; k < length; ++k) {
85 // array[k] = Math.fround(start + k * step); 75 // array[k] = Math.fround(start + k * step);
86 var t = (startFrame + k) / sampleRate; 76 let t = (startFrame + k) / sampleRate;
87 array[k] = startValue + slope * (t - startTime); 77 array[k] = startValue + slope * (t - startTime);
88 } 78 }
89 79
90 return array; 80 return array;
91 } 81 }
92 82
93 // Create an exponential ramp starting at |startValue| and ending at |endValue|. 83 // Create an exponential ramp starting at |startValue| and ending at |endValue|.
94 // The ramp starts at time |startTime| and ends at |endTime|. (The start and 84 // The ramp starts at time |startTime| and ends at |endTime|. (The start and
95 // end times are only used to compute how many samples to return.) 85 // end times are only used to compute how many samples to return.)
96 function createExponentialRampArray( 86 function createExponentialRampArray(
97 startTime, endTime, startValue, endValue, sampleRate) { 87 startTime, endTime, startValue, endValue, sampleRate) {
98 var deltaTime = endTime - startTime; 88 let deltaTime = endTime - startTime;
99 89
100 var frameInfo = getStartEndFrames(startTime, endTime, sampleRate); 90 let frameInfo = getStartEndFrames(startTime, endTime, sampleRate);
101 var startFrame = frameInfo.startFrame; 91 let startFrame = frameInfo.startFrame;
102 var endFrame = frameInfo.endFrame; 92 let endFrame = frameInfo.endFrame;
103 var length = endFrame - startFrame; 93 let length = endFrame - startFrame;
104 var array = new Array(length); 94 let array = new Array(length);
105 95
106 var ratio = endValue / startValue; 96 let ratio = endValue / startValue;
107 97
108 // v(t) = v0*(v1/v0)^((t-t0)/(t1-t0)) 98 // v(t) = v0*(v1/v0)^((t-t0)/(t1-t0))
109 for (var k = 0; k < length; ++k) { 99 for (let k = 0; k < length; ++k) {
110 var t = Math.fround((startFrame + k) / sampleRate); 100 let t = Math.fround((startFrame + k) / sampleRate);
111 array[k] = 101 array[k] =
112 Math.fround(startValue * Math.pow(ratio, (t - startTime) / deltaTime)); 102 Math.fround(startValue * Math.pow(ratio, (t - startTime) / deltaTime));
113 } 103 }
114 104
115 return array; 105 return array;
116 } 106 }
117 107
118 function discreteTimeConstantForSampleRate(timeConstant, sampleRate) { 108 function discreteTimeConstantForSampleRate(timeConstant, sampleRate) {
119 return 1 - Math.exp(-1 / (sampleRate * timeConstant)); 109 return 1 - Math.exp(-1 / (sampleRate * timeConstant));
120 } 110 }
121 111
122 // Create a signal that starts at |startValue| and exponentially approaches the 112 // Create a signal that starts at |startValue| and exponentially approaches the
123 // target value of |targetValue|, using a time constant of |timeConstant|. The 113 // target value of |targetValue|, using a time constant of |timeConstant|. The
124 // ramp starts at time |startTime| and ends at |endTime|. (The start and end 114 // ramp starts at time |startTime| and ends at |endTime|. (The start and end
125 // times are only used to compute how many samples to return.) 115 // times are only used to compute how many samples to return.)
126 function createExponentialApproachArray( 116 function createExponentialApproachArray(
127 startTime, endTime, startValue, targetValue, sampleRate, timeConstant) { 117 startTime, endTime, startValue, targetValue, sampleRate, timeConstant) {
128 var startFrameFloat = startTime * sampleRate; 118 let startFrameFloat = startTime * sampleRate;
129 var frameInfo = getStartEndFrames(startTime, endTime, sampleRate); 119 let frameInfo = getStartEndFrames(startTime, endTime, sampleRate);
130 var startFrame = frameInfo.startFrame; 120 let startFrame = frameInfo.startFrame;
131 var endFrame = frameInfo.endFrame; 121 let endFrame = frameInfo.endFrame;
132 var length = Math.floor(endFrame - startFrame); 122 let length = Math.floor(endFrame - startFrame);
133 var array = new Array(length); 123 let array = new Array(length);
134 var c = discreteTimeConstantForSampleRate(timeConstant, sampleRate); 124 let c = discreteTimeConstantForSampleRate(timeConstant, sampleRate);
135 125
136 var delta = startValue - targetValue; 126 let delta = startValue - targetValue;
137 127
138 // v(t) = v1 + (v0 - v1) * exp(-(t-t0)/tau) 128 // v(t) = v1 + (v0 - v1) * exp(-(t-t0)/tau)
139 for (var k = 0; k < length; ++k) { 129 for (let k = 0; k < length; ++k) {
140 var t = (startFrame + k) / sampleRate; 130 let t = (startFrame + k) / sampleRate;
141 var value = targetValue + delta * Math.exp(-(t - startTime) / timeConstant); 131 let value = targetValue + delta * Math.exp(-(t - startTime) / timeConstant);
142 array[k] = value; 132 array[k] = value;
143 } 133 }
144 134
145 return array; 135 return array;
146 } 136 }
147 137
148 // Create a sine wave of the specified duration. 138 // Create a sine wave of the specified duration.
149 function createReferenceSineArray( 139 function createReferenceSineArray(
150 startTime, endTime, startValue, endValue, sampleRate) { 140 startTime, endTime, startValue, endValue, sampleRate) {
151 // Ignore |startValue| and |endValue| for the sine wave. 141 // Ignore |startValue| and |endValue| for the sine wave.
152 var curve = createSineWaveArray( 142 let curve = createSineWaveArray(
153 endTime - startTime, freqHz, sineAmplitude, sampleRate); 143 endTime - startTime, freqHz, sineAmplitude, sampleRate);
154 // Sample the curve appropriately. 144 // Sample the curve appropriately.
155 var frameInfo = getStartEndFrames(startTime, endTime, sampleRate); 145 let frameInfo = getStartEndFrames(startTime, endTime, sampleRate);
156 var startFrame = frameInfo.startFrame; 146 let startFrame = frameInfo.startFrame;
157 var endFrame = frameInfo.endFrame; 147 let endFrame = frameInfo.endFrame;
158 var length = Math.floor(endFrame - startFrame); 148 let length = Math.floor(endFrame - startFrame);
159 var array = new Array(length); 149 let array = new Array(length);
160 150
161 // v(t) = linearly interpolate between V[k] and V[k + 1] where k = 151 // v(t) = linearly interpolate between V[k] and V[k + 1] where k =
162 // floor((N-1)/duration*(t - t0)) 152 // floor((N-1)/duration*(t - t0))
163 var f = (length - 1) / (endTime - startTime); 153 let f = (length - 1) / (endTime - startTime);
164 154
165 for (var k = 0; k < length; ++k) { 155 for (let k = 0; k < length; ++k) {
166 var t = (startFrame + k) / sampleRate; 156 let t = (startFrame + k) / sampleRate;
167 var indexFloat = f * (t - startTime); 157 let indexFloat = f * (t - startTime);
168 var index = Math.floor(indexFloat); 158 let index = Math.floor(indexFloat);
169 if (index + 1 < length) { 159 if (index + 1 < length) {
170 var v0 = curve[index]; 160 let v0 = curve[index];
171 var v1 = curve[index + 1]; 161 let v1 = curve[index + 1];
172 array[k] = v0 + (v1 - v0) * (indexFloat - index); 162 array[k] = v0 + (v1 - v0) * (indexFloat - index);
173 } else { 163 } else {
174 array[k] = curve[length - 1]; 164 array[k] = curve[length - 1];
175 } 165 }
176 } 166 }
177 167
178 return array; 168 return array;
179 } 169 }
180 170
181 // Create a sine wave of the given frequency and amplitude. The sine wave is 171 // Create a sine wave of the given frequency and amplitude. The sine wave is
182 // offset by half the amplitude so that result is always positive. 172 // offset by half the amplitude so that result is always positive.
183 function createSineWaveArray(durationSeconds, freqHz, amplitude, sampleRate) { 173 function createSineWaveArray(durationSeconds, freqHz, amplitude, sampleRate) {
184 var length = timeToSampleFrame(durationSeconds, sampleRate); 174 let length = timeToSampleFrame(durationSeconds, sampleRate);
185 var signal = new Float32Array(length); 175 let signal = new Float32Array(length);
186 var omega = 2 * Math.PI * freqHz / sampleRate; 176 let omega = 2 * Math.PI * freqHz / sampleRate;
187 var halfAmplitude = amplitude / 2; 177 let halfAmplitude = amplitude / 2;
188 178
189 for (var k = 0; k < length; ++k) { 179 for (let k = 0; k < length; ++k) {
190 signal[k] = halfAmplitude + halfAmplitude * Math.sin(omega * k); 180 signal[k] = halfAmplitude + halfAmplitude * Math.sin(omega * k);
191 } 181 }
192 182
193 return signal; 183 return signal;
194 } 184 }
195 185
196 // Return the difference between the starting value and the ending value for 186 // Return the difference between the starting value and the ending value for
197 // time interval |timeIntervalIndex|. We alternate between an end value that is 187 // time interval |timeIntervalIndex|. We alternate between an end value that is
198 // above or below the starting value. 188 // above or below the starting value.
199 function endValueDelta(timeIntervalIndex) { 189 function endValueDelta(timeIntervalIndex) {
(...skipping 18 matching lines...) Expand all
218 // the starting value at the next time interval. Since we started at a large 208 // the starting value at the next time interval. Since we started at a large
219 // initial value, we decrease the value at each time interval. 209 // initial value, we decrease the value at each time interval.
220 function valueUpdate(timeIntervalIndex) { 210 function valueUpdate(timeIntervalIndex) {
221 return -startingValueDelta; 211 return -startingValueDelta;
222 } 212 }
223 213
224 // Compare a section of the rendered data against our expected signal. 214 // Compare a section of the rendered data against our expected signal.
225 function comparePartialSignals( 215 function comparePartialSignals(
226 should, rendered, expectedFunction, startTime, endTime, valueInfo, 216 should, rendered, expectedFunction, startTime, endTime, valueInfo,
227 sampleRate, errorMetric) { 217 sampleRate, errorMetric) {
228 var startSample = timeToSampleFrame(startTime, sampleRate); 218 let startSample = timeToSampleFrame(startTime, sampleRate);
229 var expected = expectedFunction( 219 let expected = expectedFunction(
230 startTime, endTime, valueInfo.startValue, valueInfo.endValue, sampleRate, 220 startTime, endTime, valueInfo.startValue, valueInfo.endValue, sampleRate,
231 timeConstant); 221 timeConstant);
232 222
233 var n = expected.length; 223 let n = expected.length;
234 var maxError = -1; 224 let maxError = -1;
235 var maxErrorIndex = -1; 225 let maxErrorIndex = -1;
236 226
237 for (var k = 0; k < n; ++k) { 227 for (let k = 0; k < n; ++k) {
238 // Make sure we don't pass these tests because a NaN has been generated in 228 // Make sure we don't pass these tests because a NaN has been generated in
239 // either the 229 // either the
240 // rendered data or the reference data. 230 // rendered data or the reference data.
241 if (!isValidNumber(rendered[startSample + k])) { 231 if (!isValidNumber(rendered[startSample + k])) {
242 maxError = Infinity; 232 maxError = Infinity;
243 maxErrorIndex = startSample + k; 233 maxErrorIndex = startSample + k;
244 should( 234 should(
245 isValidNumber(rendered[startSample + k]), 235 isValidNumber(rendered[startSample + k]),
246 'NaN or infinity for rendered data at ' + maxErrorIndex) 236 'NaN or infinity for rendered data at ' + maxErrorIndex)
247 .beTrue(); 237 .beTrue();
248 break; 238 break;
249 } 239 }
250 if (!isValidNumber(expected[k])) { 240 if (!isValidNumber(expected[k])) {
251 maxError = Infinity; 241 maxError = Infinity;
252 maxErrorIndex = startSample + k; 242 maxErrorIndex = startSample + k;
253 should( 243 should(
254 isValidNumber(expected[k]), 244 isValidNumber(expected[k]),
255 'NaN or infinity for rendered data at ' + maxErrorIndex) 245 'NaN or infinity for rendered data at ' + maxErrorIndex)
256 .beTrue(); 246 .beTrue();
257 break; 247 break;
258 } 248 }
259 var error = Math.abs(errorMetric(rendered[startSample + k], expected[k])); 249 let error = Math.abs(errorMetric(rendered[startSample + k], expected[k]));
260 if (error > maxError) { 250 if (error > maxError) {
261 maxError = error; 251 maxError = error;
262 maxErrorIndex = k; 252 maxErrorIndex = k;
263 } 253 }
264 } 254 }
265 255
266 return {maxError: maxError, index: maxErrorIndex, expected: expected}; 256 return {maxError: maxError, index: maxErrorIndex, expected: expected};
267 } 257 }
268 258
269 // Find the discontinuities in the data and compare the locations of the 259 // Find the discontinuities in the data and compare the locations of the
270 // discontinuities with the times that define the time intervals. There is a 260 // discontinuities with the times that define the time intervals. There is a
271 // discontinuity if the difference between successive samples exceeds the 261 // discontinuity if the difference between successive samples exceeds the
272 // threshold. 262 // threshold.
273 function verifyDiscontinuities(should, values, times, threshold) { 263 function verifyDiscontinuities(should, values, times, threshold) {
274 var n = values.length; 264 let n = values.length;
275 var success = true; 265 let success = true;
276 var badLocations = 0; 266 let badLocations = 0;
277 var breaks = []; 267 let breaks = [];
278 268
279 // Find discontinuities. 269 // Find discontinuities.
280 for (var k = 1; k < n; ++k) { 270 for (let k = 1; k < n; ++k) {
281 if (Math.abs(values[k] - values[k - 1]) > threshold) { 271 if (Math.abs(values[k] - values[k - 1]) > threshold) {
282 breaks.push(k); 272 breaks.push(k);
283 } 273 }
284 } 274 }
285 275
286 var testCount; 276 let testCount;
287 277
288 // If there are numberOfTests intervals, there are only numberOfTests - 1 278 // If there are numberOfTests intervals, there are only numberOfTests - 1
289 // internal interval boundaries. Hence the maximum number of discontinuties we 279 // internal interval boundaries. Hence the maximum number of discontinuties we
290 // expect to find is numberOfTests - 1. If we find more than that, we have no 280 // expect to find is numberOfTests - 1. If we find more than that, we have no
291 // reference to compare against. We also assume that the actual 281 // reference to compare against. We also assume that the actual
292 // discontinuities are close to the expected ones. 282 // discontinuities are close to the expected ones.
293 // 283 //
294 // This is just a sanity check when something goes really wrong. For example, 284 // This is just a sanity check when something goes really wrong. For example,
295 // if the threshold is too low, every sample frame looks like a discontinuity. 285 // if the threshold is too low, every sample frame looks like a discontinuity.
296 if (breaks.length >= numberOfTests) { 286 if (breaks.length >= numberOfTests) {
297 testCount = numberOfTests - 1; 287 testCount = numberOfTests - 1;
298 should(breaks.length, 'Number of discontinuities') 288 should(breaks.length, 'Number of discontinuities')
299 .beLessThan(numberOfTests); 289 .beLessThan(numberOfTests);
300 success = false; 290 success = false;
301 } else { 291 } else {
302 testCount = breaks.length; 292 testCount = breaks.length;
303 } 293 }
304 294
305 // Compare the location of each discontinuity with the end time of each 295 // Compare the location of each discontinuity with the end time of each
306 // interval. (There is no discontinuity at the start of the signal.) 296 // interval. (There is no discontinuity at the start of the signal.)
307 for (var k = 0; k < testCount; ++k) { 297 for (let k = 0; k < testCount; ++k) {
308 var expectedSampleFrame = timeToSampleFrame(times[k + 1], sampleRate); 298 let expectedSampleFrame = timeToSampleFrame(times[k + 1], sampleRate);
309 if (breaks[k] != expectedSampleFrame) { 299 if (breaks[k] != expectedSampleFrame) {
310 success = false; 300 success = false;
311 ++badLocations; 301 ++badLocations;
312 should(breaks[k], 'Discontinuity at index') 302 should(breaks[k], 'Discontinuity at index')
313 .beEqualTo(expectedSampleFrame); 303 .beEqualTo(expectedSampleFrame);
314 } 304 }
315 } 305 }
316 306
317 if (badLocations) { 307 if (badLocations) {
318 should(badLocations, 'Number of discontinuites at incorrect locations') 308 should(badLocations, 'Number of discontinuites at incorrect locations')
(...skipping 20 matching lines...) Expand all
339 // 329 //
340 // expectedFunction - function to compute the expected data 330 // expectedFunction - function to compute the expected data
341 // 331 //
342 // timeValueInfo - array containing information about the start and end times 332 // timeValueInfo - array containing information about the start and end times
343 // and the start and end values of each interval. 333 // and the start and end values of each interval.
344 // 334 //
345 // breakThreshold - threshold to use for determining discontinuities. 335 // breakThreshold - threshold to use for determining discontinuities.
346 function compareSignals( 336 function compareSignals(
347 should, testName, maxError, renderedData, expectedFunction, timeValueInfo, 337 should, testName, maxError, renderedData, expectedFunction, timeValueInfo,
348 breakThreshold, errorMetric) { 338 breakThreshold, errorMetric) {
349 var success = true; 339 let success = true;
350 var failedTestCount = 0; 340 let failedTestCount = 0;
351 var times = timeValueInfo.times; 341 let times = timeValueInfo.times;
352 var values = timeValueInfo.values; 342 let values = timeValueInfo.values;
353 var n = values.length; 343 let n = values.length;
354 var expectedSignal = []; 344 let expectedSignal = [];
355 345
356 success = verifyDiscontinuities(should, renderedData, times, breakThreshold); 346 success = verifyDiscontinuities(should, renderedData, times, breakThreshold);
357 347
358 for (var k = 0; k < n; ++k) { 348 for (let k = 0; k < n; ++k) {
359 var result = comparePartialSignals( 349 let result = comparePartialSignals(
360 should, renderedData, expectedFunction, times[k], times[k + 1], 350 should, renderedData, expectedFunction, times[k], times[k + 1],
361 values[k], sampleRate, errorMetric); 351 values[k], sampleRate, errorMetric);
362 352
363 expectedSignal = 353 expectedSignal =
364 expectedSignal.concat(Array.prototype.slice.call(result.expected)); 354 expectedSignal.concat(Array.prototype.slice.call(result.expected));
365 355
366 should( 356 should(
367 result.maxError, 357 result.maxError,
368 'Max error for test ' + k + ' at offset ' + 358 'Max error for test ' + k + ' at offset ' +
369 (result.index + timeToSampleFrame(times[k], sampleRate))) 359 (result.index + timeToSampleFrame(times[k], sampleRate)))
(...skipping 17 matching lines...) Expand all
387 // with the rendered data. 377 // with the rendered data.
388 // 378 //
389 // jumpThreshold - optional parameter that specifies the threshold to use for 379 // jumpThreshold - optional parameter that specifies the threshold to use for
390 // detecting discontinuities. If not specified, defaults to 380 // detecting discontinuities. If not specified, defaults to
391 // discontinuityThreshold. 381 // discontinuityThreshold.
392 // 382 //
393 function checkResultFunction( 383 function checkResultFunction(
394 task, should, testName, error, referenceFunction, jumpThreshold, 384 task, should, testName, error, referenceFunction, jumpThreshold,
395 errorMetric) { 385 errorMetric) {
396 return function(event) { 386 return function(event) {
397 var buffer = event.renderedBuffer; 387 let buffer = event.renderedBuffer;
398 renderedData = buffer.getChannelData(0); 388 renderedData = buffer.getChannelData(0);
399 389
400 var threshold; 390 let threshold;
401 391
402 if (!jumpThreshold) { 392 if (!jumpThreshold) {
403 threshold = discontinuityThreshold; 393 threshold = discontinuityThreshold;
404 } else { 394 } else {
405 threshold = jumpThreshold; 395 threshold = jumpThreshold;
406 } 396 }
407 397
408 compareSignals( 398 compareSignals(
409 should, testName, error, renderedData, referenceFunction, timeValueInfo, 399 should, testName, error, renderedData, referenceFunction, timeValueInfo,
410 threshold, errorMetric); 400 threshold, errorMetric);
(...skipping 10 matching lines...) Expand all
421 // setValueFunction - function that sets the specified value at the start of a 411 // setValueFunction - function that sets the specified value at the start of a
422 // time interval. 412 // time interval.
423 // 413 //
424 // automationFunction - function that sets the end value for the time interval. 414 // automationFunction - function that sets the end value for the time interval.
425 // It specifies how the value approaches the end value. 415 // It specifies how the value approaches the end value.
426 // 416 //
427 // An object is returned containing an array of start times for each time 417 // An object is returned containing an array of start times for each time
428 // interval, and an array giving the start and end values for the interval. 418 // interval, and an array giving the start and end values for the interval.
429 function doAutomation( 419 function doAutomation(
430 numberOfTests, initialValue, setValueFunction, automationFunction) { 420 numberOfTests, initialValue, setValueFunction, automationFunction) {
431 var timeInfo = [0]; 421 let timeInfo = [0];
432 var valueInfo = []; 422 let valueInfo = [];
433 var value = initialValue; 423 let value = initialValue;
434 424
435 for (var k = 0; k < numberOfTests; ++k) { 425 for (let k = 0; k < numberOfTests; ++k) {
436 var startTime = k * timeInterval; 426 let startTime = k * timeInterval;
437 var endTime = (k + 1) * timeInterval; 427 let endTime = (k + 1) * timeInterval;
438 var endValue = value + endValueDelta(k); 428 let endValue = value + endValueDelta(k);
439 429
440 // Set the value at the start of the time interval. 430 // Set the value at the start of the time interval.
441 setValueFunction(value, startTime); 431 setValueFunction(value, startTime);
442 432
443 // Specify the end or target value, and how we should approach it. 433 // Specify the end or target value, and how we should approach it.
444 automationFunction(endValue, startTime, endTime); 434 automationFunction(endValue, startTime, endTime);
445 435
446 // Keep track of the start times, and the start and end values for each time 436 // Keep track of the start times, and the start and end values for each time
447 // interval. 437 // interval.
448 timeInfo.push(endTime); 438 timeInfo.push(endTime);
(...skipping 27 matching lines...) Expand all
476 // jumpThreshold - optional parameter that specifies the threshold to use for 466 // jumpThreshold - optional parameter that specifies the threshold to use for
477 // detecting discontinuities. If not specified, defaults to 467 // detecting discontinuities. If not specified, defaults to
478 // discontinuityThreshold. 468 // discontinuityThreshold.
479 // 469 //
480 function createAudioGraphAndTest( 470 function createAudioGraphAndTest(
481 task, should, numberOfTests, initialValue, setValueFunction, 471 task, should, numberOfTests, initialValue, setValueFunction,
482 automationFunction, testName, maxError, referenceFunction, jumpThreshold, 472 automationFunction, testName, maxError, referenceFunction, jumpThreshold,
483 errorMetric) { 473 errorMetric) {
484 // Create offline audio context. 474 // Create offline audio context.
485 context = new OfflineAudioContext(2, renderLength(numberOfTests), sampleRate); 475 context = new OfflineAudioContext(2, renderLength(numberOfTests), sampleRate);
486 var constantBuffer = 476 let constantBuffer =
487 createConstantBuffer(context, renderLength(numberOfTests), 1); 477 createConstantBuffer(context, renderLength(numberOfTests), 1);
488 478
489 // We use an AudioGainNode here simply as a convenient way to test the 479 // We use an AudioGainNode here simply as a convenient way to test the
490 // AudioParam automation, since it's easy to pass a constant value through the 480 // AudioParam automation, since it's easy to pass a constant value through the
491 // node, automate the .gain attribute and observe the resulting values. 481 // node, automate the .gain attribute and observe the resulting values.
492 482
493 gainNode = context.createGain(); 483 gainNode = context.createGain();
494 484
495 var bufferSource = context.createBufferSource(); 485 let bufferSource = context.createBufferSource();
496 bufferSource.buffer = constantBuffer; 486 bufferSource.buffer = constantBuffer;
497 bufferSource.connect(gainNode); 487 bufferSource.connect(gainNode);
498 gainNode.connect(context.destination); 488 gainNode.connect(context.destination);
499 489
500 // Set up default values for the parameters that control how the automation 490 // Set up default values for the parameters that control how the automation
501 // test values progress for each time interval. 491 // test values progress for each time interval.
502 startingValueDelta = initialValue / numberOfTests; 492 startingValueDelta = initialValue / numberOfTests;
503 startEndValueChange = startingValueDelta / 2; 493 startEndValueChange = startingValueDelta / 2;
504 discontinuityThreshold = startEndValueChange / 2; 494 discontinuityThreshold = startEndValueChange / 2;
505 495
506 // Run the automation tests. 496 // Run the automation tests.
507 timeValueInfo = doAutomation( 497 timeValueInfo = doAutomation(
508 numberOfTests, initialValue, setValueFunction, automationFunction); 498 numberOfTests, initialValue, setValueFunction, automationFunction);
509 bufferSource.start(0); 499 bufferSource.start(0);
510 500
511 context.oncomplete = checkResultFunction( 501 context.oncomplete = checkResultFunction(
512 task, should, testName, maxError, referenceFunction, jumpThreshold, 502 task, should, testName, maxError, referenceFunction, jumpThreshold,
513 errorMetric || relativeErrorMetric); 503 errorMetric || relativeErrorMetric);
514 context.startRendering(); 504 context.startRendering();
515 } 505 }
506
507
508 // Export local references to global scope. All the new objects in this file
509 // must be exported through this if it is to be used in the actual test HTML
510 // page.
511 let exports = {
512 'sampleRate': 44100,
513 'gainNode': null,
514
515 // Time interval between value changes. It is best if 1 / numberOfTests is no t
516 // close to timeInterval.
517 'timeInterval': .03,
518
519 // Some suitable time constant so that we can see a significant change over a
520 // timeInterval. This is only needed by setTargetAtTime() which needs a time
521 // constant.
522 'timeConstant': .03 / 3,
Raymond Toy 2017/05/16 18:03:45 This was previously computed from timeInterval. I
hongchan 2017/05/16 18:10:07 I can certainly do that, but it does not add much
523
524 'renderLength': renderLength,
525 'createConstantArray': createConstantArray,
526 'getStartEndFrames': getStartEndFrames,
527 'createLinearRampArray': createLinearRampArray,
528 'createExponentialRampArray': createExponentialRampArray,
529 'discreteTimeConstantForSampleRate': discreteTimeConstantForSampleRate,
530 'createExponentialApproachArray': createExponentialApproachArray,
531 'createReferenceSineArray': createReferenceSineArray,
532 'createSineWaveArray': createSineWaveArray,
533 'endValueDelta': endValueDelta,
534 'relativeErrorMetric': relativeErrorMetric,
535 'differenceErrorMetric': differenceErrorMetric,
536 'valueUpdate': valueUpdate,
537 'comparePartialSignals': comparePartialSignals,
538 'verifyDiscontinuities': verifyDiscontinuities,
539 'compareSignals': compareSignals,
540 'checkResultFunction': checkResultFunction,
541 'doAutomation': doAutomation,
542 'createAudioGraphAndTest': createAudioGraphAndTest
543 };
544
545
546 for (let reference in exports) {
547 global[reference] = exports[reference];
548 }
549
550
551 })(window);
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698