Open History to see the last commit
1import { useCallback, useRef, useState } from 'react'
2
3/**
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 })
64
65 if (!response.ok) {
66 const errorData = await response.json()
67 throw new Error(errorData.error || 'Transcription failed')
68 }
69
70 const result = await response.json()
71 setIsTranscribing(false)
72 resolve(result.text || null)
73 } catch (error) {
74 console.error('Transcription error:', error)
75 setIsTranscribing(false)
76 resolve(null)
77 }
78 }
79
80 mediaRecorder.stop()
81 })
82 }, [])
83
84 return { isRecording, isTranscribing, startRecording, stopRecording }
85}