// postPause.mjs: PAUSES IN AN APPROVED READ, no TTS -- the spec, the word found // (case and edge punctuation aside, #n its n-th time, a target's words only), // the cut just after it, everything after moved on (words, parts, the fx that // read them), the re-cut (absolute), the refusals; the file (ffmpeg, its // length the source's plus the pauses) picked in ONE write, undoable; the CLI. import assert from "node:assert/strict"; import { execFileSync } from "node:child_process"; import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { after, test } from "node:test"; import { validatePostItem } from "umtool-report-to-video/post-item"; import { wordZoomTimes } from "umtool-report-to-video/word-zoom"; import { PauseError, cutAfter, cutWindow, parsePauseSpec, pauseArgs, pausePlan, pauseTake } from "./postPause.mjs"; import { RereadError } from "./postReread.mjs"; import { writeWav16 } from "./audioSplice.mjs"; import { postsCommand } from "./postsCli.mjs"; import { undoStructural } from "./manifest.mjs"; const ROOT = await mkdtemp(path.join(tmpdir(), "umtool-post-pause-")); after(() => rm(ROOT, { recursive: true, force: true })); const REPORTS = path.join(ROOT, "reports"); // "I'll give you 45 seconds. 20, 10, 4, 0. Alright." in two parts; then "Who are you? 4 more." const TEXT = "I'll give you 45 seconds. 20, 10, 4, 0. Alright.\n\nWho are you? 4 more."; const words = () => [ { w: "I'll", start: 0.1, end: 0.3 }, { w: "give", start: 0.35, end: 0.55 }, { w: "you", start: 0.6, end: 0.7 }, { w: "45", start: 0.75, end: 1.2 }, { w: "seconds.", start: 1.25, end: 1.7 }, { w: "20,", start: 2.0, end: 2.3 }, { w: "10,", start: 2.4, end: 2.6 }, { w: "4,", start: 2.62, end: 2.8 }, { w: "0.", start: 3.1, end: 3.4 }, { w: "Alright.", start: 3.6, end: 4.0 }, { w: "Who", start: 4.6, end: 4.8 }, { w: "are", start: 4.85, end: 5.0 }, { w: "you?", start: 5.05, end: 5.3 }, { w: "4", start: 5.5, end: 5.7 }, { w: "more.", start: 5.75, end: 6.1 }, ]; const parts = () => [ { n: 1, para: 0, text: "I'll give you 45 seconds. 20, 10, 4, 0. Alright.", start: 0, seconds: 4.2, file: "narration/parts-cache/a.wav", role: "post" }, { n: 2, para: 1, text: "Who are you? 4 more.", start: 4.5, seconds: 1.8, role: "post" }, ]; const item = (o = {}) => ({ type: "post", id: "p-1", post: "subj-x/1", platform: "x", author: "THE SUBJECT", handle: "Subject", date: "2026-10-30T10:00:00.000Z", text: TEXT, narration: { script: TEXT, takes: [{ file: "narration/posts/1/take-2.orpheus-thread.wav", engine: "orpheus", voice: "leo+zac", seconds: 6.4, slurs: "said", words: words(), parts: parts() }], pick: 0 }, ...o, }); test("THE SPEC: words and their seconds, #n, a phrase; 0 takes one away; the refusals are usage errors", () => { assert.deepEqual(parsePauseSpec("20=0.45, 10=0.45,4#2=0.6"), [{ match: "20", occurrence: 1, seconds: 0.45 }, { match: "10", occurrence: 1, seconds: 0.45 }, { match: "4", occurrence: 2, seconds: 0.6 }]); assert.deepEqual(parsePauseSpec("who are you=1"), [{ match: "who are you", occurrence: 1, seconds: 1 }]); assert.deepEqual(parsePauseSpec("20=0"), [{ match: "20", occurrence: 1, seconds: 0 }]); for (const bad of ["", "20", "=0.4", "20=4", "20=0.01", "20=x", "20#0=0.4", "...=0.4"]) assert.throws(() => parsePauseSpec(bad), RereadError, bad); }); test("THE WORD: case and edge punctuation aside, #n its n-th time, a phrase pauses after its last word; the cut half way into the quiet, at most 0.1 s", () => { const p = pausePlan(item(), { pauses: parsePauseSpec("20=0.45,10=0.45,4=0.45") }); assert.deepEqual(p.pauses.map((x) => [x.word, x.w, x.at, x.seconds]), [[5, "20,", 2.35, 0.45], [6, "10,", 2.61, 0.45], [7, "4,", 2.9, 0.45]]); // "4" first is "4," of the countdown; #2 is the second part's. assert.equal(pausePlan(item(), { pauses: parsePauseSpec("4#2=0.5") }).pauses[0].word, 13); assert.equal(pausePlan(item(), { pauses: parsePauseSpec("WHO ARE YOU=0.5") }).pauses[0].word, 12); // The cut: never into the next word (overlapping timings: at the end); the last word: 0.1 s past it, inside the take. assert.equal(cutAfter([{ w: "a", start: 0, end: 1 }, { w: "b", start: 0.9, end: 2 }], 0), 1); assert.equal(cutAfter([{ w: "a", start: 0, end: 1 }], 0, 1.05), 1.05); assert.equal(cutAfter([{ w: "a", start: 0, end: 1 }, { w: "b", start: 1.5, end: 2 }], 0), 1.1); }); test("WHAT MOVES: every word after a pause by the pauses before it, a part after it moved, a part it falls in grown (its own file dropped); the zooms read the words", () => { const p = pausePlan(item(), { pauses: parsePauseSpec("20=0.45,4=0.3") }); assert.equal(p.added, 0.75); assert.equal(p.seconds, 7.15); assert.deepEqual(p.words.slice(4, 9).map((w) => [w.w, w.start, w.end]), [["seconds.", 1.25, 1.7], ["20,", 2, 2.3], ["10,", 2.85, 3.05], ["4,", 3.07, 3.25], ["0.", 3.85, 4.15]]); assert.deepEqual(p.words.at(-1), { w: "more.", start: 6.5, end: 6.85 }); assert.equal(p.words.length, words().length, "one per script word still (the beeps find theirs)"); assert.deepEqual(p.parts.map((x) => [x.n, x.start, x.seconds, x.file ?? null, x.role]), [[1, 0, 4.95, null, "post"], [2, 5.25, 1.8, null, "post"]]); assert.deepEqual(p.spans, [{ from: 0, to: 2.35, gap: 0 }, { from: 2.35, to: 2.9, gap: 0.45 }, { from: 2.9, to: null, gap: 0.3 }]); assert.deepEqual(p.steps.map((s) => [s.op, s.pauses.map((x) => [x.word, x.seconds])]), [["pause", [[5, 0.45], [7, 0.3]]]]); // A word zoom on "0" is timed by the take's words: on the new take it lands 0.75 s later. const e = item({ fx: [{ kind: "word-zoom", match: "0" }] }); const before = wordZoomTimes(e)[0].t; const t = { file: "narration/posts/1/take-3.pause-orpheus.wav", engine: "derived", voice: "leo+zac", seconds: p.seconds, source: { kind: "take", from: "take-2.orpheus-thread" }, steps: p.steps, words: p.words, parts: p.parts, slurs: "said" }; const e2 = { ...e, narration: { ...e.narration, takes: [...e.narration.takes, t], pick: 1 } }; assert.deepEqual(validatePostItem(e2), []); assert.ok(Math.abs(wordZoomTimes(e2)[0].t - before - 0.75) < 0.01, `zoom ${before} -> ${wordZoomTimes(e2)[0].t}`); // An effect with a set second is named, not moved. assert.equal(pausePlan(item({ fx: [{ kind: "word-zoom", match: "0", at: 3 }] }), { pauses: parsePauseSpec("20=0.45") }).fixedAt, 1); // The validator: a pause step's record. const bad = { ...e2, narration: { ...e2.narration, takes: [e2.narration.takes[0], { ...t, steps: [{ op: "pause", pauses: [{ word: -1, at: 1, seconds: 9 }] }] }] } }; assert.equal(validatePostItem(bad).filter((x) => /pauses/.test(x)).length, 1); }); test("A RE-CUT: a pause on a paused take is cut from the take before it, the pauses it had kept, a word named again takes the new seconds, =0 takes one away", () => { const e = item(); const p1 = pausePlan(e, { pauses: parsePauseSpec("20=0.45,10=0.45") }); const t1 = { file: "narration/posts/1/take-3.pause-orpheus.wav", engine: "derived", voice: "leo+zac", seconds: p1.seconds, source: { kind: "take", from: "take-2.orpheus-thread" }, steps: p1.steps, words: p1.words, parts: p1.parts }; const e2 = { ...e, narration: { ...e.narration, takes: [...e.narration.takes, t1], pick: 1 } }; const p2 = pausePlan(e2, { pauses: parsePauseSpec("10=0.8,4=0.3") }); assert.equal(p2.recut, true); assert.equal(p2.take.file, "narration/posts/1/take-2.orpheus-thread.wav", "cut from the first read"); assert.deepEqual(p2.pauses.map((x) => [x.w, x.seconds]), [["20,", 0.45], ["10,", 0.8], ["4,", 0.3]]); assert.equal(p2.seconds, 7.95); assert.deepEqual(pausePlan(e2, { pauses: parsePauseSpec("20=0") }).pauses.map((x) => x.w), ["10,"]); assert.throws(() => pausePlan(e2, { pauses: parsePauseSpec("20=0.45") }), /already: nothing to change/); assert.throws(() => pausePlan(e2, { pauses: parsePauseSpec("20=0,10=0") }), /takes every pause away -- pick the take it was cut from \(take-2\.orpheus-thread\)/); }); test("THE REFUSALS (exit 1): no word timings names posts narrate --words; a word not read; an occurrence not there; a target it does not read", () => { const nw = item(); delete nw.narration.takes[0].words; assert.throws(() => pausePlan(nw, { pauses: parsePauseSpec("20=0.4") }), (err) => err instanceof PauseError && /no word timings.*umtool posts narrate

