jsmad/index.html

105 lines
2.7 KiB
HTML
Raw Normal View History

2018-12-27 15:29:06 +00:00
<!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>
2019-01-14 02:19:37 +00:00
var minPortion = 20480;
var maxPortion = 40960;
var time = 0;
var ctx = new AudioContext();
ctx.suspend();
var decoder;
2019-01-14 02:19:37 +00:00
var fragments = [];
var noize = [];
var decoding = false;
2018-12-27 15:29:06 +00:00
function onChange(e) {
var file = e.target.files[0];
var reader = new FileReader();
reader.onload = function(e) {
decoder = new Module.Decoder();
2019-01-14 02:19:37 +00:00
createFileFragments(reader.result);
scheduleData();
2019-01-14 14:50:41 +00:00
setTimeout(.bind(ctx), 1000);
}
reader.readAsArrayBuffer(file);
}
function schedule() {
2019-01-14 02:19:37 +00:00
decoding = false;
if (decoder.hasMore()) {
2019-01-14 02:19:37 +00:00
var audio = decoder.decode(20);
if (audio) {
2018-12-29 12:39:49 +00:00
var source = ctx.createBufferSource();
source.buffer = audio;
source.connect(ctx.destination);
source.start(time);
time += audio.duration;
setTimeout(schedule, Math.floor(audio.duration * 200));
2019-01-14 02:19:37 +00:00
decoding = true;
} else {
2019-01-14 02:19:37 +00:00
//decoder.delete();
2018-12-29 12:39:49 +00:00
}
} else {
2019-01-14 02:19:37 +00:00
//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();
}
2018-12-27 15:29:06 +00:00
}
}
2019-01-14 02:19:37 +00:00
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));
}
}
2018-12-27 15:29:06 +00:00
document.getElementById("file").addEventListener("change", onChange, false);
</script>
</body>
</html>