SPB Git

Repository Public

media-speech.test.ts 6.7 KB 108 lines typescript

1import { rm } from "node:fs/promises";2import path from "node:path";3import { afterAll, beforeAll, describe, expect, it } from "vitest";4import { cuesFromWords } from "../src/media/captions";5import { probe } from "../src/media/ffmpeg";6import { speechCapabilities, synthesize, transcribe } from "../src/media/speech";7import { makeVideo, tmpDir } from "./fixtures/media";89/**10 * Voix et transcription locales. Les tests Kokoro / Whisper sont ignorés automatiquement si les paquets ne sont pas installés11 * (`speechCapabilities`) ; ils téléchargent les modèles au premier lancement (d'où les délais longs).12 */13const T = 30 * 60_000;14const caps = await speechCapabilities();15let dir = "";16const f = (name: string) => path.join(dir, name);17const norm = (s: string) => s.toLowerCase().normalize("NFD").replace(/[̀-ͯ]/g, "").replace(/[^a-z0-9 ]/g, " ").replace(/\s+/g, " ").trim();1819beforeAll(async () => { dir = await tmpDir("speech"); });20afterAll(async () => { if (dir) await rm(dir, { recursive: true, force: true }); });2122describe("speechCapabilities", () => {23  it("rapporte l'état réel de l'installation", () => {24    console.log("speechCapabilities :", JSON.stringify(caps, null, 2));25    expect(typeof caps.kokoro).toBe("boolean");26    expect(typeof caps.whisper).toBe("boolean");27    expect(caps.say).toBe(process.platform === "darwin");28    for (const k of ["kokoro", "whisper", "say"]) expect(caps.detail[k]).toBeTruthy();29  });30  it("refuse un fournisseur inconnu et un texte vide", async () => {31    await expect(synthesize({ text: "Bonjour", provider: "xai", voiceId: "x", language: "fr", speed: 1, output: f("x.wav") })).rejects.toThrow(/non pris en charge/);32    await expect(synthesize({ text: "  ", provider: "kokoro", voiceId: "ff_siwis", language: "fr", speed: 1, output: f("x.wav") })).rejects.toThrow(/vide/);33  });34});3536describe.skipIf(!caps.kokoro)("synthesize — Kokoro", () => {37  const cases = [38    { language: "fr" as const, voiceId: "ff_siwis", text: "Bonjour et bienvenue. Le Québec produit la quasi-totalité de son électricité grâce à ses barrages hydroélectriques.", expect: ["bonjour", "quebec", "electricite", "barrages"] },39    { language: "en" as const, voiceId: "af_heart", text: "Hello and welcome. Quebec generates almost all of its electricity from hydroelectric dams.", expect: ["hello", "quebec", "electricity", "dams"] },40    { language: "en" as const, voiceId: "am_michael", text: "This is a second English voice, used for narration.", expect: ["second", "english", "voice", "narration"] },41  ];42  it.each(cases)("$language / $voiceId : WAV 48 kHz, durée plausible, alignement par mot si Whisper est là", async (c) => {43    const out = f(`tts-${c.voiceId}.wav`);44    const t0 = Date.now();45    const r = await synthesize({ text: c.text, provider: "kokoro", voiceId: c.voiceId, language: c.language, speed: 1, output: out });46    const wall = (Date.now() - t0) / 1000;47    const p = await probe(out);48    console.log(`TTS ${c.language}/${c.voiceId} : ${r.seconds.toFixed(2)} s d'audio en ${wall.toFixed(2)} s (synthèse + alignement) ; fournisseur ${r.provider} ; ${r.words?.length ?? 0} mots alignés`);49    expect(r.provider).toBe("kokoro");50    expect(r.fallbackReason).toBeUndefined();51    expect(p).toMatchObject({ hasAudio: true, hasVideo: false, sampleRate: 48000, audioCodec: "pcm_s16le" });52    expect(r.seconds).toBeCloseTo(p.duration, 1);53    const wordsPerSecond = c.text.split(/\s+/).length / r.seconds;54    expect(wordsPerSecond).toBeGreaterThan(1.2);55    expect(wordsPerSecond).toBeLessThan(5);56    if (caps.whisper) {57      expect(r.words).not.toBeNull();58      const heard = norm(r.words!.map((w) => w.word).join(" "));59      for (const w of c.expect) expect(heard).toContain(w);60      expect(r.words!.every((w, i) => w.end >= w.start && w.start >= 0 && w.end <= r.seconds + 0.5 && (i === 0 || w.start >= r.words![i - 1].start))).toBe(true);61      const cues = cuesFromWords(r.words!);62      expect(cues.length).toBeGreaterThanOrEqual(1);63      expect(cues.every((q) => q.text.split("\n").every((l) => l.length <= 42))).toBe(true);64    } else expect(r.words).toBeNull();65  }, T);6667  it("la vitesse raccourcit la durée", async () => {68    const text = "Cette phrase sert à mesurer l'effet du réglage de vitesse sur la durée.";69    const slow = await synthesize({ text, provider: "kokoro", voiceId: "ff_siwis", language: "fr", speed: 0.9, output: f("slow.wav") });70    const fast = await synthesize({ text, provider: "kokoro", voiceId: "ff_siwis", language: "fr", speed: 1.3, output: f("fast.wav") });71    expect(fast.seconds).toBeLessThan(slow.seconds * 0.85);72  }, T);73});7475describe.skipIf(!caps.say)("synthesize — repli say", () => {76  it.each([["fr", "Bonjour, ceci est la voix de repli du système."], ["en", "Hello, this is the system fallback voice."]] as const)("%s : WAV 48 kHz", async (language, text) => {77    const r = await synthesize({ text, provider: "say", voiceId: "", language, speed: 1, output: f(`say-${language}.wav`) });78    expect(r.provider).toBe("say");79    expect(r.voice).toBeTruthy();80    expect(r.seconds).toBeGreaterThan(1);81    expect(await probe(f(`say-${language}.wav`))).toMatchObject({ sampleRate: 48000, hasAudio: true });82    console.log(`say ${language} : voix ${r.voice}, ${r.seconds.toFixed(2)} s`);83  }, T);84});8586describe.skipIf(!caps.whisper || !caps.kokoro)("transcribe — Whisper", () => {87  it("transcrit un média importé (vidéo avec piste voix) avec horodatage par mot, langue détectée", async () => {88    const r0 = await synthesize({ text: "The quick brown fox jumps over the lazy dog. Then it rests by the river.", provider: "kokoro", voiceId: "af_heart", language: "en", speed: 1, output: f("fox.wav") });89    const { ff } = await import("../src/media/ffmpeg");90    await ff(["-f", "lavfi", "-i", "testsrc2=s=320x240:r=30", "-i", f("fox.wav"), "-shortest", "-c:v", "libx264", "-preset", "ultrafast", "-pix_fmt", "yuv420p", "-c:a", "aac", f("fox.mp4")]);91    const t0 = Date.now();92    const r = await transcribe({ file: f("fox.mp4") });93    console.log(`Whisper (${r.model}) : ${r0.seconds.toFixed(2)} s d'audio transcrits en ${((Date.now() - t0) / 1000).toFixed(2)} s`);94    expect(r.language).toBe("en");95    expect(norm(r.text)).toContain("quick brown fox jumps over the lazy dog");96    expect(r.model).toContain("whisper");97    const words = r.segments.flatMap((s) => s.words ?? []);98    expect(words.length).toBeGreaterThanOrEqual(14);99    expect(words.every((w) => w.end >= w.start)).toBe(true);100    expect(words.at(-1)!.end).toBeLessThanOrEqual(r0.seconds + 0.6);101  }, T);102103  it("erreur explicite sans piste audio", async () => {104    await makeVideo(f("mute.mp4"), { width: 320, height: 240, seconds: 1, audio: "none" });105    await expect(transcribe({ file: f("mute.mp4") })).rejects.toThrow(/piste audio/);106  }, T);107});108
Files main
Menu

Theme