// Sanity checks for the analysis, run with `npm test`. import { analyze, detectSegment, DEFAULT_SETTINGS, energyBand, bandLevels, bandpass, smoothLog } from "../src/analysis/analyze.ts"; import { syntheticTear } from "../src/audio/synthetic.ts"; import { decodeWav, encodeWavFloat32 } from "../src/audio/wav.ts"; let failed = 0; function check(name: string, ok: boolean, detail = "") { console.log(`${ok ? "ok " : "FAIL"} ${name}${detail ? " " + detail : ""}`); if (!ok) failed++; } const fs = 48000; // Parseval: white noise of known power, full band { const x = new Float32Array(fs * 2); let s = 7; for (let i = 0; i < x.length; i++) { s = (s * 1103515245 + 12345) & 0x7fffffff; x[i] = (s / 0x7fffffff - 0.5) * 0.2; } let e = 0; for (const v of x) e += v * v; e /= fs; const a = analyze(x, fs, { start: 0, end: 2 }, { ...DEFAULT_SETTINGS, energyLow: 0, energyHigh: fs / 2 }); check("Parseval (energy of white noise)", Math.abs(a.energyRaw / e - 1) < 0.02, `${a.energyRaw.toExponential(3)} vs ${e.toExponential(3)}`); } // Band levels: white noise in 15–20 kHz carries 5/24 of the power { const x = new Float32Array(fs); let s = 11; for (let i = 0; i < x.length; i++) { s = (s * 1103515245 + 12345) & 0x7fffffff; x[i] = (s / 0x7fffffff - 0.5) * 0.2; } let p = 0; for (const v of x) p += v * v; p /= x.length; const lv = bandLevels(x, fs, { lo: 15000, hi: 20000 }); const mid = lv.power.slice(10, -10); const mean = mid.reduce((a, v) => a + v, 0) / mid.length; check("band level of white noise", Math.abs(mean / (p * 5 / 24) - 1) < 0.05, `${(mean / p).toFixed(4)} vs ${(5 / 24).toFixed(4)}`); } // Band-pass: a 15 kHz tone passes, a 1 kHz tone is removed { const n = 48000; const tone = (f: number) => Float32Array.from({ length: n }, (_, i) => Math.sin((2 * Math.PI * f * i) / fs)); const rms = (v: Float32Array) => Math.sqrt(v.slice(4800, -4800).reduce((a, x) => a + x * x, 0) / (v.length - 9600)); const pass = rms(bandpass(tone(15000), fs, { lo: 10000, hi: 20000 })) / rms(tone(15000)); const stop = rms(bandpass(tone(1000), fs, { lo: 10000, hi: 20000 })) / rms(tone(1000)); check("band-pass keeps 15 kHz, removes 1 kHz", Math.abs(pass - 1) < 0.01 && stop < 1e-3, `pass ${pass.toFixed(4)}, stop ${stop.toExponential(1)}`); } // Synthetic tears: same clicks at different durations const results: { T: number; energyDb: number; shapeDb: number[] }[] = []; for (const T of [0.3, 1, 3, 10]) { const x = syntheticTear({ tearSeconds: T, seed: 3 }); const seg = detectSegment(x, fs, energyBand(DEFAULT_SETTINGS)); check(`detect segment T=${T}`, !!seg && Math.abs(seg.start - 1) < 0.08 && Math.abs(seg.end - (1 + T)) < 0.08, seg ? `${seg.start.toFixed(3)}–${seg.end.toFixed(3)}` : "none"); if (!seg) continue; const a = analyze(x, fs, seg, DEFAULT_SETTINGS); const shape = smoothLog(a.freqs, a.netEsd.map((v) => v / a.energy), 1000, 20000, 1 / 3).v.map((v) => 10 * Math.log10(v)); results.push({ T, energyDb: 10 * Math.log10(a.energy), shapeDb: shape }); console.log(` T=${T}s energy=${(10 * Math.log10(a.energy)).toFixed(2)} dB snr=${a.snrDb.toFixed(1)} dB peak=${a.peakDbfs.toFixed(1)} dBFS`); } const es = results.map((r) => r.energyDb); check("synthetic energy independent of duration (range < 1 dB)", Math.max(...es) - Math.min(...es) < 1); let worst = 0; for (const r of results) r.shapeDb.forEach((v, i) => (worst = Math.max(worst, Math.abs(v - results[0].shapeDb[i])))); check("synthetic spectrum shape independent of duration (1/3 octave, 1–20 kHz, within 1.5 dB)", worst < 1.5, `worst ${worst.toFixed(2)} dB`); // WAV round trip { const x = syntheticTear({ tearSeconds: 0.5 }); const blob = encodeWavFloat32(x, fs); const d = decodeWav(await blob.arrayBuffer()); check("WAV float32 round trip", !!d && d.sampleRate === fs && d.samples.length === x.length && d.samples.every((v, i) => v === x[i])); } if (failed) { console.log(`${failed} check(s) failed`); throw new Error("checks failed"); }