The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { errorDisplayMessage } from "@t3tools/client-runtime/errors"; |
| 2 | import { m } from "~/i18n/messages"; |
| 3 | import { MICROPHONE_PERMISSION_ERROR } from "@t3tools/client-runtime/voice-input"; |
| 4 | import { MicIcon, PauseIcon, PlayIcon, RotateCcwIcon, SquareIcon, XIcon } from "lucide-react"; |
| 5 | import { T, useGT, useMessages } from "gt-react"; |
| 6 | import { useEffect, useRef, useState } from "react"; |
| 7 | |
| 8 | import { useStableCallback } from "~/hooks/useStableCallback"; |
| 9 | import { requestMicrophone } from "../../speech/requestMicrophone"; |
| 10 | import workletUrl from "../../speech/pcmWorklet.ts?worker&url"; |
| 11 | import { VoiceWaveform } from "../chat/VoiceInputPill"; |
| 12 | import { cn } from "../../lib/utils"; |
| 13 | import { Button } from "../ui/button"; |
| 14 | import { SettingsRow } from "./settingsLayout"; |
| 15 | import { useSearchableSetting } from "./settingsSearch"; |
| 16 | |
| 17 | const LOOPBACK_SECONDS = 10; |
| 18 | const formatTime = (seconds: number) => |
| 19 | `${Math.floor(seconds / 60)}:${String(Math.floor(seconds % 60)).padStart(2, "0")}`; |
| 20 | |
| 21 | type Capture = { |
| 22 | stream?: MediaStream; |
| 23 | recorder?: MediaRecorder; |
| 24 | context?: AudioContext; |
| 25 | worklet?: AudioWorkletNode; |
| 26 | timer?: ReturnType<typeof setTimeout>; |
| 27 | elapsedTimer?: ReturnType<typeof setInterval>; |
| 28 | startedAt?: number; |
| 29 | }; |
| 30 | |
| 31 | /** |
| 32 | * Records a few seconds and plays them back in the browser. The audio never leaves this |
| 33 | * device: no environment, transcriber, or provider is involved, so a silent result points |
| 34 | * at the microphone rather than at transcription. |
| 35 | */ |
| 36 | export function MicrophoneTest({ |
| 37 | microphoneId, |
| 38 | onMicrophoneAccess, |
| 39 | }: { |
| 40 | microphoneId: string; |
| 41 | /** Labels and device ids become readable once the browser grants access. */ |
| 42 | onMicrophoneAccess: () => void; |
| 43 | }) { |
| 44 | const searchableSetting = useSearchableSetting(); |
| 45 | const gt = useGT(); |
| 46 | const messages = useMessages(); |
| 47 | const [phase, setPhase] = useState<"idle" | "starting" | "recording" | "ready">("idle"); |
| 48 | const [recording, setRecording] = useState<{ url: string; duration: number } | null>(null); |
| 49 | const [error, setError] = useState<string | null>(null); |
| 50 | const [level, setLevel] = useState(0); |
| 51 | const [elapsed, setElapsed] = useState(0); |
| 52 | const capture = useRef<Capture | null>(null); |
| 53 | const recordingUrl = useRef<string | null>(null); |
| 54 | |
| 55 | const releaseCapture = useStableCallback(() => { |
| 56 | const current = capture.current; |
| 57 | if (!current) return; |
| 58 | capture.current = null; |
| 59 | clearTimeout(current.timer); |
| 60 | clearInterval(current.elapsedTimer); |
| 61 | current.worklet?.disconnect(); |
| 62 | current.worklet?.port.close(); |
| 63 | if (current.recorder && current.recorder.state !== "inactive") current.recorder.stop(); |
| 64 | current.stream?.getTracks().forEach((track) => track.stop()); |
| 65 | void current.context?.close().catch(() => undefined); |
| 66 | }); |
| 67 | const discard = () => { |
| 68 | if (recordingUrl.current) URL.revokeObjectURL(recordingUrl.current); |
| 69 | recordingUrl.current = null; |
| 70 | setRecording(null); |
| 71 | setPhase("idle"); |
| 72 | }; |
| 73 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- releases the microphone and the recording's object URL on unmount |
| 74 | useEffect( |
| 75 | () => () => { |
| 76 | releaseCapture(); |
| 77 | if (recordingUrl.current) URL.revokeObjectURL(recordingUrl.current); |
| 78 | }, |
| 79 | [releaseCapture], |
| 80 | ); |
| 81 | |
| 82 | const start = async () => { |
| 83 | releaseCapture(); |
| 84 | discard(); |
| 85 | setError(null); |
| 86 | setLevel(0); |
| 87 | setElapsed(0); |
| 88 | setPhase("starting"); |
| 89 | if ( |
| 90 | !navigator.mediaDevices?.getUserMedia || |
| 91 | typeof MediaRecorder === "undefined" || |
| 92 | typeof AudioWorkletNode === "undefined" |
| 93 | ) { |
| 94 | setError( |
| 95 | gt("Microphone testing is unavailable in this browser. Use HTTPS or the desktop app."), |
| 96 | ); |
| 97 | setPhase("idle"); |
| 98 | return; |
| 99 | } |
| 100 | const current: Capture = {}; |
| 101 | capture.current = current; |
| 102 | try { |
| 103 | const stream = await requestMicrophone(microphoneId); |
| 104 | if (capture.current !== current) { |
| 105 | stream.getTracks().forEach((track) => track.stop()); |
| 106 | return; |
| 107 | } |
| 108 | current.stream = stream; |
| 109 | onMicrophoneAccess(); |
| 110 | const recorder = new MediaRecorder(stream); |
| 111 | current.recorder = recorder; |
| 112 | const chunks: Blob[] = []; |
| 113 | recorder.addEventListener("dataavailable", ({ data }) => { |
| 114 | if (capture.current === current && data.size) chunks.push(data); |
| 115 | }); |
| 116 | recorder.addEventListener("stop", () => { |
| 117 | if (capture.current !== current) return; |
| 118 | const blob = new Blob(chunks, { type: recorder.mimeType }); |
| 119 | const duration = |
| 120 | current.startedAt === undefined ? 0 : (performance.now() - current.startedAt) / 1_000; |
| 121 | releaseCapture(); |
| 122 | setLevel(0); |
| 123 | if (!blob.size) { |
| 124 | setError(gt("No audio was recorded. Try again.")); |
| 125 | setPhase("idle"); |
| 126 | return; |
| 127 | } |
| 128 | recordingUrl.current = URL.createObjectURL(blob); |
| 129 | setRecording({ url: recordingUrl.current, duration }); |
| 130 | setPhase("ready"); |
| 131 | }); |
| 132 | const onRecordingError = () => { |
| 133 | if (capture.current !== current) return; |
| 134 | releaseCapture(); |
| 135 | setLevel(0); |
| 136 | setError(gt("Could not record microphone audio. Try again.")); |
| 137 | setPhase("idle"); |
| 138 | }; |
| 139 | recorder.addEventListener("error", onRecordingError); |
| 140 | for (const track of stream.getAudioTracks()) { |
| 141 | track.addEventListener( |
| 142 | "ended", |
| 143 | () => { |
| 144 | if (capture.current !== current) return; |
| 145 | setError(gt("The microphone disconnected.")); |
| 146 | if (recorder.state !== "inactive") recorder.stop(); |
| 147 | }, |
| 148 | { once: true }, |
| 149 | ); |
| 150 | } |
| 151 | const context = new AudioContext(); |
| 152 | current.context = context; |
| 153 | await context.audioWorklet.addModule(workletUrl); |
| 154 | if (capture.current !== current) return; |
| 155 | const worklet = new AudioWorkletNode(context, "t3-pcm-capture", { |
| 156 | channelCount: 1, |
| 157 | processorOptions: { meterOnly: true }, |
| 158 | }); |
| 159 | current.worklet = worklet; |
| 160 | worklet.port.onmessage = ({ data }: MessageEvent<number | "stopped">) => { |
| 161 | if (capture.current === current && typeof data === "number") setLevel(data); |
| 162 | }; |
| 163 | worklet.onprocessorerror = onRecordingError; |
| 164 | context.createMediaStreamSource(stream).connect(worklet); |
| 165 | worklet.connect(context.destination); |
| 166 | await context.resume(); |
| 167 | if (capture.current !== current) return; |
| 168 | const startedAt = performance.now(); |
| 169 | current.startedAt = startedAt; |
| 170 | current.elapsedTimer = setInterval(() => { |
| 171 | setElapsed(Math.min(LOOPBACK_SECONDS, Math.floor((performance.now() - startedAt) / 1_000))); |
| 172 | }, 100); |
| 173 | recorder.start(); |
| 174 | worklet.port.postMessage("start", []); |
| 175 | setPhase("recording"); |
| 176 | current.timer = setTimeout(() => { |
| 177 | if (recorder.state !== "inactive") recorder.stop(); |
| 178 | }, LOOPBACK_SECONDS * 1_000); |
| 179 | } catch (cause) { |
| 180 | if (capture.current !== current) return; |
| 181 | releaseCapture(); |
| 182 | setPhase("idle"); |
| 183 | const name = typeof cause === "object" && cause !== null && "name" in cause && cause.name; |
| 184 | setError( |
| 185 | name === "NotAllowedError" |
| 186 | ? messages(MICROPHONE_PERMISSION_ERROR) |
| 187 | : name === "NotFoundError" |
| 188 | ? gt("No microphone found.") |
| 189 | : cause instanceof Error |
| 190 | ? (errorDisplayMessage(cause, m) ?? cause.message) |
| 191 | : gt("Could not access the microphone."), |
| 192 | ); |
| 193 | } |
| 194 | }; |
| 195 | const cancel = () => { |
| 196 | releaseCapture(); |
| 197 | discard(); |
| 198 | setError(null); |
| 199 | }; |
| 200 | |
| 201 | const control = |
| 202 | phase === "recording" || phase === "starting" ? ( |
| 203 | <Button |
| 204 | size="sm" |
| 205 | variant="outline" |
| 206 | aria-label={gt("Stop recording")} |
| 207 | disabled={phase !== "recording"} |
| 208 | onClick={() => { |
| 209 | const recorder = capture.current?.recorder; |
| 210 | if (recorder && recorder.state !== "inactive") recorder.stop(); |
| 211 | }} |
| 212 | > |
| 213 | <SquareIcon /> |
| 214 | <T>Stop</T> |
| 215 | </Button> |
| 216 | ) : ( |
| 217 | <Button size="sm" variant="outline" onClick={() => void start()}> |
| 218 | {phase === "ready" ? <RotateCcwIcon /> : <MicIcon />} |
| 219 | {phase === "ready" ? gt("Record again") : gt("Record")} |
| 220 | </Button> |
| 221 | ); |
| 222 | const panel = error ? ( |
| 223 | <p role="alert" className="min-w-0 flex-1 text-xs text-destructive"> |
| 224 | {error} |
| 225 | </p> |
| 226 | ) : phase === "starting" ? ( |
| 227 | <span role="status" className="min-w-0 flex-1 truncate text-xs text-muted-foreground"> |
| 228 | <T>Opening microphone…</T> |
| 229 | </span> |
| 230 | ) : phase === "recording" ? ( |
| 231 | <> |
| 232 | <span aria-hidden className="size-1.5 shrink-0 rounded-full bg-destructive" /> |
| 233 | <div className="flex w-24 min-w-0"> |
| 234 | <VoiceWaveform level={level} /> |
| 235 | </div> |
| 236 | <span |
| 237 | className="ms-auto shrink-0 text-xs tabular-nums text-muted-foreground" |
| 238 | aria-label={gt("Recording time")} |
| 239 | > |
| 240 | {formatTime(elapsed)} / {formatTime(LOOPBACK_SECONDS)} |
| 241 | </span> |
| 242 | <meter |
| 243 | aria-label={gt("Microphone input level")} |
| 244 | min={0} |
| 245 | max={1} |
| 246 | value={level} |
| 247 | className="sr-only" |
| 248 | /> |
| 249 | </> |
| 250 | ) : phase === "ready" && recording ? ( |
| 251 | <MicrophonePlayback key={recording.url} src={recording.url} duration={recording.duration} /> |
| 252 | ) : null; |
| 253 | |
| 254 | return ( |
| 255 | <SettingsRow |
| 256 | {...searchableSetting("microphone-test")} |
| 257 | title={gt("Test microphone")} |
| 258 | description={gt("Record ten seconds and play it back. The audio stays on this device.")} |
| 259 | control={control} |
| 260 | > |
| 261 | {panel ? ( |
| 262 | <div |
| 263 | className={cn( |
| 264 | "mt-3 mb-2 flex min-h-10 items-center gap-3 rounded-lg border border-border/60 bg-muted/30 py-1 pe-1", |
| 265 | phase === "ready" && !error ? "ps-1" : "ps-3", |
| 266 | )} |
| 267 | > |
| 268 | {panel} |
| 269 | <Button |
| 270 | size="icon-sm" |
| 271 | variant="ghost-muted" |
| 272 | aria-label={phase === "ready" ? gt("Discard recording") : gt("Cancel microphone test")} |
| 273 | onClick={cancel} |
| 274 | > |
| 275 | <XIcon /> |
| 276 | </Button> |
| 277 | </div> |
| 278 | ) : null} |
| 279 | </SettingsRow> |
| 280 | ); |
| 281 | } |
| 282 | |
| 283 | function MicrophonePlayback({ src, duration }: { src: string; duration: number }) { |
| 284 | const gt = useGT(); |
| 285 | const audio = useRef<HTMLAudioElement>(null); |
| 286 | const [playing, setPlaying] = useState(false); |
| 287 | const [position, setPosition] = useState(0); |
| 288 | const [failed, setFailed] = useState(false); |
| 289 | const progress = duration > 0 ? Math.min(position / duration, 1) * 100 : 0; |
| 290 | |
| 291 | const togglePlayback = async () => { |
| 292 | const player = audio.current; |
| 293 | if (!player) return; |
| 294 | if (!player.paused) { |
| 295 | player.pause(); |
| 296 | return; |
| 297 | } |
| 298 | setFailed(false); |
| 299 | if (player.ended) player.currentTime = 0; |
| 300 | try { |
| 301 | await player.play(); |
| 302 | } catch { |
| 303 | setFailed(true); |
| 304 | } |
| 305 | }; |
| 306 | |
| 307 | return ( |
| 308 | <> |
| 309 | <audio |
| 310 | ref={audio} |
| 311 | src={src} |
| 312 | preload="metadata" |
| 313 | onPlay={() => setPlaying(true)} |
| 314 | onPause={() => setPlaying(false)} |
| 315 | onEnded={() => setPlaying(false)} |
| 316 | onTimeUpdate={() => setPosition(audio.current?.currentTime ?? 0)} |
| 317 | onError={() => setFailed(true)} |
| 318 | /> |
| 319 | <Button |
| 320 | size="icon-sm-pill" |
| 321 | variant="outline" |
| 322 | aria-label={playing ? gt("Pause recording") : gt("Play recording")} |
| 323 | onClick={() => void togglePlayback()} |
| 324 | > |
| 325 | {playing ? <PauseIcon /> : <PlayIcon />} |
| 326 | </Button> |
| 327 | {failed ? ( |
| 328 | <p role="alert" className="min-w-0 flex-1 text-xs text-destructive"> |
| 329 | <T>Could not play this recording. Try recording again.</T> |
| 330 | </p> |
| 331 | ) : ( |
| 332 | <input |
| 333 | type="range" |
| 334 | aria-label={gt("Recording playback position")} |
| 335 | min={0} |
| 336 | max={duration} |
| 337 | step={0.01} |
| 338 | value={Math.min(position, duration)} |
| 339 | style={{ |
| 340 | background: `linear-gradient(to right, var(--primary) ${progress}%, var(--muted) ${progress}%)`, |
| 341 | }} |
| 342 | disabled={duration <= 0} |
| 343 | onChange={(event) => { |
| 344 | const next = Number(event.target.value); |
| 345 | if (audio.current) audio.current.currentTime = next; |
| 346 | setPosition(next); |
| 347 | }} |
| 348 | className="h-1 min-w-0 flex-1 cursor-pointer appearance-none rounded-full bg-muted accent-primary focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-ring [&::-webkit-slider-thumb]:size-2.5 [&::-webkit-slider-thumb]:appearance-none [&::-webkit-slider-thumb]:rounded-full [&::-webkit-slider-thumb]:bg-primary [&::-moz-range-thumb]:size-2.5 [&::-moz-range-thumb]:rounded-full [&::-moz-range-thumb]:border-0 [&::-moz-range-thumb]:bg-primary" |
| 349 | /> |
| 350 | )} |
| 351 | <span className="shrink-0 text-xs tabular-nums text-muted-foreground"> |
| 352 | {formatTime(position)} / {formatTime(duration)} |
| 353 | </span> |
| 354 | </> |
| 355 | ); |
| 356 | } |