MCPcopy Index your code
hub / github.com/TanStack/ai / useAudioRecorder

Function useAudioRecorder

testing/panel/src/hooks/useAudioRecorder.ts:6–85  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

4 * Hook for recording audio and transcribing it via the transcription API.
5 */
6export function useAudioRecorder() {
7 const [isRecording, setIsRecording] = useState(false)
8 const [isTranscribing, setIsTranscribing] = useState(false)
9 const mediaRecorderRef = useRef<MediaRecorder | null>(null)
10 const chunksRef = useRef<Blob[]>([])
11
12 const startRecording = useCallback(async () => {
13 try {
14 const stream = await navigator.mediaDevices.getUserMedia({ audio: true })
15 const mediaRecorder = new MediaRecorder(stream, {
16 mimeType: 'audio/webm;codecs=opus',
17 })
18 mediaRecorderRef.current = mediaRecorder
19 chunksRef.current = []
20
21 mediaRecorder.ondataavailable = (e) => {
22 if (e.data.size > 0) {
23 chunksRef.current.push(e.data)
24 }
25 }
26
27 mediaRecorder.start()
28 setIsRecording(true)
29 } catch (error) {
30 console.error('Failed to start recording:', error)
31 alert('Could not access microphone. Please check permissions.')
32 }
33 }, [])
34
35 const stopRecording = useCallback(async (): Promise<string | null> => {
36 return new Promise((resolve) => {
37 const mediaRecorder = mediaRecorderRef.current
38 if (!mediaRecorder) {
39 resolve(null)
40 return
41 }
42
43 mediaRecorder.onstop = async () => {
44 setIsRecording(false)
45 setIsTranscribing(true)
46
47 const audioBlob = new Blob(chunksRef.current, { type: 'audio/webm' })
48
49 // Stop all tracks
50 mediaRecorder.stream.getTracks().forEach((track) => track.stop())
51
52 try {
53 const formData = new FormData()
54 formData.append(
55 'audio',
56 new File([audioBlob], 'recording.webm', { type: 'audio/webm' }),
57 )
58 formData.append('model', 'whisper-1')
59
60 const response = await fetch('/api/transcription', {
61 method: 'POST',
62 body: formData,
63 })

Callers 1

ChatPageFunction · 0.90

Calls 7

jsonMethod · 0.80
startMethod · 0.65
errorMethod · 0.65
appendMethod · 0.65
fetchFunction · 0.50
pushMethod · 0.45
stopMethod · 0.45

Tested by

no test coverage detected