gecko-dev/content/media/webaudio/test/test_scriptProcessorNode.html
Ehsan Akhgari 3a03d6982c Bug 870167 - Create a mini test framework for Web Audio; r=roc
--HG--
rename : content/media/webaudio/test/test_audioBufferSourceNodeLoop.html => content/media/webaudio/test/test_audioBufferSourceNodeEnded.html
extra : rebase_source : 787d1d19b6463906ff9db8cbfb5e61d76a2ab667
2013-05-09 08:45:35 -04:00

149 lines
7.2 KiB
HTML

<!DOCTYPE HTML>
<html>
<head>
<title>Test ScriptProcessorNode</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="webaudio.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<pre id="test">
<script class="testbody" type="text/javascript">
// We do not use our generic graph test framework here because
// the testing logic here is sort of complicated, and would
// not be easy to map to OfflineAudioContext, as ScriptProcessorNodes
// can experience delays.
SimpleTest.waitForExplicitFinish();
addLoadEvent(function() {
SpecialPowers.setBoolPref("media.webaudio.enabled", true);
var context = new AudioContext();
var buffer = null;
var sourceSP = context.createJavaScriptNode(2048);
sourceSP.addEventListener("audioprocess", function(e) {
// generate the audio
for (var i = 0; i < 2048; ++i) {
// Make sure our first sample won't be zero
e.outputBuffer.getChannelData(0)[i] = Math.sin(440 * 2 * Math.PI * (i + 1) / context.sampleRate);
e.outputBuffer.getChannelData(0)[i] = Math.sin(880 * 2 * Math.PI * (i + 1) / context.sampleRate);
}
// Remember our generated audio
buffer = e.outputBuffer;
sourceSP.removeEventListener("audioprocess", arguments.callee);
}, false);
expectException(function() {
context.createScriptProcessor(1);
}, DOMException.INDEX_SIZE_ERR);
expectException(function() {
context.createScriptProcessor(2);
}, DOMException.INDEX_SIZE_ERR);
expectException(function() {
context.createScriptProcessor(128);
}, DOMException.INDEX_SIZE_ERR);
expectException(function() {
context.createScriptProcessor(255);
}, DOMException.INDEX_SIZE_ERR);
is(sourceSP.channelCount, 2, "script processor node has 2 input channels by default");
is(sourceSP.channelCountMode, "explicit", "Correct channelCountMode for the script processor node");
is(sourceSP.channelInterpretation, "speakers", "Correct channelCountInterpretation for the script processor node");
function findFirstNonZeroSample(buffer) {
for (var i = 0; i < buffer.length; ++i) {
if (buffer.getChannelData(0)[i] != 0) {
return i;
}
}
return buffer.length;
}
var sp = context.createScriptProcessor(2048);
sourceSP.connect(sp);
sp.connect(context.destination);
var lastPlaybackTime = 0;
sp.onaudioprocess = function(e) {
isnot(buffer, null, "The audioprocess handler for sourceSP must be run at this point");
is(e.target, sp, "Correct event target");
ok(e.playbackTime > lastPlaybackTime, "playbackTime correctly set");
lastPlaybackTime = e.playbackTime;
is(e.inputBuffer.numberOfChannels, 2, "Correct number of channels for the input buffer");
is(e.inputBuffer.length, 2048, "Correct length for the input buffer");
is(e.inputBuffer.sampleRate, context.sampleRate, "Correct sample rate for the input buffer");
is(e.outputBuffer.numberOfChannels, 2, "Correct number of channels for the output buffer");
is(e.outputBuffer.length, 2048, "Correct length for the output buffer");
is(e.outputBuffer.sampleRate, context.sampleRate, "Correct sample rate for the output buffer");
// Because of the initial latency added by the second script processor node,
// we will never see any generated audio frames in the first callback.
var emptyBuffer = context.createBuffer(1, 2048, context.sampleRate);
compareBuffers(e.inputBuffer.getChannelData(0), emptyBuffer.getChannelData(0));
compareBuffers(e.inputBuffer.getChannelData(1), emptyBuffer.getChannelData(0));
compareBuffers(e.outputBuffer.getChannelData(0), emptyBuffer.getChannelData(0));
compareBuffers(e.outputBuffer.getChannelData(1), emptyBuffer.getChannelData(0));
sp.onaudioprocess = function(e) {
is(e.target, sp, "Correct event target");
ok(e.playbackTime > lastPlaybackTime, "playbackTime correctly set");
lastPlaybackTime = e.playbackTime;
is(e.inputBuffer.numberOfChannels, 2, "Correct number of channels for the input buffer");
is(e.inputBuffer.length, 2048, "Correct length for the input buffer");
is(e.inputBuffer.sampleRate, context.sampleRate, "Correct sample rate for the input buffer");
is(e.outputBuffer.numberOfChannels, 2, "Correct number of channels for the output buffer");
is(e.outputBuffer.length, 2048, "Correct length for the output buffer");
is(e.outputBuffer.sampleRate, context.sampleRate, "Correct sample rate for the output buffer");
var firstNonZero = findFirstNonZeroSample(e.inputBuffer);
compareBuffers(e.inputBuffer.getChannelData(0), emptyBuffer.getChannelData(0), 0, Math.min(firstNonZero, 2048));
compareBuffers(e.inputBuffer.getChannelData(1), emptyBuffer.getChannelData(0), 0, Math.min(firstNonZero, 2048));
compareBuffers(e.inputBuffer.getChannelData(0), buffer.getChannelData(0), Math.min(firstNonZero, 2048), 2048 - firstNonZero, 0, -firstNonZero);
compareBuffers(e.inputBuffer.getChannelData(1), buffer.getChannelData(1), Math.min(firstNonZero, 2048), 2048 - firstNonZero, 0, -firstNonZero);
compareBuffers(e.outputBuffer.getChannelData(0), emptyBuffer.getChannelData(0));
compareBuffers(e.outputBuffer.getChannelData(1), emptyBuffer.getChannelData(0));
if (firstNonZero == 0) {
// If we did not experience any delays, the test is done!
sp.onaudioprocess = null;
SpecialPowers.clearUserPref("media.webaudio.enabled");
SimpleTest.finish();
} else if (firstNonZero != 2048) {
// In case we just saw a zero buffer this time, wait one more round
sp.onaudioprocess = function(e) {
is(e.target, sp, "Correct event target");
ok(e.playbackTime > lastPlaybackTime, "playbackTime correctly set");
lastPlaybackTime = e.playbackTime;
is(e.inputBuffer.numberOfChannels, 2, "Correct number of channels for the input buffer");
is(e.inputBuffer.length, 2048, "Correct length for the input buffer");
is(e.inputBuffer.sampleRate, context.sampleRate, "Correct sample rate for the input buffer");
is(e.outputBuffer.numberOfChannels, 2, "Correct number of channels for the output buffer");
is(e.outputBuffer.length, 2048, "Correct length for the output buffer");
is(e.outputBuffer.sampleRate, context.sampleRate, "Correct sample rate for the output buffer");
compareBuffers(e.inputBuffer.getChannelData(0), buffer.getChannelData(0), 0, firstNonZero, 0, 2048 - firstNonZero);
compareBuffers(e.inputBuffer.getChannelData(1), buffer.getChannelData(1), 0, firstNonZero, 0, 2048 - firstNonZero);
compareBuffers(e.inputBuffer.getChannelData(0), emptyBuffer.getChannelData(0), firstNonZero);
compareBuffers(e.inputBuffer.getChannelData(1), emptyBuffer.getChannelData(0), firstNonZero);
compareBuffers(e.outputBuffer.getChannelData(0), emptyBuffer.getChannelData(0));
compareBuffers(e.outputBuffer.getChannelData(1), emptyBuffer.getChannelData(0));
sp.onaudioprocess = null;
SpecialPowers.clearUserPref("media.webaudio.enabled");
SimpleTest.finish();
};
}
};
};
});
</script>
</pre>
</body>
</html>