// narration.mjs: the validator (the build's and the lint's), the resolved // settings, `seconds: "auto"`, the sound's filter, the key and the disclosure. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import test from "node:test"; import { NARRATION_DEFAULTS, NARRATION_TAGS, cardNarrationSeconds, disclosureLines, estimatedNarratedCardSeconds, narratedCardSeconds, narrationAudioFilter, narrationDisclosure, narrationGainDb, narrationKey, narrationWords, narrationsOf, resolveNarration, stripNarrationTags, validateNarration, } from "./narration.mjs"; import { estimatedDuration } from "./deck.mjs"; import { lintManifest, narrationLengthErrors } from "./lint.mjs"; const card = (extra = {}) => ({ type: "card", id: "k1", heading: "Then", seconds: "auto", narration: { text: "Six months later, the county said yes." }, ...extra }); const M = (timeline, render = {}) => ({ render: { transition: 0.5, ...render }, timeline }); test("a valid narration, and a manifest without any, say nothing", () => { assert.deepEqual(validateNarration(M([card()])), []); assert.deepEqual(validateNarration(M([{ type: "card", id: "a", seconds: 4 }, { type: "clip", id: "c" }])), []); assert.deepEqual(validateNarration(M([card()], { narration: { engine: "orpheus", voice: "tara", speed: 1.1, lufs: -16, lead: 1, tail: 0.5 } })), []); }); test("the text: non-empty, the expressive tags only, words capped without the tags", () => { const errs = (text) => validateNarration(M([card({ narration: { text } })])); assert.match(errs(" ")[0], /k1\.narration: text must be non-empty words/); assert.deepEqual(errs("Well that went well ."), []); // Exact, as the editor reads them. assert.match(errs("Well .")[0], /the tag \(did you mean \?\) is not one of/); assert.match(errs("Well < laugh >.")[0], /the tag < laugh > \(did you mean \?\)/); for (const t of NARRATION_TAGS) assert.deepEqual(errs(`Then <${t}> on.`), [], t); const bad = errs("Then on, and this."); assert.equal(bad.length, 3, bad.join("\n")); assert.match(bad[0], /the tag is not one of /); assert.match(bad[1], //); assert.match(bad[2], /<\/b>/); assert.match(errs(" ")[0], /no words outside its tags/); // 300 words pass, 301 do not -- the tags never count. const words = (n) => Array.from({ length: n }, (_, i) => `w${i}`).join(" "); assert.deepEqual(errs(`${words(300)} `), []); assert.match(errs(words(301))[0], /301 words; at most 300 \(tags not counted\)/); assert.equal(narrationWords("A b, c — d!"), 4); assert.equal(stripNarrationTags("A b"), "A b"); }); test("the keys, the engine, the voice, the settings and their bounds", () => { const one = (narration, render) => validateNarration(M([card({ narration })], render)); assert.match(one({ text: "Hi.", tone: "warm" })[0], /k1\.narration\.tone is not a key/); assert.match(one({ text: "Hi.", engine: "piper" })[0], /engine must be one of kokoro, orpheus, espeak-ng/); assert.match(one({ text: "Hi.", voice: "../ref.wav" })[0], /voice must be a stock voice's id/); assert.deepEqual(one({ text: "Hi.", voice: "bm_george", engine: "kokoro" }), []); assert.match(one({ text: "Hi." }, { narration: { speed: 3 } })[0], /render\.narration\.speed must be a number from 0\.5 to 2/); assert.match(one({ text: "Hi." }, { narration: { lufs: -5 } })[0], /lufs must be a number from -30 to -10/); assert.match(one({ text: "Hi." }, { narration: { reference: "x.wav" } })[0], /render\.narration\.reference is not a setting/); assert.match(one({ text: "Hi." }, { narration: "loud" })[0], /render\.narration must be an object/); // The lead and the tail at least the crossfade: no word under a dissolve. const short = one({ text: "Hi." }, { transition: 1, narration: { lead: 0.5 } }); assert.equal(short.length, 2, short.join("\n")); assert.match(short[0], /render\.narration\.lead is 0\.5s, shorter than the 1s crossfade/); assert.match(short[1], /render\.narration\.tail is 0\.8s, shorter than the 1s crossfade/); assert.deepEqual(one({ text: "Hi." }, { transition: 0, narration: { lead: 0, tail: 0 } }), []); }); test("narration on cards only; seconds \"auto\" only with one", () => { const e = validateNarration(M([{ type: "clip", id: "c1", narration: { text: "No." } }, { type: "card", id: "k2", seconds: "auto" }])); assert.equal(e.length, 2, e.join("\n")); assert.match(e[0], /timeline\[0\] c1: narration is on cards and post items only \(this is a clip entry\)/); assert.match(e[1], /timeline\[1\] k2: seconds "auto" is the narration's length, and this entry has no narration/); assert.match(validateNarration(M([card({ seconds: "long" })]))[0], /seconds must be a number of seconds or "auto"/); assert.deepEqual(validateNarration(M([card({ seconds: 6 })])), []); }); test("resolved settings: the card's own, else the manifest's, else kokoro · af_heart", () => { assert.deepEqual(resolveNarration(card(), {}), { text: "Six months later, the county said yes.", engine: "kokoro", voice: "af_heart", speed: 1 }); assert.deepEqual( resolveNarration(card({ narration: { text: " Hi. ", voice: "am_adam" } }), { narration: { voice: "bf_emma", speed: 0.9 } }), { text: "Hi.", engine: "kokoro", voice: "am_adam", speed: 0.9 }, ); assert.equal(resolveNarration({ type: "card", id: "x" }, {}), null); assert.equal(resolveNarration({ type: "clip", id: "x", narration: { text: "a" } }, {}), null); assert.equal(NARRATION_DEFAULTS.lufs, -17.7); }); test("the key moves with the words, the voice and the speed, and nothing else", () => { const base = { text: "Hi.", engine: "kokoro", voice: "af_heart", speed: 1 }; const k = narrationKey(base); assert.match(k, /^[0-9a-f]{64}$/); assert.equal(narrationKey({ ...base }), k); for (const change of [{ text: "Hi!" }, { voice: "am_adam" }, { speed: 1.1 }, { engine: "orpheus" }]) { assert.notEqual(narrationKey({ ...base, ...change }), k, JSON.stringify(change)); } const list = narrationsOf(M([card(), { type: "card", id: "plain", seconds: 3 }, card({ id: "k2" })])); assert.deepEqual(list.map((n) => n.id), ["k1", "k2"]); assert.equal(list[0].key, list[1].key, "the same words, voice and speed are one sound"); }); test("a card's tuning and reading: checked in the editor's ranges, asked and keyed only when set (lane orpheus-tuning)", () => { // Captured on main 976efc77e, before the tuning: a card without one keys exactly as it did. const base = { text: "The archive spoke.", engine: "orpheus", voice: "zac", speed: 1 }; assert.equal(narrationKey(base), "11ec71a5326048c695cb4f228550747bdc6999b00a451b8a8fdb14754856c95d"); assert.equal(narrationKey({ ...base, tuning: {}, reading: 0 }), narrationKey(base)); const warm = narrationKey({ ...base, tuning: { temperature: 0.7 } }); assert.notEqual(warm, narrationKey(base)); assert.notEqual(narrationKey({ ...base, reading: 1 }), narrationKey(base)); assert.notEqual(narrationKey({ ...base, reading: 1 }), narrationKey({ ...base, reading: 2 })); // A card spelling a default -- or a default within rounding -- asks and keys as one without it (the editor's // normaliser, lib/ttsTuning.mjs); takes is kept as written (its default is the editor's settings). assert.equal(narrationKey({ ...base, tuning: { temperature: 0.55, top_p: 0.85, top_k: 50, min_p: -0, repeat_penalty: 1.2 } }), narrationKey(base)); assert.equal(narrationKey({ ...base, tuning: { temperature: 0.550000001 } }), narrationKey(base)); assert.equal(narrationKey({ ...base, tuning: { temperature: 0.7000000000000001 } }), warm); assert.notEqual(narrationKey({ ...base, tuning: { takes: 3 } }), narrationKey(base)); const spelled = resolveNarration(card({ narration: { text: "Six.", engine: "orpheus", tuning: { temperature: 0.55 }, reading: 0 } }), {}); assert.ok(!("tuning" in spelled) && !("reading" in spelled)); // The fields' order does not matter. assert.equal(narrationKey({ ...base, tuning: { top_k: 40, temperature: 0.7 } }), narrationKey({ ...base, tuning: { temperature: 0.7, top_k: 40 } })); const tuned = card({ narration: { text: "Six months later.", engine: "orpheus", voice: "zac", tuning: { temperature: 0.7 }, reading: 2 } }); assert.deepEqual(validateNarration(M([tuned])), []); const n = resolveNarration(tuned, {}); assert.deepEqual({ tuning: n.tuning, reading: n.reading }, { tuning: { temperature: 0.7 }, reading: 2 }); // Not asked: nothing added. const plain = resolveNarration(card(), {}); assert.ok(!("tuning" in plain) && !("reading" in plain)); assert.deepEqual( validateNarration(M([card({ narration: { text: "Words.", tuning: { temperature: 3, seed: 1 }, reading: -1 } })])), [ "timeline[0] k1.narration.tuning.temperature must be a number from 0.1 to 1.5 (got 3)", "timeline[0] k1.narration.tuning.seed is not a tuning field (temperature, top_p, top_k, min_p, repeat_penalty, takes)", "timeline[0] k1.narration.reading must be a whole number from 0 to 99999 (0 = the first reading; got -1)", ], ); }); test("seconds \"auto\": lead + words + tail, up to the tenth; a number is itself, and short when under", () => { assert.equal(narratedCardSeconds(3.21, {}), 4.9); // 0.8 + 3.21 + 0.8 = 4.81 -> 4.9 assert.equal(narratedCardSeconds(3.2, { narration: { lead: 1, tail: 1 } }), 5.2); assert.deepEqual(cardNarrationSeconds(card(), 3.21, {}), { seconds: 4.9, need: 4.9, short: false }); assert.deepEqual(cardNarrationSeconds(card({ seconds: 6 }), 3.21, {}), { seconds: 6, need: 4.9, short: false }); assert.deepEqual(cardNarrationSeconds(card({ seconds: 4.5 }), 3.21, {}), { seconds: 4.5, need: 4.9, short: true }); }); test("before a build, the deck estimates a narrated card from its words", () => { // 7 words at 2.7 a second ≈ 2.59 s, plus 1.6 s of lead and tail -> 4.2. assert.equal(estimatedNarratedCardSeconds(card(), {}), 4.2); assert.equal(estimatedDuration(card(), { transition: 0.5 }), 4.2); assert.equal(estimatedDuration(card({ seconds: 9 }), { transition: 0.5 }), 9); assert.equal(estimatedDuration({ type: "card", id: "x", seconds: 3 }, {}), 3); }); test("the sound's filter: rate, layout, level, limit, lead, exact length", () => { const render = { audioRate: 48000, audioChannels: 2 }; assert.equal( narrationAudioFilter({ seconds: 4.9, lead: 0.8, gainDb: 2.25, render }), "aresample=48000,aformat=sample_fmts=fltp:channel_layouts=stereo,volume=2.25dB," + "alimiter=limit=0.891251:level=0:latency=1:attack=5:release=50,adelay=800:all=1," + "apad=whole_dur=4.9,atrim=end=4.9,asetpts=PTS-STARTPTS", ); assert.match(narrationAudioFilter({ seconds: 3, lead: 0, gainDb: 0, render: { audioRate: 44100, audioChannels: 1 } }), /^aresample=44100,aformat=sample_fmts=fltp:channel_layouts=mono,volume=0dB,.*adelay=0:all=1/); assert.equal(narrationGainDb(-21.2, -17.7), 3.5); assert.equal(narrationGainDb(-60, -17.7), 24); assert.equal(narrationGainDb(null, -17.7), 0); }); test("the disclosure: the record's own line, else the editor's form; each once; none without narration", () => { assert.equal(narrationDisclosure(M([card()])), "Narration: synthetic voice (Kokoro-82M · af_heart)"); assert.equal( narrationDisclosure(M([card(), card({ id: "k2", narration: { text: "Two.", voice: "am_adam" } }), card({ id: "k3" })])), "Narration: synthetic voice (Kokoro-82M · af_heart)\nNarration: synthetic voice (Kokoro-82M · am_adam)", ); assert.equal(narrationDisclosure(M([{ type: "card", id: "a", seconds: 3 }])), null); // The editor's line wins, verbatim, attribution and all. const recs = new Map([["k1", { disclosure: "Narration: synthetic voice (Kokoro-82M · af_heart). Some attribution." }]]); assert.equal(narrationDisclosure(M([card(), card({ id: "k2" })]), recs, " "), "Narration: synthetic voice (Kokoro-82M · af_heart). Some attribution. Narration: synthetic voice (Kokoro-82M · af_heart)"); assert.deepEqual(disclosureLines(M([card({ narration: { text: "a", engine: "espeak-ng", voice: "en-us" } })])), ["Narration: synthetic voice (eSpeak NG · en-us)"]); }); test("the lint: an authored seconds shorter than the narration is an ERROR; an unsynthesized one is a note", () => { const m = M([card({ seconds: 3 }), card({ id: "k2" }), card({ id: "k3", seconds: 10 })]); const errs = narrationLengthErrors(m, new Map([["k1", 3.21], ["k2", 3.21], ["k3", 3.21]])); assert.equal(errs.length, 1); assert.match(errs[0], /timeline k1: seconds is 3, and its narration needs 4\.9s \(3\.21s of words plus the lead and the tail\) — set "auto" or at least 4\.9/); const lint = lintManifest(m, { narrationSeconds: new Map([["k1", 3.21]]) }); assert.equal(lint.errors.filter((e) => /narration needs/.test(e)).length, 1); assert.equal(lint.warnings.filter((w) => /not synthesized yet/.test(w)).length, 2); // The validator speaks through the lint too. const bad = lintManifest(M([card({ narration: { text: "a b" } })])); assert.ok(bad.errors.some((e) => //.test(e)), bad.errors.join("\n")); }); test("the tags are the editor's: TTS_EXPRESSIVE_TAGS (common/lib/ttsText.ts)", async () => { const { TTS_EXPRESSIVE_TAGS } = await import("yt-dlp-transcript-common/lib/ttsText"); assert.deepEqual([...NARRATION_TAGS], [...TTS_EXPRESSIVE_TAGS]); });