File size: 6,312 Bytes
5ca6cf1
 
 
b919616
5ca6cf1
 
 
 
 
 
 
 
 
8172156
 
 
 
 
5ca6cf1
 
 
8172156
5ca6cf1
 
 
 
 
 
b919616
 
5ca6cf1
 
 
8172156
6889c8b
 
 
8172156
 
b919616
 
8172156
017da10
 
 
 
 
 
8172156
 
5ca6cf1
 
 
 
 
 
b919616
5ca6cf1
8172156
 
 
5ca6cf1
 
 
b919616
8172156
 
 
 
 
 
 
 
b919616
 
 
 
 
 
 
 
 
8172156
 
 
 
 
 
 
 
 
 
 
 
5ca6cf1
 
 
b919616
 
 
5ca6cf1
b919616
 
 
 
 
 
 
5ca6cf1
b919616
 
5ca6cf1
b919616
5ca6cf1
b919616
 
 
5ca6cf1
 
b919616
 
5ca6cf1
b919616
 
5ca6cf1
b919616
 
 
 
 
 
 
5ca6cf1
b919616
 
 
 
 
 
 
5ca6cf1
 
 
 
 
8172156
 
 
 
 
 
 
 
 
 
 
 
 
 
5ca6cf1
b919616
5ca6cf1
 
 
b919616
 
 
5ca6cf1
 
 
b919616
4de5d25
 
5ca6cf1
4de5d25
5ca6cf1
 
b919616
5ca6cf1
 
 
 
 
 
 
 
 
 
 
 
b919616
 
5ca6cf1
 
 
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { useState, useRef, useEffect, useCallback } from "react";
import { AudioRecorder } from "../audio/AudioRecorder";
import { AudioPlayer } from "../audio/AudioPlayer";
import { createWavBlob, speechToText } from "../services/voiceApi";

export type VoiceState =
  | "IDLE"
  | "CONNECTING"
  | "LISTENING"
  | "PROCESSING"
  | "SPEAKING"
  | "ERROR";

export interface VoiceSessionParams {
  sttProvider?: string;
  ttsProvider?: string;
}

interface UseVoiceSessionOptions {
  onTranscript: (text: string) => void;
  onError?: (code: string, message: string) => void;
  sessionParams?: VoiceSessionParams;
}

export interface UseVoiceSessionReturn {
  voiceState: VoiceState;
  start: () => Promise<void>;
  stop: () => void;
  stopRecording: () => void;
  setStateExternal: (s: VoiceState) => void;
  isActive: boolean;
}

const BUFFER_SOUNDS = [
  "/sounds/01_Baik_Saya_sedang_memproses_pertanyaanmu.wav",
  "/sounds/02_Oke_mohon_ditunggu_saya_sedang_siapkan_P.wav",
  "/sounds/03_Sip_saya_terima_Sedang_saya_proses_Pesan.wav",
];

const RECORDER_SAMPLE_RATE = 16000;

function getVoiceHttpBaseUrl(): string {
  const w = window as unknown as { __APP_CONFIG__?: { VOICE_API_URL?: string } };
  return (
    w.__APP_CONFIG__?.VOICE_API_URL ||
    (import.meta as unknown as { env: Record<string, string> }).env.VITE_API_BASE_VOICE_URL ||
    "http://localhost:7861"
  );
}

