// postReread.mjs: A READ WITHOUT A PIECE, RE-JOINED -- the picked take's kept // parts cut from its own file and joined with the read's pauses (no TTS); the // plan (spans, the parts re-timed with their roles, the words moved, the // guard), the file (ffmpeg, measured), the one write (picked, undoable), and // set-item's re-join: a piece drawn context that the take reads is re-joined // first; one it does not read is the knob alone (idempotent). 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 { threadReadErrors } from "umtool-report-to-video/post-thread"; import { piecesHidden, rejoinArgs, rereadPlan, rereadTake } from "./postReread.mjs"; import { postsCommand } from "./postsCli.mjs"; import { undoStructural } from "./manifest.mjs"; const ROOT = await mkdtemp(path.join(tmpdir(), "umtool-post-reread-")); after(() => rm(ROOT, { recursive: true, force: true })); const REPORTS = path.join(ROOT, "reports"); const PARENT = "The first thing anyone notices is the noise from the street.\n\nThen the lights in the hall go out without warning."; const REPLY = "That is exactly what I was saying yesterday morning."; const parts = () => [ { n: 1, para: 0, text: "The first thing anyone notices is the noise from the street.", start: 0, seconds: 3.6, file: "narration/parts-cache/a.wav" }, { n: 2, para: 1, text: "Then the lights in the hall go out without warning.", start: 4.2, seconds: 3.2 }, { n: 3, para: 100, text: REPLY, start: 8.2, seconds: 3.1 }, ]; const words = () => [ { w: "The", start: 0.1, end: 0.3 }, { w: "Then", start: 4.3, end: 4.6 }, { w: "That", start: 8.3, end: 8.5 }, { w: "morning", start: 10.9, end: 11.2 }, ]; 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: REPLY, parents: [{ id: "900", author: "Neighbour", handle: "neighbour_one", date: "2026-10-29T10:00:00.000Z", text: PARENT }], narration: { script: REPLY, takes: [{ file: "narration/posts/1/take-2.orpheus-thread.wav", engine: "orpheus", voice: "leo+zac", seconds: 11.3, slurs: "said", words: words(), parts: parts() }], pick: 0 }, ...o, }); test("THE PLAN: the parts reading the post it answers dropped; the rest re-timed with the read's pauses, their roles stored, the words moved with them", () => { const p = rereadPlan(item(), { without: ["parent:0"] }); assert.deepEqual(p.drop.map((d) => [d.k, d.why]), [[0, "parent:0"], [1, "parent:0"]]); assert.deepEqual(p.keep, [2]); assert.deepEqual(p.spans, [{ k: 2, from: 8.2, to: 11.3, at: 0, gap: 0 }]); assert.deepEqual(p.parts, [{ n: 1, para: 100, text: REPLY, start: 0, seconds: 3.1, role: "post" }]); assert.deepEqual(p.words, [{ w: "That", start: 0.1, end: 0.3 }, { w: "morning", start: 2.7, end: 3 }]); assert.equal(p.script, REPLY); assert.equal(p.seconds, 3.1); assert.deepEqual(p.steps.map((s) => [s.op, s.drop]), [["rejoin", ["parent:0"]]]); // A part by number: the pauses of a read (a paragraph's 0.6 s between pieces). const q = rereadPlan(item(), { dropParts: [2] }); assert.deepEqual(q.spans.map((s) => [s.k, s.at, s.gap]), [[0, 0, 0], [2, 4.2, 0.6]]); assert.equal(q.parts[0].file, "narration/parts-cache/a.wav", "a part's own file kept"); // Links: only the parts that are nothing but one. const linked = item({ narration: { ...item().narration, takes: [{ ...item().narration.takes[0], parts: [...parts().slice(0, 2), { n: 3, para: 2, text: "https://. web.archive.org slash web slash 2021032015. 1733/. …", start: 8.2, seconds: 2 }, { ...parts()[2], n: 4, start: 10.8 }] }] } }); assert.deepEqual(rereadPlan(linked, { dropUrls: true }).drop.map((d) => [d.k, d.why]), [[2, "url"]]); }); test("the plan's refusals: nothing to drop (the take does not read it), every part dropped, a take read in one go", () => { assert.throws(() => rereadPlan(item(), { without: ["quoted"] }), /reads none of that \(quoted\): nothing to drop/); assert.throws(() => rereadPlan(item(), { dropParts: [1, 2, 3] }), /drops every part/); assert.throws(() => rereadPlan(item(), { without: ["nonsense"] }), /a piece is parent:0, parent:1 or quoted/); const oneGo = item({ narration: { script: REPLY, takes: [{ file: "narration/posts/1/take-1.orpheus-zac.wav", seconds: 3 }], pick: 0 } }); assert.throws(() => rereadPlan(oneGo, { without: ["parent:0"] }), /read in one go/); // set-item's pieces: only what the take reads, as context or hidden. assert.deepEqual(piecesHidden(item(), { parent: ["context", "auto"] }), ["parent:0"]); assert.deepEqual(piecesHidden(item(), { parent: "full" }), []); assert.deepEqual(piecesHidden(item(), { quoted: "hidden" }), []); }); test("the ffmpeg arguments: each kept span trimmed, the pauses silence, joined in order", () => { const p = rereadPlan(item(), { dropParts: [2] }); const argv = rejoinArgs("/in.wav", "/out.wav", p, { rate: 24000, layout: "mono" }); const fc = argv[argv.indexOf("-filter_complex") + 1]; assert.match(fc, /\[0:a\]atrim=start=0:end=3\.6,asetpts=PTS-STARTPTS,aformat=sample_rates=24000:channel_layouts=mono\[a0\]/); assert.match(fc, /anullsrc=r=24000:cl=mono,atrim=duration=0\.6\[g1\]/); assert.match(fc, /\[a0\]\[g1\]\[a1\]concat=n=3:v=0:a=1\[out\]/); }); async function project(name, timeline) { const dir = path.join(REPORTS, name); await mkdir(path.join(dir, "narration", "posts", "1"), { recursive: true }); // The take: 11.3 s of tone, as a read's file stands in for one. execFileSync("ffmpeg", ["-nostdin", "-loglevel", "error", "-y", "-f", "lavfi", "-i", "sine=frequency=220:duration=11.3: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("RE-JOINED: the file cut and joined (its length the kept parts'), a derived take (source, steps) picked in ONE write, undoable; the guard is satisfied once the parent is context", async () => { const p = await project("rejoin", [item()]); const e = (await manifest(p)).timeline[0]; const r = await rereadTake(p, e, { without: ["parent:0"] }); assert.match(r.take.file, /^narration\/posts\/1\/take-3\.rejoin-orpheus\.wav$/); assert.ok(Math.abs(r.take.seconds - 3.1) < 0.06, `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.steps[0].drop, t.slurs], ["derived", { kind: "take", from: "take-2.orpheus-thread" }, "rejoin", ["parent:0"], "said"]); assert.deepEqual(t.parts.map((x) => x.role), ["post"]); assert.deepEqual(validatePostItem(now), []); assert.deepEqual(threadReadErrors({ ...now, thread: { parent: "context" } }), [], "the parent drawn as context, read by no part"); await undoStructural(p.dir); assert.equal((await manifest(p)).timeline[0].narration.takes.length, 1, "undone"); }); test("set-item thread.parent.0=context on a piece the take reads RE-JOINS first (no TTS), then sets the knob; again: the knob alone; --no-reread: refused", async () => { const p = await project("setitem", [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 refused = await cli(["set-item", "setitem", "p-1", "thread.parent.0=context", "--no-reread"]); assert.notEqual(refused.code, 0); assert.match(refused.err + refused.out, /drawn as context/); const r = await cli(["set-item", "setitem", "p-1", "thread.parent.0=context"]); assert.equal(r.code, 0, r.err); assert.match(r.out, /re-read p-1 without parent:0: narration\/posts\/1\/take-3\.rejoin-orpheus\.wav/); let e = (await manifest(p)).timeline[0]; assert.deepEqual([e.narration.takes.length, e.narration.pick, e.thread.parent[0]], [2, 1, "context"]); // Idempotent: the picked take reads no parent now -- the knob alone. const again = await cli(["set-item", "setitem", "p-1", "thread.parent.0=context"]); assert.equal(again.code, 0, again.err); assert.doesNotMatch(again.out, /re-read/); e = (await manifest(p)).timeline[0]; assert.equal(e.narration.takes.length, 2); // posts reread --dry-run: the plan only. const dry = await cli(["reread", "setitem", "p-1", "--drop-parts", "1", "--dry-run"]); assert.match(dry.err + dry.out, /only part|drops every part/); }); // ---- PER-PART TEMPO (operator, 4 Nov: "sped it up ... and then slowing back down here on the juicy persecution // complex paragraph"): the parts named stretched (pitch kept), the rest untouched, the words scaled with their part. import { parseTempoSpec, rereadCommand, tempoPlan, tempoRanges } from "./postReread.mjs"; test("THE TEMPO SPEC: runs and their tempo, one tempo for --parts, the refusals; runs back from per-part tempos", () => { assert.deepEqual(parseTempoSpec({ spec: "4-6=1.35,8=1.3", count: 9 }), { 4: 1.35, 5: 1.35, 6: 1.35, 8: 1.3 }); assert.deepEqual(parseTempoSpec({ tempo: "1.3", parts: "1-2,4", count: 4 }), { 1: 1.3, 2: 1.3, 4: 1.3 }); // --tempo-for wins over --tempo for a part both name. assert.deepEqual(parseTempoSpec({ tempo: 1.2, parts: "1-3", spec: "2=1.4", count: 3 }), { 1: 1.2, 2: 1.4, 3: 1.2 }); assert.throws(() => parseTempoSpec({ spec: "4-16=1.35", count: 10 }), /the take has parts 1-10/); assert.throws(() => parseTempoSpec({ spec: "1=1.7", count: 3 }), /a tempo is 1–1\.6/); assert.throws(() => parseTempoSpec({ spec: "1=0.9", count: 3 }), /a tempo is 1–1\.6/); assert.throws(() => parseTempoSpec({ tempo: 1.3, count: 3 }), /--tempo needs --parts/); assert.throws(() => parseTempoSpec({ count: 3 }), /name the parts/); assert.deepEqual(tempoRanges([1, 1.35, 1.35, 1, 1.3]), { "2-3": 1.35, 5: 1.3 }); }); test("THE TEMPO's PLAN: a stretched part shorter by its factor, the parts after it moved up, its words scaled; every part kept, the gaps the read's", () => { const p = tempoPlan(item(), { tempo: { 1: 1.5, 3: 1.2 } }); assert.deepEqual(p.spans.map((s) => [s.k, s.at, s.len, s.factor ?? 1]), [[0, 0, 2.4, 1.5], [1, 3, 3.2, 1], [2, 6.8, 2.583, 1.2]]); assert.deepEqual(p.parts.map((x) => [x.n, x.start, x.seconds, x.tempo ?? 1]), [[1, 0, 2.4, 1.5], [2, 3, 3.2, 1], [3, 6.8, 2.583, 1.2]]); // Words: in a stretched part scaled by its factor from the part's start; in an untouched one moved with it. assert.deepEqual(p.words, [{ w: "The", start: 0.067, end: 0.2 }, { w: "Then", start: 3.1, end: 3.4 }, { w: "That", start: 6.883, end: 7.05 }, { w: "morning", start: 9.05, end: 9.3 }]); assert.equal(p.seconds, 9.383); assert.deepEqual(p.steps.map((s) => [s.op, s.tempo, s.params]), [["tempo", { 1: 1.5, 3: 1.2 }, { stretch: "atempo" }]]); assert.throws(() => tempoPlan(item(), { tempo: { 1: 1 } }), /at that tempo already/); // The ffmpeg: atempo on the stretched part, padded and trimmed to its length (sample-exact); the untouched as before. const fc = rejoinArgs("/in.wav", "/o.wav", p, { rate: 24000, layout: "mono" })[8]; assert.match(fc, /\[0:a\]atrim=start=0:end=3\.6,asetpts=PTS-STARTPTS,aformat=sample_rates=24000:channel_layouts=mono,atempo=1\.5,apad,atrim=end_sample=57600,asetpts=PTS-STARTPTS\[a0\]/); assert.match(fc, /\[0:a\]atrim=start=4\.2:end=7\.4,asetpts=PTS-STARTPTS,aformat=sample_rates=24000:channel_layouts=mono\[a1\]/); assert.match(rejoinArgs("/in.wav", "/o.wav", { ...p, stretch: "rubberband" }, { rate: 24000 })[8], /rubberband=tempo=1\.5:transients=mixed:pitchq=quality/); }); test("A RE-TUNE: a tempo on a re-timed take is cut from the take before it (each part stretched once), the parts it does not name keeping theirs", () => { const e = item(); const p1 = tempoPlan(e, { tempo: { 1: 1.5, 3: 1.2 } }); const t1 = { file: "narration/posts/1/take-3.tempo-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 } }; assert.deepEqual(validatePostItem(e2), []); const p2 = tempoPlan(e2, { tempo: { 1: 1.25 } }); assert.equal(p2.take.file, "narration/posts/1/take-2.orpheus-thread.wav", "cut from the first read"); assert.deepEqual(p2.spans.map((s) => s.factor ?? 1), [1.25, 1, 1.2]); assert.deepEqual(p2.tempos, [1.25, 1, 1.2]); assert.deepEqual(p2.steps[0].tempo, { 1: 1.25, 3: 1.2 }); // The validator: a part's tempo inside 1-1.6, a step's runs. const bad = { ...e2, narration: { ...e2.narration, takes: [e2.narration.takes[0], { ...t1, parts: [{ ...t1.parts[0], tempo: 2 }], steps: [{ op: "tempo", tempo: { "1-x": 1.2 } }] }] } }; assert.deepEqual(validatePostItem(bad).filter((x) => /tempo/.test(x)).length, 2); }); test("RE-TIMED: the file's length the plan's (sample-exact), the derived take picked in ONE write with its parts' tempo and the words scaled; set-item narration.tempo= runs it", async () => { const p = await project("tempo", [item()]); const lines = []; assert.equal(await rereadCommand(p, "p-1", { tempoSpec: "1=1.5,3=1.2", out: (s) => lines.push(s) }), 0); assert.match(lines.join("\n"), /re-timed p-1's take \(atempo, pitch kept\): parts 1 at 1\.5x, 3 at 1\.2x -- 9\.38 s \(was 11\.30 s\)/); let e = (await manifest(p)).timeline[0]; const t = e.narration.takes[1]; assert.equal(e.narration.pick, 1); assert.match(t.file, /take-3\.tempo-orpheus\.wav$/); assert.ok(Math.abs(t.seconds - 9.383) < 0.01, `measured ${t.seconds}`); assert.deepEqual([t.source.from, t.steps[0].op, t.parts.map((x) => x.tempo ?? 1)], ["take-2.orpheus-thread", "tempo", [1.5, 1, 1.2]]); assert.deepEqual(validatePostItem(e), []); // set-item: a re-tune, cut from the first read again. const out = []; const code = await postsCommand(["set-item", "tempo", "p-1", "narration.tempo=1-3=1.1"], { root: REPORTS, out: (s) => out.push(s), err: (s) => out.push(s) }); assert.equal(code, 0, out.join("\n")); e = (await manifest(p)).timeline[0]; assert.deepEqual([e.narration.pick, e.narration.takes[2].source.from, e.narration.takes[2].parts.map((x) => x.tempo)], [2, "take-2.orpheus-thread", [1.1, 1.1, 1.1]]); await undoStructural(p.dir); assert.ok((await manifest(p)).timeline[0].narration.takes.length < 3, "undone"); });