Archilyzer · Source

archilyzer

Archilyzer
git clone https://archilyzer.pages.dev/source/archilyzer.git
Log | Files | Refs | README | LICENSE

commit 9d831764b194a57a074a10b4c6f01e18da0a5049
parent 1e287a181b5705c5a1be62c0374fa58b0a87280d
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date:   Sat, 21 Nov 2026 19:13:44 -0500

common: the clock test expects the toolchain's own mp3 lag (lame 4.0 + ffmpeg 9.1 decode 33 ms late)

The fixture assumed a gapless mp3. On this toolchain lame 4.0's tag says an
encoder delay of 47 samples where the stream has 576 (ffmpeg: "pad 47",
skip 576), so the decoded audio.mp3 lags its input by 529 samples, 33 ms at
16 kHz, while the AAC mp4 decodes exactly. The clock measured 2.467 for the
pair, which is right: it maps the files as decoded, as the transcriber hears
them. A test fault, not a code fault. The fixture now finds each file's
decoded lag to the sample, independently of the code under test, and the
expected offsets include it; the sample-exact tolerance is unchanged.

Co-Authored-By: Claude <noreply@anthropic.com>

Diffstat:
Mcommon/lib/recordingClock-server.test.ts | 43+++++++++++++++++++++++++++++++++++++------
1 file changed, 37 insertions(+), 6 deletions(-)

diff --git a/common/lib/recordingClock-server.test.ts b/common/lib/recordingClock-server.test.ts @@ -60,6 +60,31 @@ async function encode(samples: Float32Array, out: string, codec: string[]) { await rm(raw); } +// WHERE THE ENCODER PUT THE START (seconds the decoded file lags its input). +// Not always zero: lame 4.0's gapless tag says an encoder delay of 47 samples +// where the stream has 576, so ffmpeg (9.1) decodes its mp3 529 samples — +// 33 ms at 16 kHz — late. The clock maps the files AS DECODED (what the +// transcriber hears), so the offsets it must find are the spliced ones plus +// the toolchain's lags: found here to the sample, independently of the code +// under test, by matching a decoded second against the input. +async function decodedLag(file: string, input: Float32Array): Promise<number> { + const r = await execa(FFMPEG, ["-v", "error", "-i", file, "-t", "3", "-ac", "1", "-ar", String(SR), "-f", "f32le", "pipe:1"], { + encoding: "buffer", + }); + const buf = Buffer.from(r.stdout as Uint8Array); + const dec = new Float32Array(Math.floor(buf.length / 4)); + for (let i = 0; i < dec.length; i += 1) dec[i] = buf.readFloatLE(i * 4); + const MAX = Math.round(0.2 * SR); + let best = 0; + let bestScore = -Infinity; + for (let lag = -MAX; lag <= MAX; lag += 1) { + let dot = 0; + for (let k = MAX; k < MAX + SR; k += 1) dot += input[k] * (dec[k + lag] ?? 0); + if (dot > bestScore) [bestScore, best] = [dot, lag]; + } + return best / SR; +} + async function fixture() { const root = await mkdtemp(path.join(os.tmpdir(), "clock-")); dirs.push(root); @@ -81,13 +106,17 @@ async function fixture() { path.join(videoDir, "saved-video.json"), JSON.stringify({ storedAt: "2026-11-06T00:00:00.000Z", dir: store, file: "source-media.mp4", bytes }), ); - return { videoDir, container: path.join(store, "source-media.mp4") }; + // A span's offset is its splice offset plus (target lag - source lag). + const lag = (await decodedLag(path.join(store, "source-media.mp4"), tgt)) - (await decodedLag(path.join(videoDir, "audio.mp3"), src)); + return { videoDir, container: path.join(store, "source-media.mp4"), lag }; } const OPTS = { sampleRate: 4000, probeSeconds: 10, searchSeconds: 20, everySeconds: 60, minGapSeconds: 10 }; test("a step and an offset, measured from an mp3 against an AAC mp4, written as clock.json", async () => { - const { videoDir, container } = await fixture(); + const { videoDir, container, lag } = await fixture(); + const before = 2.5 + lag; + const afterSplice = 13.1 + lag; assert.equal((await planRecordingClock(videoDir)).kind, "measure"); const logs: string[] = []; const r = await ensureRecordingClock({ @@ -104,8 +133,9 @@ test("a step and an offset, measured from an mp3 against an AAC mp4, written as assert.equal(clock.segments.length, 2, JSON.stringify(clock.segments)); // Exact to the sample: the source decoded from its start, the target seeked // with a pre-roll (an input seek alone lands an AAC excerpt 10–55 ms off). - assert.ok(Math.abs(clock.segments[0].offset - 2.5) < 0.002, `before ${clock.segments[0].offset}`); - assert.ok(Math.abs(clock.segments[1].offset - 13.1) < 0.002, `after ${clock.segments[1].offset}`); + // The toolchain's lag is part of what the clock must find (decodedLag). + assert.ok(Math.abs(clock.segments[0].offset - before) < 0.002, `before ${clock.segments[0].offset} (expected ${before})`); + assert.ok(Math.abs(clock.segments[1].offset - afterSplice) < 0.002, `after ${clock.segments[1].offset} (expected ${afterSplice})`); assert.ok(Math.abs(clock.segments[1].offset - clock.segments[0].offset - 10.6) < 0.002); assert.ok(clock.gaps[0].from < 200 && clock.gaps[0].to > 200, JSON.stringify(clock.gaps)); assert.ok((clock.residual.rms ?? 1) < 0.005); @@ -117,8 +147,9 @@ test("a step and an offset, measured from an mp3 against an AAC mp4, written as const again = await ensureRecordingClock({ ffmpegBin: FFMPEG, ffprobeBin: FFPROBE, videoDir, options: OPTS }); assert.equal(again.measured, false); assert.equal(again.plan, "current"); - // The map: a span before the splice +2.5, after it +13.1, across it uncertain. - assert.ok(Math.abs(clockOffsetAt(clock, 100).offset - 2.5) < 0.002); + // The map: a span before the splice +2.5, after it +13.1 (each plus the + // toolchain's lag), across it uncertain. + assert.ok(Math.abs(clockOffsetAt(clock, 100).offset - before) < 0.002); assert.equal(mapSpan(clock, 300, 310).certain, true); assert.equal(mapSpan(clock, 190, 210).certain, false); });