// The cast (cast.mjs): its shape refused whole, a voice with its engine, a character by handle, the cast as a voice // layer under the project's map (post-say.mjs voiceSourceOf: override → project → cast → narrator → default), what a // picked take reads each author in, the drift and the overrides. import assert from "node:assert/strict"; import { test } from "node:test"; import { castDrift, castVoiceMap, characterOf, convertPitchLabel, convertTargetOf, emptyCast, findCharacter, isQualifiedVoice, narratorOf, speakerFor, takeReads, validateCast, voiceDrift, voiceRefOf, } from "./cast.mjs"; import { resolvePieceVoice, validateVoiceMap, voiceForPiece, voiceOverridesOf, voiceSourceOf } from "./post-say.mjs"; const Q = { id: "thequartering", name: "TheQuartering", handles: ["thequartering"], voice: "kokoro:am_michael", speakers: ["Jeremy"] }; const S = { id: "savvy", name: "MadamSavvy", handles: ["madamsavvy"], voice: "orpheus:tara", convert: { engine: "seed-vc" }, narrator: true }; const cast = { ...emptyCast(), characters: [Q, S] }; test("a voice is a stock one, bare or with its engine -- never a path", () => { assert.deepEqual(voiceRefOf("kokoro:am_michael"), { engine: "kokoro", voice: "am_michael" }); assert.deepEqual(voiceRefOf("tara", "orpheus"), { engine: "orpheus", voice: "tara" }); assert.equal(voiceRefOf("/home/me/voice.wav"), null); assert.equal(voiceRefOf("file:me.wav"), null, "an engine the editor has not"); assert.equal(voiceRefOf("https://x/y"), null); assert.ok(isQualifiedVoice("orpheus:zac")); assert.ok(!isQualifiedVoice("zac")); // The project's map takes either form. assert.deepEqual(validateVoiceMap({ thequartering: "kokoro:am_michael", madamsavvy: "tara" }), []); assert.equal(validateVoiceMap({ x: "seedvc:me.wav" }).length, 1); assert.deepEqual(voiceOverridesOf(["thequartering=kokoro:am_onyx"]), { thequartering: "kokoro:am_onyx" }); }); test("validateCast: the shape, ids, one owner a handle, stock voices only, one narrator", () => { assert.deepEqual(validateCast(cast), []); assert.match(validateCast({ ...cast, format: "x" })[0], /format must be "umtool-cast"/); const two = { ...emptyCast(), characters: [Q, { ...S, handles: ["thequartering"] }] }; assert.ok(validateCast(two).some((e) => /is thequartering's already/.test(e))); assert.ok(validateCast({ ...emptyCast(), characters: [{ ...Q, voice: "/tmp/jeremy.wav" }] }).some((e) => /STOCK voice with its engine/.test(e))); assert.ok(validateCast({ ...emptyCast(), characters: [{ ...Q, convert: { voice: "me.wav" } }] }).some((e) => /never a file/.test(e))); assert.ok(validateCast({ ...emptyCast(), characters: [{ ...Q, narrator: true }, S] }).some((e) => /one character at most is the narrator/.test(e))); // The pitch a converted read lands on: a mode, semitones in range, and only what an engine can follow. const conv = (convert) => validateCast({ ...emptyCast(), characters: [{ ...Q, convert }] }); assert.deepEqual(conv({ engine: "seed-vc", pitch: "contour", semitones: 2 }), []); assert.deepEqual(conv({ pitch: "source" }), [], "no engine: the editor's default answers for itself"); assert.deepEqual(conv({ engine: "openvoice", pitch: "target" }), []); assert.ok(conv({ pitch: "high" }).some((e) => /convert\.pitch must be one of target, source, contour/.test(e))); assert.ok(conv({ pitch: "source", semitones: 13 }).some((e) => /convert\.semitones must be a number from -12 to 12/.test(e))); assert.ok(conv({ engine: "openvoice", pitch: "contour" }).some((e) => /convert\.pitch "contour" is seed-vc's: openvoice lands every read on the target's own pitch/.test(e))); assert.ok(conv({ engine: "seed-vc", semitones: 2 }).some((e) => /convert\.semitones moves a followed pitch/.test(e))); assert.ok(conv({ engine: "seed-vc", speed: 2 }).some((e) => /convert must be \{engine\?, voice\?, pitch\?, semitones\?\}/.test(e))); assert.ok(validateCast({ ...emptyCast(), characters: [{ ...Q, handles: ["@TheQuartering"] }] }).some((e) => /lower case, no @/.test(e))); assert.ok(validateCast({ ...emptyCast(), characters: [{ ...Q, extra: 1 }] }).some((e) => /extra is not a character key/.test(e))); }); test("a character by handle, id or name; the narrator; a recording's target; the real voice it goes by", () => { assert.equal(characterOf(cast, "@TheQuartering")?.id, "thequartering"); assert.equal(findCharacter(cast, "MadamSavvy")?.id, "savvy"); assert.equal(findCharacter(cast, "savvy")?.id, "savvy"); assert.equal(findCharacter(cast, "nobody"), null); assert.equal(narratorOf(cast)?.id, "savvy"); assert.deepEqual(castVoiceMap(cast), { thequartering: "kokoro:am_michael", madamsavvy: "orpheus:tara" }); assert.deepEqual(convertTargetOf(S), { engine: "seed-vc", voice: "orpheus:tara" }); assert.deepEqual(convertTargetOf({ ...Q, convert: { voice: "kokoro:am_onyx" } }), { engine: null, voice: "kokoro:am_onyx" }); assert.deepEqual(convertTargetOf({ ...S, convert: { engine: "seed-vc", pitch: "contour", semitones: 2 } }), { engine: "seed-vc", voice: "orpheus:tara", pitch: "contour", semitones: 2 }); assert.deepEqual([convertPitchLabel(undefined), convertPitchLabel({ pitch: "target" }), convertPitchLabel({ pitch: "source" }), convertPitchLabel({ pitch: "contour", semitones: 2 }), convertPitchLabel({ pitch: "source", semitones: -1.5 })], ["", "", "source", "contour +2 st", "source −1.5 st"]); const speakers = [{ name: "Jeremy" }, { name: "Melonie Mac" }]; assert.equal(speakerFor(Q, speakers)?.name, "Jeremy"); assert.equal(speakerFor({ id: "mm", name: "MelonieMac", handles: [] }, speakers)?.name, "Melonie Mac", "the name, its punctuation and case aside"); assert.equal(speakerFor(S, speakers), null); }); test("the cast is a layer: the read's override, then the project's map, then the cast, then the narrator, then the default", () => { const castMap = castVoiceMap(cast); const o = { map: {}, cast: castMap, narrator: "zac", engine: "orpheus", self: "TheQuartering" }; assert.deepEqual(voiceSourceOf({ handle: "TheQuartering", role: "post" }, o), { spec: "kokoro:am_michael", from: "cast" }); assert.deepEqual(resolvePieceVoice({ handle: "TheQuartering", role: "post" }, o), { engine: "kokoro", voice: "am_michael", from: "cast" }); assert.deepEqual(voiceSourceOf({ handle: "TheQuartering", role: "post" }, { ...o, map: { thequartering: "zac" } }), { spec: "zac", from: "project" }, "the project overrides the cast"); assert.deepEqual(resolvePieceVoice({ handle: "TheQuartering", role: "post" }, { ...o, map: { thequartering: "zac" } }), { engine: "orpheus", voice: "zac", from: "project" }); assert.deepEqual(voiceSourceOf({ handle: "TheQuartering", role: "post" }, { ...o, overrides: { thequartering: "leo" } }), { spec: "leo", from: "override" }); assert.deepEqual(voiceSourceOf({ handle: "TheQuartering", role: "post" }, { ...o, voice: "dan" }), { spec: "dan", from: "override" }, "--voice: his post"); assert.deepEqual(voiceSourceOf({ handle: "someone", role: "parent:0" }, o), { spec: "leo", from: "default" }); assert.deepEqual(voiceSourceOf({ handle: "someone", role: "parent:0" }, { ...o, map: { "*": "dan" } }), { spec: "dan", from: "project" }); assert.deepEqual(voiceSourceOf({ handle: "nobody", role: "post" }, { ...o, self: "nobody" }), { spec: "zac", from: "narrator" }); // The cast never beats a map entry, nor does "*" beat the cast. assert.equal(voiceForPiece({ handle: "MadamSavvy", role: "quoted" }, { ...o, map: { "*": "dan" } }), "orpheus:tara"); }); const post = (id, handle, take, o = {}) => ({ type: "post", id, handle, author: handle, narration: { script: "words", takes: take ? [take] : [], pick: 0 }, ...o }); test("takeReads: a single read is his post; a thread read by its parts; a recording and a conversion say so", () => { const e = post("p-1", "TheQuartering", { file: "narration/posts/1/take-1.orpheus-zac.wav", engine: "orpheus", voice: "zac", seconds: 1 }); assert.deepEqual(takeReads(e), [{ handle: "TheQuartering", role: "post", engine: "orpheus", voice: "zac", kind: "tts" }]); const thread = post("p-2", "TheQuartering", { file: "narration/posts/2/take-1.orpheus-thread.wav", engine: "orpheus", voice: "tara+am_michael", seconds: 2, read: { kind: "thread", parents: "0" }, parts: [{ n: 1, role: "parent:0", voice: "tara", start: 0, seconds: 1 }, { n: 2, role: "post", voice: "am_michael", engine: "kokoro", start: 1, seconds: 1 }], }, { parents: [{ id: "9", handle: "MadamSavvy" }] }); assert.deepEqual(takeReads(thread).map((r) => [r.handle, r.role, r.engine, r.voice]), [["MadamSavvy", "parent:0", "orpheus", "tara"], ["TheQuartering", "post", "kokoro", "am_michael"]]); const rec = post("p-3", "TheQuartering", { file: "narration/posts/3/take-1.recorded-operator.wav", engine: "recorded", voice: "operator", seconds: 1 }); assert.equal(takeReads(rec)[0].kind, "recorded"); const conv = post("p-4", "TheQuartering", { file: "narration/posts/4/take-2.convert-seed-vc.wav", engine: "derived", voice: "am_michael", seconds: 1, source: { kind: "take", from: "take-1.recorded-operator" }, steps: [{ op: "convert", engine: "seed-vc", voice: "kokoro:am_michael" }] }); assert.deepEqual(takeReads(conv)[0], { handle: "TheQuartering", role: "post", engine: "kokoro", voice: "am_michael", kind: "converted" }); }); test("voiceDrift and castDrift: a cast character read in another voice is drift; a project override is shown, not drift", () => { const manifest = { render: {}, timeline: [ post("p-1", "TheQuartering", { file: "a.wav", engine: "orpheus", voice: "zac", seconds: 1 }), post("p-2", "TheQuartering", { file: "b.wav", engine: "kokoro", voice: "am_michael", seconds: 1 }), post("p-3", "MadamSavvy", { file: "c.wav", engine: "orpheus", voice: "leah", seconds: 1 }), post("p-4", "TheQuartering", { file: "d.wav", engine: "recorded", voice: "operator", seconds: 1 }), post("p-5", "someone", { file: "e.wav", engine: "orpheus", voice: "zac", seconds: 1 }), ], }; const map = { madamsavvy: "leah" }; const castMap = castVoiceMap(cast); const resolve = (e, role, handle) => resolvePieceVoice({ handle, role }, { map, cast: castMap, self: e.handle, narrator: "zac", engine: "orpheus" }); const all = voiceDrift(manifest, resolve); assert.deepEqual(all.map((d) => [d.id, d.reads, d.want, d.from]), [["p-1", "orpheus:zac", "kokoro:am_michael", "cast"]]); const r = castDrift(manifest, { cast, map, resolve }); assert.deepEqual(r.drift.map((d) => [d.id, d.character]), [["p-1", "thequartering"]]); assert.deepEqual(r.overrides, [{ handle: "madamsavvy", character: "savvy", cast: "orpheus:tara", project: "leah" }]); });