Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webaudio tests #235

Merged
merged 13 commits into from
Jul 3, 2013
Empty file added webaudio/.gitignore
Empty file.
5 changes: 5 additions & 0 deletions webaudio/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Our test suite is currently tracking the [editor's draft](https://dvcs.w3.org/hg/audio/raw-file/tip/webaudio/specification.html) of the Web Audio API.

The tests are arranged in subdirectories, corresponding to different
sections of the spec. So, for example, tests for the `DelayNode` are
in `the-audio-api/the-delaynode-interface`.
47 changes: 47 additions & 0 deletions webaudio/js/buffer-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* Taken from
https://raw.github.com/WebKit/webkit/master/LayoutTests/webaudio/resources/buffer-loader.js */

function BufferLoader(context, urlList, callback) {
this.context = context;
this.urlList = urlList;
this.onload = callback;
this.bufferList = new Array();
this.loadCount = 0;
}

BufferLoader.prototype.loadBuffer = function(url, index) {
// Load buffer asynchronously
var request = new XMLHttpRequest();
request.open("GET", url, true);
request.responseType = "arraybuffer";

var loader = this;

request.onload = function() {
var buffer;
try {
buffer = loader.context.createBuffer(request.response, false);
} catch(e) {
alert('error decoding file data: ' + url);
}

try {
loader.bufferList[index] = buffer;
if (++loader.loadCount == loader.urlList.length)
loader.onload(loader.bufferList);
} catch(e) {
alert('BufferLoader: callback problem');
}
}

request.onerror = function() {
alert('BufferLoader: XHR error');
}

request.send();
}

BufferLoader.prototype.load = function() {
for (var i = 0; i < this.urlList.length; ++i)
this.loadBuffer(this.urlList[i], i);
}
64 changes: 64 additions & 0 deletions webaudio/js/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
function assert_has_property(obj, name, desc) {
assert_true(undefined != obj[name], desc);
};

function assert_is_method(obj, name, desc) {
assert_true("function" === typeof obj[name], desc);
};

function assert_defined(obj, desc) {
assert_true(undefined != obj, desc);
};

// Test the passed in =obj= has the properties and methods to
// implement the AudioParam Interface
// (https://dvcs.w3.org/hg/audio/raw-file/tip/webaudio/specification.html#AudioParam)
function test_implements_audio_param_interface(obj) {
var properties = ["value", "computedValue", "minValue", "maxValue",
"defaultValue"];

_(properties).forEach(function(property) {
fn = function () { assert_has_property(obj, property, "has " + property) };
test(fn, "'" + obj.name + "'" + " implements: " + property);
});

var methods = ["setValueAtTime", "linearRampToValueAtTime",
"exponentialRampToValueAtTime", "setTargetAtTime",
"setValueCurveAtTime", "cancelScheduledValues"];

_(methods).forEach(function(meth) {
fn = function () { assert_is_method(obj, meth, "has " + meth + "()") };
test(fn, "'" + obj.name + "'" + " implements: " + meth + "()");
});
};


function fuzzyCompare(a, b) {
return Math.abs(a - b) < 5e-5;
}

function compareBuffers(buf1, buf2,
/*optional*/ offset,
/*optional*/ length,
/*optional*/ sourceOffset,
/*optional*/ destOffset) {
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])) {
difference++;
maxDifference = Math.max(maxDifference, Math.abs(buf1[i + sourceOffset] - buf2[i + destOffset]));
if (firstBadIndex == -1) {
firstBadIndex = i;
}
}
};

return difference == 0;
}
Loading