Bug 834513 - Part 4: Add tests for ScriptProcessorNode, and also test AudioBufferSourceNode using it; r=roc

This commit is contained in:
Ehsan Akhgari 2013-04-20 11:17:58 -04:00
parent e6ef206ecb
commit 5dc9f3b37e
4 changed files with 228 additions and 0 deletions

View File

@ -22,6 +22,7 @@ MOCHITEST_FILES := \
test_AudioContext.html \
test_AudioListener.html \
test_AudioParam.html \
test_audioBufferSourceNode.html \
test_badConnect.html \
test_biquadFilterNode.html \
test_currentTime.html \
@ -30,6 +31,7 @@ MOCHITEST_FILES := \
test_dynamicsCompressorNode.html \
test_gainNode.html \
test_pannerNode.html \
test_scriptProcessorNode.html \
test_singleSourceDest.html \
ting.ogg \
ting-expected.wav \

View File

@ -0,0 +1,52 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test AudioBufferSourceNode</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">
SimpleTest.waitForExplicitFinish();
addLoadEvent(function() {
SpecialPowers.setBoolPref("media.webaudio.enabled", true);
var context = new AudioContext();
var buffer = context.createBuffer(1, 2048, context.sampleRate);
for (var i = 0; i < 2048; ++i) {
buffer.getChannelData(0)[i] = Math.sin(440 * 2 * Math.PI * i / context.sampleRate);
}
var source = context.createBufferSource();
source.buffer = buffer;
var sp = context.createScriptProcessor(2048);
source.start(0);
source.connect(sp);
sp.connect(context.destination);
sp.onaudioprocess = function(e) {
compareBuffers(e.inputBuffer.getChannelData(0), buffer.getChannelData(0));
compareBuffers(e.inputBuffer.getChannelData(1), buffer.getChannelData(0));
// On the next iteration, we'll get a silence buffer
sp.onaudioprocess = function(e) {
var emptyBuffer = context.createBuffer(1, 2048, context.sampleRate);
compareBuffers(e.inputBuffer.getChannelData(0), emptyBuffer.getChannelData(0));
compareBuffers(e.inputBuffer.getChannelData(1), emptyBuffer.getChannelData(0));
sp.onaudioprocess = null;
sp.disconnect(context.destination);
SpecialPowers.clearUserPref("media.webaudio.enabled");
SimpleTest.finish();
};
};
});
</script>
</pre>
</body>
</html>

View File

@ -0,0 +1,139 @@
<!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">
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);
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>

View File

@ -22,3 +22,38 @@ function expectTypeError(func) {
}
ok(threw, "The exception was thrown");
}
function fuzzyCompare(a, b) {
return Math.abs(a - b) < 1e-5;
}
function compareBuffers(buf1, buf2,
/*optional*/ offset,
/*optional*/ length,
/*optional*/ sourceOffset,
/*optional*/ destOffset) {
is(buf1.length, buf2.length, "Buffers must have the same length");
if (length == undefined) {
length = buf1.length - (offset || 0);
}
sourceOffset = sourceOffset || 0;
destOffset = destOffset || 0;
var difference = 0;
var maxDifference = 0;
var firstBadIndex = -1;
for (var i = offset || 0; i < Math.min(buf1.length, (offset || 0) + length); ++i) {
if (!fuzzyCompare(buf1[i + sourceOffset], buf2[i + destOffset])) {
console.log(buf1[i+sourceOffset] + " " + buf2[i+destOffset]);
difference++;
maxDifference = Math.max(maxDifference, Math.abs(buf1[i + sourceOffset] - buf2[i + destOffset]));
if (firstBadIndex == -1) {
firstBadIndex = i;
}
}
};
is(difference, 0, "Found " + difference + " different samples, maxDifference: " +
maxDifference + ", first bad index: " + firstBadIndex +
" with source offset " + sourceOffset + " and desitnation offset " +
destOffset);
}