"use client"; import { useEffect, useRef, useState } from "react"; import { useRouter } from "next/navigation"; import { buttonVariants } from "@/components/ui/button"; import { JobProgress } from "@/components/ui/job-progress"; // --------------------------------------------------------------------------- // "Score the voices" (release 34): who is speaking, per montage clip. // // One job (`umtool voice-match `, lib/report/voiceMatch.mjs): each // clip's phrase, voiced frames only, against the host's voice — the clips // ruled keep when there are three, else solo recordings on disk. Local // (TitaNet-small through sherpa-onnx) and light: no network, no LLM. // // It is an aid to the ear and says so: the score sorts and flags; the ruling // is still a person listening. The rows below show each clip's score once a // run has written voice-match.json. // --------------------------------------------------------------------------- type JobView = { id: string; state: "running" | "done" | "failed"; error: string | null; log: string[]; events: Record[] }; export type VoiceMatchSummary = { madeAt: string; counts: { clips: number; scored: number; likely: number; unsure: number; unlikely: number }; reference: string; }; const REFERENCES = [ ["auto", "keeps, else solo recordings"], ["rulings", "the clips ruled keep"], ["recordings", "solo recordings on disk"], ] as const; export default function VoiceMatchPanel({ project, summary, reload = false, }: { project: string; summary: VoiceMatchSummary | null; /** Reload the page when the run is done (the clip deck keeps its cards in state). */ reload?: boolean; }) { const router = useRouter(); const [reference, setReference] = useState("auto"); const [job, setJob] = useState(null); const [error, setError] = useState(null); const alive = useRef(true); useEffect(() => () => void (alive.current = false), []); async function watch(id: string) { for (;;) { await new Promise((r) => setTimeout(r, 1000)); if (!alive.current) return; const r = await fetch(`/api/report/voice-match?job=${encodeURIComponent(id)}`, { cache: "no-store" }); const j = (await r.json().catch(() => null)) as (JobView & { error?: string | null }) | null; if (!r.ok || !j || !j.state) { setError(j?.error ?? "the job is gone"); return; } setJob(j); if (j.state !== "running") { if (j.state === "done") { if (reload) window.location.reload(); else router.refresh(); } return; } } } async function start() { setError(null); const r = await fetch("/api/report/voice-match", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ project, reference }), cache: "no-store", }); const j = (await r.json().catch(() => ({}))) as { job?: JobView; error?: string }; if (!r.ok || !j.job) { setError(j.error ?? `HTTP ${r.status}`); return; } setJob(j.job); void watch(j.job.id); } const running = job?.state === "running"; const last = [...(job?.events ?? [])].reverse().find((e) => e.ev === "voice-match") as { phase?: string; n?: number; of?: number } | undefined; return (
{summary && ( voices: {summary.counts.likely} likely · {summary.counts.unsure} unsure · {summary.counts.unlikely} unlikely of{" "} {summary.counts.scored} scored · against {summary.reference} {summary.madeAt ? ` · ${summary.madeAt.slice(0, 16).replace("T", " ")}` : ""} )} {job && ( void fetch(`/api/report/voice-match?cancel=${encodeURIComponent(job.id)}`, { method: "POST" }) : undefined } /> )} {error && (

{error}

)}
); }