// A SCREEN LINE RECORDED BY THE OPERATOR (lane/screens-2; docs/report-video.md, "Screens" -- "A line in your own // voice"): one line of a post's screen (post-screens.mjs) read by a person, kept as a NEW take of the item -- recorded, // cleaned, converted to the line's SPEAKER's character's stock voice -- each step a new take, NONE of them the item's // read (never picked): a line take is read in its line's place by the item's thread read only once the operator // CHOOSES it (`--line-take`, the screen's `lineTakes`: the bench's per-line select, `posts screen … set take.=`). // // umtool posts screen

record --recording [--as ] [--no-clean|--clean] // [--no-trim|--trim] [--no-convert] [--pitch …] [--semitones n] [--label l] [--check-words [--worker w]] // // WHAT IS READ: the line as the read says it (screenLineSaid: its read run, its shapes left out; draftScript; the // redaction left out, an anonymous handle said as its shown name: post-redact.mjs spokenRead) -- the PROMPT the person // reads is that, without the pronunciation lexicon's spellings, and the take's script is it with them (what the read // holds the take to: postThreadRead.mjs lineTakeProblem). The take carries `screenLine: { screen, n, said, speaker }`: // the line as a read writes it and its speaker, so a line edited or given to someone else since is refused, never // played (postThreadRead.mjs threadReadPlan). The recording itself is postNarrationCli.mjs importRecording's chain. // // --check-words (opt-in; the editor's transcribe op, a job): the recording TRANSCRIBED before anything is kept -- // REFUSED when it is heard saying a shape (an e-mail, a number: post-screens.mjs shapeSpans), an @handle, an anonymous // handle or a redacted term; a warning when its words are not the line's in order. Without it, the recording is the // operator's: what he reads is what he chose. // // Plain ESM: the CLI runs it under bare node. import { mkdir, mkdtemp, readFile, rm } from "node:fs/promises"; import path from "node:path"; import { characterOf, projectCharactersOf } from "umtool-report-to-video/cast"; import { draftScript, isNarrated, pickedTake, postIdOf, takesDirOf } from "umtool-report-to-video/post-narration"; import { spokenRead, termsFor } from "umtool-report-to-video/post-redact"; import { handleKey } from "umtool-report-to-video/post-say"; import { parseScreenRole, screenLineSaid, screenLineSpoken, screenOf, screenReadLines, screenWho, shapeSpans } from "umtool-report-to-video/post-screens"; import { readManifest } from "../projects/report.mjs"; const str = (v) => (typeof v === "string" ? v : ""); export class LineRecordError extends Error { constructor(message) { super(message); this.name = "LineRecordError"; } } /** * THE LINE TO RECORD, pure: `{ role, screen, n, written, script, prompt, speaker, handle, self }` -- `written` the line * as a read writes it (the take's `screenLine.said`), `script` what the read says (the take's script), `prompt` what the * person reads (the same without the lexicon's spellings). Throws LineRecordError: no such screen or line, a line * redacted or not read, nothing left to say. * * @param {Record} e the post item * @param {string} role `screen::` * @param {{ red?: any, names?: Record }} [o] */ export function lineToRecord(e, role, { red = null, names = {} } = {}) { const p = parseScreenRole(role); if (!p) throw new LineRecordError(`a screen line is screen:: (got ${role})`); const sc = screenOf(e, p.id); if (!sc) throw new LineRecordError(`${e?.id} has no screen ${p.id}`); const l = sc.lines[p.n]; if (!l) throw new LineRecordError(`screen ${p.id} has no line ${p.n} (lines 0–${sc.lines.length - 1})`); if (l.redact === true) throw new LineRecordError(`line ${p.n} of screen ${p.id} is redacted: it is never read`); if (!screenReadLines(sc).some((r) => r.n === p.n)) throw new LineRecordError(`line ${p.n} of screen ${p.id} is not read by the screen: tick it first (umtool posts screen

