if (window.AudioContext == undefined) {
window.AudioContext = window.webkitAudioContext;
window.OfflineAudioContext = window.webkitOfflineAudioContext;
}
$ = document.querySelectorAll.bind(document);
let DURATION = null;
if (location.search) {
let duration = location.search.match(/rendering-buffer-length=(\d+)/);
if (duration) {
DURATION = duration[1];
} else {
DURATION = 120;
}
} else {
DURATION = 120;
}
// Global sample rate at which we run the context.
var sampleRate = 48000;
// Array containing at first the url of the audio resources to fetch, and the
// the actual buffers audio buffer we have at our disposal to for tests.
var sources = [];
// Array containing the results, for each benchmark.
var results = [];
// Array containing the offline contexts used to run the testcases.
var testcases = [];
// Array containing the functions that can return a runnable testcase.
var testcases_registered = [];
// Array containing the audio buffers for each benchmark
var buffers = [];
var playingSource = null;
// audiocontext used to play back the result of the benchmarks
var ac = new AudioContext();
function getFile(source, callback) {
var request = new XMLHttpRequest();
request.open("GET", source.url, true);
request.responseType = "arraybuffer";
request.onload = function() {
// decode buffer at its initial sample rate
var ctx = new OfflineAudioContext(1, 1, source.sampleRate);
ctx.decodeAudioData(request.response, function(buffer) {
callback(buffer, undefined);
}, function() {
callback(undefined, "Error decoding the file " + source.url);
});
}
request.send();
}
function recordResult(result) {
results.push(result);
}
function benchmark(testcase, ended) {
var context = testcase.ctx;
var start;
context.oncomplete = function(e) {
var end = Date.now();
recordResult({
name: testcase.name,
duration: end - start,
buffer: e.renderedBuffer
});
ended();
};
start = Date.now();
context.startRendering();
}
function getMonoFile() {
return getSpecificFile({ numberOfChannels: 1 });
}
function getStereoFile() {
return getSpecificFile({ numberOfChannels: 2 });
}
function matchIfSpecified(a, b) {
if (b) {
return a == b;
}
return true;
}
function getSpecificFile(spec) {
for (var i = 0 ; i < sources.length; i++) {
if (matchIfSpecified(sources[i].numberOfChannels, spec.numberOfChannels) &&
matchIfSpecified(sources[i].sampleRate, spec.sampleRate)) {
return sources[i];
}
}
throw new Error("Could not find a file that matches the specs.");
}
function allDone() {
document.getElementById("in-progress").style.display = "none";
var result = document.getElementById("results");
var str = "
Test name
Time in ms
Speedup vs. realtime
Sound
";
var buffers_base = buffers.length;
var product_of_durations = 1.0;
for (var i = 0 ; i < results.length; i++) {
var r = results[i];
product_of_durations *= r.duration;
str += "