blob: 34e3b21501e2095c3952b4cc1dd1b2550319a2fb [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/panner-formulas.js"></script>
<title>Test Automation of PannerNode Positions</title>
</head>
<body>
<script>
description("Test Automation of PannerNode Positions.");
window.jsTestIsAsync = true;
var sampleRate = 48000;
// These tests are quite slow, so don't run for many frames. 256 frames should be enough to
// demonstrate that automations are working.
var renderFrames = 256;
var renderDuration = renderFrames / sampleRate;
var context;
var panner;
var audit = Audit.createTaskRunner();
// Set of tests for the panner node with automations applied to the position of the source.
var testConfigs = [{
// Distance model parameters for the panner
distanceModel: {
model: "inverse",
rolloff: 1
},
// Initial location of the source
startPosition: [0, 0, 1],
// Final position of the source. For this test, we only want to move on the z axis which
// doesn't change the azimuth angle.
endPosition: [0, 0, 10000],
}, {
distanceModel: {
model: "inverse",
rolloff: 1
},
startPosition: [0, 0, 1],
// An essentially random end position, but it should be such that azimuth angle changes as
// we move from the start to the end.
endPosition: [20000, 30000, 10000],
errorThreshold: [{
// Error threshold for 1-channel case
relativeThreshold: 4.8124e-7
}, {
// Error threshold for 2-channel case
relativeThreshold: 4.3267e-7
}],
}, {
distanceModel: {
model: "exponential",
rolloff: 1.5
},
startPosition: [0, 0, 1],
endPosition: [20000, 30000, 10000],
errorThreshold: [{
relativeThreshold: 5.0783e-7
}, {
relativeThreshold: 5.2180e-7
}]
}, {
distanceModel: {
model: "linear",
rolloff: 1
},
startPosition: [0, 0, 1],
endPosition: [20000, 30000, 10000],
errorThreshold: [{
relativeThreshold: 6.5324e-6
}, {
relativeThreshold: 6.5756e-6
}]
}];
for (var k = 0; k < testConfigs.length; ++k) {
var config = testConfigs[k];
var tester = function (c, channelCount) {
return function (done) {
runTest(c, channelCount).then(done);
}
};
var baseTestName = config.distanceModel.model + " rolloff: " + config.distanceModel.rolloff;
// Define tasks for both 1-channel and 2-channel
audit.defineTask(k + ": 1-channel " + baseTestName, tester(config, 1));
audit.defineTask(k + ": 2-channel " + baseTestName, tester(config, 2));
}
audit.defineTask("finish", function (done) {
finishJSTest();
done();
});
audit.runTasks();
function runTest(options, channelCount) {
// Output has 5 channels: channels 0 and 1 are for the stereo output of the panner node.
// Channels 2-5 are the for automation of the x,y,z coordinate so that we have actual
// coordinates used for the panner automation.
context = new OfflineAudioContext(5, renderFrames, sampleRate);
// Stereo source for the panner.
var source = context.createBufferSource();
source.buffer = createConstantBuffer(context, renderFrames, channelCount == 1 ? 1 : [1, 2]);
panner = context.createPanner();
panner.distanceModel = options.distanceModel.model;
panner.rolloffFactor = options.distanceModel.rolloff;
panner.panningModel = "equalpower";
// Source and gain node for the z-coordinate calculation.
var dist = context.createBufferSource();
dist.buffer = createConstantBuffer(context, 1, 1);
dist.loop = true;
var gainX = context.createGain();
var gainY = context.createGain();
var gainZ = context.createGain();
dist.connect(gainX);
dist.connect(gainY);
dist.connect(gainZ);
// Set the gain automation to match the z-coordinate automation of the panner.
// End the automation some time before the end of the rendering so we can verify that
// automation has the correct end time and value.
var endAutomationTime = 0.75 * renderDuration;
gainX.gain.setValueAtTime(options.startPosition[0], 0);
gainX.gain.linearRampToValueAtTime(options.endPosition[0], endAutomationTime);
gainY.gain.setValueAtTime(options.startPosition[1], 0);
gainY.gain.linearRampToValueAtTime(options.endPosition[1], endAutomationTime);
gainZ.gain.setValueAtTime(options.startPosition[2], 0);
gainZ.gain.linearRampToValueAtTime(options.endPosition[2], endAutomationTime);
dist.start();
// Splitter and merger to map the panner output and the z-coordinate automation to the
// correct channels in the destination.
var splitter = context.createChannelSplitter(2);
var merger = context.createChannelMerger(5);
source.connect(panner);
// Split the output of the panner to separate channels
panner.connect(splitter);
// Merge the panner outputs and the z-coordinate output to the correct destination channels.
splitter.connect(merger, 0, 0);
splitter.connect(merger, 1, 1);
gainX.connect(merger, 0, 2);
gainY.connect(merger, 0, 3);
gainZ.connect(merger, 0, 4);
merger.connect(context.destination);
// Initialize starting point of the panner.
panner.positionX.setValueAtTime(options.startPosition[0], 0);
panner.positionY.setValueAtTime(options.startPosition[1], 0);
panner.positionZ.setValueAtTime(options.startPosition[2], 0);
// Automate z coordinate to move away from the listener
panner.positionX.linearRampToValueAtTime(options.endPosition[0], 0.75 * renderDuration);
panner.positionY.linearRampToValueAtTime(options.endPosition[1], 0.75 * renderDuration);
panner.positionZ.linearRampToValueAtTime(options.endPosition[2], 0.75 * renderDuration);
source.start();
// Go!
return context.startRendering()
.then(function (renderedBuffer) {
// Get the panner outputs
var data0 = renderedBuffer.getChannelData(0);
var data1 = renderedBuffer.getChannelData(1);
var xcoord = renderedBuffer.getChannelData(2);
var ycoord = renderedBuffer.getChannelData(3);
var zcoord = renderedBuffer.getChannelData(4);
// We're doing a linear ramp on the Z axis with the equalpower panner, so the equalpower
// panning gain remains constant. We only need to model the distance effect.
// Compute the distance gain
var distanceGain = new Float32Array(xcoord.length);;
if (panner.distanceModel === "inverse") {
for (var k = 0; k < distanceGain.length; ++k) {
distanceGain[k] = inverseDistance(panner, xcoord[k], ycoord[k], zcoord[k])
}
} else if (panner.distanceModel === "linear") {
for (var k = 0; k < distanceGain.length; ++k) {
distanceGain[k] = linearDistance(panner, xcoord[k], ycoord[k], zcoord[k])
}
} else if (panner.distanceModel === "exponential") {
for (var k = 0; k < distanceGain.length; ++k) {
distanceGain[k] = exponentialDistance(panner, xcoord[k], ycoord[k], zcoord[k])
}
}
// Compute the expected result. Since we're on the z-axis, the left and right channels
// pass through the equalpower panner unchanged. Only need to apply the distance gain.
var buffer0 = source.buffer.getChannelData(0);
var buffer1 = channelCount == 2 ? source.buffer.getChannelData(1) : buffer0;
var azimuth = new Float32Array(buffer0.length);
for (var k = 0; k < data0.length; ++k) {
azimuth[k] = calculateAzimuth([
xcoord[k],
ycoord[k],
zcoord[k]
], [
context.listener.positionX.value,
context.listener.positionY.value,
context.listener.positionZ.value
], [
context.listener.forwardX.value,
context.listener.forwardY.value,
context.listener.forwardZ.value
], [
context.listener.upX.value,
context.listener.upY.value,
context.listener.upZ.value
]);
}
var expected = applyPanner(azimuth, buffer0, buffer1, channelCount);
var expected0 = expected.left;
var expected1 = expected.right;
for (var k = 0; k < expected0.length; ++k) {
expected0[k] *= distanceGain[k];
expected1[k] *= distanceGain[k];
}
var info = options.distanceModel.model + ", rolloff: " + options.distanceModel.rolloff;
var prefix = channelCount + "-channel "
+ "[" + options.startPosition[0] + ", "
+ options.startPosition[1] + ", "
+ options.startPosition[2] + "] -> ["
+ options.endPosition[0] + ", "
+ options.endPosition[1] + ", "
+ options.endPosition[2] + "]: ";
var errorThreshold = 0;
if (options.errorThreshold)
errorThreshold = options.errorThreshold[channelCount - 1]
Should(prefix + "distanceModel: " + info + ", left channel", data0, {
verbose: true
})
.beCloseToArray(expected0, errorThreshold);
Should(prefix + "distanceModel: " + info + ", right channel", data1, {
verbose: true
})
.beCloseToArray(expected1, errorThreshold);
});
}
</script>
</body>
</html>