| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>Test Linear-/Exponential-RampToValueAtTime</title> | |
| <script src="../../resources/testharness.js"></script> | |
| <script src="../../resources/testharnessreport.js"></script> | |
| <script src="../resources/audit-util.js"></script> | |
| </head> | |
| <body> | |
| <script> | |
| const sampleRate = 12800; | |
| // Basically want to test that if neither the start time nor end time is | |
| // on a frame boundary that we sample the automation curve correctly. The | |
| // start times and end times are mostly arbitrary, except that they cannot | |
| // be on a frame boundary. | |
| const testConfigs = [ | |
| { | |
| desc: 'linearRamp', | |
| startTime: 0.1 / sampleRate, | |
| endTime: 128.1 / sampleRate, | |
| startValue: 1, | |
| endValue: 0, | |
| startValueThreshold: 1.201e-8, | |
| endValueThreshold: 1.526e-5, | |
| automationFunction: linearAutomation, | |
| expectedFunction: expectedLinear | |
| }, | |
| { | |
| desc: 'linearRamp:short', | |
| startTime: 0.1 / sampleRate, | |
| endTime: 5.1 / sampleRate, | |
| startValue: 1, | |
| endValue: 0, | |
| startValueThreshold: 8.723e-9, | |
| endValueThreshold: 9.537e-7, | |
| automationFunction: linearAutomation, | |
| expectedFunction: expectedLinear | |
| }, | |
| { | |
| desc: 'linearRamp:long', | |
| startTime: 0.1 / sampleRate, | |
| endTime: 200.1 / sampleRate, | |
| startValue: 1, | |
| endValue: 0, | |
| startValueThreshold: 2.827e-8, | |
| endValueThreshold: 4.674e-5, | |
| automationFunction: linearAutomation, | |
| expectedFunction: expectedLinear | |
| }, | |
| { | |
| desc: 'exponentialRamp', | |
| startTime: 0.1 / sampleRate, | |
| endTime: 128.1 / sampleRate, | |
| startValue: 1, | |
| endValue: 1e-5, | |
| startValueThreshold: 2.505e-8, | |
| endValueThreshold: 1.484e-7, | |
| automationFunction: exponentialAutomation, | |
| expectedFunction: expectedExponential | |
| }, | |
| { | |
| desc: 'exponentialRamp:short', | |
| startTime: 0.1 / sampleRate, | |
| endTime: 5.1 / sampleRate, | |
| startValue: 1, | |
| endValue: 1e-5, | |
| startValueThreshold: 5.027e-8, | |
| endValueThreshold: 3.821e-7, | |
| automationFunction: exponentialAutomation, | |
| expectedFunction: expectedExponential | |
| }, | |
| { | |
| desc: 'exponentialRamp:long', | |
| startTime: 0.1 / sampleRate, | |
| endTime: 200.1 / sampleRate, | |
| startValue: 1, | |
| endValue: 1e-5, | |
| startValueThreshold: 8.035e-9, | |
| endValueThreshold: 1.337e-6, | |
| automationFunction: exponentialAutomation, | |
| expectedFunction: expectedExponential | |
| }, | |
| ]; | |
| function expectedLinear(t) { | |
| const slope = (this.endValue - this.startValue) / | |
| (this.endTime - this.startTime); | |
| return this.startValue + slope * (t - this.startTime); | |
| } | |
| function expectedExponential(t) { | |
| const ratio = this.endValue / this.startValue; | |
| const exponent = (t - this.startTime) / | |
| (this.endTime - this.startTime); | |
| return this.startValue * Math.pow(ratio, exponent); | |
| } | |
| function linearAutomation(gainNode) { | |
| gainNode.gain.linearRampToValueAtTime(this.endValue, this.endTime); | |
| } | |
| function exponentialAutomation(gainNode) { | |
| gainNode.gain.exponentialRampToValueAtTime(this.endValue, this.endTime); | |
| } | |
| function runTest(config) { | |
| // Create a short context with a constant signal source connected to a | |
| // gain node that will be automated. | |
| const context = new OfflineAudioContext(1, 256, sampleRate); | |
| // Create a constant unit amplitude source. | |
| const source = new AudioBufferSourceNode(context); | |
| const buffer = createConstantBuffer(context, 1, 1); | |
| source.buffer = buffer; | |
| source.loop = true; | |
| // Gain node that is to be automated. | |
| const gain = context.createGain(); | |
| gain.gain.value = 0; | |
| gain.gain.setValueAtTime(config.startValue, config.startTime); | |
| config.automationFunction.call(config, gain); | |
| source.connect(gain); | |
| gain.connect(context.destination); | |
| source.start(); | |
| return context.startRendering().then(resultBuffer => { | |
| // Check that the automation has the correct sampling. | |
| const resultData = resultBuffer.getChannelData(0); | |
| // The automation has starts at config.startTime, so the frame just | |
| // after this should have the automation applied. | |
| const startFrame = Math.ceil(config.startTime * sampleRate); | |
| // The automation ends at config.endTime so the frame just before this | |
| // should have the automation applied. | |
| const endFrame = Math.floor(config.endTime * sampleRate); | |
| // Use the true automation to find the expected values. | |
| const expectedStart = config.expectedFunction.call( | |
| config, startFrame / sampleRate); | |
| const expectedEnd = config.expectedFunction.call( | |
| config, endFrame / sampleRate); | |
| assert_approx_equals( | |
| resultData[startFrame], expectedStart, config.startValueThreshold, | |
| `${config.desc}: startFrame value`); | |
| assert_approx_equals( | |
| resultData[endFrame], expectedEnd, config.endValueThreshold, | |
| `${config.desc}: endFrame value`); | |
| }); | |
| } | |
| for (const config of testConfigs) { | |
| promise_test( | |
| () => runTest(config), | |
| `${config.desc} Test`); | |
| } | |
| </script> | |
| </body> | |
| </html> |