// SYNTHETIC DAMAGED AUDIO (release 30, lane Q) — a tone-burst recording, the // same recording with a deterministic run of its bytes overwritten (the way a // source that serves malformed AAC mid-stream damages a file), and the readers // a test needs to say WHERE the bursts are in a decoded WAV. // // The bursts are the clock: a 0.3 s 1 kHz tone every `every` seconds from // `first`, silence between. A decode that keeps the source's timeline has every // surviving burst at its source time; one that closes up the damage has every // burst after it early by the damage's length. // // Needs ffmpeg on PATH (the tests that use it skip without it). import { execFileSync } from "node:child_process"; import { readFileSync, writeFileSync } from "node:fs"; export const TONE_RATE = 44100; export function hasFfmpeg() { try { execFileSync("ffmpeg", ["-version"], { stdio: "ignore" }); execFileSync("ffprobe", ["-version"], { stdio: "ignore" }); return true; } catch { return false; } } // Where the bursts start, in seconds. export function burstTimes({ duration = 30, first = 2, every = 4 } = {}) { const out = []; for (let t = first; t < duration; t += every) out.push(t); return out; } // An AAC-in-MP4 tone-burst recording at `file`. // `faststart` (release 32 R7) puts the MP4's index at the START, as a web // player's file has it — a growing partial of it decodes up to where it ends. export function makeToneFile(file, { duration = 30, first = 2, every = 4, faststart = false } = {}) { const expr = `if(gte(t,${first})*lt(mod(t-${first},${every}),0.3),0.5*sin(2*PI*1000*t),0)`; execFileSync("ffmpeg", [ "-nostdin", "-v", "error", "-y", "-f", "lavfi", "-i", `aevalsrc='${expr}':s=${TONE_RATE}:d=${duration}`, "-c:a", "aac", "-b:a", "64k", ...(faststart ? ["-movflags", "+faststart"] : []), file, ]); return file; } // `src` copied to `out` with the bytes from `fromFrac` to `toFrac` of its // length overwritten by a seeded LCG — the same bytes every run. With the // MP4's index at the END (ffmpeg's default, no faststart) the range falls in // the sample data: the frames there fail to decode, their timestamps survive. export function corruptBytes(src, out, { fromFrac = 0.4, toFrac = 0.55, seed = 12345 } = {}) { const buf = readFileSync(src); let s = seed; const a = Math.floor(buf.length * fromFrac); const z = Math.floor(buf.length * toFrac); for (let k = a; k < z; k += 1) { s = (s * 1103515245 + 12345) & 0x7fffffff; buf[k] = s & 0xff; } writeFileSync(out, buf); return out; } // A 16-bit mono PCM WAV's samples and rate, read off its `fmt ` and `data` // chunks (ffmpeg writes a LIST chunk too, so the header is not 44 bytes). export function readWavMono16(file) { const b = readFileSync(file); let rate = 16000; let off = 12; while (off + 8 <= b.length) { const id = b.toString("ascii", off, off + 4); let size = b.readUInt32LE(off + 4); if (id === "fmt ") rate = b.readUInt32LE(off + 12); if (id === "data") { // A streamed WAV may carry a 0 or 0xFFFFFFFF size: read to the end. if (size === 0 || off + 8 + size > b.length) size = b.length - off - 8; const n = Math.floor(size / 2); const samples = new Int16Array(n); for (let i = 0; i < n; i += 1) samples[i] = b.readInt16LE(off + 8 + i * 2); return { rate, samples, seconds: n / rate }; } off += 8 + size + (size % 2); } throw new Error(`no data chunk in ${file}`); } // Burst onsets (seconds) in decoded samples: the first loud sample after at // least `quietSec` of quiet. AAC's own start-up click is not loud enough. export function toneOnsets(samples, rate, { threshold = 3000, quietSec = 0.5 } = {}) { const out = []; const quiet = Math.round(quietSec * rate); let lastLoud = -Infinity; for (let i = 0; i < samples.length; i += 1) { if (Math.abs(samples[i]) <= threshold) continue; if (i - lastLoud > quiet) out.push(Math.round((i / rate) * 1000) / 1000); lastLoud = i; } return out; }