import { test } from "node:test"; import assert from "node:assert/strict"; import { DEFAULT_TTS_ENGINE_ID, DEFAULT_TTS_VOICE, KOKORO_FILES, TTS_ENGINES, getTtsEngine, listTtsEngines, ttsDisclosure, ttsEngineIds, ttsInstallDir, ttsVenvPython, ORPHEUS_FILES, ORPHEUS_SAMPLER, ORPHEUS_SEED_BASE, normalizeOrpheusAsk, } from "./ttsApps"; import { DEFAULT_TTS_SETTINGS, sanitizeTts } from "./ttsSettings"; import { ORPHEUS_DEFAULT_TAKES, ORPHEUS_TUNING_DEFAULTS } from "./ttsTuning.mjs"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test lib/ttsApps.test.ts const BINS = { ffmpegBin: "/bin/ffmpeg", espeakNgBin: "/bin/espeak-ng" }; test("the registry: kokoro (af_heart) is the default, espeak-ng the zero-install fallback, orpheus the GPU one", () => { assert.deepEqual(ttsEngineIds(), ["kokoro", "espeak-ng", "orpheus"]); assert.equal(DEFAULT_TTS_ENGINE_ID, "kokoro"); assert.equal(DEFAULT_TTS_VOICE, "af_heart"); assert.equal(getTtsEngine("kokoro")!.defaultVoice, "af_heart"); // A typo is not silently the default. assert.equal(getTtsEngine("kokoro2"), undefined); assert.equal(getTtsEngine(undefined), undefined); for (const e of Object.values(TTS_ENGINES)) { assert.ok(e.voices.includes(e.defaultVoice), e.id); assert.ok(e.license.length > 0, e.id); assert.ok(e.model.length > 0, e.id); assert.equal(e.device, e.id === "orpheus" ? "gpu" : "cpu", e.id); assert.equal(e.runtime, e.id === "orpheus" ? "server" : "process", e.id); assert.ok(e.speedRange[0] <= 1 && e.speedRange[1] >= 1, e.id); } assert.deepEqual( listTtsEngines().map((e) => [e.id, e.defaultVoice]), [["kokoro", "af_heart"], ["espeak-ng", "en-us"], ["orpheus", "zac"]], ); }); test("Kokoro offers its English stock voices only, and pins its model files", () => { const k = getTtsEngine("kokoro")!; assert.equal(k.voices.length, 28); assert.ok(k.voices.every((v) => /^[ab][fm]_[a-z]+$/.test(v)), k.voices.join(" ")); assert.deepEqual(KOKORO_FILES.map((f) => f.name), ["kokoro-v1.0.onnx", "voices-v1.0.bin"]); for (const f of KOKORO_FILES) { assert.match(f.sha256, /^[0-9a-f]{64}$/); assert.ok(f.bytes > 1_000_000); assert.ok(f.url.startsWith("https://github.com/thewh1teagle/kokoro-onnx/releases/download/model-files-v1.0/")); assert.ok(f.url.endsWith(`/${f.name}`)); } assert.equal(k.setup.kind, "python-venv"); if (k.setup.kind === "python-venv") assert.deepEqual(k.setup.packages, ["kokoro-onnx==0.6.1"]); }); test("each engine builds the wrapper's command line from its install dir", () => { const home = "/h/tts"; const k = getTtsEngine("kokoro")!; const dir = ttsInstallDir(home, k); assert.equal(dir, "/h/tts/kokoro"); assert.equal(ttsVenvPython(dir), "/h/tts/kokoro/venv/bin/python"); const kb = k.build({ textFile: "/c/x.txt", outWav: "/c/x.wav", voice: "bf_emma", speed: 0.9, installDir: dir, bins: BINS }); assert.deepEqual(kb.argv, [ "--engine-kind", "kokoro", "--python", "/h/tts/kokoro/venv/bin/python", "--model", "/h/tts/kokoro/kokoro-v1.0.onnx", "--voices", "/h/tts/kokoro/voices-v1.0.bin", "--voice", "bf_emma", "--speed", "0.9", "--ffmpeg", "/bin/ffmpeg", "--output", "/c/x.wav", "/c/x.txt", ]); const e = getTtsEngine("espeak-ng")!; const eb = e.build({ textFile: "/c/y.txt", outWav: "/c/y.wav", voice: "en-gb", speed: 1, installDir: ttsInstallDir(home, e), bins: BINS }); assert.deepEqual(eb.argv, [ "--engine-kind", "espeak-ng", "--espeak", "/bin/espeak-ng", "--voice", "en-gb", "--speed", "1", "--ffmpeg", "/bin/ffmpeg", "--output", "/c/y.wav", "/c/y.txt", ]); }); test("Kokoro and espeak-ng strip the expressive tags and speak years", () => { for (const id of ["kokoro", "espeak-ng"]) { assert.equal( getTtsEngine(id)!.normalize("Back in 1999 we said\nso."), "Back in nineteen ninety-nine we said so.", id, ); } }); test("the disclosure names the engine and voice, plus any attribution its licence asks for", () => { assert.equal( ttsDisclosure(getTtsEngine("kokoro")!, "af_heart"), "Narration: synthetic voice (Kokoro-82M · af_heart)", ); assert.equal( ttsDisclosure({ label: "Some Engine", attribution: "Built with Something." }, "v1"), "Narration: synthetic voice (Some Engine · v1). Built with Something.", ); }); test("Orpheus: eight stock voices, zac by default, no speed control, the Llama licence and attribution", () => { const o = getTtsEngine("orpheus")!; assert.deepEqual([...o.voices], ["tara", "leah", "jess", "leo", "dan", "mia", "zac", "zoe"]); assert.equal(o.defaultVoice, "zac"); assert.ok(o.voices.includes("leah")); assert.deepEqual([...o.speedRange], [1, 1]); assert.equal(o.license, "Llama 3.2 Community License + Apache-2.0"); assert.equal(ttsDisclosure(o, "leah"), "Narration: synthetic voice (Orpheus 3B · leah). Built with Llama"); assert.equal(o.setup.kind, "python-venv"); if (o.setup.kind === "python-venv") { assert.deepEqual(o.setup.packages, ["onnxruntime==1.31.0", "numpy==2.5.4"]); assert.deepEqual(o.setup.files.map((f) => f.name), ["orpheus-3b-0.1-ft-Q8_0.gguf", "snac_decoder.onnx", "snac_config.json"]); } for (const f of ORPHEUS_FILES) assert.match(f.sha256, /^[0-9a-f]{64}$/); // Tags are performed, so kept; the guarded year rule; paragraphs kept for the chunker. assert.equal( o.normalize("Back in 1999 1950 people\n\nsaid so."), "Back in nineteen ninety-nine one thousand nine hundred fifty people\n\nsaid so.", ); }); test("Orpheus's server command comes from settings.tts.orpheus, never hard-coded", () => { const o = getTtsEngine("orpheus")!; const cmd = o.serverCommand!("/h/tts/orpheus", DEFAULT_TTS_SETTINGS); assert.equal(cmd.bin, "/usr/lib/ollama/llama-server"); assert.deepEqual(cmd.env, { LD_LIBRARY_PATH: "/usr/lib/ollama", GGML_BACKEND_PATH: "/usr/lib/ollama/vulkan/libggml-vulkan.so", }); assert.deepEqual(cmd.args, [ "-m", "/h/tts/orpheus/orpheus-3b-0.1-ft-Q8_0.gguf", "--host", "127.0.0.1", "--port", "8799", "-ngl", "99", "-c", "8192", "-np", "1", "--no-webui", ]); assert.equal(cmd.estimateMB, 4000); const mine = sanitizeTts({ engine: "orpheus", orpheus: { serverBin: "/opt/llama/llama-server", libraryPath: "", ggmlBackendPath: "", port: 9001, gpuLayers: 0, idleStopSeconds: 30 }, }); const c2 = o.serverCommand!("/h/tts/orpheus", mine); assert.equal(c2.bin, "/opt/llama/llama-server"); assert.deepEqual(c2.env, {}); assert.equal(c2.port, 9001); assert.ok(c2.args.includes("0")); assert.equal(c2.idleStopMs, 30_000); assert.deepEqual(o.requires!(mine), [{ what: "llama-server (settings.tts.orpheus.serverBin)", path: "/opt/llama/llama-server" }]); }); test("Orpheus's input is the editor's chunks, budgets, sampler and takes; the wrapper gets the server", () => { const o = getTtsEngine("orpheus")!; const spoken = o.normalize("One file. No title. Just a date.\n\nAnd then... there it was. "); const input = JSON.parse(o.prepareInput!(spoken, "zac", DEFAULT_TTS_SETTINGS)); assert.equal(input.voice, "zac"); assert.deepEqual(input.sampler, { ...ORPHEUS_SAMPLER }); assert.equal(input.sampler.repeat_last_n, 64); assert.equal(input.seedBase, 1234); assert.equal(input.takes, 3); assert.equal(input.tagCheck, false); assert.deepEqual( input.chunks.map((c: { text: string; ends: string; pauseAfter: number }) => [c.text, c.ends, c.pauseAfter]), [ ["One file. No title. Just a date.", "paragraph", 0.4], ["And then... there it was. ", "end", 0], ], ); assert.ok(input.chunks.every((c: { nPredict: number }) => c.nPredict > 0 && c.nPredict % 7 === 0)); assert.deepEqual(o.keyExtra!(DEFAULT_TTS_SETTINGS), { takes: 3, tagCheck: false }); const b = o.build({ textFile: "/c/x.json", outWav: "/c/x.wav", voice: "zac", speed: 1, installDir: "/h/tts/orpheus", bins: BINS, server: "http://127.0.0.1:8799", }); assert.deepEqual(b.argv, [ "--engine-kind", "orpheus", "--python", "/h/tts/orpheus/venv/bin/python", "--snac", "/h/tts/orpheus/snac_decoder.onnx", "--server", "http://127.0.0.1:8799", "--ffmpeg", "/bin/ffmpeg", "--output", "/c/x.wav", "/c/x.json", ]); }); // --- a tuning and a reading (lane orpheus-tuning, S1) ----------------------------- // Orpheus's input.json for "One file. No title. \n\nThen in 2024, it came back." in zac, captured // on main 976efc77e BEFORE the tuning existed: an ask without one must give these bytes exactly. const GOLDEN_INPUT_MAIN = "{\"version\":1,\"voice\":\"zac\",\"sampler\":{\"temperature\":0.55,\"top_p\":0.85,\"top_k\":50,\"min_p\":0,\"repeat_penalty\":1.2,\"repeat_last_n\":64,\"presence_penalty\":0,\"frequency_penalty\":0},\"seedBase\":1234,\"takes\":3,\"tagCheck\":false,\"chunks\":[{\"text\":\"One file. No title. \",\"ends\":\"paragraph\",\"pauseAfter\":0.4,\"nPredict\":525},{\"text\":\"Then in twenty twenty-four, it came back.\",\"ends\":\"end\",\"pauseAfter\":0,\"nPredict\":525}]}"; test("no tuning and no reading: Orpheus's input.json and key are main's, byte for byte -- the defaults spelled out too", () => { const o = getTtsEngine("orpheus")!; const spoken = o.normalize("One file. No title. \n\nThen in 2024, it came back."); assert.equal(o.prepareInput!(spoken, "zac", DEFAULT_TTS_SETTINGS), GOLDEN_INPUT_MAIN); assert.equal(o.prepareInput!(spoken, "zac", DEFAULT_TTS_SETTINGS, {}), GOLDEN_INPUT_MAIN); const defaults = { tuning: { temperature: 0.55, top_p: 0.85, top_k: 50, min_p: 0, repeat_penalty: 1.2, takes: 3 }, reading: 0, }; assert.equal(o.prepareInput!(spoken, "zac", DEFAULT_TTS_SETTINGS, defaults), GOLDEN_INPUT_MAIN); assert.deepEqual(normalizeOrpheusAsk(DEFAULT_TTS_SETTINGS, defaults), {}); for (const ask of [undefined, {}, defaults]) { assert.deepEqual(o.keyExtra!(DEFAULT_TTS_SETTINGS, ask), { takes: 3, tagCheck: false }, JSON.stringify(ask)); } // The report: every value, the defaults included. assert.deepEqual(o.tuned!(DEFAULT_TTS_SETTINGS), { tuning: { temperature: 0.55, top_p: 0.85, top_k: 50, min_p: 0, repeat_penalty: 1.2, takes: 3 }, reading: 0, seedBase: 1234, }); }); test("a tuning changes only what it names; a reading moves the seed base by 9973", () => { const o = getTtsEngine("orpheus")!; const spoken = o.normalize("One line."); const input = JSON.parse(o.prepareInput!(spoken, "leah", DEFAULT_TTS_SETTINGS, { tuning: { temperature: 0.7, top_k: 40, takes: 5 }, reading: 2 })); assert.deepEqual(input.sampler, { ...ORPHEUS_SAMPLER, temperature: 0.7, top_k: 40 }); // The sampler's keys keep the constant's order. assert.deepEqual(Object.keys(input.sampler), Object.keys(ORPHEUS_SAMPLER)); assert.equal(input.seedBase, ORPHEUS_SEED_BASE + 2 * 9973); assert.equal(input.takes, 5); assert.equal(input.tagCheck, false); assert.deepEqual( o.keyExtra!(DEFAULT_TTS_SETTINGS, { tuning: { temperature: 0.7, top_k: 40, takes: 5, min_p: 0 }, reading: 2 }), { takes: 5, tagCheck: false, sampler: { temperature: 0.7, top_k: 40 }, reading: 2 }, ); assert.deepEqual(o.tuned!(DEFAULT_TTS_SETTINGS, { tuning: { repeat_penalty: 1.1 }, reading: 1 }), { tuning: { temperature: 0.55, top_p: 0.85, top_k: 50, min_p: 0, repeat_penalty: 1.1, takes: 3 }, reading: 1, seedBase: 11_207, }); // A tuning's takes equal to the SETTINGS' (not the default's) is no tuning: the key it would have anyway. const five = sanitizeTts({ engine: "orpheus", orpheus: { takes: 5 } }); assert.deepEqual(normalizeOrpheusAsk(five, { tuning: { takes: 5 } }), {}); assert.deepEqual(o.keyExtra!(five, { tuning: { takes: 3 } }), o.keyExtra!(DEFAULT_TTS_SETTINGS)); }); test("the shared defaults (lib/ttsTuning.mjs) are ORPHEUS_SAMPLER's and the settings' takes", () => { for (const [k, v] of Object.entries(ORPHEUS_TUNING_DEFAULTS)) { assert.equal(ORPHEUS_SAMPLER[k as keyof typeof ORPHEUS_SAMPLER], v, k); } assert.equal(DEFAULT_TTS_SETTINGS.orpheus.takes, ORPHEUS_DEFAULT_TAKES); }); test("kokoro and espeak-ng take no tuning: no key extra, no report", () => { for (const id of ["kokoro", "espeak-ng"]) { const e = getTtsEngine(id)!; assert.equal(e.tuned, undefined, id); assert.equal(e.keyExtra, undefined, id); } });