blob: e9a66512694431ec4f4997d568e09e94fb3cc52a [file] [log] [blame]
<!doctype html>
<html>
<head>
<script src="../resources/js-test.js"></script>
<script src="resources/compatibility.js"></script>
<script src="resources/audio-testing.js"></script>
<script src="resources/audioparam-testing.js"></script>
<title>Test Sampling for SetTargetAtTime</title>
</head>
<body>
<script>
description("Test Sampling of SetTargetAtTime");
window.jsTestIsAsync = true;
// Some slow sample rate, but otherwise arbitrary.
var sampleRate = 12800;
// Time constant for setTargetValue. Make it short, but is otherwise arbitrary.
var timeConstant = 10 / sampleRate;
// Defaut initial gain for test. Arbitrary, but we want it large so the changes look large,
// even if the relative change isn't.
var initialGain = 10000;
var audit = Audit.createTaskRunner();
// Test sampling of setTargetAtTime that starts at |startFrame|. A gain node is used for
// testing. |initializeGainFunction| initializes the gain value.
function doTest(message, startFrame, threshold, initializeGainFunction) {
var context = new OfflineAudioContext(1, 256, sampleRate);
var source = context.createBufferSource();
var b = context.createBuffer(1, 1, sampleRate);
b.getChannelData(0)[0] = 1;
source.buffer = b;
source.loop = true;
var gain = context.createGain();
// Initialize the value of the gain node appropriately.
initializeGainFunction(gain);
gain.gain.setTargetAtTime(0, startFrame / sampleRate, timeConstant);
source.connect(gain);
gain.connect(context.destination);
source.start();
return context.startRendering().then(function (resultBuffer) {
// Verify that the sampling of the setTargetAtTime automation was done correctly. We just
// look at the sample just past the start of the automation.
var resultData = resultBuffer.getChannelData(0);
// Compute the true result at the frame just past startFrame and verify that the actual
// rendered result is within |threshold| of the expected value.
var frame = Math.ceil(startFrame);
var v = 10000 * Math.exp(-(frame / sampleRate - startFrame / sampleRate) / timeConstant);
Should(message + ": Target value at frame " + frame, resultData[frame]).beCloseTo(v, threshold);
});
}
function initializeGainBySetter (g) {
g.gain.value = initialGain;
}
function initializeGainBySetValue (g) {
g.gain.setValueAtTime(initialGain, 0);
}
audit.defineTask("setValue;128.1", function (done) {
doTest("Initialize by setValueAtTime", 128.1, 3.6029e-8, initializeGainBySetValue).then(done);
});
audit.defineTask("setValue;0.1", function (done) {
doTest("Initialize by setValueAtTime", 0.1, 3.6029e-8, initializeGainBySetValue).then(done);
});
audit.defineTask("setValue;0.0", function (done) {
doTest("Initialize by setValueAtTime", 0, 3.6029e-8, initializeGainBySetValue).then(done);
});
audit.defineTask("setter;128.1", function (done) {
doTest("Initialize by setter", 128.1, 3.6029e-8, initializeGainBySetter).then(done);
});
audit.defineTask("setter;0.1", function (done) {
doTest("Initialize by setter", 0.1, 3.6029e-8, initializeGainBySetter).then(done);
});
audit.defineTask("setter;0.0", function (done) {
doTest("Initialize by setter", 0, 0, initializeGainBySetter).then(done);
});
audit.defineTask("finish", function (done) {
finishJSTest();
done();
});
audit.runTasks();
successfullyParsed = true;
</script>
</body>
</html>