Chromium Code Reviews| 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/audio-testing.js"></script> | |
| 7 <script src="resources/audioparam-testing.js"></script> | |
| 8 <title>SetTarget Followed by Linear or Exponential Ramp Is Continuous</title > | |
| 9 </head> | |
| 10 | |
| 11 <body> | |
| 12 <script> | |
| 13 description("Test SetTarget Followed by Linear or Exponential Ramp"); | |
| 14 window.jsTestIsAsync = true; | |
| 15 | |
| 16 var sampleRate = 48000; | |
| 17 var renderQuantum = 128; | |
| 18 // Test doesn't need to run for very long. | |
| 19 var renderDuration = 0.1; | |
| 20 // Where the ramp should end | |
| 21 var rampEndTime = renderDuration - .05; | |
| 22 var renderFrames = renderDuration * sampleRate; | |
| 23 var timeConstant = 0.01; | |
| 24 | |
| 25 var audit = Audit.createTaskRunner(); | |
| 26 | |
| 27 audit.defineTask("linear ramp replace", function (done) { | |
|
hongchan
2016/01/29 18:53:58
Can we have a brief description for each test task
Raymond Toy
2016/04/26 16:38:53
Done.
| |
| 28 runTest("Linear ramp", { | |
| 29 automationFunction: function (audioparam, endValue, endTime) { | |
| 30 audioparam.linearRampToValueAtTime(endValue, endTime); | |
| 31 }, | |
| 32 referenceFunction: linearResult, | |
| 33 automationTime: renderQuantum / sampleRate, | |
| 34 thresholdSetTarget: 0, | |
| 35 thresholdRamp: 1.26765e-6 | |
| 36 }).then(done); | |
| 37 }); | |
| 38 | |
| 39 audit.defineTask("delayed linear ramp", function (done) { | |
|
hongchan
2016/01/29 18:53:58
Ditto.
Raymond Toy
2016/04/26 16:38:53
Done.
| |
| 40 runTest("Delayed linear ramp", { | |
| 41 automationFunction: function (audioparam, endValue, endTime) { | |
| 42 audioparam.linearRampToValueAtTime(endValue, endTime); | |
| 43 }, | |
| 44 referenceFunction: linearResult, | |
| 45 automationTime: 4 * renderQuantum / sampleRate, | |
| 46 thresholdSetTarget: 2.19417e-7, | |
| 47 thresholdRamp: 1.07972e-6 | |
| 48 }).then(done); | |
| 49 }); | |
| 50 | |
| 51 audit.defineTask("expo ramp", function (done) { | |
|
hongchan
2016/01/29 18:53:58
Ditto.
Raymond Toy
2016/04/26 16:38:53
Done.
| |
| 52 runTest("Exponential ramp", { | |
| 53 automationFunction: function (audioparam, endValue, endTime) { | |
| 54 audioparam.exponentialRampToValueAtTime(endValue, endTime); | |
| 55 }, | |
| 56 referenceFunction: exponentialResult, | |
| 57 automationTime: renderQuantum / sampleRate, | |
| 58 thresholdSetTarget: 0, | |
| 59 thresholdRamp: 1.13249e-5 | |
| 60 }).then(done); | |
| 61 }); | |
| 62 | |
| 63 audit.defineTask("delayed expo ramp", function (done) { | |
|
hongchan
2016/01/29 18:53:58
Ditto.
Raymond Toy
2016/04/26 16:38:53
Done.
| |
| 64 runTest("Delayed exponential ramp", { | |
| 65 automationFunction: function (audioparam, endValue, endTime) { | |
| 66 audioparam.exponentialRampToValueAtTime(endValue, endTime); | |
| 67 }, | |
| 68 referenceFunction: exponentialResult, | |
| 69 automationTime: 4 * renderQuantum / sampleRate, | |
| 70 thresholdSetTarget: 2.19417e-7, | |
| 71 thresholdRamp: 4.17233e-6 | |
| 72 }).then(done); | |
| 73 }); | |
| 74 | |
| 75 audit.defineTask("finish", function (done) { | |
| 76 finishJSTest(); | |
| 77 done(); | |
| 78 }); | |
| 79 | |
| 80 audit.runTasks(); | |
| 81 | |
| 82 function computeExpectedResult(automationTime, timeConstant, endValue, end Time, rampFunction) { | |
| 83 // The result is a constant value of 1 for one rendering quantum, then a SetTarget event | |
| 84 // lasting to |automationTime|, at which point a ramp starts which ends at |endValue| | |
| 85 // at |endTime|. Then the rest of curve should be held constant at |end Value|. | |
| 86 var initialPart = new Array(renderQuantum); | |
| 87 initialPart.fill(1); | |
| 88 | |
| 89 // Generate 1 extra frame so that we know where to start the linear ramp . The last sample | |
| 90 // of the array is where the ramp should start from. | |
| 91 var setTargetPart = createExponentialApproachArray(renderQuantum / sampl eRate, | |
| 92 automationTime + 1 / sampleRate, 1, 0, sampleRate, timeConstant); | |
| 93 var setTargetLength = setTargetPart.length; | |
| 94 | |
| 95 // Generate the ramp starting at |automationTime| with a value from last value of the | |
| 96 // SetTarget curve above. | |
| 97 var rampPart = rampFunction(automationTime, endTime, | |
| 98 setTargetPart[setTargetLength - 1], endValue, sampleRate); | |
| 99 | |
| 100 // Finally finish out the rest with a constant value of |endValue|, if n eeded. | |
| 101 var finalPart = new Array(Math.floor((renderDuration - endTime) * sample Rate)); | |
| 102 finalPart.fill(endValue); | |
| 103 | |
| 104 // Return the four parts separately for testing. | |
| 105 return { | |
| 106 initialPart: initialPart, | |
| 107 setTargetPart: setTargetPart.slice(0, setTargetLength - 1), | |
| 108 rampPart: rampPart, | |
| 109 tailPart: finalPart | |
| 110 }; | |
| 111 } | |
| 112 | |
| 113 function linearResult(automationTime, timeConstant, endValue, endTime) { | |
| 114 return computeExpectedResult(automationTime, timeConstant, endValue, end Time, createLinearRampArray); | |
| 115 } | |
| 116 | |
| 117 function exponentialResult(automationTime, timeConstant, endValue, endTime ) { | |
| 118 return computeExpectedResult(automationTime, timeConstant, endValue, end Time, createExponentialRampArray); | |
| 119 } | |
| 120 | |
| 121 // Run test to verify that a SetTarget followed by a ramp produces a conti nuous curve. | |
| 122 function runTest(prefix, options) { | |
| 123 var automationFunction = options.automationFunction; | |
| 124 var referenceFunction = options.referenceFunction; | |
| 125 var automationTime = options.automationTime; | |
| 126 var thresholdSetTarget = options.thresholdSetTarget || 0; | |
| 127 var thresholdRamp = options.thresholdRamp || 0; | |
| 128 | |
| 129 var rampEndValue = 2; | |
| 130 var context = new OfflineAudioContext(1, renderFrames, sampleRate); | |
| 131 | |
| 132 // A constant source of amplitude 1. | |
| 133 var source = context.createBufferSource(); | |
| 134 source.buffer = createConstantBuffer(context, 1, 1); | |
| 135 source.loop = true; | |
| 136 | |
| 137 var gain = context.createGain(); | |
| 138 | |
| 139 // The SetTarget starts after one rendering quantum. | |
| 140 gain.gain.setTargetAtTime(0, renderQuantum / context.sampleRate, timeCon stant); | |
| 141 | |
| 142 // Schedule the ramp at |automationTime|. If this time is past the firs t rendering quantum, | |
| 143 // the SetTarget event will run for a bit before running the ramp. Othe rwise, the SetTarget | |
| 144 // should be completely replaced by the ramp. | |
| 145 context.suspend(automationTime) | |
| 146 .then(function () { | |
| 147 automationFunction(gain.gain, rampEndValue, rampEndTime); | |
| 148 context.resume(); | |
| 149 }); | |
| 150 | |
| 151 source.connect(gain); | |
| 152 gain.connect(context.destination); | |
| 153 | |
| 154 source.start(); | |
| 155 | |
| 156 return context.startRendering().then(function (resultBuffer) { | |
| 157 var success = true; | |
| 158 var result = resultBuffer.getChannelData(0); | |
| 159 var expected = referenceFunction(automationTime, timeConstant, rampEnd Value, rampEndTime); | |
| 160 | |
| 161 // Verify each part of the curve separately. | |
| 162 var startIndex = 0; | |
| 163 var length = expected.initialPart.length; | |
| 164 | |
| 165 // Verify that the initial part of the curve is constant. | |
| 166 success = Should(prefix + ": Initial part", result.slice(0, length)) | |
| 167 .beCloseToArray(expected.initialPart, 0) && success; | |
| 168 | |
| 169 // Verify the SetTarget part of the curve, if the SetTarget did actual ly run. | |
| 170 startIndex += length; | |
| 171 length = expected.setTargetPart.length; | |
| 172 if (length) { | |
| 173 success = Should(prefix + ": SetTarget part", result.slice(startInde x, startIndex + | |
| 174 length)) | |
| 175 .beCloseToArray(expected.setTargetPart, thresholdSetTarget) && suc cess; | |
| 176 } else { | |
| 177 testPassed("SetTarget part was correctly replaced by the ramp"); | |
| 178 } | |
| 179 | |
| 180 // Verify the ramp part of the curve | |
| 181 startIndex += length; | |
| 182 length = expected.rampPart.length; | |
| 183 success = Should(prefix, result.slice(startIndex, startIndex + length) ) | |
| 184 .beCloseToArray(expected.rampPart, thresholdRamp) && success; | |
| 185 | |
| 186 // Verify that the end of the curve after the ramp (if any) is a const ant. | |
| 187 startIndex += length; | |
| 188 success = Should(prefix + ": Tail part", result.slice(startIndex)) | |
| 189 .beCloseToArray(expected.tailPart, 0) && success; | |
| 190 | |
| 191 if (success) | |
| 192 testPassed(prefix + " preceded by SetTarget is continuous.\n"); | |
| 193 else | |
| 194 testFailed(prefix + " preceded by SetTarget was not continuous.\n"); | |
| 195 }); | |
| 196 } | |
| 197 </script> | |
| 198 </body> | |
| 199 </html> | |
| OLD | NEW |