// A character is a preset (cast.mjs): version 1's `voice` / `convert` read as the synthetic and recorded presets and // back (a view), what each layer refuses, a project's own characters (anonymous ones, "Anon 1"), the chain one read // asks -- the command's flags, the item's own character, the project's, the cast's, the narrator's, the default // (post-say.mjs voiceSourceOf for a synthesized read; characterForRead for a recording) -- and the speed a part is // asked at. import assert from "node:assert/strict"; import { test } from "node:test"; import { ANON_NAME_PATTERN, CAST_VERSION, PROJECT_CHARACTERS_FILE, castPresetMap, castVoiceMap, characterById, characterForRead, characterView, charactersOf, convertTargetOf, emptyCast, foldWritten, emptyProjectCharacters, itemCharacterOf, itemPresetOf, nextAnonName, projectCharactersOf, projectPresetMap, recordedPresetOf, shownNameOf, upgradeCast, upgradeCharacter, validateCast, validateProjectCharacters, } from "./cast.mjs"; import { readSpeedOf, resolvePieceVoice, voiceSourceOf } from "./post-say.mjs"; // Version 1, as the files on disk are today. const V1 = { format: "umtool-cast", version: 1, characters: [ { id: "thequartering", name: "TheQuartering", handles: ["thequartering"], voice: "kokoro:am_michael", speakers: ["Jeremy"], note: "the host" }, { id: "savvy", name: "MadamSavvy", handles: ["madamsavvy"], voice: "orpheus:tara", convert: { engine: "seed-vc", pitch: "contour", semitones: 2 }, narrator: true }, ], }; test("migration on read: version 1's voice and convert are the synthetic and recorded presets, the version bumps", () => { assert.deepEqual(validateCast(V1), [], "a version 1 file is fine as it is"); const up = upgradeCast(V1); assert.equal(up.version, CAST_VERSION); assert.equal(CAST_VERSION, 2); assert.deepEqual(up.characters[0], { id: "thequartering", name: "TheQuartering", handles: ["thequartering"], synthetic: { voice: "kokoro:am_michael" }, speakers: ["Jeremy"], note: "the host" }); assert.deepEqual(up.characters[1], { id: "savvy", name: "MadamSavvy", handles: ["madamsavvy"], synthetic: { voice: "orpheus:tara" }, recorded: { convert: { engine: "seed-vc", pitch: "contour", semitones: 2 } }, narrator: true }); assert.deepEqual(validateCast(up), []); assert.deepEqual(upgradeCast(up), up, "upgrading version 2 changes nothing"); assert.equal(V1.characters[0].synthetic, undefined, "a copy: the document read is not touched"); // Not a cast, or a version nobody reads: left as it is, and refused in words. assert.equal(upgradeCast({ format: "x", version: 1, characters: [] }).version, 1); assert.match(validateCast({ ...V1, version: 3 })[0], /version must be 2 \(or 1, read as it\)/); }); test("migration the other way: a reader is handed the presets AND the voice / convert they answer to", () => { const v2 = upgradeCast(V1); const views = charactersOf(v2); assert.equal(views[0].voice, "kokoro:am_michael"); assert.deepEqual(views[0].synthetic, { voice: "kokoro:am_michael" }); assert.deepEqual(views[1].convert, { engine: "seed-vc", pitch: "contour", semitones: 2 }); assert.deepEqual(charactersOf(V1), views, "either version reads the same"); assert.deepEqual(castVoiceMap(V1), castVoiceMap(v2)); assert.deepEqual(convertTargetOf(V1.characters[1]), convertTargetOf(v2.characters[1])); assert.deepEqual(convertTargetOf(v2.characters[1]), { engine: "seed-vc", voice: "orpheus:tara", pitch: "contour", semitones: 2 }); // A view written back is the same character: its `voice` says what its preset says. assert.deepEqual(upgradeCharacter(views[1]), v2.characters[1]); assert.deepEqual(characterView(views[1]), views[1]); // A VIEW WHOSE PRESET WAS CHANGED folds to the new value: the preset wins over its own (now stale) alias. const edited = { ...views[0], synthetic: { ...views[0].synthetic, voice: "kokoro:am_onyx", speed: 1.1 } }; assert.equal(edited.voice, "kokoro:am_michael", "the alias still says the old voice"); assert.deepEqual(upgradeCharacter(edited).synthetic, { voice: "kokoro:am_onyx", speed: 1.1 }); assert.equal(characterView(edited).voice, "kokoro:am_onyx", "and a view of it says the new one"); assert.deepEqual(upgradeCharacter({ ...views[1], recorded: { convert: { engine: "openvoice" } } }).recorded, { convert: { engine: "openvoice" } }); // A WRITE's mutation that sets `c.voice` the old way, on the character it was handed (no alias): the newer word. const handed = v2.characters[0]; assert.deepEqual(foldWritten({ ...handed, voice: "kokoro:am_onyx" }, handed).synthetic, { voice: "kokoro:am_onyx" }); assert.deepEqual(foldWritten({ ...v2.characters[1], convert: { engine: "openvoice" } }, v2.characters[1]).recorded, { convert: { engine: "openvoice" } }); assert.deepEqual(foldWritten({ ...handed, synthetic: { voice: "kokoro:am_michael", speed: 1.1 }, voice: "kokoro:am_onyx" }, handed).synthetic, { voice: "kokoro:am_onyx", speed: 1.1 }, "its speed kept"); // …but a mutation that changed the preset itself wins over a stale alias beside it (a view written back). assert.deepEqual(foldWritten(edited, handed).synthetic, { voice: "kokoro:am_onyx", speed: 1.1 }); assert.deepEqual(foldWritten({ id: "new", name: "New", handles: [], voice: "kokoro:am_echo" }, null).synthetic, { voice: "kokoro:am_echo" }, "a new character the old way"); assert.equal(foldWritten(handed, handed).voice, undefined, "no alias is written"); }); test("validation: the presets' shape, in the cast -- a voice and a convert target are STOCK voices, never a file", () => { const one = (c) => validateCast({ ...emptyCast(), characters: [{ id: "q", name: "Q", handles: ["q"], synthetic: { voice: "kokoro:am_michael" }, ...c }] }); assert.deepEqual(one({}), []); assert.deepEqual(one({ synthetic: { voice: "kokoro:am_michael", speed: 1.15 }, recorded: { convert: { engine: "seed-vc", voice: "kokoro:am_onyx", pitch: "source" }, enhance: false, trim: { on: true, padMs: 120 }, direction: "higher, brisk, amused" } }), []); assert.ok(one({ synthetic: undefined }).some((e) => /voice must be a STOCK voice with its engine/.test(e)), "a cast character has a stock voice"); assert.ok(one({ synthetic: { voice: "/home/me/jeremy.wav" } }).some((e) => /STOCK voice with its engine/.test(e))); assert.ok(one({ synthetic: { voice: "kokoro:am_michael", speed: 3 } }).some((e) => /synthetic\.speed must be a number from 0\.5 to 2/.test(e))); assert.ok(one({ synthetic: { voice: "kokoro:am_michael", emotion: "on" } }).some((e) => /synthetic must be \{voice, speed\?, tuning\?\}/.test(e))); assert.ok(one({ recorded: { convert: { voice: "me.wav" } } }).some((e) => /recorded\.convert\.voice must be a STOCK voice.*never a file or a recording/.test(e))); assert.ok(one({ recorded: { convert: { voice: "seedvc:/home/me.wav" } } }).some((e) => /never a file or a recording/.test(e))); assert.ok(one({ recorded: { convert: { engine: "openvoice", pitch: "contour" } } }).some((e) => /pitch "contour" is seed-vc's/.test(e))); assert.ok(one({ recorded: { enhance: "yes" } }).some((e) => /recorded\.enhance must be true or false/.test(e))); assert.ok(one({ recorded: { trim: { on: "off" } } }).some((e) => /recorded\.trim must be \{on\?: true \| false, padMs\?\}/.test(e))); assert.ok(one({ recorded: { trim: { padMs: 5000 } } }).some((e) => /trim\.padMs must be a number of milliseconds from 0 to 2000/.test(e))); assert.ok(one({ recorded: { direction: "two\nlines" } }).some((e) => /direction must be one line for the person reading/.test(e))); assert.ok(one({ recorded: { direction: "x".repeat(201) } }).some((e) => /at most 200/.test(e))); assert.ok(one({ recorded: { model: "x" } }).some((e) => /recorded must be \{convert\?, enhance\?, trim\?, direction\?\}/.test(e))); assert.ok(one({ anonymous: true }).some((e) => /anonymous is not a character key/.test(e)), "an anonymous character is never the pool's"); }); test("a project's own characters: the same shape, a preset optional, anonymous ones under a shown name, no narrator", () => { const doc = (chars) => ({ ...emptyProjectCharacters(), characters: chars }); assert.equal(PROJECT_CHARACTERS_FILE, "narration/characters.json"); assert.deepEqual(validateProjectCharacters(doc([])), []); const anon = { id: "anon-1", name: "the reply guy", anonymous: true, shown: "Anon 1", handles: ["replyguy99"], synthetic: { voice: "kokoro:am_puck", speed: 0.9 } }; const bare = { id: "witness", name: "A witness", handles: ["w1"] }; assert.deepEqual(validateProjectCharacters(doc([anon, bare])), [], "a character with no preset leaves the voice to the layers under it"); assert.match(validateProjectCharacters({ format: "umtool-cast", version: 2, characters: [] })[0], /format must be "umtool-project-characters"/); assert.ok(validateProjectCharacters(doc([{ ...anon, narrator: true }])).some((e) => /narrator is not a character key/.test(e))); assert.ok(validateProjectCharacters(doc([{ ...bare, shown: "Anon 2" }])).some((e) => /shown is an anonymous character's shown name/.test(e))); assert.ok(validateProjectCharacters(doc([{ ...anon, anonymous: false }])).some((e) => /anonymous must be true or absent/.test(e))); assert.ok(validateProjectCharacters(doc([anon, { ...bare, handles: ["replyguy99"] }])).some((e) => /@replyguy99 is anon-1's already/.test(e))); assert.ok(validateProjectCharacters(doc([anon, { ...bare, id: "anon-1" }])).some((e) => /two characters are "anon-1"/.test(e))); // Both layers refuse a convert target that is not a stock synthetic voice. assert.ok(validateProjectCharacters(doc([{ ...bare, recorded: { convert: { voice: "narration/me.wav" } } }])).some((e) => /never a file or a recording/.test(e))); assert.ok(validateProjectCharacters(doc([{ ...bare, synthetic: { voice: "operator" } }])).some((e) => /STOCK voice with its engine/.test(e))); // The shown name: an anonymous character's, never its own; the next unused one of the pattern. assert.equal(ANON_NAME_PATTERN, "Anon {n}"); assert.equal(shownNameOf(anon), "Anon 1"); assert.equal(shownNameOf(bare), "A witness"); assert.equal(nextAnonName([]), "Anon 1"); assert.equal(nextAnonName([anon, bare]), "Anon 2"); assert.equal(nextAnonName([anon, { id: "b", name: "x", anonymous: true, shown: "anon 2" }]), "Anon 3", "any case"); assert.deepEqual(projectCharactersOf(doc([anon, bare])).map((c) => [c.id, c.voice ?? null]), [["anon-1", "kokoro:am_puck"], ["witness", null]]); }); // The layers of one project: the cast (V1), the project's own characters, its voice map. const cast = upgradeCast(V1); const own = { ...emptyProjectCharacters(), characters: [ { id: "anon-1", name: "the reply guy", anonymous: true, shown: "Anon 1", handles: ["replyguy99"], synthetic: { voice: "kokoro:am_puck", speed: 0.9 }, recorded: { enhance: false, direction: "flat, tired" } }, { id: "savvy-here", name: "Savvy, this video", handles: ["madamsavvy"], synthetic: { voice: "kokoro:af_bella", speed: 1.2 } }, { id: "witness", name: "A witness", handles: ["thequartering"] }, { id: "guest-star", name: "Guest star", handles: [], synthetic: { voice: "kokoro:bm_george", speed: 1.1 }, recorded: { convert: { voice: "kokoro:bm_lewis" }, trim: { on: false } } }, ], }; const voiceMap = { madamsavvy: "leah", someone: "dan", "*": "zoe" }; test("the layers as voiceSourceOf takes them: the project's characters over its voice map, the cast with its speeds", () => { const map = projectPresetMap(own, voiceMap); assert.deepEqual(map, { madamsavvy: { voice: "kokoro:af_bella", speed: 1.2, character: "savvy-here" }, someone: "dan", "*": "zoe", replyguy99: { voice: "kokoro:am_puck", speed: 0.9, character: "anon-1" }, }, "a character with a voice wins its handle; one with none (witness) leaves it; voices.json is kept for the rest"); assert.deepEqual(projectPresetMap(null, voiceMap), voiceMap, "no characters.json: the voice map as it is"); assert.deepEqual(castPresetMap(cast), { thequartering: { voice: "kokoro:am_michael", character: "thequartering" }, madamsavvy: { voice: "orpheus:tara", character: "savvy" } }); assert.deepEqual(characterById("savvy", { project: own, cast })?.layer, "cast"); assert.deepEqual(characterById("anon-1", { project: own, cast })?.layer, "project"); assert.equal(characterById("nobody", { project: own, cast }), null); }); test("the chain for a synthesized read, first hit wins: flags, the item's character, the project's, the cast's, the narrator's, the default", () => { const e = { id: "p-1", handle: "MadamSavvy", narration: { character: "guest-star" } }; const layers = { project: own, cast }; const full = { map: projectPresetMap(own, voiceMap), cast: castPresetMap(cast), item: itemPresetOf(e, layers), self: e.handle, narrator: "zac", engine: "orpheus" }; const her = { handle: "MadamSavvy", role: "post" }; // 1. the command's flags. assert.deepEqual(voiceSourceOf(her, { ...full, overrides: { madamsavvy: "leo" }, voice: "dan" }), { spec: "leo", from: "override" }); assert.deepEqual(voiceSourceOf(her, { ...full, voice: "dan" }), { spec: "dan", from: "override" }); // 2. the item's own character. assert.deepEqual(voiceSourceOf(her, full), { spec: "kokoro:bm_george", from: "item", speed: 1.1, character: "guest-star" }); assert.deepEqual(resolvePieceVoice(her, full), { engine: "kokoro", voice: "bm_george", from: "item", speed: 1.1, character: "guest-star" }); // 3. the project's character for the handle -- over its own voice map, which is next. assert.deepEqual(voiceSourceOf(her, { ...full, item: null }), { spec: "kokoro:af_bella", from: "project", speed: 1.2, character: "savvy-here" }); assert.deepEqual(voiceSourceOf(her, { ...full, item: null, map: voiceMap }), { spec: "leah", from: "project" }, "voices.json alone: a project character with only a synthetic voice"); // 4. the cast's. assert.deepEqual(voiceSourceOf(her, { ...full, item: null, map: {} }), { spec: "orpheus:tara", from: "cast", character: "savvy" }); // 5. the narrator's (the item's own author), 6. the map's "*", the default. assert.deepEqual(voiceSourceOf(her, { ...full, item: null, map: {}, cast: {} }), { spec: "zac", from: "narrator" }); assert.deepEqual(voiceSourceOf({ handle: "stranger", role: "parent:0" }, full), { spec: "zoe", from: "project" }); assert.deepEqual(voiceSourceOf({ handle: "stranger", role: "parent:0" }, { ...full, map: {} }), { spec: "leo", from: "default" }); // The item's character is its OWN words' -- another author's piece of the same read asks its own layers. assert.deepEqual(voiceSourceOf({ handle: "replyguy99", role: "parent:0" }, full), { spec: "kokoro:am_puck", from: "project", speed: 0.9, character: "anon-1" }); assert.deepEqual(voiceSourceOf({ handle: "TheQuartering", role: "quoted" }, full), { spec: "kokoro:am_michael", from: "cast", character: "thequartering" }, "a project character with no voice leaves it to the cast"); // An item naming a character nobody has, or one with no voice: the layers under it. assert.deepEqual(itemCharacterOf({ narration: { character: "ghost" } }, layers), { id: "ghost", character: null, layer: null }); assert.equal(itemPresetOf({ narration: { character: "ghost" } }, layers), null); assert.equal(itemPresetOf({ narration: { character: "witness" } }, layers), null); assert.equal(itemPresetOf({ narration: {} }, layers), null); }); test("the speed a part is asked at: the flag, the item's own, the preset's -- none for Orpheus, or at 1", () => { assert.equal(readSpeedOf({ flag: 1.3, preset: 1.1, own: 0.9, engine: "kokoro" }), 1.3); assert.equal(readSpeedOf({ preset: 1.1, own: 0.9, engine: "kokoro" }), 0.9, "set on the one item: it beats its character's"); assert.equal(readSpeedOf({ preset: 1.1, engine: "kokoro" }), 1.1); assert.equal(readSpeedOf({ own: 0.9, engine: "kokoro" }), 0.9); assert.equal(readSpeedOf({ engine: "kokoro" }), null); assert.equal(readSpeedOf({ flag: 1.3, engine: "orpheus" }), null, "Orpheus has no speed"); assert.equal(readSpeedOf({ flag: 1, preset: 1.2, engine: "kokoro" }), null, "the flag wins, and 1 is no speed"); }); test("the chain for a recording: --as, the item's character, the project's for the author, the cast's, the narrator", () => { const layers = { project: own, cast }; const e = { id: "p-1", handle: "MadamSavvy", narration: { character: "guest-star" } }; assert.deepEqual([characterForRead(e, { ...layers, as: "anon-1" }).character.id, characterForRead(e, { ...layers, as: "anon-1" }).layer], ["anon-1", "flag"]); assert.equal(characterForRead(e, { ...layers, as: "@replyguy99" }).character.id, "anon-1", "by a handle"); assert.equal(characterForRead(e, { ...layers, as: "TheQuartering" }).character.id, "thequartering", "the cast's, when the project has none of that name"); assert.deepEqual(characterForRead(e, { ...layers, as: "nobody" }), { character: null, layer: "flag" }); assert.deepEqual([characterForRead(e, layers).character.id, characterForRead(e, layers).layer], ["guest-star", "item"]); const plain = { id: "p-2", handle: "MadamSavvy" }; assert.deepEqual([characterForRead(plain, layers).character.id, characterForRead(plain, layers).layer], ["savvy-here", "project"]); assert.deepEqual([characterForRead(plain, { cast }).character.id, characterForRead(plain, { cast }).layer], ["savvy", "cast"]); // A project character with neither a voice nor a recorded preset names someone: it is not what a recording is made into. assert.deepEqual([characterForRead({ handle: "TheQuartering" }, layers).character.id, characterForRead({ handle: "TheQuartering" }, layers).layer], ["thequartering", "cast"]); assert.deepEqual([characterForRead({ handle: "stranger" }, layers).character.id, characterForRead({ handle: "stranger" }, layers).layer], ["savvy", "narrator"]); assert.equal(characterForRead({ handle: "stranger" }, { cast: { ...cast, characters: [cast.characters[0]] } }), null, "nobody to convert to"); }); test("a recorded preset, its defaults filled: cleaned and trimmed unless it says not, the convert target a stock voice", () => { const [anon, , , star] = projectCharactersOf(own); assert.deepEqual(recordedPresetOf(anon), { convert: { engine: null, voice: "kokoro:am_puck" }, enhance: false, trim: { on: true, padMs: null }, direction: "flat, tired" }); assert.deepEqual(recordedPresetOf(star), { convert: { engine: null, voice: "kokoro:bm_lewis" }, enhance: true, trim: { on: false, padMs: null }, direction: null }); assert.deepEqual(recordedPresetOf(charactersOf(cast)[1]), { convert: { engine: "seed-vc", voice: "orpheus:tara", pitch: "contour", semitones: 2 }, enhance: true, trim: { on: true, padMs: null }, direction: null }); assert.deepEqual(recordedPresetOf({ id: "x", name: "X", recorded: { trim: { padMs: 120 } } }).trim, { on: true, padMs: 120 }); });