audioPlugin["handle"] = function audioPlugin_handle(byteArray, name, onload, onerror) {
var done = false;
function finish(audio) {
if (done) return;
done = true;
Module["preloadedAudios"][name] = audio;
if (onload) onload(byteArray)
}
function fail() {
if (done) return;
done = true;
Module["preloadedAudios"][name] = new Audio;
if (onerror) onerror()
}
if (Browser.hasBlobConstructor) {
try {
var b = new Blob([byteArray], {
type: Browser.getMimetype(name)
})
} catch (e) {
return fail()
}
var url = Browser.URLObject.createObjectURL(b);
var audio = new Audio;
audio.addEventListener("canplaythrough", function() {
finish(audio)
}, false);
audio.onerror = function audio_onerror(event) {
if (done) return;
console.log("warning: browser could not fully decode audio " + name + ", trying slower base64 approach");
function encode64(data) {
var BASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var PAD = "=";
var ret = "";
var leftchar = 0;
var leftbits = 0;
for (var i = 0; i < data.length; i++) {
leftchar = leftchar << 8 | data[i];
leftbits += 8;
while (leftbits >= 6) {
var curr = leftchar >> leftbits - 6 & 63;
leftbits -= 6;
ret += BASE[curr]
}
}
if (leftbits == 2) {
ret += BASE[(leftchar & 3) << 4];
ret += PAD + PAD
} else if (leftbits == 4) {
ret += BASE[(leftchar & 15) << 2];
ret += PAD
}
return ret
}
audio.src = "data:audio/x-" + name.substr(-3) + ";base64," + encode64(byteArray);
finish(audio)
};
audio.src = url;
Browser.safeSetTimeout(function() {
finish(audio)
}, 1e4)
} else {
return fail()
}
};
Module["preloadPlugins"].push(audioPlugin);
var audio = new Audio;
audio.addEventListener("canplaythrough", function() {
finish(audio)
}, false);
audio.onerror = function audio_onerror(event) {
if (done) return;
console.log("warning: browser could not fully decode audio " + name + ", trying slower base64 approach");
function encode64(data) {
var BASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var PAD = "=";
var ret = "";
var leftchar = 0;
var leftbits = 0;
for (var i = 0; i < data.length; i++) {
leftchar = leftchar << 8 | data[i];
leftbits += 8;
while (leftbits >= 6) {
var curr = leftchar >> leftbits - 6 & 63;
leftbits -= 6;
ret += BASE[curr]
}
}
if (leftbits == 2) {
ret += BASE[(leftchar & 3) << 4];
ret += PAD + PAD
} else if (leftbits == 4) {
ret += BASE[(leftchar & 15) << 2];
ret += PAD
}
return ret
}
audio.src = "data:audio/x-" + name.substr(-3) + ";base64," + encode64(byteArray);
finish(audio)
console.log("Audio1");
};
audio.src = url;
Browser.safeSetTimeout(function() {
finish(audio)
console.log("Audio2");
}, 1)