// lib/report/derivePitch.mjs: a convert's pitch between umtool and the editor -- what a derive is asked with, what the // new take's step keeps, and which editor refusals mean "no engine". The app's derive route // (app/api/report/post-narration/route.ts) and the CLI's (postNarrationCli.mjs deriveTake) both go through it. // Run: node --test umtool/lib/report/derivePitch.test.mjs import assert from "node:assert/strict"; import { test } from "node:test"; import { validatePostNarration } from "umtool-report-to-video/post-narration"; import { convertAskOf, derivePitchOf, deriveRefusalOf, stepPitchOf } from "./derivePitch.mjs"; test("a derive's pitch as asked: only what was given, a body's number or a query's string, a convert's alone", () => { assert.deepEqual(derivePitchOf({}), { ok: true, value: {} }); assert.deepEqual(derivePitchOf({ pitch: null, semitones: "" }), { ok: true, value: {} }, "a query with neither"); assert.deepEqual(derivePitchOf({ pitch: "contour", semitones: 2 }), { ok: true, value: { pitch: "contour", semitones: 2 } }); assert.deepEqual(derivePitchOf({ pitch: "source", semitones: "-1.5" }), { ok: true, value: { pitch: "source", semitones: -1.5 } }); assert.deepEqual(derivePitchOf({ pitch: "target" }), { ok: true, value: { pitch: "target" } }); assert.deepEqual(derivePitchOf({ semitones: 0 }), { ok: true, value: { semitones: 0 } }, "0 is said, not dropped: the editor's default may be another"); for (const [given, re] of [ [{ pitch: "up" }, /^pitch is one of target, source, contour \(got "up"\)$/], [{ pitch: 2 }, /pitch is one of/], [{ pitch: "source", semitones: 13 }, /^semitones is a number from -12 to 12 \(got 13\)$/], [{ pitch: "source", semitones: "two" }, /semitones is a number from -12 to 12 \(got "two"\)/], [{ pitch: "source", semitones: true }, /semitones is a number/], ]) { const r = derivePitchOf(given); assert.equal(r.ok, false, JSON.stringify(given)); assert.match(r.error, re); } const en = derivePitchOf({ pitch: "source" }, "enhance"); assert.equal(en.ok, false); assert.match(en.error, /pitch and semitones are a convert's: enhance keeps the read's own pitch/); assert.deepEqual(derivePitchOf({}, "enhance"), { ok: true, value: {} }); }); test("a convert derive is the character's: what the ask names wins, the rest is its recorded preset's, and no voice is refused", () => { const savvy = { engine: "seed-vc", voice: "orpheus:mia", pitch: "contour", semitones: 2 }; // No flags: the character's, whole (the 11 Nov bug: the editor's default voice was asked instead). assert.deepEqual(convertAskOf({}, savvy, "madam-savvy"), { ok: true, value: savvy, from: "character" }); assert.deepEqual(convertAskOf({ engine: null, voice: null }, savvy, "madam-savvy").value, savvy, "a flag not given is null"); // Each key by itself. assert.deepEqual(convertAskOf({ semitones: 0 }, savvy).value, { ...savvy, semitones: 0 }, "0 is asked, not absent"); assert.deepEqual(convertAskOf({ pitch: "source" }, savvy).value, { ...savvy, pitch: "source" }, "the nudge goes with another followed pitch"); assert.deepEqual(convertAskOf({ pitch: "target" }, savvy).value, { engine: "seed-vc", voice: "orpheus:mia", pitch: "target" }, "the target's own pitch takes no semitones"); assert.deepEqual(convertAskOf({ voice: "orpheus:tara" }, savvy), { ok: true, value: { ...savvy, voice: "orpheus:tara" }, from: "asked" }); // Another engine than the character's: its pitch is not that engine's to follow. assert.deepEqual(convertAskOf({ engine: "openvoice" }, savvy).value, { engine: "openvoice", voice: "orpheus:mia" }); assert.deepEqual(convertAskOf({ engine: "openvoice", pitch: "target" }, savvy).value, { engine: "openvoice", voice: "orpheus:mia", pitch: "target" }); assert.deepEqual(convertAskOf({ engine: "seed-vc" }, savvy).value, savvy, "the same engine, named"); // A character with only a voice (its synthetic one): the editor's engine and pitch. assert.deepEqual(convertAskOf({}, { engine: null, voice: "kokoro:am_puck" }, "anon-a").value, { engine: null, voice: "kokoro:am_puck" }); // No character: the flags alone; none: refused in one sentence, with what answers it. assert.deepEqual(convertAskOf({ voice: "zac" }, null), { ok: true, value: { engine: null, voice: "zac" }, from: "asked" }); const none = convertAskOf({ engine: "seed-vc", pitch: "contour" }, null); assert.equal(none.ok, false); assert.equal(none.error, "a convert needs a stock voice to convert to, and this post has no character with one"); assert.match(none.fix, /--voice , --as /); const mute = convertAskOf({}, { engine: null, voice: null }, "ghost"); assert.equal(mute.error, "a convert needs a stock voice to convert to, and ghost has none"); assert.match(mute.fix, /umtool cast set ghost --convert-voice/); }); test("the step keeps the pitch the editor ran, else the one asked; never the target's own, never 0 semitones", () => { assert.deepEqual(stepPitchOf({}, {}), {}); assert.deepEqual(stepPitchOf({ pitch: "target" }, {}), {}); assert.deepEqual(stepPitchOf({ pitch: "contour", semitones: 2 }, {}), { pitch: "contour", semitones: 2 }, "an editor that says nothing"); assert.deepEqual(stepPitchOf({ pitch: "source", semitones: 0 }, null), { pitch: "source" }); // The editor's default, when none was asked. assert.deepEqual(stepPitchOf({}, { pitch: "contour", semitones: 2 }), { pitch: "contour", semitones: 2 }); // What it ran beats what was asked (its settings' semitones under an asked pitch). assert.deepEqual(stepPitchOf({ pitch: "source" }, { pitch: "source", semitones: -1 }), { pitch: "source", semitones: -1 }); assert.deepEqual(stepPitchOf({ pitch: "contour", semitones: 2 }, { pitch: "target" }), {}); assert.deepEqual(stepPitchOf({}, { pitch: "sideways" }), {}, "not a pitch the manifest takes"); // Whatever it returns is a step the manifest's validator takes. for (const p of [{ pitch: "contour", semitones: 2 }, { pitch: "source" }, {}]) { const entry = { id: "p-1", narration: { script: "x", takes: [{ file: "narration/posts/1/take-2.convert-seed-vc.wav", engine: "derived", voice: "af_bella", seconds: 1, source: { kind: "take", from: "take-1.recorded-operator" }, steps: [{ op: "convert", engine: "seed-vc", voice: "kokoro:af_bella", ...stepPitchOf(p, {}) }] }], pick: 0 } }; assert.deepEqual(validatePostNarration(entry).filter((e) => /steps/.test(e)), [], JSON.stringify(p)); } }); test("an editor refusal: no engine, an ordinary error, or a missing optional model said in the editor's words", () => { assert.deepEqual(deriveRefusalOf("enhance", 404, "HTTP 404"), { installed: false, error: "the editor has no enhance engine installed (not installed: HTTP 404)" }); assert.deepEqual( deriveRefusalOf("convert", 400, "the editor refused it: OpenVoice v2 is not installed (/v/openvoice): missing the python venv — run `archilyzer voice setup --engine openvoice`"), { installed: false, error: "the editor has no convert engine installed (not installed: OpenVoice v2 is not installed (/v/openvoice): missing the python venv — run `archilyzer voice setup --engine openvoice`)" }, ); // seed-vc IS installed; its pitch-following model is not: the editor's sentence, as it is. const f0 = "pitch \"source\" needs Seed-VC (v1, 22 kHz speech)'s f0 model, which is not installed (/v/seed-vc: missing rmvpe.pt) — run `archilyzer voice setup --engine seed-vc --with f0`"; assert.deepEqual(deriveRefusalOf("convert", 400, `the editor refused it: ${f0}`), { installed: false, error: f0 }); assert.equal(/no convert engine/.test(deriveRefusalOf("convert", 400, f0).error), false); // Not about an install: the caller answers it as the error it is. assert.equal(deriveRefusalOf("convert", 400, "OpenVoice v2 (tone-colour converter) has no pitch control — it lands every read on the target's own pitch"), null); assert.equal(deriveRefusalOf("convert", 400, "\"semitones\" moves a followed pitch"), null); });