jsmad/index.html

105 lines
2.7 KiB
HTML

<!doctype html>
<html>
<head>
</head>
<body>
<script>
var Module = {
onRuntimeInitialized: function() {
}
};
</script>
<script src="wrapper.js"></script>
<input type="file" id="file" name="file" />
<script>
var minPortion = 20480;
var maxPortion = 40960;
var time = 0;
var ctx = new AudioContext();
ctx.suspend();
var decoder;
var fragments = [];
var noize = [];
var decoding = false;
function onChange(e) {
var file = e.target.files[0];
var reader = new FileReader();
reader.onload = function(e) {
decoder = new Module.Decoder();
createFileFragments(reader.result);
scheduleData();
setTimeout(.bind(ctx), 1000);
}
reader.readAsArrayBuffer(file);
}
function schedule() {
decoding = false;
if (decoder.hasMore()) {
var audio = decoder.decode(20);
if (audio) {
var source = ctx.createBufferSource();
source.buffer = audio;
source.connect(ctx.destination);
source.start(time);
time += audio.duration;
setTimeout(schedule, Math.floor(audio.duration * 200));
decoding = true;
} else {
//decoder.delete();
}
} else {
//decoder.delete();
}
}
function scheduleData() {
if (fragments.length) {
var frag = fragments.shift();
decoder.addFragment(frag.ptr, frag.length);
if (fragments.length) {
setTimeout(scheduleData, 50);
}
if (!decoding) {
schedule();
}
}
}
function createFileFragments(buffer) {
var src = new Uint8Array(buffer);
var shoved = 0;
while (shoved !== src.length) {
var ps = Math.min(Math.floor(Math.random() * (maxPortion - minPortion) + minPortion), src.length - shoved);
var ptr = Module._malloc(ps);
var arr = new Uint8Array(Module.HEAPU8.buffer, ptr, ps);
var portion = new Uint8Array(buffer, shoved, ps)
arr.set(portion, 0);
shoved += ps;
fragments.push({
ptr: ptr,
length: ps
});
noize.push(Module._malloc(256));
}
}
document.getElementById("file").addEventListener("change", onChange, false);
</script>
</body>
</html>