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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/WebKit/LayoutTests/webaudio/resources/audioparam-testing.js
diff --git a/third_party/WebKit/LayoutTests/webaudio/resources/audioparam-testing.js b/third_party/WebKit/LayoutTests/webaudio/resources/audioparam-testing.js
index 530c98d45a85aa3c149b85f321dc987b81766cdc..0dc7d9989be705a2854b1e5053cb8b459fd0f14f 100644
--- a/third_party/WebKit/LayoutTests/webaudio/resources/audioparam-testing.js
+++ b/third_party/WebKit/LayoutTests/webaudio/resources/audioparam-testing.js
@@ -1,11 +1,12 @@
-var sampleRate = 44100;
+(function(global) {
+
// Information about the starting/ending times and starting/ending values for
// each time interval.
-var timeValueInfo;
+let timeValueInfo;
// The difference between starting values between each time interval.
-var startingValueDelta;
+let startingValueDelta;
// For any automation function that has an end or target value, the end value is
// based the starting value of the time interval. The starting value will be
@@ -13,24 +14,13 @@ var startingValueDelta;
// |startingValueDelta| so that the ending value will be distinct from the
// starting value for next time interval. This allows us to detect where the
// ramp begins and ends.
-var startEndValueChange;
+let startEndValueChange;
// Default threshold to use for detecting discontinuities that should appear at
// each time interval.
-var discontinuityThreshold;
-
-// Time interval between value changes. It is best if 1 / numberOfTests is not
-// close to timeInterval.
-var timeInterval = .03;
-
-// Some suitable time constant so that we can see a significant change over a
-// timeInterval. This is only needed by setTargetAtTime() which needs a time
-// constant.
-var timeConstant = timeInterval / 3;
+let discontinuityThreshold;
-var gainNode;
-
-var context;
+let context;
// Make sure we render long enough to capture all of our test data.
function renderLength(numberOfTests) {
@@ -41,11 +31,11 @@ function renderLength(numberOfTests) {
// same as |createConstantBuffer|, but with the parameters to match the other
// create functions. The |endValue| is ignored.
function createConstantArray(startTime, endTime, value, endValue, sampleRate) {
- var startFrame = timeToSampleFrame(startTime, sampleRate);
- var endFrame = timeToSampleFrame(endTime, sampleRate);
- var length = endFrame - startFrame;
+ let startFrame = timeToSampleFrame(startTime, sampleRate);
+ let endFrame = timeToSampleFrame(endTime, sampleRate);
+ let length = endFrame - startFrame;
- var buffer = createConstantBuffer(context, length, value);
+ let buffer = createConstantBuffer(context, length, value);
return buffer.getChannelData(0);
}
@@ -54,8 +44,8 @@ function getStartEndFrames(startTime, endTime, sampleRate) {
// Start frame is the ceiling of the start time because the ramp starts at or
// after the sample frame. End frame is the ceiling because it's the
// exclusive ending frame of the automation.
- var startFrame = Math.ceil(startTime * sampleRate);
- var endFrame = Math.ceil(endTime * sampleRate);
+ let startFrame = Math.ceil(startTime * sampleRate);
+ let endFrame = Math.ceil(endTime * sampleRate);
return {startFrame: startFrame, endFrame: endFrame};
}
@@ -65,25 +55,25 @@ function getStartEndFrames(startTime, endTime, sampleRate) {
// times are only used to compute how many samples to return.)
function createLinearRampArray(
startTime, endTime, startValue, endValue, sampleRate) {
- var frameInfo = getStartEndFrames(startTime, endTime, sampleRate);
- var startFrame = frameInfo.startFrame;
- var endFrame = frameInfo.endFrame;
- var length = endFrame - startFrame;
- var array = new Array(length);
+ let frameInfo = getStartEndFrames(startTime, endTime, sampleRate);
+ let startFrame = frameInfo.startFrame;
+ let endFrame = frameInfo.endFrame;
+ let length = endFrame - startFrame;
+ let array = new Array(length);
- var step =
+ let step =
Math.fround((endValue - startValue) / (endTime - startTime) / sampleRate);
- var start = Math.fround(
+ let start = Math.fround(
startValue +
(endValue - startValue) * (startFrame / sampleRate - startTime) /
(endTime - startTime));
- var slope = (endValue - startValue) / (endTime - startTime);
+ let slope = (endValue - startValue) / (endTime - startTime);
// v(t) = v0 + (v1 - v0)*(t-t0)/(t1-t0)
for (k = 0; k < length; ++k) {
// array[k] = Math.fround(start + k * step);
- var t = (startFrame + k) / sampleRate;
+ let t = (startFrame + k) / sampleRate;
array[k] = startValue + slope * (t - startTime);
}
@@ -95,19 +85,19 @@ function createLinearRampArray(
// end times are only used to compute how many samples to return.)
function createExponentialRampArray(
startTime, endTime, startValue, endValue, sampleRate) {
- var deltaTime = endTime - startTime;
+ let deltaTime = endTime - startTime;
- var frameInfo = getStartEndFrames(startTime, endTime, sampleRate);
- var startFrame = frameInfo.startFrame;
- var endFrame = frameInfo.endFrame;
- var length = endFrame - startFrame;
- var array = new Array(length);
+ let frameInfo = getStartEndFrames(startTime, endTime, sampleRate);
+ let startFrame = frameInfo.startFrame;
+ let endFrame = frameInfo.endFrame;
+ let length = endFrame - startFrame;
+ let array = new Array(length);
- var ratio = endValue / startValue;
+ let ratio = endValue / startValue;
// v(t) = v0*(v1/v0)^((t-t0)/(t1-t0))
- for (var k = 0; k < length; ++k) {
- var t = Math.fround((startFrame + k) / sampleRate);
+ for (let k = 0; k < length; ++k) {
+ let t = Math.fround((startFrame + k) / sampleRate);
array[k] =
Math.fround(startValue * Math.pow(ratio, (t - startTime) / deltaTime));
}
@@ -125,20 +115,20 @@ function discreteTimeConstantForSampleRate(timeConstant, sampleRate) {
// times are only used to compute how many samples to return.)
function createExponentialApproachArray(
startTime, endTime, startValue, targetValue, sampleRate, timeConstant) {
- var startFrameFloat = startTime * sampleRate;
- var frameInfo = getStartEndFrames(startTime, endTime, sampleRate);
- var startFrame = frameInfo.startFrame;
- var endFrame = frameInfo.endFrame;
- var length = Math.floor(endFrame - startFrame);
- var array = new Array(length);
- var c = discreteTimeConstantForSampleRate(timeConstant, sampleRate);
+ let startFrameFloat = startTime * sampleRate;
+ let frameInfo = getStartEndFrames(startTime, endTime, sampleRate);
+ let startFrame = frameInfo.startFrame;
+ let endFrame = frameInfo.endFrame;
+ let length = Math.floor(endFrame - startFrame);
+ let array = new Array(length);
+ let c = discreteTimeConstantForSampleRate(timeConstant, sampleRate);
- var delta = startValue - targetValue;
+ let delta = startValue - targetValue;
// v(t) = v1 + (v0 - v1) * exp(-(t-t0)/tau)
- for (var k = 0; k < length; ++k) {
- var t = (startFrame + k) / sampleRate;
- var value = targetValue + delta * Math.exp(-(t - startTime) / timeConstant);
+ for (let k = 0; k < length; ++k) {
+ let t = (startFrame + k) / sampleRate;
+ let value = targetValue + delta * Math.exp(-(t - startTime) / timeConstant);
array[k] = value;
}
@@ -149,26 +139,26 @@ function createExponentialApproachArray(
function createReferenceSineArray(
startTime, endTime, startValue, endValue, sampleRate) {
// Ignore |startValue| and |endValue| for the sine wave.
- var curve = createSineWaveArray(
+ let curve = createSineWaveArray(
endTime - startTime, freqHz, sineAmplitude, sampleRate);
// Sample the curve appropriately.
- var frameInfo = getStartEndFrames(startTime, endTime, sampleRate);
- var startFrame = frameInfo.startFrame;
- var endFrame = frameInfo.endFrame;
- var length = Math.floor(endFrame - startFrame);
- var array = new Array(length);
+ let frameInfo = getStartEndFrames(startTime, endTime, sampleRate);
+ let startFrame = frameInfo.startFrame;
+ let endFrame = frameInfo.endFrame;
+ let length = Math.floor(endFrame - startFrame);
+ let array = new Array(length);
// v(t) = linearly interpolate between V[k] and V[k + 1] where k =
// floor((N-1)/duration*(t - t0))
- var f = (length - 1) / (endTime - startTime);
+ let f = (length - 1) / (endTime - startTime);
- for (var k = 0; k < length; ++k) {
- var t = (startFrame + k) / sampleRate;
- var indexFloat = f * (t - startTime);
- var index = Math.floor(indexFloat);
+ for (let k = 0; k < length; ++k) {
+ let t = (startFrame + k) / sampleRate;
+ let indexFloat = f * (t - startTime);
+ let index = Math.floor(indexFloat);
if (index + 1 < length) {
- var v0 = curve[index];
- var v1 = curve[index + 1];
+ let v0 = curve[index];
+ let v1 = curve[index + 1];
array[k] = v0 + (v1 - v0) * (indexFloat - index);
} else {
array[k] = curve[length - 1];
@@ -181,12 +171,12 @@ function createReferenceSineArray(
// Create a sine wave of the given frequency and amplitude. The sine wave is
// offset by half the amplitude so that result is always positive.
function createSineWaveArray(durationSeconds, freqHz, amplitude, sampleRate) {
- var length = timeToSampleFrame(durationSeconds, sampleRate);
- var signal = new Float32Array(length);
- var omega = 2 * Math.PI * freqHz / sampleRate;
- var halfAmplitude = amplitude / 2;
+ let length = timeToSampleFrame(durationSeconds, sampleRate);
+ let signal = new Float32Array(length);
+ let omega = 2 * Math.PI * freqHz / sampleRate;
+ let halfAmplitude = amplitude / 2;
- for (var k = 0; k < length; ++k) {
+ for (let k = 0; k < length; ++k) {
signal[k] = halfAmplitude + halfAmplitude * Math.sin(omega * k);
}
@@ -225,16 +215,16 @@ function valueUpdate(timeIntervalIndex) {
function comparePartialSignals(
should, rendered, expectedFunction, startTime, endTime, valueInfo,
sampleRate, errorMetric) {
- var startSample = timeToSampleFrame(startTime, sampleRate);
- var expected = expectedFunction(
+ let startSample = timeToSampleFrame(startTime, sampleRate);
+ let expected = expectedFunction(
startTime, endTime, valueInfo.startValue, valueInfo.endValue, sampleRate,
timeConstant);
- var n = expected.length;
- var maxError = -1;
- var maxErrorIndex = -1;
+ let n = expected.length;
+ let maxError = -1;
+ let maxErrorIndex = -1;
- for (var k = 0; k < n; ++k) {
+ for (let k = 0; k < n; ++k) {
// Make sure we don't pass these tests because a NaN has been generated in
// either the
// rendered data or the reference data.
@@ -256,7 +246,7 @@ function comparePartialSignals(
.beTrue();
break;
}
- var error = Math.abs(errorMetric(rendered[startSample + k], expected[k]));
+ let error = Math.abs(errorMetric(rendered[startSample + k], expected[k]));
if (error > maxError) {
maxError = error;
maxErrorIndex = k;
@@ -271,19 +261,19 @@ function comparePartialSignals(
// discontinuity if the difference between successive samples exceeds the
// threshold.
function verifyDiscontinuities(should, values, times, threshold) {
- var n = values.length;
- var success = true;
- var badLocations = 0;
- var breaks = [];
+ let n = values.length;
+ let success = true;
+ let badLocations = 0;
+ let breaks = [];
// Find discontinuities.
- for (var k = 1; k < n; ++k) {
+ for (let k = 1; k < n; ++k) {
if (Math.abs(values[k] - values[k - 1]) > threshold) {
breaks.push(k);
}
}
- var testCount;
+ let testCount;
// If there are numberOfTests intervals, there are only numberOfTests - 1
// internal interval boundaries. Hence the maximum number of discontinuties we
@@ -304,8 +294,8 @@ function verifyDiscontinuities(should, values, times, threshold) {
// Compare the location of each discontinuity with the end time of each
// interval. (There is no discontinuity at the start of the signal.)
- for (var k = 0; k < testCount; ++k) {
- var expectedSampleFrame = timeToSampleFrame(times[k + 1], sampleRate);
+ for (let k = 0; k < testCount; ++k) {
+ let expectedSampleFrame = timeToSampleFrame(times[k + 1], sampleRate);
if (breaks[k] != expectedSampleFrame) {
success = false;
++badLocations;
@@ -346,17 +336,17 @@ function verifyDiscontinuities(should, values, times, threshold) {
function compareSignals(
should, testName, maxError, renderedData, expectedFunction, timeValueInfo,
breakThreshold, errorMetric) {
- var success = true;
- var failedTestCount = 0;
- var times = timeValueInfo.times;
- var values = timeValueInfo.values;
- var n = values.length;
- var expectedSignal = [];
+ let success = true;
+ let failedTestCount = 0;
+ let times = timeValueInfo.times;
+ let values = timeValueInfo.values;
+ let n = values.length;
+ let expectedSignal = [];
success = verifyDiscontinuities(should, renderedData, times, breakThreshold);
- for (var k = 0; k < n; ++k) {
- var result = comparePartialSignals(
+ for (let k = 0; k < n; ++k) {
+ let result = comparePartialSignals(
should, renderedData, expectedFunction, times[k], times[k + 1],
values[k], sampleRate, errorMetric);
@@ -394,10 +384,10 @@ function checkResultFunction(
task, should, testName, error, referenceFunction, jumpThreshold,
errorMetric) {
return function(event) {
- var buffer = event.renderedBuffer;
+ let buffer = event.renderedBuffer;
renderedData = buffer.getChannelData(0);
- var threshold;
+ let threshold;
if (!jumpThreshold) {
threshold = discontinuityThreshold;
@@ -428,14 +418,14 @@ function checkResultFunction(
// interval, and an array giving the start and end values for the interval.
function doAutomation(
numberOfTests, initialValue, setValueFunction, automationFunction) {
- var timeInfo = [0];
- var valueInfo = [];
- var value = initialValue;
+ let timeInfo = [0];
+ let valueInfo = [];
+ let value = initialValue;
- for (var k = 0; k < numberOfTests; ++k) {
- var startTime = k * timeInterval;
- var endTime = (k + 1) * timeInterval;
- var endValue = value + endValueDelta(k);
+ for (let k = 0; k < numberOfTests; ++k) {
+ let startTime = k * timeInterval;
+ let endTime = (k + 1) * timeInterval;
+ let endValue = value + endValueDelta(k);
// Set the value at the start of the time interval.
setValueFunction(value, startTime);
@@ -483,7 +473,7 @@ function createAudioGraphAndTest(
errorMetric) {
// Create offline audio context.
context = new OfflineAudioContext(2, renderLength(numberOfTests), sampleRate);
- var constantBuffer =
+ let constantBuffer =
createConstantBuffer(context, renderLength(numberOfTests), 1);
// We use an AudioGainNode here simply as a convenient way to test the
@@ -492,7 +482,7 @@ function createAudioGraphAndTest(
gainNode = context.createGain();
- var bufferSource = context.createBufferSource();
+ let bufferSource = context.createBufferSource();
bufferSource.buffer = constantBuffer;
bufferSource.connect(gainNode);
gainNode.connect(context.destination);
@@ -513,3 +503,49 @@ function createAudioGraphAndTest(
errorMetric || relativeErrorMetric);
context.startRendering();
}
+
+
+// Export local references to global scope. All the new objects in this file
+// must be exported through this if it is to be used in the actual test HTML
+// page.
+let exports = {
+ 'sampleRate': 44100,
+ 'gainNode': null,
+
+ // Time interval between value changes. It is best if 1 / numberOfTests is not
+ // close to timeInterval.
+ 'timeInterval': .03,
+
+ // Some suitable time constant so that we can see a significant change over a
+ // timeInterval. This is only needed by setTargetAtTime() which needs a time
+ // constant.
+ '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
+
+ 'renderLength': renderLength,
+ 'createConstantArray': createConstantArray,
+ 'getStartEndFrames': getStartEndFrames,
+ 'createLinearRampArray': createLinearRampArray,
+ 'createExponentialRampArray': createExponentialRampArray,
+ 'discreteTimeConstantForSampleRate': discreteTimeConstantForSampleRate,
+ 'createExponentialApproachArray': createExponentialApproachArray,
+ 'createReferenceSineArray': createReferenceSineArray,
+ 'createSineWaveArray': createSineWaveArray,
+ 'endValueDelta': endValueDelta,
+ 'relativeErrorMetric': relativeErrorMetric,
+ 'differenceErrorMetric': differenceErrorMetric,
+ 'valueUpdate': valueUpdate,
+ 'comparePartialSignals': comparePartialSignals,
+ 'verifyDiscontinuities': verifyDiscontinuities,
+ 'compareSignals': compareSignals,
+ 'checkResultFunction': checkResultFunction,
+ 'doAutomation': doAutomation,
+ 'createAudioGraphAndTest': createAudioGraphAndTest
+};
+
+
+for (let reference in exports) {
+ global[reference] = exports[reference];
+}
+
+
+})(window);
« 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