|
| 1 | +import { type ChangeEvent, type FormEvent, useCallback, useState } from 'react'; |
| 2 | +import { |
| 3 | + useAudioDevices, |
| 4 | + usePcmAudioRecorder, |
| 5 | +} from '@speechmatics/browser-audio-input-react'; |
| 6 | +import { |
| 7 | + type RealtimeTranscriptionConfig, |
| 8 | + useRealtimeTranscription, |
| 9 | +} from '@speechmatics/real-time-client-react'; |
| 10 | +import { getJWT } from '../actions'; |
| 11 | +import { configFromFormData } from '@/lib/real-time/config-from-form-data'; |
| 12 | +import { RECORDING_SAMPLE_RATE } from '@/lib/constants'; |
| 13 | + |
| 14 | +export function Controls() { |
| 15 | + const { startTranscription } = useRealtimeTranscription(); |
| 16 | + const { startRecording } = usePcmAudioRecorder(); |
| 17 | + |
| 18 | + const [deviceId, setDeviceId] = useState<string>(); |
| 19 | + |
| 20 | + const startSession = useCallback( |
| 21 | + async (config: RealtimeTranscriptionConfig) => { |
| 22 | + const jwt = await getJWT('rt'); |
| 23 | + await startTranscription(jwt, config); |
| 24 | + await startRecording({ deviceId, sampleRate: RECORDING_SAMPLE_RATE }); |
| 25 | + }, |
| 26 | + [startTranscription, startRecording, deviceId], |
| 27 | + ); |
| 28 | + |
| 29 | + const handleSubmit = useCallback( |
| 30 | + (e: FormEvent<HTMLFormElement>) => { |
| 31 | + e.preventDefault(); |
| 32 | + const formData = new FormData(e.currentTarget); |
| 33 | + const config = configFromFormData(formData); |
| 34 | + config.audio_format = { |
| 35 | + type: 'raw', |
| 36 | + encoding: 'pcm_f32le', |
| 37 | + sample_rate: RECORDING_SAMPLE_RATE, |
| 38 | + }; |
| 39 | + startSession(config); |
| 40 | + }, |
| 41 | + [startSession], |
| 42 | + ); |
| 43 | + |
| 44 | + return ( |
| 45 | + <article> |
| 46 | + <form onSubmit={handleSubmit}> |
| 47 | + <div className="grid"> |
| 48 | + <MicrophoneSelect setDeviceId={setDeviceId} /> |
| 49 | + <label> |
| 50 | + Select language |
| 51 | + <select name="language"> |
| 52 | + <option value="en" label="English" defaultChecked /> |
| 53 | + <option value="es" label="Spanish" /> |
| 54 | + <option value="ar" label="Arabic" /> |
| 55 | + </select> |
| 56 | + </label> |
| 57 | + </div> |
| 58 | + <div className="grid"> |
| 59 | + <StartStopButton /> |
| 60 | + </div> |
| 61 | + </form> |
| 62 | + </article> |
| 63 | + ); |
| 64 | +} |
| 65 | + |
| 66 | +function StartStopButton() { |
| 67 | + const { stopRecording } = usePcmAudioRecorder(); |
| 68 | + const { stopTranscription } = useRealtimeTranscription(); |
| 69 | + |
| 70 | + const stopSession = useCallback(() => { |
| 71 | + stopTranscription(); |
| 72 | + stopRecording(); |
| 73 | + }, [stopRecording, stopTranscription]); |
| 74 | + |
| 75 | + const connected = useRealtimeTranscription().socketState === 'open'; |
| 76 | + |
| 77 | + if (connected) { |
| 78 | + return ( |
| 79 | + <button type="button" onClick={stopSession}> |
| 80 | + Stop transcription |
| 81 | + </button> |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + return <button type="submit">Transcribe audio</button>; |
| 86 | +} |
| 87 | + |
| 88 | +function MicrophoneSelect({ |
| 89 | + setDeviceId, |
| 90 | +}: { setDeviceId: (deviceId: string) => void }) { |
| 91 | + const devices = useAudioDevices(); |
| 92 | + |
| 93 | + switch (devices.permissionState) { |
| 94 | + case 'prompt': |
| 95 | + return ( |
| 96 | + <label> |
| 97 | + Enable mic permissions |
| 98 | + <select |
| 99 | + onClick={devices.promptPermissions} |
| 100 | + onKeyDown={devices.promptPermissions} |
| 101 | + /> |
| 102 | + </label> |
| 103 | + ); |
| 104 | + case 'prompting': |
| 105 | + return ( |
| 106 | + <label> |
| 107 | + Enable mic permissions |
| 108 | + <select aria-busy="true" /> |
| 109 | + </label> |
| 110 | + ); |
| 111 | + case 'granted': { |
| 112 | + const onChange = (e: ChangeEvent<HTMLSelectElement>) => { |
| 113 | + setDeviceId(e.target.value); |
| 114 | + }; |
| 115 | + return ( |
| 116 | + <label> |
| 117 | + Select audio device |
| 118 | + <select onChange={onChange}> |
| 119 | + {devices.deviceList.map((d) => ( |
| 120 | + <option key={d.deviceId} value={d.deviceId}> |
| 121 | + {d.label} |
| 122 | + </option> |
| 123 | + ))} |
| 124 | + </select> |
| 125 | + </label> |
| 126 | + ); |
| 127 | + } |
| 128 | + case 'denied': |
| 129 | + return ( |
| 130 | + <label> |
| 131 | + Microphone permission disabled |
| 132 | + <select disabled /> |
| 133 | + </label> |
| 134 | + ); |
| 135 | + default: |
| 136 | + devices satisfies never; |
| 137 | + return null; |
| 138 | + } |
| 139 | +} |
0 commit comments