export function useVoiceSession(opts: UseVoiceSessionOptions): UseVoiceSessionReturn {
  const [voiceState, setVoiceState] = useState<VoiceState>("IDLE");
  const stateRef = useRef<VoiceState>("IDLE");

  const recorderRef = useRef<AudioRecorder | null>(null);
  const playerRef = useRef<AudioPlayer | null>(null);
  const chunksRef = useRef<ArrayBuffer[]>([]);

  const bufferAudioRef = useRef<HTMLAudioElement | null>(null);
  const lastBufferIndexRef = useRef<number>(-1);

  const optsRef = useRef(opts);
  useEffect(() => { optsRef.current = opts; });

  // Defined before setState so setState can call it without circular deps.
  const stopBufferSound = useCallback(() => {
    if (bufferAudioRef.current) {
      bufferAudioRef.current.pause();
      bufferAudioRef.current.currentTime = 0;
      bufferAudioRef.current = null;
    }
  }, []);

  // Auto-stops the buffer audio when the waiting phase ends (TTS about to start, or session ends).
  const setState = useCallback((s: VoiceState) => {
    if (s === "SPEAKING" || s === "IDLE" || s === "ERROR") {
      stopBufferSound();
    }
    stateRef.current = s;
    setVoiceState(s);
  }, [stopBufferSound]);

  const playBufferSound = useCallback(() => {
    stopBufferSound();
    let idx: number;
    do {
      idx = Math.floor(Math.random() * BUFFER_SOUNDS.length);
    } while (BUFFER_SOUNDS.length > 1 && idx === lastBufferIndexRef.current);
    lastBufferIndexRef.current = idx;
    const audio = new Audio(BUFFER_SOUNDS[idx]);
    bufferAudioRef.current = audio;
    audio.play().catch(() => {});
  }, [stopBufferSound]);

  const stopSession = useCallback(() => {
    recorderRef.current?.stop();
    playerRef.current?.stopImmediately();
    chunksRef.current = [];
    setState("IDLE"); // setState("IDLE") calls stopBufferSound internally
  }, [setState]);

  const stopRecording = useCallback(() => {
    if (stateRef.current !== "LISTENING") return;
    setState("PROCESSING");
    recorderRef.current?.stop();
    // Play buffer audio — it keeps playing through STT and chatbot processing.
    // It stops automatically when setState("SPEAKING"), setState("IDLE"), or setState("ERROR") is called.
    playBufferSound();

    const chunks = chunksRef.current;
    chunksRef.current = [];

    void (async () => {
      try {
        if (chunks.length === 0) {
          setState("IDLE");
          return;
        }

        const wav = createWavBlob(chunks, RECORDER_SAMPLE_RATE);
        const { text } = await speechToText(wav, optsRef.current.sessionParams?.sttProvider ?? "chirp3");

        // Guard: session may have been cancelled while STT was in flight.
        if (stateRef.current !== "PROCESSING") return;

        if (text.trim()) {
          console.log("[Voice] STT transcript →", text);
          // Buffer audio continues to play while Main.tsx calls the chatbot API.
          // It will stop when setStateExternal("SPEAKING") or setStateExternal("IDLE") is called.
          optsRef.current.onTranscript(text);
        } else {
          setState("IDLE");
        }
      } catch (err) {
        console.error("[STT] Request failed:", err);
        optsRef.current.onError?.("STT_ERROR", (err as Error).message);
        setState("ERROR"); // setState("ERROR") calls stopBufferSound internally
      }
    })();
  }, [playBufferSound, setState]);

  const start = useCallback(async () => {
    if (stateRef.current !== "IDLE" && stateRef.current !== "ERROR") return;
    setState("CONNECTING");

    try {
      const res = await fetch(`${getVoiceHttpBaseUrl()}/health`);
      if (res.ok) {
        const data: { status: string; message?: string } = await res.json();
        if (data.status !== "ok") {
          setState("ERROR");
          optsRef.current.onError?.("HEALTH_CHECK_FAILED", data.message ?? "Service not ready");
          return;
        }
      }
    } catch {
      // Network error or CORS — proceed with connect attempt
    }

    try {
      chunksRef.current = [];
      if (!recorderRef.current) recorderRef.current = new AudioRecorder();
      if (!playerRef.current) playerRef.current = new AudioPlayer();

      await recorderRef.current.start((chunk: ArrayBuffer) => {
        if (stateRef.current === "LISTENING") {
          chunksRef.current.push(chunk);
        }
      });

      setState("LISTENING");
    } catch (err) {
      console.error("[Voice] Failed to start recorder:", err);
      recorderRef.current?.stop();
      optsRef.current.onError?.("MIC_ERROR", (err as Error).message ?? "Failed to access microphone");
      setState("ERROR");
    }
  }, [setState]);

  useEffect(() => {
    return () => {
      stopSession();
    };
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);

  return {
    voiceState,
    start,
    stop: stopSession,
    stopRecording,
    setStateExternal: setState,
    isActive: voiceState !== "IDLE" && voiceState !== "ERROR",
  };
}