File size: 4,268 Bytes
b327bf6
 
 
 
07f3ede
 
 
 
 
 
 
 
 
 
b327bf6
 
 
 
 
 
 
 
 
07f3ede
b327bf6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07f3ede
b327bf6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07f3ede
b327bf6
 
 
 
 
 
07f3ede
b327bf6
 
 
07f3ede
 
 
 
b327bf6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07f3ede
b327bf6
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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<Voice[] | null>(null);
  const [voice, setVoice] = useState(AUTO_VOICE);
  const [audioUrl, setAudioUrl] = useState<string | null>(null);
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState<string | null>(null);
  const audioUrlRef = useRef<string | null>(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 (
    <div className="border border-white/10 bg-slate-2 px-4 py-3">
      <div className="flex flex-wrap items-center gap-2.5">
        <span className="font-display text-xs tracking-wider text-chalk-dim uppercase">
          Voiceover
        </span>
        <select
          value={voice}
          onChange={(e) => setVoice(e.target.value)}
          disabled={!voices}
          className="border border-white/15 bg-ink px-2 py-1 font-script text-xs text-chalk focus:border-gaffer focus:outline-none"
        >
          <option value={AUTO_VOICE}>Auto — match the presenter (recommended)</option>
          {options.map((v) => (
            <option key={v.name} value={v.name}>
              {v.name} — {v.characteristic}
            </option>
          ))}
        </select>
        {selected ? (
          <span className="font-script text-[11px] text-chalk-dim">
            Tone: <span className="text-viewfinder">{selected.characteristic}</span>
          </span>
        ) : (
          <span className="font-script text-[11px] text-chalk-dim">
            Cast from the shot list — a woman on camera gets a woman&rsquo;s voice.
          </span>
        )}
        <button
          type="button"
          onClick={handleGenerate}
          disabled={loading}
          className="border border-viewfinder/50 px-3 py-1 font-display text-xs tracking-wider text-viewfinder uppercase transition hover:bg-viewfinder hover:text-ink disabled:cursor-not-allowed disabled:opacity-50"
        >
          {loading ? "Generating…" : "▶ Generate audio"}
        </button>
        {error && <span className="text-xs text-tally">{error}</span>}
      </div>
      {audioUrl && (
        <div className="mt-2.5 flex items-center gap-3">
          <audio controls src={audioUrl} className="h-9 max-w-full" />
          <a
            href={audioUrl}
            download={`candidate-${candidateNumber}-${voice || "auto"}.wav`}
            className="font-script text-xs text-viewfinder hover:underline"
          >
            Download
          </a>
        </div>
      )}
    </div>
  );
}