blob: 9f08039264fe3c5e807025d887808da555a9c729 [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>
</head>
<body>
<script>
description('AudioBufferSourceNode: test the "zero" playbackRate.');
window.jsTestIsAsync = true;
// Sample rate should be power of 128 to observe the change of AudioParam at
// the beginning of rendering quantum. (playbackRate is k-rate) This is the
// minimum sample rate in the valid sample rate range.
var sampleRate = 4096;
// The render duration in seconds, and the length in samples.
var renderDuration = 1.0;
var renderLength = renderDuration * sampleRate;
var context = new OfflineAudioContext(1, renderLength, sampleRate);
var audit = Audit.createTaskRunner();
// Task: Render the actual buffer and compare with the reference.
audit.defineTask('synthesize-verify', function (done) {
var ramp = context.createBufferSource();
var rampBuffer = createLinearRampBuffer(context, renderLength);
ramp.buffer = rampBuffer;
ramp.connect(context.destination);
ramp.start();
// Leave the playbackRate as 1 for the first half, then change it
// to zero at the exact half. The zero playback rate should hold the
// sample value of the buffer index at the moment. (sample-and-hold)
ramp.playbackRate.setValueAtTime(1.0, 0.0);
ramp.playbackRate.setValueAtTime(0.0, renderDuration / 2);
context.startRendering().then(function (renderedBuffer) {
var data = renderedBuffer.getChannelData(0);
var rampData = rampBuffer.getChannelData(0);
var half = rampData.length / 2;
var passed = true;
for (var i = 1; i < rampData.length; i++) {
if (i < half) {
// Before the half position, the actual should match with the
// original ramp data.
if (data[i] !== rampData[i]) {
passed = false;
break;
}
} else {
// From the half position, the actual value should not change.
if (data[i] !== rampData[half]) {
passed = false;
break;
}
}
}
if (passed) {
testPassed('The zero playbackRate held the sample value correctly.');
} else {
testFailed('The zero playbackRate should hold the sample value. ' +
'Expected ' + rampData[half] + ' but got ' + data[i] + ' at the index ' +
i + '.');
}
}).then(done);
});
audit.defineTask('finish', function (done) {
finishJSTest();
done();
});
audit.runTasks(
'synthesize-verify',
'finish'
);
successfullyParsed = true;
</script>
</body>
</html>