| 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>SetTarget Followed by Linear or Exponential Ramp Is Continuous</title
> | |
| 10 </head> | |
| 11 | |
| 12 <body> | |
| 13 <script> | |
| 14 description("Test SetTarget Followed by Linear or Exponential Ramp"); | |
| 15 window.jsTestIsAsync = true; | |
| 16 | |
| 17 var sampleRate = 48000; | |
| 18 var renderQuantum = 128; | |
| 19 // Test doesn't need to run for very long. | |
| 20 var renderDuration = 0.1; | |
| 21 // Where the ramp should end | |
| 22 var rampEndTime = renderDuration - .05; | |
| 23 var renderFrames = renderDuration * sampleRate; | |
| 24 var timeConstant = 0.01; | |
| 25 | |
| 26 var audit = Audit.createTaskRunner(); | |
| 27 | |
| 28 // All of the tests start a SetTargetAtTime after one rendering quantum.
The following tests | |
| 29 // handle various cases where a linear or exponential ramp is scheduled at
or after | |
| 30 // SetTargetAtTime starts. | |
| 31 | |
| 32 audit.defineTask("linear ramp replace", function (done) { | |
| 33 // Schedule a linear ramp to start at the same time as SetTargetAtTime.
This effectively | |
| 34 // replaces the SetTargetAtTime as if it never existed. | |
| 35 runTest("Linear ramp", { | |
| 36 automationFunction: function (audioparam, endValue, endTime) { | |
| 37 audioparam.linearRampToValueAtTime(endValue, endTime); | |
| 38 }, | |
| 39 referenceFunction: linearResult, | |
| 40 automationTime: renderQuantum / sampleRate, | |
| 41 thresholdSetTarget: 0, | |
| 42 thresholdRamp: 1.26765e-6 | |
| 43 }).then(done); | |
| 44 }); | |
| 45 | |
| 46 audit.defineTask("delayed linear ramp", function (done) { | |
| 47 // Schedule a linear ramp to start after the SetTargetAtTime has already
started rendering. | |
| 48 // This is the main test to verify that the linear ramp is continuous wi
th the | |
| 49 // SetTargetAtTime curve. | |
| 50 runTest("Delayed linear ramp", { | |
| 51 automationFunction: function (audioparam, endValue, endTime) { | |
| 52 audioparam.linearRampToValueAtTime(endValue, endTime); | |
| 53 }, | |
| 54 referenceFunction: linearResult, | |
| 55 automationTime: 4 * renderQuantum / sampleRate, | |
| 56 thresholdSetTarget: 3.43632e-7, | |
| 57 thresholdRamp: 1.07972e-6 | |
| 58 }).then(done); | |
| 59 }); | |
| 60 | |
| 61 audit.defineTask("expo ramp replace", function (done) { | |
| 62 // Like "linear ramp replace", but with an exponential ramp instead. | |
| 63 runTest("Exponential ramp", { | |
| 64 automationFunction: function (audioparam, endValue, endTime) { | |
| 65 audioparam.exponentialRampToValueAtTime(endValue, endTime); | |
| 66 }, | |
| 67 referenceFunction: exponentialResult, | |
| 68 automationTime: renderQuantum / sampleRate, | |
| 69 thresholdSetTarget: 0, | |
| 70 thresholdRamp: 1.14441e-5 | |
| 71 }).then(done); | |
| 72 }); | |
| 73 | |
| 74 audit.defineTask("delayed expo ramp", function (done) { | |
| 75 // Like "delayed linear ramp", but with an exponential ramp instead. | |
| 76 runTest("Delayed exponential ramp", { | |
| 77 automationFunction: function (audioparam, endValue, endTime) { | |
| 78 audioparam.exponentialRampToValueAtTime(endValue, endTime); | |
| 79 }, | |
| 80 referenceFunction: exponentialResult, | |
| 81 automationTime: 4 * renderQuantum / sampleRate, | |
| 82 thresholdSetTarget: 3.43632e-7, | |
| 83 thresholdRamp: 4.29154e-6 | |
| 84 }).then(done); | |
| 85 }); | |
| 86 | |
| 87 audit.defineTask("finish", function (done) { | |
| 88 finishJSTest(); | |
| 89 done(); | |
| 90 }); | |
| 91 | |
| 92 audit.runTasks(); | |
| 93 | |
| 94 function computeExpectedResult(automationTime, timeConstant, endValue, end
Time, rampFunction) { | |
| 95 // The result is a constant value of 1 for one rendering quantum, then a
SetTarget event | |
| 96 // lasting to |automationTime|, at which point a ramp starts which ends
at |endValue| | |
| 97 // at |endTime|. Then the rest of curve should be held constant at |end
Value|. | |
| 98 var initialPart = new Array(renderQuantum); | |
| 99 initialPart.fill(1); | |
| 100 | |
| 101 // Generate 1 extra frame so that we know where to start the linear ramp
. The last sample | |
| 102 // of the array is where the ramp should start from. | |
| 103 var setTargetPart = createExponentialApproachArray(renderQuantum / sampl
eRate, | |
| 104 automationTime + 1 / sampleRate, 1, 0, sampleRate, timeConstant); | |
| 105 var setTargetLength = setTargetPart.length; | |
| 106 | |
| 107 // Generate the ramp starting at |automationTime| with a value from last
value of the | |
| 108 // SetTarget curve above. | |
| 109 var rampPart = rampFunction(automationTime, endTime, | |
| 110 setTargetPart[setTargetLength - 1], endValue, sampleRate); | |
| 111 | |
| 112 // Finally finish out the rest with a constant value of |endValue|, if n
eeded. | |
| 113 var finalPart = new Array(Math.floor((renderDuration - endTime) * sample
Rate)); | |
| 114 finalPart.fill(endValue); | |
| 115 | |
| 116 // Return the four parts separately for testing. | |
| 117 return { | |
| 118 initialPart: initialPart, | |
| 119 setTargetPart: setTargetPart.slice(0, setTargetLength - 1), | |
| 120 rampPart: rampPart, | |
| 121 tailPart: finalPart | |
| 122 }; | |
| 123 } | |
| 124 | |
| 125 function linearResult(automationTime, timeConstant, endValue, endTime) { | |
| 126 return computeExpectedResult(automationTime, timeConstant, endValue, end
Time, createLinearRampArray); | |
| 127 } | |
| 128 | |
| 129 function exponentialResult(automationTime, timeConstant, endValue, endTime
) { | |
| 130 return computeExpectedResult(automationTime, timeConstant, endValue, end
Time, createExponentialRampArray); | |
| 131 } | |
| 132 | |
| 133 // Run test to verify that a SetTarget followed by a ramp produces a conti
nuous curve. | |
| 134 // |prefix| is a string to use as a prefix for the messages. |options| is
a dictionary | |
| 135 // describing how the test is run: | |
| 136 // | |
| 137 // |options.automationFunction| | |
| 138 // The function to use to start the automation, which should be a line
ar or exponential | |
| 139 // ramp automation. This function has three arguments: | |
| 140 // audioparam - the AudioParam to be automated | |
| 141 // endValue - the end value of the ramp | |
| 142 // endTime - the end time fo the ramp. | |
| 143 // |options.referenceFunction| | |
| 144 // The function to generated the expected result. This function has f
our arguments: | |
| 145 // automationTime - the value of |options.automationTime| | |
| 146 // timeConstant - time constant used for SetTargetAtTime | |
| 147 // rampEndValue - end value for the ramp (same value used for auto
mationFunction) | |
| 148 // rampEndTime - end time for the ramp (same value used for autom
ationFunction) | |
| 149 // |options.automationTime| | |
| 150 // Time at which the |automationFunction| is called to start the autom
ation. | |
| 151 // |options.thresholdSetTarget| | |
| 152 // Threshold to use for verifying that the initial (if any) SetTargetA
tTime portion had | |
| 153 // the correct values. | |
| 154 // |options.thresholdRamp| | |
| 155 // Threshold to use for verifying that the ramp portion had the correc
t values. | |
| 156 function runTest(prefix, options) { | |
| 157 var automationFunction = options.automationFunction; | |
| 158 var referenceFunction = options.referenceFunction; | |
| 159 var automationTime = options.automationTime; | |
| 160 var thresholdSetTarget = options.thresholdSetTarget || 0; | |
| 161 var thresholdRamp = options.thresholdRamp || 0; | |
| 162 | |
| 163 // End value for the ramp. Fairly arbitrary, but should be distinctly d
ifferent from the | |
| 164 // target value for SetTargetAtTime and the initial value of gain.gain. | |
| 165 var rampEndValue = 2; | |
| 166 var context = new OfflineAudioContext(1, renderFrames, sampleRate); | |
| 167 | |
| 168 // A constant source of amplitude 1. | |
| 169 var source = context.createBufferSource(); | |
| 170 source.buffer = createConstantBuffer(context, 1, 1); | |
| 171 source.loop = true; | |
| 172 | |
| 173 var gain = context.createGain(); | |
| 174 | |
| 175 // The SetTarget starts after one rendering quantum. | |
| 176 gain.gain.setTargetAtTime(0, renderQuantum / context.sampleRate, timeCon
stant); | |
| 177 | |
| 178 // Schedule the ramp at |automationTime|. If this time is past the firs
t rendering quantum, | |
| 179 // the SetTarget event will run for a bit before running the ramp. Othe
rwise, the SetTarget | |
| 180 // should be completely replaced by the ramp. | |
| 181 context.suspend(automationTime) | |
| 182 .then(function () { | |
| 183 automationFunction(gain.gain, rampEndValue, rampEndTime); | |
| 184 context.resume(); | |
| 185 }); | |
| 186 | |
| 187 source.connect(gain); | |
| 188 gain.connect(context.destination); | |
| 189 | |
| 190 source.start(); | |
| 191 | |
| 192 return context.startRendering().then(function (resultBuffer) { | |
| 193 var success = true; | |
| 194 var result = resultBuffer.getChannelData(0); | |
| 195 var expected = referenceFunction(automationTime, timeConstant, rampEnd
Value, rampEndTime); | |
| 196 | |
| 197 // Verify each part of the curve separately. | |
| 198 var startIndex = 0; | |
| 199 var length = expected.initialPart.length; | |
| 200 | |
| 201 // Verify that the initial part of the curve is constant. | |
| 202 success = Should(prefix + ": Initial part", result.slice(0, length)) | |
| 203 .beCloseToArray(expected.initialPart, 0) && success; | |
| 204 | |
| 205 // Verify the SetTarget part of the curve, if the SetTarget did actual
ly run. | |
| 206 startIndex += length; | |
| 207 length = expected.setTargetPart.length; | |
| 208 if (length) { | |
| 209 success = Should(prefix + ": SetTarget part", result.slice(startInde
x, startIndex + | |
| 210 length)) | |
| 211 .beCloseToArray(expected.setTargetPart, thresholdSetTarget) && suc
cess; | |
| 212 } else { | |
| 213 testPassed("SetTarget part was correctly replaced by the ramp"); | |
| 214 } | |
| 215 | |
| 216 // Verify the ramp part of the curve | |
| 217 startIndex += length; | |
| 218 length = expected.rampPart.length; | |
| 219 success = Should(prefix, result.slice(startIndex, startIndex + length)
) | |
| 220 .beCloseToArray(expected.rampPart, thresholdRamp) && success; | |
| 221 | |
| 222 // Verify that the end of the curve after the ramp (if any) is a const
ant. | |
| 223 startIndex += length; | |
| 224 success = Should(prefix + ": Tail part", result.slice(startIndex)) | |
| 225 .beCloseToArray(expected.tailPart, 0) && success; | |
| 226 | |
| 227 if (success) | |
| 228 testPassed(prefix + " preceded by SetTarget is continuous.\n"); | |
| 229 else | |
| 230 testFailed(prefix + " preceded by SetTarget was not continuous.\n"); | |
| 231 }); | |
| 232 } | |
| 233 </script> | |
| 234 </body> | |
| 235 </html> | |
| OLD | NEW |