/* * node tools/survey.js file.wav [...] - per-recording quality figures without * a reference image: lock %, median sync, and noise = mean within-line std of * the Space A / Space B words (flat in a clean signal) over the sync contrast. */ 'use strict'; const fs = require('fs'); const path = require('path'); const APT = require(path.join(__dirname, '..', 'apt.js')); const Wav = require(path.join(__dirname, '..', 'wav.js')); function Std(w, a, b) { let s = 0, s2 = 0; for (let k = a; k < b; k++) { s += w[k]; s2 += w[k] * w[k]; } const n = b - a, m = s / n; return Math.sqrt(Math.max(0, s2 / n - m * m)); } function Survey(file, opts) { const w = Wav.Parse(fs.readFileSync(file)); const lines = []; const dec = new APT.Decoder(w.rate, Object.assign({ onLine: (l) => lines.push(l) }, opts || {})); for (let i = 0; i < w.data.length; i += 8192) dec.process(w.data.subarray(i, i + 8192)); dec.flush(); let locked = 0, nz = [], sy = []; for (const l of lines) { sy.push(l.sync); if (!l.locked || l.sync < 0.3) continue; locked++; const c = l.hi - l.lo; if (c <= 0) continue; nz.push(0.5 * (Std(l.words, 42, 84) + Std(l.words, 1082, 1124)) / c); } const med = (a) => { if (!a.length) return NaN; a.sort((x, y) => x - y); return a[a.length >> 1]; }; return { file: path.basename(file), rate: w.rate, lines: lines.length, lock: locked / Math.max(1, lines.length), sync: med(sy), noise: med(nz) }; } module.exports = Survey; if (require.main === module) for (const f of process.argv.slice(2)) { try { const r = Survey(f); console.log(`${r.noise.toFixed(4)}\t${r.sync.toFixed(3)}\t${(r.lock * 100).toFixed(0)}%\t${r.lines}\t${r.rate}\t${r.file}`); } catch (e) { console.log(`ERR\t${path.basename(f)}\t${e.message}`); } }