OLD | NEW |
(Empty) | |
| 1 <!doctype html> |
| 2 <html> |
| 3 <head> |
| 4 <script src="resources/compatibility.js"></script> |
| 5 <script src="resources/audio-testing.js"></script> |
| 6 <script src="resources/audioparam-testing.js"></script> |
| 7 <script src="../resources/js-test.js"></script> |
| 8 <title>Test Sampling of LinearRampToValueAtTime</title> |
| 9 </head> |
| 10 |
| 11 <body> |
| 12 <script> |
| 13 description("Test Sampling of LinearRampToValueAtTime"); |
| 14 |
| 15 window.jsTestIsAsync = true; |
| 16 var sampleRate = 12800; |
| 17 var context; |
| 18 |
| 19 var audit = Audit.createTaskRunner(); |
| 20 |
| 21 function runTest(config) { |
| 22 // Create a short context with a constant signal source connected to a g
ain node that will |
| 23 // be automated. |
| 24 context = new OfflineAudioContext(1, 256, sampleRate); |
| 25 |
| 26 // Create a constant unit amplitude source. |
| 27 var source = context.createBufferSource(); |
| 28 var b = createConstantBuffer(context, 1, 1); |
| 29 source.buffer = b; |
| 30 source.loop = true; |
| 31 |
| 32 // Gain node that is to be automated. |
| 33 var gain = context.createGain(); |
| 34 gain.gain.value = 0; |
| 35 gain.gain.setValueAtTime(config.startValue, config.startTime); |
| 36 config.automationFunction(gain); |
| 37 |
| 38 source.connect(gain); |
| 39 gain.connect(context.destination); |
| 40 |
| 41 source.start(); |
| 42 |
| 43 return context.startRendering().then(function (resultBuffer) { |
| 44 // Check that the automation has the correct sampling. |
| 45 var resultData = resultBuffer.getChannelData(0); |
| 46 |
| 47 // The automation has starts at config.startTime, so the frame just af
ter this should have |
| 48 // the automation applied. |
| 49 var startFrame = Math.ceil(config.startTime * sampleRate); |
| 50 |
| 51 // The automation ends at config.endTime so the frame just before this
should have the |
| 52 // automation applied. |
| 53 var endFrame = Math.floor(config.endTime * sampleRate); |
| 54 |
| 55 // Use the true automation to find the expected values. |
| 56 var expectedStart = config.expectedFunction(startFrame / sampleRate); |
| 57 var expectedEnd = config.expectedFunction(endFrame / sampleRate); |
| 58 |
| 59 var success = Should(config.desc + ": Sample " + startFrame, |
| 60 resultData[startFrame]).beCloseTo(expectedStart, config.startValueTh
reshold, 7); |
| 61 success = Should(config.desc + ": Sample " + endFrame, |
| 62 resultData[endFrame]).beCloseTo(expectedEnd, config.endValueThreshol
d, 7) && success; |
| 63 |
| 64 if (success) |
| 65 testPassed(config.desc + " passed.\n"); |
| 66 else |
| 67 testFailed(config.desc + " failed.\n"); |
| 68 }); |
| 69 } |
| 70 |
| 71 function expectedLinear(t) { |
| 72 var slope = (this.endValue - this.startValue) / (this.endTime - this.sta
rtTime); |
| 73 return this.startValue + slope * (t - this.startTime); |
| 74 }; |
| 75 |
| 76 function expectedExponential(t) { |
| 77 var ratio = this.endValue / this.startValue; |
| 78 var exponent = (t - this.startTime) / (this.endTime - this.startTime); |
| 79 return this.startValue * Math.pow(ratio, exponent); |
| 80 }; |
| 81 |
| 82 function linearAutomation(g) { |
| 83 g.gain.linearRampToValueAtTime(this.endValue, this.endTime); |
| 84 } |
| 85 |
| 86 function exponentialAutomation(g) { |
| 87 g.gain.exponentialRampToValueAtTime(this.endValue, this.endTime); |
| 88 } |
| 89 |
| 90 // Basically want to test that if neither the start time nor end time is o
n a frame boundary |
| 91 // that we sample the automation curve correctly. The start times and end
times are mostly |
| 92 // arbitrary, except that they cannot be on a frame boundary. |
| 93 var testConfigs = [{ |
| 94 desc: "linearRamp", |
| 95 startTime: .1 / sampleRate, |
| 96 endTime: 128.1 / sampleRate, |
| 97 startValue: 1, |
| 98 endValue: 0, |
| 99 startValueThreshold: 1.201e-8, |
| 100 endValueThreshold: 1.526e-5, |
| 101 automationFunction: linearAutomation, |
| 102 expectedFunction: expectedLinear |
| 103 }, { |
| 104 desc: "linearRamp:short", |
| 105 startTime: .1 / sampleRate, |
| 106 endTime: 5.1 / sampleRate, |
| 107 startValue: 1, |
| 108 endValue: 0, |
| 109 startValueThreshold: 8.723e-9, |
| 110 endValueThreshold: 9.537e-7, |
| 111 automationFunction: linearAutomation, |
| 112 expectedFunction: expectedLinear |
| 113 }, { |
| 114 desc: "linearRamp:long", |
| 115 startTime: .1 / sampleRate, |
| 116 endTime: 200.1 / sampleRate, |
| 117 startValue: 1, |
| 118 endValue: 0, |
| 119 startValueThreshold: 2.827e-8, |
| 120 endValueThreshold: 4.674e-5, |
| 121 automationFunction: linearAutomation, |
| 122 expectedFunction: expectedLinear |
| 123 }, { |
| 124 desc: "exponentialRamp", |
| 125 startTime: .1 / sampleRate, |
| 126 endTime: 128.1 / sampleRate, |
| 127 startValue: 1, |
| 128 endValue: 1e-5, |
| 129 startValueThreshold: 2.505e-8, |
| 130 endValueThreshold: 1.484e-7, |
| 131 automationFunction: exponentialAutomation, |
| 132 expectedFunction: expectedExponential |
| 133 }, { |
| 134 desc: "exponentialRamp:short", |
| 135 startTime: .1 / sampleRate, |
| 136 endTime: 5.1 / sampleRate, |
| 137 startValue: 1, |
| 138 endValue: 1e-5, |
| 139 startValueThreshold: 5.027e-8, |
| 140 endValueThreshold: 3.821e-7, |
| 141 automationFunction: exponentialAutomation, |
| 142 expectedFunction: expectedExponential |
| 143 }, { |
| 144 desc: "exponentialRamp:long", |
| 145 startTime: .1 / sampleRate, |
| 146 endTime: 200.1 / sampleRate, |
| 147 startValue: 1, |
| 148 endValue: 1e-5, |
| 149 startValueThreshold: 8.035e-9, |
| 150 endValueThreshold: 1.337e-6, |
| 151 automationFunction: exponentialAutomation, |
| 152 expectedFunction: expectedExponential |
| 153 }, |
| 154 ]; |
| 155 |
| 156 function createTaskFunction(config) { |
| 157 return function (done) { |
| 158 runTest(config).then(done); |
| 159 }; |
| 160 } |
| 161 |
| 162 // Create all of the tasks from the test configs |
| 163 for (var k = 0; k < testConfigs.length; ++k) { |
| 164 var config = testConfigs[k]; |
| 165 var taskName = config.desc + ":task" + k; |
| 166 audit.defineTask(taskName, createTaskFunction(config)); |
| 167 } |
| 168 |
| 169 audit.defineTask("finish", function (done) { |
| 170 finishJSTest(); |
| 171 done(); |
| 172 }); |
| 173 |
| 174 audit.runTasks(); |
| 175 successfullyParsed = true; |
| 176 </script> |
| 177 </body> |
| 178 </html> |
OLD | NEW |