// A CHARACTER's TUNING REACHES ITS READS (lane character-tuning, the wiring after S3): a single post's take // (makeTake), a thread read and a screen line (postThreadRead.mjs threadTake) are asked with the character's Orpheus // tuning when their line asks none of its own, and record `tuningAsked: "preset"`; a line's own tuning beats it // (`true`). THE PIN: a character WITHOUT a tuning asks, keys and records exactly as main did (5958688a0) -- the // hashes below were taken from main's code before a line of this wiring was written. No editor (a stub tts); // temp dirs; invented people. import assert from "node:assert/strict"; import { createHash } from "node:crypto"; 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 { encodeWav16 } from "yt-dlp-transcript-common/lib/montage/voiceMatch.mjs"; import { VOICE_B, syntheticVoice, withSilence } from "yt-dlp-transcript-common/testing/audio/syntheticVoice.mjs"; import { castPresetMap } from "umtool-report-to-video/cast"; import { makeTake } from "./postNarrationCli.mjs"; import { threadTake } from "./postThreadRead.mjs"; const ROOT = await mkdtemp(path.join(tmpdir(), "umtool-tuning-wiring-")); after(() => rm(ROOT, { recursive: true, force: true })); delete process.env.UMTOOL_CAST_FILE; const LIVELY = { temperature: 0.75, top_p: 0.92, repeat_penalty: 1.25 }; const wavOf = async (file, seconds = 0.6) => { await mkdir(path.dirname(file), { recursive: true }); await writeFile(file, encodeWav16(withSilence(syntheticVoice({ ...VOICE_B, seconds }), { before: 0.1, after: 0.1 }))); }; const stubAsk = (asked) => async (body) => { asked.push(body); await wavOf(body.out); return { jobId: `job-${asked.length}`, result: { wav: body.out, engine: body.engine, voice: body.voice, ...(body.engine === "orpheus" ? { tuning: { temperature: 0.55, top_p: 0.85, top_k: 50, min_p: 0, repeat_penalty: 1.2, takes: 3, ...(body.tuning ?? {}) }, reading: body.reading ?? 0 } : {}) }, }; }; const NO_RED = { terms: [], markup: {}, slurs: null }; const IMG = { src: "posts-media/ferry-x/7/7_1.png", kind: "image", width: 900, height: 1400 }; const ITEM = { type: "post", id: "p-7", post: "ferry-x/7", platform: "x", author: "Ferry Teller", handle: "invented_ferry", date: "2026-10-02T10:00:00.000Z", text: "Here is what they sent.", media: [IMG], narration: { script: "Here is what they sent." }, screens: [{ id: "s1", media: 0, kind: "dm", read: "all", lines: [{ who: "invented_river", text: "Are the tolls going up?" }, { who: "invented_ferry", text: "Not this year." }] }], }; /** The cast: the item's author (orpheus:tara) and the screen's other speaker (orpheus:leo), each with `tuning` when given. */ const castDoc = (ferry = null, river = null) => ({ format: "umtool-cast", version: 2, characters: [ { id: "ferry", name: "Ferry Teller", handles: ["invented_ferry"], synthetic: { voice: "orpheus:tara", ...(ferry ? { tuning: ferry } : {}) } }, { id: "river", name: "River Talk", handles: ["invented_river"], synthetic: { voice: "orpheus:leo", ...(river ? { tuning: river } : {}) } }, ], }); async function project(name, cast) { const root = path.join(ROOT, name); const dir = path.join(root, "p"); await mkdir(path.join(dir, "narration"), { recursive: true }); await mkdir(path.join(root, ".umtool"), { recursive: true }); await writeFile(path.join(root, ".umtool", "cast.json"), JSON.stringify(cast, null, 1)); await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify({ schemaVersion: 1, slug: name, title: name, provenance: { channelsDir: "../channels", channelSlug: "chan" }, render: { width: 1920, height: 1080, fps: 30, postNarration: { engine: "orpheus", voice: "zac" } }, timeline: [ITEM], }, null, 2)); return { id: name, dir }; } const read = async (p) => JSON.parse(await readFile(path.join(p.dir, "video.manifest.json"), "utf8")); const sha = (v) => createHash("sha1").update(JSON.stringify(v)).digest("hex"); /** A take as the pin compares it: everything but its time and job. */ const stable = (t) => { const { at: _a, jobId: _j, ...rest } = t; return { ...rest, ...(Array.isArray(rest.parts) ? { parts: rest.parts.map(({ at: _pa, jobId: _pj, ...p }) => p) } : {}) }; }; const bodies = (asked, dir) => asked.map((b) => ({ ...b, out: path.relative(dir, b.out) })); const layersOf = (cast) => ({ map: {}, cast: castPresetMap(cast), item: null, names: {} }); /** A single post's take of p-7, and its thread read with the screen (two of its pieces another character's). */ async function readsOf(name, cast, { tuning = null, tuned = {} } = {}) { const p = await project(name, cast); const asked = []; const one = await makeTake(p, (await read(p)).timeline[0], { ask: stubAsk(asked), redaction: NO_RED, ...(tuning ? { tuning } : {}) }); const single = asked.splice(0); const thread = await threadTake(p, (await read(p)).timeline[0], { red: NO_RED, ask: stubAsk(asked), lanes: null, ...layersOf(cast), tuned }); return { p, single, take: one.take, threadAsks: asked, thread }; } // Taken on main's code (5958688a0, merged as 8824428b1) before the wiring: a character with NO tuning. const MAIN = { single: "9f675482a1264083457304cfdb409dcdc35774db", take: "923bcf133ac164c200e898c18656c2dda2ae2075", threadAsks: "126f1f4da6c72d2605b0ea15cf6399938608cd83", threadTake: "6b3b40cf343afd894f348c40a672b3d67d190938", keys: "FAKE-TEST-VALUE", }; test("THE PIN: a character with no tuning -- the post's take, the thread read and its screen lines ask, key and record as main did", async () => { const r = await readsOf("pin", castDoc()); const got = { single: sha(bodies(r.single, r.p.dir)), take: sha(stable(r.take)), threadAsks: sha(bodies(r.threadAsks, r.p.dir)), threadTake: sha(stable(r.thread.take)), keys: sha(r.thread.parts.map((x) => x.key)), }; if (process.env.PIN_PRINT) console.log(JSON.stringify(got, null, 2)); assert.deepEqual(got, MAIN); }); const STEADY = { temperature: 0.4, top_p: 0.8 }; test("a post's take: the character's tuning asked and recorded as \"preset\"; the line's own beats it, field by field", async () => { const r = await readsOf("single", castDoc(LIVELY)); assert.deepEqual(r.single[0].tuning, LIVELY); assert.equal(r.take.tuningAsked, "preset"); assert.deepEqual(r.take.tuning, { temperature: 0.75, top_p: 0.92, top_k: 50, min_p: 0, repeat_penalty: 1.25, takes: 3 }); // The line's own sampler value: asked over the character's, recorded as its own. const p = await project("single-own", castDoc(LIVELY)); const asked = []; let t = (await makeTake(p, (await read(p)).timeline[0], { ask: stubAsk(asked), redaction: NO_RED, tuning: { temperature: 0.9 } })).take; assert.deepEqual(asked[0].tuning, { ...LIVELY, temperature: 0.9 }); assert.equal(t.tuningAsked, true); // Only its takes: the character's sampler kept, still the character's. t = (await makeTake(p, (await read(p)).timeline[0], { ask: stubAsk(asked), redaction: NO_RED, tuning: { takes: 5 } })).take; assert.deepEqual(asked[1].tuning, { ...LIVELY, takes: 5 }); assert.equal(t.tuningAsked, "preset"); // A Kokoro voice is asked none of the character's (no sampler); the take records none. const k = await project("single-kokoro", { ...castDoc(LIVELY), characters: [{ ...castDoc(LIVELY).characters[0], synthetic: { voice: "kokoro:af_heart", tuning: LIVELY } }, castDoc().characters[1]] }); const ka = []; t = (await makeTake(k, (await read(k)).timeline[0], { ask: stubAsk(ka), redaction: NO_RED })).take; assert.equal("tuning" in ka[0], false); assert.equal(t.tuningAsked, undefined); // The take validates (the writer refused nothing): it is the item's now. assert.equal((await read(p)).timeline[0].narration.takes.at(-1).tuningAsked, "preset"); }); test("a thread read and its screen lines: each piece asked its own character's tuning; the read's own beats it; read.tuned keeps only the read's", async () => { const r = await readsOf("thread", castDoc(LIVELY, STEADY)); // Post piece and the author's own line: Lively; the other speaker's line: Steady (repeat penalty the default's: not asked). const byText = Object.fromEntries(r.threadAsks.map((b) => [b.text, b.tuning])); assert.deepEqual(byText["Here is what they sent."], LIVELY); assert.deepEqual(byText["Not this year."], LIVELY); assert.deepEqual(byText["Are the tolls going up?"], STEADY); const parts = r.thread.take.parts; assert.ok(parts.every((x) => x.tuningAsked === "preset"), JSON.stringify(parts.map((x) => [x.role, x.tuningAsked]))); assert.equal(r.thread.take.read.tuned, undefined, "a character's tuning is not the read's: a later read asks the character's as it is then"); // The keys: a tuned part is another file than the untuned one. const pin = await readsOf("thread-pin", castDoc()); assert.notDeepEqual(r.thread.parts.map((x) => x.key), pin.thread.parts.map((x) => x.key)); // One line's own tuning (the Tune, --for): that piece's, asked over the character's and recorded as the read's. const p = await project("thread-own", castDoc(LIVELY, STEADY)); const asked = []; const own = await threadTake(p, (await read(p)).timeline[0], { red: NO_RED, ask: stubAsk(asked), lanes: null, ...layersOf(castDoc(LIVELY, STEADY)), tuned: { "screen:s1:0": { tuning: { temperature: 0.9 } } } }); const line = own.take.parts.find((x) => x.role === "screen:s1:0"); assert.deepEqual([line.tuning, line.tuningAsked], [{ ...STEADY, temperature: 0.9 }, true]); // read.tuned keeps the read's OWN values as asked (review 2 #9): a later read lays them over the character's then. assert.deepEqual(own.take.read.tuned["screen:s1:0"].tuning, { temperature: 0.9 }); assert.equal(own.take.parts.find((x) => x.role === "post").tuningAsked, "preset"); }); test("stale over real takes: the character's tuning changed -- its post take, its thread pieces and its screen line; the line's own never", async () => { const { tuningStale } = await import("umtool-report-to-video/cast"); const { resolverFor } = await import("./castProject.mjs"); const r = await readsOf("stale", castDoc(LIVELY, STEADY)); const m = await read(r.p); // The thread read is the playing take (picked last). The character as it was: nothing stale. assert.deepEqual(tuningStale(m, resolverFor({ cast: castDoc(LIVELY, STEADY) }), { settingTakes: 3 }), []); // Ferry is Steady now: his post piece and his own screen line are stale; the other speaker's line is not. const rows = tuningStale(m, resolverFor({ cast: castDoc(STEADY, STEADY) }), { settingTakes: 3 }); assert.deepEqual(rows.map((x) => [x.role, x.character]).sort(), [["post", "ferry"], ["screen:s1:1", "ferry"]]); // The single post take (not playing) is not counted: only what plays is. const m2 = { ...m, timeline: m.timeline.map((e) => ({ ...e, narration: { ...e.narration, pick: 0 } })) }; assert.deepEqual(tuningStale(m2, resolverFor({ cast: castDoc(STEADY, STEADY) }), { settingTakes: 3 }).map((x) => x.role), ["post"]); // A line asked its own tuning: never stale, whatever the character does. const p = await project("stale-own", castDoc(LIVELY, STEADY)); await threadTake(p, (await read(p)).timeline[0], { red: NO_RED, ask: stubAsk([]), lanes: null, ...layersOf(castDoc(LIVELY, STEADY)), tuned: { "screen:s1:1": { tuning: { temperature: 0.9 } } } }); const m3 = await read(p); assert.deepEqual(tuningStale(m3, resolverFor({ cast: castDoc(STEADY, STEADY) }), { settingTakes: 3 }).map((x) => x.role), ["post"]); }); test("rereadPlan: a take whose tuning was its character's is re-read with the character's as it is then (its reading kept, no tuning flags)", async () => { const { rereadPlan } = await import("./castProject.mjs"); const made = (o) => ({ temperature: 0.75, top_p: 0.92, top_k: 50, min_p: 0, repeat_penalty: 1.25, takes: 3, ...o }); const m = { timeline: [ { type: "post", id: "p-1", handle: "x", narration: { takes: [{ file: "a.wav", engine: "orpheus", voice: "zac", tuning: made(), tuningAsked: "preset", reading: 2 }], pick: 0 } }, { type: "post", id: "p-2", handle: "x", narration: { takes: [{ file: "b.wav", engine: "orpheus", voice: "zac", tuning: made({ temperature: 0.9 }), tuningAsked: true }], pick: 0 } }, ] }; const drift = (id) => ({ id, handle: "x", role: "post", reads: "orpheus:zac", want: "orpheus:leah", kind: "tts", thread: false }); const plan = rereadPlan("p", m, [drift("p-1"), drift("p-2")], ["x"]); assert.deepEqual(plan.items.map((x) => x.argv.slice(4)), [ ["--take", "--reading", "2"], ["--take", "--temperature", "0.9", "--top-p", "0.92", "--repeat-penalty", "1.25", "--chunk-takes", "3"], ]); }); test("review 2 #2: a thread piece's ownership is decided from the values AS ASKED -- an explicit default over a character's is the read's own", async () => { const p = await project("thread-explicit", castDoc(LIVELY, STEADY)); const asked = []; const r = await threadTake(p, (await read(p)).timeline[0], { red: NO_RED, ask: stubAsk(asked), lanes: null, ...layersOf(castDoc(LIVELY, STEADY)), tuned: { post: { tuning: { temperature: 0.55 } }, "screen:s1:0": { tuning: { temperature: 0.55, top_p: 0.85, repeat_penalty: 1.2 } } } }); const post = r.take.parts.find((x) => x.role === "post"); // temperature 0.55 over Lively: read with Lively's top_p and repeat penalty and the default temperature -- the read's own. assert.deepEqual([post.tuning, post.tuningAsked], [{ top_p: 0.92, repeat_penalty: 1.25 }, true]); assert.deepEqual(r.take.read.tuned.post.tuning, { temperature: 0.55 }); // All three defaults over Steady: read with the defaults (nothing left to key), still the read's own. const line = r.take.parts.find((x) => x.role === "screen:s1:0"); assert.deepEqual([line.tuning, line.tuningAsked], [undefined, true]); assert.deepEqual(r.take.read.tuned["screen:s1:0"].tuning, { temperature: 0.55, top_p: 0.85, repeat_penalty: 1.2 }); // Neither is stale when the characters change: they are the read's own. const { tuningStale } = await import("umtool-report-to-video/cast"); const { resolverFor } = await import("./castProject.mjs"); const roles = tuningStale(await read(p), resolverFor({ cast: castDoc(STEADY, LIVELY) }), { settingTakes: 3 }).map((x) => x.role); assert.ok(!roles.includes("post") && !roles.includes("screen:s1:0"), JSON.stringify(roles)); }); test("review 2 #9: a thread read's own takes over a character's tuning are kept in read.tuned (the sampler still the character's)", async () => { const p = await project("thread-takes", castDoc(LIVELY)); const r = await threadTake(p, (await read(p)).timeline[0], { red: NO_RED, ask: stubAsk([]), lanes: null, ...layersOf(castDoc(LIVELY)), tuned: { post: { tuning: { takes: 5 } } } }); const post = r.take.parts.find((x) => x.role === "post"); assert.deepEqual([post.tuning, post.tuningAsked], [{ ...LIVELY, takes: 5 }, "preset"]); assert.deepEqual(r.take.read.tuned.post.tuning, { takes: 5 }); });