Bug 836599 - Part 14: Run most of the existing Web Audio tests using OfflineAudioContext as well as AudioContext; r=roc

This helps us get a decent amount of test coverage on OfflineAudioContext in
both the existing tests and the future tests.
This commit is contained in:
Ehsan Akhgari 2013-05-16 19:31:08 -04:00
Родитель 1d49386d36
Коммит 56b05d7b13
1 изменённых файлов: 85 добавлений и 41 удалений

Просмотреть файл

@ -31,8 +31,11 @@ function compareBuffers(buf1, buf2,
/*optional*/ offset,
/*optional*/ length,
/*optional*/ sourceOffset,
/*optional*/ destOffset) {
is(buf1.length, buf2.length, "Buffers must have the same length");
/*optional*/ destOffset,
/*optional*/ skipLengthCheck) {
if (!skipLengthCheck) {
is(buf1.length, buf2.length, "Buffers must have the same length");
}
if (length == undefined) {
length = buf1.length - (offset || 0);
}
@ -100,48 +103,89 @@ function runTest()
gTest.numberOfChannels = 2; // default
}
var context = new AudioContext();
if (!gTest.createExpectedBuffers) {
// Assume that the output is silence
var expectedBuffers = getEmptyBuffer(context, gTest.length);
} else {
var expectedBuffers = gTest.createExpectedBuffers(context);
}
if (!(expectedBuffers instanceof Array)) {
expectedBuffers = [expectedBuffers];
}
var expectedFrames = 0;
for (var i = 0; i < expectedBuffers.length; ++i) {
is(expectedBuffers[i].numberOfChannels, gTest.numberOfChannels,
"Correct number of channels for expected buffer " + i);
expectedFrames += expectedBuffers[i].length;
}
is(expectedFrames, gTest.length, "Correct number of expected frames");
var testLength;
if (gTest.createGraphAsync) {
gTest.createGraphAsync(context, function(nodeToInspect) {
testOutput(nodeToInspect);
});
} else {
testOutput(gTest.createGraph(context));
function runTestOnContext(context, callback, testOutput) {
if (!gTest.createExpectedBuffers) {
// Assume that the output is silence
var expectedBuffers = getEmptyBuffer(context, gTest.length);
} else {
var expectedBuffers = gTest.createExpectedBuffers(context);
}
if (!(expectedBuffers instanceof Array)) {
expectedBuffers = [expectedBuffers];
}
var expectedFrames = 0;
for (var i = 0; i < expectedBuffers.length; ++i) {
is(expectedBuffers[i].numberOfChannels, gTest.numberOfChannels,
"Correct number of channels for expected buffer " + i);
expectedFrames += expectedBuffers[i].length;
}
is(expectedFrames, gTest.length, "Correct number of expected frames");
if (gTest.createGraphAsync) {
gTest.createGraphAsync(context, function(nodeToInspect) {
testOutput(nodeToInspect, expectedBuffers, callback);
});
} else {
testOutput(gTest.createGraph(context), expectedBuffers, callback);
}
}
function testOutput(nodeToInspect) {
var sp = context.createScriptProcessor(expectedBuffers[0].length, gTest.numberOfChannels);
nodeToInspect.connect(sp);
sp.connect(context.destination);
sp.onaudioprocess = function(e) {
var expectedBuffer = expectedBuffers.shift();
is(e.inputBuffer.numberOfChannels, expectedBuffer.numberOfChannels,
"Correct number of input buffer channels");
for (var i = 0; i < e.inputBuffer.numberOfChannels; ++i) {
compareBuffers(e.inputBuffer.getChannelData(i), expectedBuffer.getChannelData(i));
}
if (expectedBuffers.length == 0) {
sp.onaudioprocess = null;
done();
}
};
function testOnNormalContext(callback) {
function testOutput(nodeToInspect, expectedBuffers, callback) {
testLength = 0;
var sp = context.createScriptProcessor(expectedBuffers[0].length, gTest.numberOfChannels);
nodeToInspect.connect(sp);
sp.connect(context.destination);
sp.onaudioprocess = function(e) {
var expectedBuffer = expectedBuffers.shift();
testLength += expectedBuffer.length;
is(e.inputBuffer.numberOfChannels, expectedBuffer.numberOfChannels,
"Correct number of input buffer channels");
for (var i = 0; i < e.inputBuffer.numberOfChannels; ++i) {
compareBuffers(e.inputBuffer.getChannelData(i), expectedBuffer.getChannelData(i));
}
if (expectedBuffers.length == 0) {
sp.onaudioprocess = null;
callback();
}
};
}
var context = new AudioContext();
runTestOnContext(context, callback, testOutput);
}
function testOnOfflineContext(callback) {
function testOutput(nodeToInspect, expectedBuffers, callback) {
nodeToInspect.connect(context.destination);
context.oncomplete = function(e) {
var samplesSeen = 0;
while (expectedBuffers.length) {
var expectedBuffer = expectedBuffers.shift();
is(e.renderedBuffer.numberOfChannels, expectedBuffer.numberOfChannels,
"Correct number of input buffer channels");
for (var i = 0; i < e.renderedBuffer.numberOfChannels; ++i) {
compareBuffers(e.renderedBuffer.getChannelData(i),
expectedBuffer.getChannelData(i),
undefined,
expectedBuffer.length,
samplesSeen,
undefined,
true);
}
samplesSeen += expectedBuffer.length;
}
callback();
};
context.startRendering();
}
var context = new OfflineAudioContext(gTest.numberOfChannels, testLength, 48000);
runTestOnContext(context, callback, testOutput);
}
testOnNormalContext(function() {
testOnOfflineContext(done);
});
});
}