-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControlPanel.jsx
More file actions
81 lines (71 loc) · 2.45 KB
/
ControlPanel.jsx
File metadata and controls
81 lines (71 loc) · 2.45 KB
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
// src/components/ControlPanel.jsx
import React, { useState, useRef, useEffect } from "react";
import AudioRecorder from "../utils/AudioRecorder";
export default function ControlPanel({ onResult }) {
const [recording, setRecording] = useState(false);
const wsRef = useRef(null);
const recorderRef = useRef(null);
useEffect(() => {
wsRef.current = new WebSocket("ws://localhost:8000/ws");
wsRef.current.binaryType = "arraybuffer";
wsRef.current.onmessage = (event) => {
const msg = JSON.parse(event.data);
// partial: true → リアルタイム部分テキスト(status:partial)
onResult(msg);
};
wsRef.current.onclose = () => console.log("WebSocket closed");
wsRef.current.onerror = (err) => console.error("WebSocket error", err);
return () => {
wsRef.current.close();
};
}, [onResult]);
const startRecording = async () => {
setRecording(true);
recorderRef.current = new AudioRecorder((float32Data) => {
// PCM16bit に変換して送信
const int16Buffer = float32ToInt16(float32Data);
if (wsRef.current && wsRef.current.readyState === WebSocket.OPEN) {
wsRef.current.send(int16Buffer);
}
});
try {
await recorderRef.current.start();
// バックエンドに /init_ws → /start_ws を呼ぶ
await fetch("/init_ws");
await fetch("/start_ws");
} catch (err) {
console.error("AudioRecorder start error:", err);
}
};
const stopRecording = async () => {
setRecording(false);
if (recorderRef.current) {
recorderRef.current.stop();
recorderRef.current = null;
}
// バックエンドに録音終了通知
await fetch("/stop_ws3");
await fetch("/end_ws");
};
const float32ToInt16 = (float32Array) => {
const buffer = new ArrayBuffer(float32Array.length * 2);
const view = new DataView(buffer);
for (let i = 0; i < float32Array.length; i++) {
let s = Math.max(-1, Math.min(1, float32Array[i]));
view.setInt16(i * 2, s < 0 ? s * 0x8000 : s * 0x7fff, true);
}
return buffer;
};
return (
<div className="mb-4">
<button
onClick={recording ? stopRecording : startRecording}
className={`px-4 py-2 rounded text-white ${
recording ? "bg-red-500" : "bg-green-500"
}`}
>
{recording ? "Stop" : "Start Recording"}
</button>
</div>
);
}