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 for SetTargetAtTime</title> |
| 9 </head> |
| 10 |
| 11 <body> |
| 12 <script> |
| 13 description("Test Sampling of SetTargetAtTime"); |
| 14 |
| 15 window.jsTestIsAsync = true; |
| 16 // Some slow sample rate, but otherwise arbitrary. |
| 17 var sampleRate = 12800; |
| 18 |
| 19 // Time constant for setTargetValue. Make it short, but is otherwise arbit
rary. |
| 20 var timeConstant = 10 / sampleRate; |
| 21 |
| 22 // Defaut initial gain for test. Arbitrary, but we want it large so the c
hanges look large, |
| 23 // even if the relative change isn't. |
| 24 var initialGain = 10000; |
| 25 |
| 26 var audit = Audit.createTaskRunner(); |
| 27 |
| 28 // Test sampling of setTargetAtTime that starts at |startFrame|. A gain n
ode is used for |
| 29 // testing. |initializeGainFunction| initializes the gain value. |
| 30 function doTest(message, startFrame, threshold, initializeGainFunction) { |
| 31 var context = new OfflineAudioContext(1, 256, sampleRate); |
| 32 var source = context.createBufferSource(); |
| 33 var b = context.createBuffer(1, 1, sampleRate); |
| 34 b.getChannelData(0)[0] = 1; |
| 35 source.buffer = b; |
| 36 source.loop = true; |
| 37 |
| 38 var gain = context.createGain(); |
| 39 // Initialize the value of the gain node appropriately. |
| 40 initializeGainFunction(gain); |
| 41 gain.gain.setTargetAtTime(0, startFrame / sampleRate, timeConstant); |
| 42 |
| 43 source.connect(gain); |
| 44 gain.connect(context.destination); |
| 45 |
| 46 source.start(); |
| 47 |
| 48 return context.startRendering().then(function (resultBuffer) { |
| 49 // Verify that the sampling of the setTargetAtTime automation was done
correctly. We just |
| 50 // look at the sample just past the start of the automation. |
| 51 var resultData = resultBuffer.getChannelData(0); |
| 52 // Compute the true result at the frame just past startFrame and verif
y that the actual |
| 53 // rendered result is within |threshold| of the expected value. |
| 54 var frame = Math.ceil(startFrame); |
| 55 var v = 10000 * Math.exp(-(frame / sampleRate - startFrame / sampleRat
e) / timeConstant); |
| 56 Should(message + ": Target value at frame " + frame, resultData[frame]
).beCloseTo(v, threshold); |
| 57 }); |
| 58 } |
| 59 |
| 60 function initializeGainBySetter (g) { |
| 61 g.gain.value = initialGain; |
| 62 } |
| 63 |
| 64 function initializeGainBySetValue (g) { |
| 65 g.gain.setValueAtTime(initialGain, 0); |
| 66 } |
| 67 |
| 68 audit.defineTask("setValue;128.1", function (done) { |
| 69 doTest("Initialize by setValueAtTime", 128.1, 3.6029e-8, initializeGainB
ySetValue).then(done); |
| 70 }); |
| 71 |
| 72 audit.defineTask("setValue;0.1", function (done) { |
| 73 doTest("Initialize by setValueAtTime", 0.1, 3.6029e-8, initializeGainByS
etValue).then(done); |
| 74 }); |
| 75 |
| 76 audit.defineTask("setValue;0.0", function (done) { |
| 77 doTest("Initialize by setValueAtTime", 0, 3.6029e-8, initializeGainBySet
Value).then(done); |
| 78 }); |
| 79 |
| 80 audit.defineTask("setter;128.1", function (done) { |
| 81 doTest("Initialize by setter", 128.1, 3.6029e-8, initializeGainBySetter)
.then(done); |
| 82 }); |
| 83 |
| 84 audit.defineTask("setter;0.1", function (done) { |
| 85 doTest("Initialize by setter", 0.1, 3.6029e-8, initializeGainBySetter).t
hen(done); |
| 86 }); |
| 87 |
| 88 audit.defineTask("setter;0.0", function (done) { |
| 89 doTest("Initialize by setter", 0, 0, initializeGainBySetter).then(done); |
| 90 }); |
| 91 |
| 92 |
| 93 audit.defineTask("finish", function (done) { |
| 94 finishJSTest(); |
| 95 done(); |
| 96 }); |
| 97 |
| 98 audit.runTasks(); |
| 99 successfullyParsed = true; |
| 100 </script> |
| 101 </body> |
| 102 </html> |
OLD | NEW |