${e.id} set ${p.id} read.${p.n}=on)`); const written = draftScript(screenLineSaid(sc, p.n)).script; // What is SAID: its "said as" (post-screens.mjs saidAs) when it has one -- the prompt you read is what the voice says. const say = draftScript(screenLineSpoken(sc, p.n)).script || written; // A beep needs the slur's time in the read; a joined read is not word-timed as it is made (postThreadRead.mjs): skipped. const voice = red?.slurs?.voice === "beep" ? "skip" : undefined; const script = str(spokenRead(say, e, red, { voice }).text).replace(/\s+/g, " ").trim(); const prompt = str(spokenRead(say, e, red ? { ...red, say: [] } : red, { voice }).text).replace(/\s+/g, " ").trim(); if (!script) throw new LineRecordError(`line ${p.n} of screen ${p.id} has nothing left to say (all of it masked or redacted)`); const sw = screenWho(l.who, e, names); return { role, screen: p.id, n: p.n, written, script, prompt, speaker: str(l.who), handle: sw.self ? str(e.handle).replace(/^@/, "") || null : sw.handle, self: sw.self }; } /** * THE CHARACTER A LINE's RECORDING IS MADE INTO, pure: its SPEAKER's -- the post's author's own lines the item's * chain (characterForRead: the item's own, this video's for her, the cast's; never the narrator), anyone else's the * character holding their handle (this video's characters first, then the cast). Null: none (kept as recorded). */ export function lineCharacterOf(layers, e, target) { if (target.self) { const r = layers.characterForRead(e); return r && r.layer !== "narrator" ? r.character : null; } const h = handleKey(target.handle); if (!h) return null; return projectCharactersOf(layers.project).find((c) => (c.handles ?? []).map(handleKey).includes(h)) ?? characterOf(layers.cast, h) ?? null; } /** A digit said as a word (ASR writes numbers both ways). */ const DIGIT_WORDS = new Set(["zero", "oh", "o", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "double", "triple"]); /** * WHAT A RECORDING IS HEARD SAYING AMISS, pure (--check-words): `{ refused: string | null, warning: string | null }`. * REFUSED when the words heard hold a shape (an e-mail, a number: post-screens.mjs shapeSpans), a number SAID IN WORDS * (seven digits or more, as words or digits in a row: "five five five two oh one…"), an @handle, an anonymous author's * handle (`handles`; bare, possessive "secretkid's", or spaced "secret kid") or display name (`names`, the same forms), * or a redacted term (`terms`); a warning when its words are not the line's in order (postThreadRead.mjs * lineTakeProblem's "other words": ASR slack is the person's to judge). Names what, never the words. */ export function heardProblem(heard, script, { handles = [], names = [], terms = [], lineTakeProblem }) { const h = str(heard); if (shapeSpans(h).length) return { refused: "it is heard saying an e-mail address or a number the card masks", warning: null }; const raw = h.toLowerCase().replace(/[’‘]/g, "'").split(/\s+/).filter(Boolean); if (raw.some((w) => /^[^\p{L}\p{N}]*@/u.test(w))) return { refused: "it is heard saying an @handle", warning: null }; // Each word bare: its edge punctuation and a possessive dropped. const words = raw.map((w) => w.replace(/^[^\p{L}\p{N}]+|[^\p{L}\p{N}']+$/gu, "").replace(/'s$|'$/u, "").replace(/[^\p{L}\p{N}]/gu, "")).filter(Boolean); // A number said in words (or in digits a word at a time). let run = 0; for (const w of words) { if (DIGIT_WORDS.has(w)) run += w === "double" ? 1 : w === "triple" ? 2 : 1; else if (/^\d+$/.test(w)) run += w.length; else run = 0; if (run >= 7) return { refused: "it is heard saying a number in words (a number the card masks)", warning: null }; } // A handle or a name: a word, or two or three words run together ("secret kid"), or the name's words in a row. const joined = (i, k) => words.slice(i, i + k).join(""); const says = (target) => { const t = String(target).toLowerCase().replace(/^@+/, ""); const flat = t.replace(/[^\p{L}\p{N}]/gu, ""); if (!flat) return false; const tw = t.split(/[^\p{L}\p{N}]+/u).filter(Boolean); for (let i = 0; i < words.length; i += 1) { for (let k = 1; k <= 3 && i + k <= words.length; k += 1) if (joined(i, k) === flat) return true; if (tw.length > 1 && tw.every((x, j) => words[i + j] === x)) return true; } return false; }; if (handles.some(says)) return { refused: "it is heard saying an anonymous author's handle", warning: null }; if (names.filter((n) => str(n).trim().length >= 3).some(says)) return { refused: "it is heard saying an anonymous author's name", warning: null }; const esc = (t) => String(t).replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); if (terms.some((t) => new RegExp(`(? String(x).replace(/^@+/, "").toLowerCase())); const out = new Set(); for (const c of Array.isArray(projectDoc?.characters) ? projectDoc.characters : []) if (c?.anonymous === true && str(c.name).trim()) out.add(str(c.name).trim()); for (const e of Array.isArray(manifest?.timeline) ? manifest.timeline : []) { for (const w of [e, ...(Array.isArray(e?.parents) ? e.parents : []), e?.quoted].filter((x) => x && typeof x === "object")) { if (hs.has(str(w.handle).replace(/^@+/, "").toLowerCase()) && str(w.author).trim()) out.add(str(w.author).trim()); } } return [...out]; } /** * RECORD ONE SCREEN LINE of post item `id` (see the head): `file` a sound file; the takes made (`{ takes: [{ step, file * }], picked: null, notes, disclosure, character, line: { role, prompt } }`). Refused (LineRecordError) on an item with * no read of its own (a line is read inside one), a line that cannot be read, `as` naming no one, and -- with * `checkWords` -- a recording heard saying what is never said. `ask` / `editor`: the editor's ops (tests stub them). */ export async function recordScreenLine(project, id, role, file, opts = {}) { const { as = null, label = "operator", clean = undefined, trim = undefined, convert = true, pitch = undefined, semitones = undefined, note = null, checkWords = false, worker = null, emit = () => {}, editor = null, ask = null } = opts; const manifest = await readManifest(project.dir); const e = (manifest?.timeline ?? []).find((x) => x?.type === "post" && x.id === id); if (!e) throw new LineRecordError(`no post item ${id} in ${project.id}`); const t = pickedTake(e); if (!isNarrated(e) || t?.screenLine) throw new LineRecordError(`${id} has no read of its own yet: a screen line is read inside the item's read -- make it first (umtool posts narrate

${id} --take, or record it), then record the line`); const { projectRedaction } = await import("./postRedaction.mjs"); const red = await projectRedaction(project.dir, manifest); const { readLayers } = await import("./characterLayers.mjs"); const layers = await readLayers(project.dir); const { characterNames, lineTakeProblem } = await import("./postThreadRead.mjs"); const target = lineToRecord(e, role, { red, names: characterNames(layers) }); // A line converts ONLY to its SPEAKER's character (operator, 13 Nov): --as may name that one, never another, never // the narrator. const character = lineCharacterOf(layers, e, target); if (as) { const named = layers.characterForRead(e, as)?.character ?? null; if (!named) throw new LineRecordError(`--as ${as}: no such character in this video's characters or the cast (umtool cast project ${project.id}, umtool cast ls)`); if (named.narrator === true) throw new LineRecordError(`--as ${as}: the narrator is never a screen line's voice -- a line converts only to its speaker's character`); if (!character || named.id !== character.id) throw new LineRecordError(`--as ${as}: not the character of line ${target.n}'s speaker${character ? ` (that is ${character.id})` : " (the speaker has none: the line is kept as recorded)"} -- a line converts only to its speaker's character`); } const notes = []; let heardRec = null; if (checkWords) { // Heard BEFORE anything is kept: a refusal keeps nothing. const { decodePart } = await import("./postRecord.mjs"); const dir = path.join(project.dir, ...takesDirOf(postIdOf(e)).split("/")); await mkdir(dir, { recursive: true }); const tmp = await mkdtemp(path.join(dir, ".line-check-")); try { const wav = path.join(tmp, "heard.wav"); await decodePart(path.resolve(file), wav); const out = path.join(tmp, "heard.words.json"); const askOp = ask ?? (await import("umtool-report-to-video/narrate-via-editor")).askEditorOp; emit("note", { message: `${id}: the recording of ${role} transcribed on the editor (--check-words)…` }); await askOp("/api/ops/transcribe", { path: wav, words: true, out, ...(worker ? { workerId: worker } : {}) }, { ...(editor ? { editor } : {}), emit }); const doc = JSON.parse(await readFile(out, "utf8").catch(() => "{}")); const heard = Array.isArray(doc?.words) && doc.words.length ? doc.words.map((w) => str(w?.w)).join(" ") : str(doc?.text); if (!heard.trim()) throw new LineRecordError(`--check-words: the editor heard no words in the recording of ${role} -- nothing kept`); const anonHandles = red?.anon instanceof Map ? [...red.anon.keys()] : Object.keys(red?.anon ?? {}).filter((x) => x !== "postIds"); const p = heardProblem(heard, target.script, { handles: anonHandles, names: anonymousNamesOf(manifest, layers.project, anonHandles), terms: termsFor(e, red).map((x) => x.term), lineTakeProblem }); if (p.refused) throw new LineRecordError(`--check-words: the recording of ${role} is refused -- ${p.refused}; nothing kept: record the line again`); if (p.warning) notes.push(`--check-words: ${p.warning}`); else notes.push("--check-words: heard saying the line"); // What it was heard saying, against which words: kept with the take (an unheard one says it was not). heardRec = { at: new Date().toISOString(), against: target.script, words: heard.replace(/\s+/g, " ").trim().slice(0, 2000) }; } finally { await rm(tmp, { recursive: true, force: true }); } } const { importRecording } = await import("./postNarrationCli.mjs"); const r = await importRecording(project, e, file, { label, clean, trim, convert, pitch, semitones, note, emit, editor, ask, character, screenLine: { screen: target.screen, n: target.n, said: target.written, speaker: target.speaker, ...(heardRec ? { heard: heardRec } : {}) }, script: target.script, }); if (!heardRec) notes.push("not heard: nothing checked what the recording says -- the read takes it as the line's words (--check-words hears it)"); return { ...r, notes: [...notes, ...r.notes], line: { role, prompt: target.prompt, speaker: target.speaker }, heard: !!heardRec }; }