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-22 21:36:50 +00:00
|
|
|
var minPortion = 60000;
|
|
|
|
var maxPortion = 100000;
|
2018-12-29 18:08:22 +00:00
|
|
|
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) {
|
2018-12-29 18:08:22 +00:00
|
|
|
decoder = new Module.Decoder();
|
2019-01-14 02:19:37 +00:00
|
|
|
createFileFragments(reader.result);
|
|
|
|
|
|
|
|
scheduleData();
|
2019-01-14 14:50:41 +00:00
|
|
|
|
2019-01-18 01:50:48 +00:00
|
|
|
setTimeout(ctx.resume.bind(ctx), 1000);
|
2018-12-29 18:08:22 +00:00
|
|
|
}
|
|
|
|
reader.readAsArrayBuffer(file);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function schedule() {
|
2019-01-14 02:19:37 +00:00
|
|
|
decoding = false;
|
2018-12-29 18:08:22 +00:00
|
|
|
if (decoder.hasMore()) {
|
2019-01-14 02:19:37 +00:00
|
|
|
var audio = decoder.decode(20);
|
2018-12-29 18:08:22 +00:00
|
|
|
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;
|
2018-12-29 18:08:22 +00:00
|
|
|
|
|
|
|
setTimeout(schedule, Math.floor(audio.duration * 200));
|
2019-01-14 02:19:37 +00:00
|
|
|
decoding = true;
|
2018-12-29 18:08:22 +00:00
|
|
|
} else {
|
2019-01-14 02:19:37 +00:00
|
|
|
//decoder.delete();
|
2018-12-29 12:39:49 +00:00
|
|
|
}
|
2018-12-29 18:08:22 +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
|
|
|
}
|
|
|
|
}
|
2018-12-29 18:08:22 +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>
|