// A CONVERT'S PITCH, between umtool and the editor's voice-convert (the pitch a converted read lands on: knobs.md, // "The pitch a converted read lands on"). The pure pieces the CLI's derive (postNarrationCli.mjs deriveTake) and the // app's (app/api/report/post-narration/route.ts, the post bench's "run AI on take") share, so they ask, record and // refuse alike: // // derivePitchOf what a derive is asked with (`pitch`, `semitones`), checked before any job // convertAskOf what a CONVERT derive is asked with: what the ask named, the rest from the character's recorded // preset -- never the editor's default voice // stepPitchOf what the new take's step keeps: the pitch as the editor RAN it, never the target's own // deriveRefusalOf an editor refusal read as "no engine for it" -- or passed through as it is, when it already // names the command that fixes it // // Dependency-free but for cast.mjs's constants. import { CAST_CONVERT_PITCH, CAST_SEMITONES_MAX } from "umtool-report-to-video/cast"; /** * A derive's `pitch` / `semitones` as asked (a body's, a flag's): `{ ok: true, value: {pitch?, semitones?} }` with only * what was given, or `{ ok: false, error }`. `semitones` may be a number or its string (a form field, a query). They * are a convert's: asked of another op they are refused. */ export function derivePitchOf({ pitch, semitones } = {}, op = "convert") { const none = (v) => v === undefined || v === null || v === ""; if (none(pitch) && none(semitones)) return { ok: true, value: {} }; if (op !== "convert") return { ok: false, error: `pitch and semitones are a convert's: ${op} keeps the read's own pitch` }; const value = {}; if (!none(pitch)) { if (!CAST_CONVERT_PITCH.includes(pitch)) return { ok: false, error: `pitch is one of ${CAST_CONVERT_PITCH.join(", ")} (got ${JSON.stringify(pitch)})` }; value.pitch = pitch; } if (!none(semitones)) { const n = typeof semitones === "number" ? semitones : typeof semitones === "string" && semitones.trim() !== "" ? Number(semitones) : NaN; if (!Number.isFinite(n) || Math.abs(n) > CAST_SEMITONES_MAX) return { ok: false, error: `semitones is a number from -${CAST_SEMITONES_MAX} to ${CAST_SEMITONES_MAX} (got ${JSON.stringify(semitones)})` }; value.semitones = n; } return { ok: true, value }; } /** * WHAT A CONVERT DERIVE IS ASKED WITH (operator, 11 Nov: a derive with no flags asked the editor for ITS defaults and * made a take in the wrong voice). `asked` is what the command or the body named (`engine`, `voice`, `pitch`, * `semitones`: only those given); `convert` the recorded preset's convert entry of the character the read is made * as (cast.mjs recordedPresetOf(character).convert -- `{engine, voice, pitch?, semitones?}`, null: no character); * `who` its id, for the sentences. What was asked wins, each key by itself; the rest is the character's: * * engine, voice the character's convert engine and stock voice * pitch the character's -- unless the ask names ANOTHER engine than the character's (a followed pitch is * one engine's: the editor refuses it of another) * semitones the character's, with a followed pitch; dropped with `pitch: "target"` asked for this read * * With no stock voice from either, `{ ok: false, error, fix }`: one sentence (and the flags that answer it), and the * editor is never asked for its default voice. Else `{ ok: true, value: {engine, voice, pitch?, semitones?}, from: "asked" | "character" }` -- * `from` says whose voice it is. * * @param {{ engine?: string | null, voice?: string | null, pitch?: string | null, semitones?: number | null }} [asked] * @param {{ engine?: string | null, voice?: string | null, pitch?: string, semitones?: number } | null} [convert] * @param {string | null} [who] * @returns {{ ok: true, value: { engine: string | null, voice: string, pitch?: string, semitones?: number }, from: "asked" | "character" } | { ok: false, error: string, fix: string }} */ export function convertAskOf(asked = {}, convert = null, who = null) { const given = (v) => v !== undefined && v !== null && v !== ""; const cv = convert ?? {}; const voice = given(asked.voice) ? asked.voice : given(cv.voice) ? cv.voice : null; if (!voice) { // `error`: the sentence, for any surface; `fix`: the commands that answer it, for a terminal. return who ? { ok: false, error: `a convert needs a stock voice to convert to, and ${who} has none`, fix: `--voice , or umtool cast set ${who} --convert-voice ` } : { ok: false, error: "a convert needs a stock voice to convert to, and this post has no character with one", fix: "--voice , --as , or umtool cast add … for its author" }; } const engine = given(asked.engine) ? asked.engine : given(cv.engine) ? cv.engine : null; // The character's pitch is its engine's: another engine asked for this read is asked for none. const own = !given(asked.engine) || !given(cv.engine) || asked.engine === cv.engine; const pitch = given(asked.pitch) ? asked.pitch : own && given(cv.pitch) ? cv.pitch : undefined; const semitones = given(asked.semitones) ? asked.semitones : own && pitch !== undefined && pitch !== "target" && given(cv.semitones) ? cv.semitones : undefined; return { ok: true, value: { engine: engine ?? null, voice, ...(pitch !== undefined && pitch !== null ? { pitch } : {}), ...(semitones !== undefined && semitones !== null ? { semitones } : {}) }, from: given(asked.voice) ? "asked" : "character", }; } /** * The pitch a derived take's step keeps: what the editor says it ran (its `@@voice-result`: its default when none was * asked), else what was asked. The stock voice's own pitch is not a step's news (`{}`), and 0 semitones is none. */ export function stepPitchOf(asked = {}, said = {}) { const pitch = typeof said?.pitch === "string" ? said.pitch : asked?.pitch; if (!pitch || pitch === "target" || !CAST_CONVERT_PITCH.includes(pitch)) return {}; const n = typeof said?.semitones === "number" ? said.semitones : asked?.semitones; return { pitch, ...(typeof n === "number" && Number.isFinite(n) && n !== 0 ? { semitones: n } : {}) }; } /** * An editor refusal of a derive, read: `null` (an ordinary error), or `{ installed: false, error }` -- the editor has * no engine for the op (an older editor has no route; a newer one names the setup to run). A refusal that names an * OPTIONAL group to fetch (`… --with f0`: the engine is there, its pitch-following model is not) is passed through in * the editor's words: "no convert engine installed" would be false. */ export function deriveRefusalOf(op, status, message) { const msg = String(message ?? "").replace(/^the editor refused it: /, ""); if (!(status === 404 || /not installed|no engine|unknown|not found|404/i.test(msg))) return null; if (/--with\b/.test(msg)) return { installed: false, error: msg }; return { installed: false, error: `the editor has no ${op} engine installed (not installed: ${msg})` }; }