SPB Git

Repository Public

timing.ts 4.1 KB 66 lines typescript

1import type { AlignedWord } from "../media/align";2import { alignScript, sentenceCues } from "../media/align";3import type { SceneRuntime } from "../media/htmlscene";4import type { Beat, DesignSystem } from "./spec";56/**7 * TEMPS D'UNE SCÈNE : de la voix réellement synthétisée aux repères de l'animation.8 * La voix commence `lead` secondes après le début de la scène. Les mots (`AlignedWord`, temps dans le fichier de voix) sont les mots du SCRIPT9 * calés par alignement forcé ; à défaut d'alignement (Whisper indisponible), ils sont répartis au prorata de leur longueur et marqués estimés.10 */11export interface SceneVoice { narration: string; seconds: number; lead: number; voiceSeconds: number | null; aligned: AlignedWord[] | null }12export interface SceneClock {13  words: { w: string; start: number; end: number }[];14  cues: { start: number; end: number; text: string }[];15  /** Vrai si les temps des mots viennent de la voix (alignement forcé), faux s'ils sont estimés. */16  measured: boolean;17  matchedRatio: number;18}19const r3 = (x: number) => Math.round(x * 1000) / 1000;2021export function sceneClock(v: SceneVoice): SceneClock {22  if (!v.narration.trim() || !v.voiceSeconds) return { words: [], cues: [], measured: false, matchedRatio: 0 };23  const measured = !!v.aligned?.length;24  const al = measured ? v.aligned! : alignScript(v.narration, [], { start: 0, end: v.voiceSeconds });25  const words = al.map((a) => ({ w: a.word, start: r3(v.lead + a.start), end: r3(v.lead + a.end) }));26  const cues = sentenceCues(al).map((c) => ({ start: r3(v.lead + c.start), end: r3(v.lead + c.end), text: c.text }));27  const matched = al.filter((a) => a.confidence === "matched").length;28  return { words, cues, measured, matchedRatio: al.length ? matched / al.length : 0 };29}3031/** Résout les repères du plan en secondes dans la scène. Un repère hors de portée est ramené dans la scène et signalé. */32export function resolveBeats(beats: Beat[], clock: SceneClock, seconds: number): { beats: Record<string, number>; problems: string[] } {33  const out: Record<string, number> = {}, problems: string[] = [], last = Math.max(0, seconds - 0.25);34  for (const b of beats) {35    const a = b.anchor; let t: number;36    if (a.type === "start") t = a.offset;37    else if (a.type === "end") t = seconds + a.offset;38    else if (a.type === "word") {39      const w = clock.words[a.index];40      if (!w) { problems.push(`repère « ${b.id} » : mot n° ${a.index} absent de la narration (${clock.words.length} mots)`); t = clock.words.length ? clock.words[clock.words.length - 1].start : 0; } else t = w.start + a.offset;41    } else {42      const c = clock.cues[a.index];43      if (!c) { problems.push(`repère « ${b.id} » : phrase n° ${a.index} absente (${clock.cues.length} phrases)`); t = clock.cues.length ? clock.cues[clock.cues.length - 1].start : 0; } else t = (a.edge === "end" ? c.end : c.start) + a.offset;44    }45    out[b.id] = r3(Math.min(last, Math.max(0, t)));46  }47  return { beats: out, problems };48}4950/** Données d'exécution injectées dans la page de la scène. */51export function sceneRuntime(o: { clock: SceneClock; beats: Record<string, number>; design: DesignSystem | null; fps: number; data?: unknown }): SceneRuntime {52  return { v: 3, fps: o.fps, words: o.clock.words, cues: o.clock.cues, beats: o.beats, theme: o.design ? { ...o.design.theme } : null, symbols: o.design?.cast.map((c) => c.svg).join("") ?? "", data: o.data ?? null };53}5455/** Narration numérotée mot à mot, par phrase, avec les temps : c'est ce que lisent le Motion Director et l'Animator pour ancrer les repères. */56export function numberedNarration(clock: SceneClock): string {57  if (!clock.words.length) return "  (pas de voix sur cette scène)";58  let i = 0;59  return clock.cues.map((c, ci) => {60    const n = c.text.trim().split(/\s+/).filter(Boolean).length, part = clock.words.slice(i, i + n).map((w, k) => `${i + k}:${w.w}`).join(" ");61    const line = `  [phrase ${ci} · ${c.start.toFixed(2)} → ${c.end.toFixed(2)} s] ${part}`;62    i += n;63    return line;64  }).join("\n") + (i < clock.words.length ? `\n  ${clock.words.slice(i).map((w, k) => `${i + k}:${w.w}`).join(" ")}` : "");65}66
Files main
Menu

Theme