| OLD | NEW |
| (Empty) |
| 1 <!doctype html> | |
| 2 <html> | |
| 3 <head> | |
| 4 <title>Test Clamping of AudioParam Time</title> | |
| 5 <script src="../resources/testharness.js"></script> | |
| 6 <script src="../resources/testharnessreport.js"></script> | |
| 7 <script src="resources/audit-util.js"></script> | |
| 8 <script src="resources/audio-testing.js"></script> | |
| 9 </head> | |
| 10 | |
| 11 <body> | |
| 12 <script> | |
| 13 // Fairly arbitrary sample rate and render frames. | |
| 14 var sampleRate = 24000; | |
| 15 var renderFrames = 1024; | |
| 16 | |
| 17 var audit = Audit.createTaskRunner(); | |
| 18 | |
| 19 audit.defineTask("setValue", function (taskDone) { | |
| 20 var suspendFrame = 128; | |
| 21 createGraph({ | |
| 22 suspendFrame: suspendFrame, | |
| 23 method: "setValueAtTime", | |
| 24 initialGain: 0, | |
| 25 arg0: 1, | |
| 26 }) | |
| 27 .then(function (resultBuffer) { | |
| 28 // Just verify that the cosine wave actually started at | |
| 29 // suspendFrame. | |
| 30 var result = resultBuffer.getChannelData(0); | |
| 31 var success = true; | |
| 32 | |
| 33 success = Should("Output[0-" + (suspendFrame - 1) + "]", | |
| 34 result.slice(0, suspendFrame)) | |
| 35 .beConstantValueOf(0) && success; | |
| 36 success = Should("Output[" + suspendFrame + "-" + ( | |
| 37 renderFrames - 1) + "]", | |
| 38 result.slice(suspendFrame)) | |
| 39 .beConstantValueOf(1) && success; | |
| 40 | |
| 41 Should("*** setValueAtTime in the past", success) | |
| 42 .summarize( | |
| 43 "correctly clamped to current time", | |
| 44 "was not correctly clamped to current time"); | |
| 45 }) | |
| 46 .then(taskDone); | |
| 47 }); | |
| 48 | |
| 49 audit.defineTask("linear", function (taskDone) { | |
| 50 var suspendFrame = 128; | |
| 51 createGraph({ | |
| 52 suspendFrame: suspendFrame, | |
| 53 method: "linearRampToValueAtTime", | |
| 54 initialGain: 1, | |
| 55 arg0: 0.5 | |
| 56 }) | |
| 57 .then(function (resultBuffer) { | |
| 58 // Just verify that the cosine wave actually started at | |
| 59 // suspendFrame. | |
| 60 var result = resultBuffer.getChannelData(0); | |
| 61 var success = true; | |
| 62 | |
| 63 success = Should("Output[0-" + (suspendFrame - 1) + "]", | |
| 64 result.slice(0, suspendFrame)) | |
| 65 .beConstantValueOf(1) && success; | |
| 66 success = Should("Output[" + suspendFrame + "-" + ( | |
| 67 renderFrames - 1) + "]", | |
| 68 result.slice(suspendFrame)) | |
| 69 .beConstantValueOf(0.5) && success; | |
| 70 | |
| 71 Should("*** linearRampToValueAtTime in the past", success) | |
| 72 .summarize( | |
| 73 "correctly clamped to current time", | |
| 74 "was not correctly clamped to current time"); | |
| 75 }) | |
| 76 .then(taskDone); | |
| 77 }); | |
| 78 | |
| 79 audit.defineTask("exponential", function (taskDone) { | |
| 80 var suspendFrame = 128; | |
| 81 createGraph({ | |
| 82 suspendFrame: suspendFrame, | |
| 83 method: "exponentialRampToValueAtTime", | |
| 84 initialGain: 1, | |
| 85 arg0: 0.5 | |
| 86 }) | |
| 87 .then(function (resultBuffer) { | |
| 88 // Just verify that the cosine wave actually started at | |
| 89 // suspendFrame. | |
| 90 var result = resultBuffer.getChannelData(0); | |
| 91 var success = true; | |
| 92 | |
| 93 success = Should("Output[0-" + (suspendFrame - 1) + "]", | |
| 94 result.slice(0, suspendFrame)) | |
| 95 .beConstantValueOf(1) && success; | |
| 96 success = Should("Output[" + suspendFrame + "-" + ( | |
| 97 renderFrames - 1) + "]", | |
| 98 result.slice(suspendFrame)) | |
| 99 .beConstantValueOf(0.5) && success; | |
| 100 | |
| 101 Should("*** exponentialRampToValueAtTime in the past", | |
| 102 success) | |
| 103 .summarize( | |
| 104 "correctly clamped to current time", | |
| 105 "was not correctly clamped to current time"); | |
| 106 }) | |
| 107 .then(taskDone); | |
| 108 }); | |
| 109 | |
| 110 audit.defineTask("setTarget", function (taskDone) { | |
| 111 var suspendFrame = 128; | |
| 112 createGraph({ | |
| 113 suspendFrame: suspendFrame, | |
| 114 method: "setTargetAtTime", | |
| 115 initialGain: 1, | |
| 116 arg0: 0.5, | |
| 117 moreArgs: 0.1 | |
| 118 }) | |
| 119 .then(function (resultBuffer) { | |
| 120 // Just verify that the cosine wave actually started at | |
| 121 // suspendFrame. | |
| 122 var result = resultBuffer.getChannelData(0); | |
| 123 var success = true; | |
| 124 | |
| 125 success = Should("Output[0-" + (suspendFrame - 1) + "]", | |
| 126 result.slice(0, suspendFrame)) | |
| 127 .beConstantValueOf(1) && success; | |
| 128 // For the samples past the suspend time, we only care that first | |
| 129 // value is 1 and that the rest are not zero. | |
| 130 success = Should("Output[" + suspendFrame + "]", | |
| 131 result[suspendFrame]).beEqualTo(1) && success; | |
| 132 | |
| 133 var positive = result.slice(suspendFrame + 1).every(x => x > | |
| 134 0); | |
| 135 success = Should("Output[" + (suspendFrame + 1) + "-" + | |
| 136 (renderFrames - 1) + "] contains only positive values", | |
| 137 positive) | |
| 138 .beEqualTo(true) && success; | |
| 139 | |
| 140 Should("*** setTargetAtTime in the past", success) | |
| 141 .summarize( | |
| 142 "correctly clamped to current time", | |
| 143 "was not correctly clamped to current time"); | |
| 144 }) | |
| 145 .then(taskDone); | |
| 146 }); | |
| 147 | |
| 148 audit.defineTask("setValueCurve", function (taskDone) { | |
| 149 var suspendFrame = 128; | |
| 150 createGraph({ | |
| 151 suspendFrame: suspendFrame, | |
| 152 method: "setValueCurveAtTime", | |
| 153 initialGain: 1, | |
| 154 arg0: Float32Array.from([2, 3]), | |
| 155 moreArgs: 0.1 | |
| 156 }) | |
| 157 .then(function (resultBuffer) { | |
| 158 // Just verify that the cosine wave actually started at | |
| 159 // suspendFrame. | |
| 160 var result = resultBuffer.getChannelData(0); | |
| 161 var success = true; | |
| 162 | |
| 163 success = Should("Output[0-" + (suspendFrame - 1) + "]", | |
| 164 result.slice(0, suspendFrame)) | |
| 165 .beConstantValueOf(1) && success; | |
| 166 | |
| 167 // The selected curve contains values greater than or equal to 2. | |
| 168 // Just verify that all values are greater than or equal to 2. | |
| 169 var biggerThan2 = result.slice(suspendFrame).every(x => x >= | |
| 170 2); | |
| 171 success = Should("Output[" + suspendFrame + "-" + ( | |
| 172 renderFrames - 1) + "]", | |
| 173 biggerThan2) | |
| 174 .beEqualTo(true) && success; | |
| 175 | |
| 176 Should("*** setValueCurveAtTime in the past", success) | |
| 177 .summarize( | |
| 178 "correctly clamped to current time", | |
| 179 "was not correctly clamped to current time"); | |
| 180 }) | |
| 181 .then(taskDone); | |
| 182 }); | |
| 183 | |
| 184 | |
| 185 // Create the test graph consisting of a constant source followed by a | |
| 186 // gain node. The gain node automations will be tested. |options| | |
| 187 // specifies the parameters for the test including | |
| 188 // | |
| 189 // suspendFrame: time at which we schedule the automation call | |
| 190 // method: the name of automation method to be tested | |
| 191 // initialGain: the initial gain at time 0 for the gain node | |
| 192 // arg0: the first argument to be supplied to the automation method. | |
| 193 // moreArgs: An array of any additional arguments for the automation met
hod. | |
| 194 function createGraph(options) { | |
| 195 var context = new OfflineAudioContext(1, renderFrames, sampleRate); | |
| 196 | |
| 197 var cosineWave = new PeriodicWave(context, { | |
| 198 real: [0, 1] | |
| 199 }); | |
| 200 | |
| 201 var src = new OscillatorNode(context, { | |
| 202 periodicWave: cosineWave, | |
| 203 frequency: 0 | |
| 204 }); | |
| 205 | |
| 206 // The gain node whose automations we're testing. | |
| 207 var gain = new GainNode(context, { | |
| 208 gain: 0 | |
| 209 }); | |
| 210 | |
| 211 src.connect(gain).connect(context.destination); | |
| 212 | |
| 213 gain.gain.setValueAtTime(options.initialGain, 0); | |
| 214 | |
| 215 // Suspend rendering so that we can call the automation method at the | |
| 216 // right time. |startTime| is the time for the automation method. It | |
| 217 // must be some time before the suspend time. | |
| 218 var suspendFrame = options.suspendFrame; | |
| 219 var startTime = (suspendFrame / 2) / context.sampleRate; | |
| 220 context.suspend(suspendFrame / context.sampleRate) | |
| 221 .then(function () { | |
| 222 // Call the appropriate automation method with the desired | |
| 223 // automation value, time, and any other arguments needed. | |
| 224 gain.gain[options.method](...[options.arg0, startTime, options.moreA
rgs]); | |
| 225 }) | |
| 226 .then(context.resume.bind(context)); | |
| 227 | |
| 228 // Start the source and begin rendering, returning the promise from | |
| 229 // rendering. | |
| 230 src.start(); | |
| 231 | |
| 232 return context.startRendering(); | |
| 233 } | |
| 234 | |
| 235 audit.runTasks(); | |
| 236 </script> | |
| 237 </body> | |
| 238 </html> | |
| OLD | NEW |