libbytebeat/parse.js

76 lines
1.7 KiB
JavaScript

// ONLY FOR TESTING //
// UNSAFE FOR UNTRUSTED INPUT //
debug = (v, ...arg) => (console.log(v, ...arg),v);
var fs = require('fs');
var parseBB = eval(fs.readFileSync(__dirname + "/src/bblib.js")+'');
var child = require('child_process');
var { pipeline } = require('node:stream/promises');
let def = parseBB(fs.readFileSync(process.argv[2])+'', console.error);
let chanc = def.chanc;
if(!def.__matched)
throw new Error('bad meta');
console.log(def);
let mpv;
if (1) {
mpv = child.spawn(`mpv`, [
`--demuxer=rawaudio`,
`--demuxer-rawaudio-format=float${['be','le'][new Uint8Array(new Uint16Array([1]).buffer)[0]]}`,
`--demuxer-rawaudio-rate=${def.freq}`,
`--demuxer-rawaudio-channels=${(def.chanc)}`,
`-`
], {
stdio: [
'pipe',
'inherit',
'inherit'
]
});
} else {
mpv = child.spawn(`cat`, [], {
stdio: [
'pipe',
'inherit',
'inherit'
]
});
}
let t = 0,
scount = Math.floor(def.freq);
let tc = 0;
let shif=0;
pipeline(async function *() {
let buf, x;
meow: while (1) {
buf = Buffer.alloc(4 * scount * chanc);
let arr = new Float32Array(buf.buffer);
let tbeg = performance.now();
let nt;
let rbuf = def.read(arr, t, nt=Math.min(def.len, t+scount)-t)
def.postproc(arr, t, nt, rbuf);
t += nt;
tc += performance.now() - tbeg;
if((tc-shif) > 1000) {
shif+=1000;
console.error();
console.error(`decoding... ${Math.round(t/def.freq*10)/10}s in ${Math.round(tc/1000*10)/10}s, ${Math.round(t/(tc/1000))}Hz`);
}
if (t >= def.len) {
x = (nt)*8*scount*chanc;
break;
}
yield buf;
}
yield buf.slice(0, x);
console.error();
console.error(`Decoded everything in ${Math.round(tc/1000*10)/10}s, ${Math.round(def.len/(tc/1000))}Hz`);
}, mpv.stdin)