import { useEffect, useRef, useState } from "react"; import { api, ApiError } from "../api/client"; import type { Voice } from "../api/types"; // Shown only if GET /tts/voices fails, so the picker is never empty. Must be a // voice the speech model actually has (see app/services/speech.py VOICES). const FALLBACK_VOICES: Voice[] = [ { name: "Roger", characteristic: "Confident, persuasive (male)" }, ]; /** Empty value = let the server cast the voice from the presenter description. * It is the default because a hand-picked voice is how an animated woman ended up * narrated by a man: the pipeline had no idea who was on screen. */ const AUTO_VOICE = ""; export function ScriptAudio({ assetId, candidateNumber, }: { assetId: string; candidateNumber: number; }) { const [voices, setVoices] = useState(null); const [voice, setVoice] = useState(AUTO_VOICE); const [audioUrl, setAudioUrl] = useState(null); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const audioUrlRef = useRef(null); useEffect(() => { api .listVoices() .then((rows) => { setVoices(rows); setVoice((current) => rows.some((v) => v.name === current) ? current : (rows[0]?.name ?? current), ); }) .catch(() => setVoices(FALLBACK_VOICES)); }, []); useEffect(() => { return () => { if (audioUrlRef.current) URL.revokeObjectURL(audioUrlRef.current); }; }, []); async function handleGenerate() { setLoading(true); setError(null); try { const blob = await api.generateCandidateAudio(assetId, candidateNumber, voice || null); if (audioUrlRef.current) URL.revokeObjectURL(audioUrlRef.current); const url = URL.createObjectURL(blob); audioUrlRef.current = url; setAudioUrl(url); } catch (err) { setError(err instanceof ApiError ? err.message : "Failed to generate audio"); } finally { setLoading(false); } } const options = voices ?? FALLBACK_VOICES; const selected = options.find((v) => v.name === voice); return (
Voiceover {selected ? ( Tone: {selected.characteristic} ) : ( Cast from the shot list — a woman on camera gets a woman’s voice. )} {error && {error}}
{audioUrl && ( )}
); }