2026-06-01 16:42:36 +07:00
|
|
|
// ─── Audio transmit hook — captures mic, encodes to PCM, sends via WebSocket ──
|
|
|
|
|
import { useCallback, useRef, useState } from "react";
|
2026-06-08 22:55:11 +07:00
|
|
|
import { getAPIURL } from "../api/client";
|
2026-06-01 16:42:36 +07:00
|
|
|
|
|
|
|
|
const SAMPLE_RATE = 24000;
|
|
|
|
|
|
2026-06-08 22:55:11 +07:00
|
|
|
async function sendTransmitCommand(command: string): Promise<void> {
|
2026-06-08 23:09:19 +07:00
|
|
|
// Send via HTTP API
|
2026-06-08 23:54:02 +07:00
|
|
|
const resp = await fetch(`${getAPIURL()}/api/voice/command`, {
|
2026-06-08 23:09:19 +07:00
|
|
|
method: "POST",
|
|
|
|
|
headers: { "Content-Type": "application/json" },
|
|
|
|
|
body: JSON.stringify({ command }),
|
|
|
|
|
});
|
|
|
|
|
if (!resp.ok) {
|
2026-06-08 23:54:02 +07:00
|
|
|
console.warn("HTTP command response:", resp.status, resp.statusText);
|
2026-06-08 23:09:19 +07:00
|
|
|
const text = await resp.text().catch(() => resp.statusText);
|
|
|
|
|
console.warn("HTTP command failed:", text);
|
|
|
|
|
throw new Error(`HTTP ${resp.status}: ${text}`);
|
2026-06-08 22:55:11 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-01 21:44:29 +07:00
|
|
|
export function useAudioTransmit(socketRef: {
|
|
|
|
|
readonly current: WebSocket | null;
|
|
|
|
|
}) {
|
2026-06-01 16:42:36 +07:00
|
|
|
const [isStreaming, setIsStreaming] = useState(false);
|
|
|
|
|
const streamRef = useRef<MediaStream | null>(null);
|
|
|
|
|
const audioContextRef = useRef<AudioContext | null>(null);
|
|
|
|
|
const processorRef = useRef<ScriptProcessorNode | null>(null);
|
|
|
|
|
|
|
|
|
|
const stop = useCallback(() => {
|
2026-06-08 22:55:11 +07:00
|
|
|
sendTransmitCommand("voice:transmit:stop").catch(() => {});
|
2026-06-08 21:29:41 +07:00
|
|
|
|
2026-06-01 16:42:36 +07:00
|
|
|
setIsStreaming(false);
|
2026-06-01 21:44:29 +07:00
|
|
|
if (processorRef.current) {
|
|
|
|
|
processorRef.current.disconnect();
|
|
|
|
|
processorRef.current = null;
|
|
|
|
|
}
|
|
|
|
|
if (audioContextRef.current) {
|
|
|
|
|
audioContextRef.current.close();
|
|
|
|
|
audioContextRef.current = null;
|
|
|
|
|
}
|
|
|
|
|
if (streamRef.current) {
|
|
|
|
|
for (const track of streamRef.current.getTracks()) track.stop();
|
|
|
|
|
streamRef.current = null;
|
|
|
|
|
}
|
2026-06-08 22:55:11 +07:00
|
|
|
}, []);
|
2026-06-01 16:42:36 +07:00
|
|
|
|
|
|
|
|
const start = useCallback(async () => {
|
2026-06-08 22:55:11 +07:00
|
|
|
await sendTransmitCommand("voice:transmit:start");
|
2026-06-08 21:29:41 +07:00
|
|
|
|
2026-06-01 16:42:36 +07:00
|
|
|
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
|
|
|
|
streamRef.current = stream;
|
|
|
|
|
setIsStreaming(true);
|
2026-06-01 21:44:29 +07:00
|
|
|
const AudioContextCtor =
|
|
|
|
|
window.AudioContext ||
|
|
|
|
|
(window as unknown as { webkitAudioContext: typeof AudioContext })
|
|
|
|
|
.webkitAudioContext;
|
2026-06-01 16:42:36 +07:00
|
|
|
const audioContext = new AudioContextCtor({ sampleRate: SAMPLE_RATE });
|
|
|
|
|
audioContextRef.current = audioContext;
|
|
|
|
|
const source = audioContext.createMediaStreamSource(stream);
|
2026-06-09 01:07:35 +07:00
|
|
|
const processor = audioContext.createScriptProcessor(1024, 1, 1);
|
2026-06-01 16:42:36 +07:00
|
|
|
processorRef.current = processor;
|
|
|
|
|
source.connect(processor);
|
2026-06-09 01:07:35 +07:00
|
|
|
// Don't connect processor to destination (no monitoring feedback)
|
2026-06-01 16:42:36 +07:00
|
|
|
processor.onaudioprocess = (event) => {
|
2026-06-01 21:44:29 +07:00
|
|
|
if (!socketRef.current || socketRef.current.readyState !== WebSocket.OPEN)
|
|
|
|
|
return;
|
2026-06-01 16:42:36 +07:00
|
|
|
const inputData = event.inputBuffer.getChannelData(0);
|
|
|
|
|
const pcmData = new Int16Array(inputData.length);
|
2026-06-01 21:44:29 +07:00
|
|
|
for (let i = 0; i < inputData.length; i++)
|
|
|
|
|
pcmData[i] = Math.max(-1, Math.min(1, inputData[i])) * 32767;
|
2026-06-08 21:29:41 +07:00
|
|
|
|
2026-06-09 01:07:35 +07:00
|
|
|
// Fast base64 using Uint8Array + btoa
|
|
|
|
|
const uint8 = new Uint8Array(pcmData.buffer);
|
|
|
|
|
let base64 = '';
|
|
|
|
|
const chunkSize = 8192;
|
|
|
|
|
for (let i = 0; i < uint8.length; i += chunkSize) {
|
|
|
|
|
const chunk = uint8.subarray(i, i + chunkSize);
|
|
|
|
|
base64 += btoa(String.fromCharCode(...chunk));
|
2026-06-08 21:29:41 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
socketRef.current.send(JSON.stringify({
|
|
|
|
|
type: 'voice_transmit',
|
|
|
|
|
buffer: base64
|
|
|
|
|
}));
|
2026-06-01 16:42:36 +07:00
|
|
|
};
|
|
|
|
|
}, [socketRef]);
|
|
|
|
|
|
|
|
|
|
const toggle = useCallback(async () => {
|
|
|
|
|
if (isStreaming) stop();
|
|
|
|
|
else await start();
|
|
|
|
|
}, [isStreaming, start, stop]);
|
|
|
|
|
|
|
|
|
|
return { isStreaming, toggle, stop, start };
|
|
|
|
|
}
|