p-1 --words/.test(err.message)); assert.throws(() => pausePlan(item(), { pauses: parsePauseSpec("twenty=0.4") }), (err) => err instanceof PauseError && /no such word/.test(err.message)); assert.throws(() => pausePlan(item(), { pauses: parsePauseSpec("4#3=0.4") }), /is in p-1's read 2 times/); assert.throws(() => pausePlan(item(), { pauses: parsePauseSpec("20=0.4"), target: "quoted" }), /reads no quoted/); assert.throws(() => pausePlan(item(), { pauses: parsePauseSpec("20=0.4"), target: "nope" }), RereadError); // A target counts only its parts' words: parts re-roled, "4" in the second part is its first. const e = item(); e.narration.takes[0].parts[1].role = "quoted"; assert.equal(pausePlan(e, { pauses: parsePauseSpec("4=0.4"), target: "quoted" }).pauses[0].word, 13); }); test("THE ffmpeg: the source cut sample-exact at each pause, the silence between, the last span to the end; 10 ms fades either side of the silence", () => { const p = pausePlan(item(), { pauses: parsePauseSpec("20=0.45") }); const fc = pauseArgs("/in.wav", "/o.wav", p, { rate: 24000, layout: "mono" })[8]; assert.equal(fc, "[0:a]aformat=sample_rates=24000:channel_layouts=mono,atrim=start_sample=0:end_sample=56400,asetpts=PTS-STARTPTS,afade=t=out:ss=56160:ns=240[a0];anullsrc=r=24000:cl=mono,atrim=end_sample=10800[g1];[0:a]aformat=sample_rates=24000:channel_layouts=mono,atrim=start_sample=56400,asetpts=PTS-STARTPTS,afade=t=in:ss=0:ns=240[a1];[a0][g1][a1]concat=n=3:v=0:a=1[out]"); }); test("THE QUIET CUT: the window a word allows (40 ms back from its end, up to the next word or 0.1 s; ±40 ms when they run together); on a take whose quiet is off the timing, the cut moves there", async () => { const w = [{ w: "a", start: 0, end: 1 }, { w: "b", start: 1.5, end: 2 }, { w: "c", start: 2, end: 2.08 }, { w: "d", start: 2.08, end: 2.2 }]; assert.deepEqual(cutWindow(w, 0), { lo: 0.96, hi: 1.1 }); assert.deepEqual(cutWindow(w, 2), { lo: 2.04, hi: 2.12 }, "run together: about the end, never before the word's middle"); // A synthetic read: tone everywhere but 30 ms of near-silence at 2.27-2.30 (the timing says 2.35 is "20,"'s cut: 2.30 + 0.05). const p = await project("quiet", [item()]); const f = path.join(p.dir, "narration", "posts", "1", "take-2.orpheus-thread.wav"); const sig = new Float32Array(Math.round(6.4 * 24000)).map((_, i) => (i / 24000 >= 2.27 && i / 24000 < 2.3 ? 0.0001 : 0.2) * Math.sin((2 * Math.PI * 220 * i) / 24000)); await writeWav16(f, sig, 24000, 1); const e = (await manifest(p)).timeline[0]; const r = await pauseTake(p, e, { pauses: parsePauseSpec("20=0.45,10=0.45"), dryRun: true }); assert.ok(Math.abs(r.plan.pauses[0].at - 2.285) <= 0.006, `cut moved into the quiet: ${r.plan.pauses[0].at}`); assert.equal(r.plan.pauses[1].at, 2.61, "a uniform tone: the cut stays at its timing"); }); async function project(name, timeline) { const dir = path.join(REPORTS, name); await mkdir(path.join(dir, "narration", "posts", "1"), { recursive: true }); execFileSync("ffmpeg", ["-nostdin", "-loglevel", "error", "-y", "-f", "lavfi", "-i", "sine=frequency=220:duration=6.4:sample_rate=24000", path.join(dir, "narration", "posts", "1", "take-2.orpheus-thread.wav")]); await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify({ schemaVersion: 1, slug: name, title: name, provenance: { siteOrigin: "" }, render: { width: 1920, height: 1080, fps: 30 }, timeline }, null, 2)); return { id: name, dir }; } const manifest = async (p) => JSON.parse(await readFile(path.join(p.dir, "video.manifest.json"), "utf8")); test("PAUSED: the file the source's length plus the pauses, a derived take (source, steps, words moved, slurs kept) picked in ONE write, undoable", async () => { const p = await project("pause", [item()]); const e = (await manifest(p)).timeline[0]; const r = await pauseTake(p, e, { pauses: parsePauseSpec("20=0.45,10=0.45,4=0.45") }); assert.match(r.take.file, /^narration\/posts\/1\/take-3\.pause-orpheus\.wav$/); assert.ok(Math.abs(r.take.seconds - 7.75) < 0.02, `measured ${r.take.seconds}`); const now = (await manifest(p)).timeline[0]; assert.equal(now.narration.pick, 1); const t = now.narration.takes[1]; assert.deepEqual([t.engine, t.source, t.steps[0].op, t.slurs, t.words[8].start], ["derived", { kind: "take", from: "take-2.orpheus-thread" }, "pause", "said", 4.45]); assert.deepEqual(validatePostItem(now), []); await undoStructural(p.dir); assert.equal((await manifest(p)).timeline[0].narration.takes.length, 1, "undone"); }); test("THE CLI: posts reread --pause --dry-run writes nothing, --json the plan; applied, the take picked; a word not read is exit 1, a bad spec exit 2", async () => { const p = await project("cli", [item()]); const cli = async (args) => { const lines = []; const errs = []; const code = await postsCommand(args, { root: REPORTS, out: (s) => lines.push(s), err: (s) => errs.push(s) }); return { code, out: lines.join("\n"), err: errs.join("\n") }; }; const dry = await cli(["reread", "cli", "p-1", "--pause", "20=0.45,10=0.45", "--dry-run"]); assert.equal(dry.code, 0, dry.err); assert.match(dry.out, /would pause p-1's take \(take-2\.orpheus-thread\): 7\.30 s \(was 6\.40 s, \+0\.9 s of pauses\)/); assert.match(dry.out, /after "20," \(word 6, after 2\.35 s\) \+0\.45 s/); assert.equal((await manifest(p)).timeline[0].narration.takes.length, 1, "a dry run writes nothing"); const js = await cli(["reread", "cli", "p-1", "--pause", "20=0.45", "--dry-run", "--json"]); const doc = JSON.parse(js.out); assert.deepEqual([doc.ok, doc.dryRun, doc.pauses.length, doc.added, doc.take], [true, true, 1, 0.45, null]); const go = await cli(["reread", "cli", "p-1", "--pause", "20=0.45"]); assert.equal(go.code, 0, go.err); assert.match(go.out, /take-3\.pause-orpheus\.wav -- picked/); assert.equal((await manifest(p)).timeline[0].narration.pick, 1); assert.equal((await cli(["reread", "cli", "p-1", "--pause", "twenty=0.45"])).code, 1); assert.equal((await cli(["reread", "cli", "p-1", "--pause", "20=9"])).code, 2); assert.equal((await cli(["reread", "cli", "p-1", "--pause", "20=0.4", "--drop-urls"])).code, 2, "a pause goes alone"); }); test("--pause @: a pause at a time of the take -- the last word starting before it, its cut pinned there (a read whose word timings drift)", () => { assert.deepEqual(parsePauseSpec("@71.2=0.45, 20=0.4"), [{ at: 71.2, seconds: 0.45 }, { match: "20", occurrence: 1, seconds: 0.4 }]); assert.throws(() => parsePauseSpec("@x=0.4"), RereadError); assert.throws(() => parsePauseSpec("@71.2=9"), RereadError); const words = [{ w: "45", start: 0, end: 0.5 }, { w: "seconds!", start: 0.5, end: 0.9 }, { w: "just", start: 2.0, end: 2.2 }, { w: "past", start: 2.2, end: 2.5 }]; const entry = { id: "p-1", type: "post", narration: { pick: 0, script: "45 seconds! just past", takes: [{ file: "narration/posts/1/take-1.x.wav", engine: "x", voice: "v", seconds: 3, words, parts: [{ n: 1, start: 0, seconds: 3, text: "45 seconds! just past" }] }] } }; const plan = pausePlan(entry, { pauses: parsePauseSpec("@1.6=0.5") }); assert.equal(plan.pauses.length, 1); assert.equal(plan.pauses[0].word, 1); // "seconds!" is the last word starting before 1.6 assert.equal(plan.pauses[0].at, 1.6); // the cut is the time given, not the word's timed end (0.9) assert.equal(plan.pauses[0].pinned, true); assert.equal(plan.seconds, 3.5); assert.equal(plan.words[2].start, 2.5); // later words move by the pause assert.throws(() => pausePlan(entry, { pauses: parsePauseSpec("@0=0.5") }), RereadError); });