// post-narration.mjs + post-item.mjs's narrated branch: the knobs, the plan // of a narrated post (the demo's timings), the mix's schedule and its // ffmpeg filters, the verbatim script, the validator. import assert from "node:assert/strict"; import { test } from "node:test"; import { POST_NARRATION_DEFAULTS, draftScript, dryGainAt, isNarrated, mixPhaseAt, narrationLevelDb, narrationSchedule, nextTakeNumber, pickedTake, speakShouts, postNarrationDisclosure, postNarrationFilters, postNarrationKnobs, readSpanSeconds, speakableText, takeFileOf, validatePostNarration, validatePostNarrationRender, wetGainAt, narrationRateOf, partAt, partStartOnItem, readSecondsOf, dropUrls, isOnlyUrls, joinSplitLinks, spokenUrls, } from "./post-narration.mjs"; import { POST_PICTURE_MIN, mediaTimeAt, postItemFiles, postItemPlan, postItemSeconds, postLayout, ruleSeconds, validatePostItem } from "./post-item.mjs"; import { postSegmentArgs } from "./post-render.mjs"; import { estimatedDuration } from "./deck.mjs"; const post = (o = {}) => ({ type: "post", id: "p-1", post: "someone-x/2117259673917386879", platform: "x", author: "MR. OBVIOUS", handle: "ObviousRises", date: "2026-11-02T14:16:57.000Z", text: "I'm tired of hearing about it. Do I need to make an example of her? I'm so irritated.", ...o, }); const video = (o = {}) => ({ src: "posts-media/x/1/1_1.mp4", kind: "video", seconds: 9.293, width: 1280, height: 720, audio: true, ...o }); const image = (o = {}) => ({ src: "posts-media/x/1/1_1.jpg", kind: "image", width: 1200, height: 900, ...o }); const narr = (seconds, o = {}) => ({ script: "words", takes: [{ file: "narration/posts/2117259673917386879/take-1.orpheus-zac.wav", engine: "orpheus", voice: "zac", seconds }], pick: 0, ...o }); /** The demo's knobs (narr-demo/mix.sh): the whole clip, 0.4 s clean after the ramp. */ const DEMO = { postNarration: { clip: "full", linger: 1.2 } }; test("the defaults are the demo's numbers, and a post's own knobs win over the cut's", () => { assert.deepEqual( { lead: POST_NARRATION_DEFAULTS.lead, tail: POST_NARRATION_DEFAULTS.tail, ramp: POST_NARRATION_DEFAULTS.ramp, wet: POST_NARRATION_DEFAULTS.wet, lowpassHz: POST_NARRATION_DEFAULTS.lowpassHz }, { lead: 0.4, tail: 0.3, ramp: 0.8, wet: 0.3, lowpassHz: 650 }, ); assert.equal(POST_NARRATION_DEFAULTS.engine, "orpheus"); assert.equal(POST_NARRATION_DEFAULTS.voice, "zac"); assert.equal(POST_NARRATION_DEFAULTS.short, "hold"); const e = post({ narration: narr(10, { wet: 0.2 }) }); const k = postNarrationKnobs(e, { postNarration: { wet: 0.5, lead: 1 } }); assert.equal(k.wet, 0.2); assert.equal(k.lead, 1); assert.equal(k.attach, "under"); }); test("which take plays: the picked one, else the first; off or none is not narrated", () => { const two = narr(5); two.takes.push({ file: "narration/posts/2117259673917386879/take-2.orpheus-zac.wav", seconds: 7 }); assert.equal(pickedTake(post({ narration: { ...two, pick: 1 } })).seconds, 7); assert.equal(pickedTake(post({ narration: { ...two, pick: undefined } })).seconds, 5); assert.equal(isNarrated(post({ narration: { ...two, off: true } })), false); assert.equal(isNarrated(post({ narration: { script: "x", takes: [] } })), false); assert.equal(isNarrated(post()), false); assert.equal(nextTakeNumber(post({ narration: two }), ["take-7.kokoro-af_heart.wav"]), 8); assert.equal(takeFileOf("123", 2, "orpheus", "zac"), "narration/posts/123/take-2.orpheus-zac.wav"); }); test("THE DEMO's TIMINGS: with clip full and linger 1.2 the plans are mix.sh's segments, the video at once", () => { // narr-demo: 30.03 s video under a 13.71 s read; 9.293 s video under 18.42 s (held); 17.926 s under 11.58 s. const a = postItemPlan(post({ media: [video({ seconds: 30.03 })], narration: narr(13.71) }), DEMO); assert.equal(a.total, 30.03); assert.equal(a.reading, 0); assert.equal(a.steps[0].at, 0); const b = postItemPlan(post({ media: [video()], narration: narr(18.42) }), DEMO); assert.equal(b.total, 20.32); assert.equal(b.held, 11.027); assert.deepEqual([b.narration.at, b.narration.voiceEnd, b.narration.rampEnd], [0.4, 19.12, 19.92]); const c = postItemPlan(post({ media: [video({ seconds: 17.926 })], narration: narr(11.58) }), DEMO); assert.equal(c.total, 17.926); }); test("NO LINGERING (the default): the item ends linger after the words; a longer video is cut to the read", () => { const e = post({ media: [video({ seconds: 30.03 })], narration: narr(13.71) }); const p = postItemPlan(e); assert.equal(readSpanSeconds(e), 16.21); assert.equal(p.total, 16.21); assert.equal(p.steps[0].seconds, 16.21); assert.equal(p.steps[0].srcFrom, 0); // Its in point (or posts fit's best window) is where it plays from; an out point set is kept as the choice. const w = postItemPlan(post({ media: [video({ seconds: 30.03, start: 12, end: 28.2 })], narration: narr(13.71) })); assert.deepEqual([w.steps[0].srcFrom, w.steps[0].seconds, w.total], [12, 16.2, 16.21]); assert.equal(postItemSeconds(e), 16.21); assert.equal(estimatedDuration(e, {}), 16.21); // A per-post linger. assert.equal(postItemPlan(post({ media: [video({ seconds: 30.03 })], narration: narr(13.71, { linger: 4 }) })).total, 18.41); }); test("a short video holds its last frame, or loops (short: loop)", () => { const held = postItemPlan(post({ media: [video()], narration: narr(18.42) })); assert.equal(held.total, 20.92); assert.ok(held.held > 11); const looped = postItemPlan(post({ media: [video()], narration: narr(18.42, { short: "loop" }) })); assert.equal(looped.held, 0); assert.equal(looped.steps[0].loop, true); assert.equal(looped.steps[0].seconds, 20.92); // On its clock it comes round again. assert.equal(mediaTimeAt(looped.steps[0], 10), 0.707); }); test("PICTURES RUN WITH THE READ: shared out over its span, each at least the minimum, not 3.5 s after it", () => { const one = post({ media: [image()], narration: narr(6) }); const un = post({ media: [image()] }); assert.ok(ruleSeconds(un) > 6.4); const p = postItemPlan(one); assert.equal(p.total, 8.5); assert.deepEqual(p.steps.map((s) => [s.at, s.seconds]), [[0, 8.5]]); const four = postItemPlan(post({ media: [image(), image(), image(), image()], narration: narr(2) })); assert.equal(four.steps.length, 4); for (const s of four.steps) assert.equal(s.seconds, POST_PICTURE_MIN); assert.equal(four.total, 4 * POST_PICTURE_MIN); // A text-only post: the read and the linger. assert.equal(postItemPlan(post({ narration: narr(5) })).total, 7.5); }); test("the words stay on screen: a post with attachments is laid out beside them, narrated or not", () => { const long = "word ".repeat(220).trim(); const box = { width: 1574, height: 886 }; assert.equal(postLayout(post({ text: long, media: [image()] }), box).mode, "side"); assert.equal(postLayout(post({ text: long, media: [image()], narration: narr(30) }), box).mode, "side"); }); test("attach after: the read over the card, then the attachments clean", () => { const p = postItemPlan(post({ media: [video()], narration: narr(5, { attach: "after" }) })); assert.equal(p.reading, 5.7); assert.equal(p.steps[0].at, 5.7); assert.equal(p.total, 14.993); assert.equal(p.narration.duck, false); }); test("the mix's schedule: muffled under the read, a linear ramp back, clean after", () => { const sch = narrationSchedule(post({ media: [video()], narration: narr(10) })); assert.deepEqual([sch.at, sch.voiceEnd, sch.rampEnd], [0.4, 10.7, 11.5]); assert.equal(wetGainAt(sch, 1), 0.3); assert.equal(dryGainAt(sch, 1), 0); assert.equal(mixPhaseAt(sch, 1), "muffled"); assert.ok(Math.abs(wetGainAt(sch, 11.1) - 0.15) < 1e-9); assert.ok(Math.abs(dryGainAt(sch, 11.1) - 0.5) < 1e-9); assert.equal(mixPhaseAt(sch, 11.1), "ramp"); assert.equal(wetGainAt(sch, 12), 0); assert.equal(dryGainAt(sch, 12), 1); assert.equal(mixPhaseAt(sch, 12), "clean"); const flat = narrationSchedule(post({ media: [video()], narration: narr(10, { duck: false }) })); assert.equal(dryGainAt(flat, 1), 1); assert.equal(mixPhaseAt(flat, 1), "clean"); }); test("THE MIX's FILTERS are mix.sh's chain: lowpass 650 twice at 0.30, the ramp, the narrator delayed by the lead", () => { const sch = narrationSchedule(post({ media: [video()], narration: narr(18.42) })); const f = postNarrationFilters({ media: "pm", voice: "5:a", out: "a", sch, gainDb: 0.3 }); assert.deepEqual(f, [ "[5:a]aresample=48000,aformat=sample_fmts=fltp:channel_layouts=stereo,volume=0.3dB,acompressor=threshold=0.1:ratio=3:attack=5:release=120,volume=1.5,adelay=400:all=1[pnv]", "[pm]asplit=2[pnd][pnw]", "[pnw]lowpass=f=650,lowpass=f=650,volume='if(lt(t,19.12),0.3,if(lt(t,19.12+0.8),0.3*(1-(t-19.12)/0.8),0))':eval=frame[pnwet]", "[pnd]volume='if(lt(t,19.12),0,if(lt(t,19.12+0.8),(t-19.12)/0.8,1))':eval=frame[pndry]", "[pnwet][pndry][pnv]amix=inputs=3:normalize=0:duration=first,alimiter=limit=0.95[a]", ]); assert.equal(narrationLevelDb(-21.3, -21), 0.3); assert.equal(narrationLevelDb(null, -21), 0); assert.equal(narrationLevelDb(-50, -21), 12); }); test("THE READ's RATE (operator, 3 Nov: \"read everything the slightest bit faster\"): the voice atempo'd, every take second divided by it", () => { assert.equal(POST_NARRATION_DEFAULTS.rate, 1); const media = [video({ seconds: 30.03 })]; const slow = post({ media, narration: narr(13.5) }); const cutFast = { postNarration: { rate: 1.08 } }; // The cut's rate, a post's own over it, the default 1. assert.equal(narrationRateOf(slow), 1); assert.equal(narrationRateOf(slow, cutFast), 1.08); assert.equal(narrationRateOf(post({ media, narration: narr(13.5, { rate: 1.25 }) }), cutFast), 1.25); assert.equal(readSecondsOf(13.5, 1.08), 12.5); // The item's length shrinks by the read's (lead + read/rate + tail + linger), the attachment cut to it. assert.equal(readSpanSeconds(slow), 16); assert.equal(readSpanSeconds(slow, cutFast), 15); const p1 = postItemPlan(slow); const p2 = postItemPlan(slow, cutFast); assert.equal(p1.total, 16); assert.equal(p2.total, 15); assert.equal(p2.steps[0].seconds, 15); assert.equal(postItemSeconds(slow, cutFast), 15); // An out point `posts fit` set (its recorded window) was sized to the read: it follows a faster read. const fitted = post({ media: [video({ seconds: 30.03, start: 4, end: 20 })], narration: narr(13.5, { window: { media: 0, start: 4, end: 20, score: 1, reason: "fit" } }) }); assert.equal(postItemPlan(fitted).total, 16); assert.equal(postItemPlan(fitted, cutFast).total, 15); assert.equal(postItemPlan(fitted, cutFast).steps[0].srcFrom, 4); // One the operator set by hand (no window, or another one) is the choice: kept. const byHand = post({ media: [video({ seconds: 30.03, start: 4, end: 24 })], narration: narr(13.5, { window: { media: 0, start: 4, end: 20, score: 1, reason: "fit" } }) }); assert.equal(postItemPlan(byHand, cutFast).total, 20); // The muffle's schedule on the item's clock: the read ends sooner, the take's own length kept beside it. const sch = p2.narration; assert.deepEqual([sch.at, sch.seconds, sch.takeSeconds, sch.rate, sch.voiceEnd, sch.rampEnd], [0.4, 12.5, 13.5, 1.08, 13.2, 14]); // A take in parts: each part's start on the item's clock is its start in the take over the rate. const parts = [{ n: 1, start: 0, seconds: 5 }, { n: 2, start: 5.4, seconds: 8.1 }]; const e = post({ media, narration: { ...narr(13.5), takes: [{ ...narr(13.5).takes[0], parts }] } }); const s2 = narrationSchedule(e, cutFast); assert.equal(partStartOnItem(s2, parts[1]), 5.4); assert.equal(partAt(e, s2, 5.3).n, 1); assert.equal(partAt(e, s2, 5.4).n, 2); assert.equal(partStartOnItem(narrationSchedule(e), parts[1]), 5.8); // The filter graph: atempo on the VOICE only (the pitch kept); at 1 the chain is as it was. const f = postNarrationFilters({ media: "pm", voice: "5:a", out: "a", sch }); assert.match(f[0], /^\[5:a\]aresample=48000,atempo=1\.08,aformat=/); assert.ok(f.slice(1).every((x) => !x.includes("atempo"))); assert.doesNotMatch(postNarrationFilters({ media: "pm", voice: "5:a", out: "a", sch: p1.narration })[0], /atempo/); // Held to 0.8-1.5, by the validator of the cut's and of a post's. assert.deepEqual(validatePostNarrationRender({ postNarration: { rate: 1.08 } }), []); assert.match(validatePostNarrationRender({ postNarration: { rate: 2 } }).join(), /rate must be 0\.8–1\.5/); assert.match(validatePostNarration(post({ narration: narr(5, { rate: 0.5 }) })).join(), /narration\.rate must be 0\.8–1\.5/); }); test("the segment's argv carries the take as an input and the mix after the item's own sound", () => { const e = post({ media: [video()], narration: narr(18.42) }); const plan = postItemPlan(e); const box = { width: 1574, height: 886 }; const argv = postSegmentArgs({ entry: e, box, plan, layout: postLayout(e, box), cards: { read: "/c/read.png" }, files: ["/p/v.mp4"], render: { fps: 30, audioRate: 48000 }, outPath: "/o.mp4", narration: { file: "/p/take.wav", gainDb: 0 }, }); const fc = argv[argv.indexOf("-filter_complex") + 1]; assert.ok(argv.includes("/p/take.wav")); assert.match(fc, /amix=inputs=2:normalize=0:duration=first\[pm\]/); assert.match(fc, /\[pnwet\]\[pndry\]\[pnv\]amix=inputs=3/); assert.equal(argv[argv.indexOf("-t", argv.indexOf("-map")) + 1], String(plan.total)); // Un-narrated: as it was. const bare = postSegmentArgs({ entry: post({ media: [video()] }), box, plan: postItemPlan(post({ media: [video()] })), layout: postLayout(post({ media: [video()] }), box), cards: { read: "/c/read.png" }, files: ["/p/v.mp4"], render: { fps: 30 }, outPath: "/o.mp4" }); assert.doesNotMatch(bare[bare.indexOf("-filter_complex") + 1], /pnv|lowpass/); }); test("SCRIPTS ARE THE POST, VERBATIM: cleaned for pronunciation only, never an excerpt", () => { assert.equal(speakableText("@KorriiNami Trust me & you w/ my plan(s) šŸ˜‚ https://t.co/x\n\nI hate the weak"), "Trust me and you with my plans.\n\nI hate the weak"); // his "this/that" is read "this slash that"; a URL's slashes are gone before it assert.equal(speakableText("muscle/fitness and sites/forums/orgs https://x.com/a/b"), "muscle slash fitness and sites slash forums slash orgs"); // shouted words are said (lowercased) and the sentence ends "!"; acronyms are left spelled assert.equal(speakShouts("MR. OBVIOUS IS RIGHT AGAIN. I carry the FBI. You are NOT unarmed. That is BS."), "Mister obvious is right again! I carry the FBI. You are not unarmed! That is BS."); assert.equal(speakShouts(" I'M NOT GONNA STOP."), " I'm not gonna stop!"); assert.equal(speakShouts("the most MISERABLE people, what kind of life?"), "the most miserable people, what kind of life?!"); assert.equal(speakableText("one\ntwo\n\nthree"), "one. two.\n\nthree"); const long = Array.from({ length: 150 }, (_, i) => `w${i}`).join(" ") + ". The end."; const d = draftScript(long); assert.equal(d.words, 152); assert.ok(d.script.endsWith("The end.")); assert.equal(draftScript("RT @someone: hello there").script, "hello there"); // A reply's leading @handles go; a handle inside the sentence is his words and stays (operator, 5 Nov: "@grok don't you agree"). assert.equal(speakableText("@grok @JustinInYT1 Its just advice, its good advice @grok don't you agree?"), "Its just advice, its good advice @grok don't you agree?"); assert.equal(speakableText("@a, @b_c: hi there"), "hi there"); assert.equal(speakableText("mail me at a@b.com"), "mail me at a@b.com"); }); test("LINKS ARE NEVER SPOKEN: X's link split over lines is put back together and dropped whole -- its \"…\" line too; a spelled-out link is still found", () => { const q = "THIS IS THE FINDING OUT STAGE STRAWB.\n\nhttps://\nweb.archive.org/web/2021032015\n1733/https://twitter.com/someone/status/1373102987070017537\n…\n\nhttps://\nweb.archive.org/web/2021032004\n0213/https://twitter.com/other/status/1373122620334477314\n…"; assert.equal(joinSplitLinks(q), "THIS IS THE FINDING OUT STAGE STRAWB.\n\nhttps://web.archive.org/web/20210320151733/https://twitter.com/someone/status/1373102987070017537\n\nhttps://web.archive.org/web/20210320040213/https://twitter.com/other/status/1373122620334477314"); assert.equal(speakableText(q), "this is the finding out stage strawb!"); assert.deepEqual(spokenUrls(speakableText(q)), []); // www., a bare host with a path, X's t.co: gone; "e.g." and a name with a dot are words. assert.equal(dropUrls("go to www.foo.com/bar, t.co/abc or kiwifarms.net/threads/1 e.g. Mr.Obvious").replace(/\s+/g, " "), "go to or e.g. Mr.Obvious"); // A script that already says one (the old draft's): found, and a part that is nothing else is whole. const said = "https://. web.archive.org slash web slash 2021032015. 1733/. …"; assert.deepEqual(spokenUrls(said), ["https://.", "web.archive.org/web/2021032015."]); assert.equal(isOnlyUrls(said), true); assert.equal(isOnlyUrls("read this https://x.com/a/b before you go"), false); assert.equal(isOnlyUrls("no link here at all"), false); assert.deepEqual(spokenUrls("I said it twice, e.g. here and there."), []); }); test("the validator: shapes, tags, takes, the pick, the cut's knobs; the build's files", () => { assert.deepEqual(validatePostNarration(post({ narration: narr(5) })), []); const bad = validatePostNarration(post({ narration: { script: "hi ", takes: [{ file: "/abs.wav", seconds: -1 }], pick: 3, wet: 2, short: "freeze", bogus: 1 } })); assert.ok(bad.some((e) => e.includes(" is not an expressive tag"))); assert.ok(bad.some((e) => e.includes("takes[0].file must stay inside the project"))); assert.ok(bad.some((e) => e.includes("takes[0].seconds"))); assert.ok(bad.some((e) => e.includes(".pick must be the index"))); assert.ok(bad.some((e) => e.includes(".wet must be 0–1"))); assert.ok(bad.some((e) => e.includes(".short must be hold or loop"))); assert.ok(bad.some((e) => e.includes(".bogus is not a narration key"))); assert.ok(validatePostItem(post({ narration: { script: 3 } })).some((e) => e.includes("narration.script"))); assert.deepEqual(validatePostNarrationRender({ postNarration: { wet: 0.2, linger: 2, clip: "full" } }), []); assert.ok(validatePostNarrationRender({ postNarration: { attach: "after" } }).some((e) => e.includes("attach is not a knob"))); assert.deepEqual(postItemFiles(post({ narration: narr(5) })).map((f) => f.src), ["narration/posts/2117259673917386879/take-1.orpheus-zac.wav"]); assert.deepEqual(postItemFiles(post({ narration: narr(5, { off: true }) })), []); assert.equal(postNarrationDisclosure(post({ narration: narr(5) })), "Narration: synthetic voice (Orpheus Ā· zac)"); });