|
| 1 | +import { useState, useRef, useCallback, useEffect } from 'react'; |
| 2 | +import { |
| 3 | + initSpeechRecognitionWithProvider, |
| 4 | + startSpeechRecognition, |
| 5 | + stopSpeechRecognition, |
| 6 | + cleanupSpeechRecognition |
| 7 | +} from '@/services/speech-recognition'; |
| 8 | +import { SpeechRecognitionConfig } from '@/models/speech-recognition/speech-recognition-base'; |
| 9 | +import { logger } from '@/utils/logger'; |
| 10 | +import { useLanguageStore } from '@/stores/languageStore'; |
| 11 | + |
| 12 | +export type VoiceInputStatus = 'idle' | 'recording' | 'error'; |
| 13 | + |
| 14 | +interface UseVoiceInputOptions { |
| 15 | + onTextRecognized?: (text: string) => void; |
| 16 | + onError?: (error: string) => void; |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Hook for voice input functionality |
| 21 | + * Manages speech recognition state and provides control methods |
| 22 | + */ |
| 23 | +export const useVoiceInput = ({ onTextRecognized, onError }: UseVoiceInputOptions = {}) => { |
| 24 | + const [status, setStatus] = useState<VoiceInputStatus>('idle'); |
| 25 | + const isInitialized = useRef(false); |
| 26 | + const isRecordingRef = useRef(false); // Track actual recording state |
| 27 | + const onTextRecognizedRef = useRef(onTextRecognized); |
| 28 | + const onErrorRef = useRef(onError); |
| 29 | + const { language } = useLanguageStore(); |
| 30 | + |
| 31 | + // Update refs when callbacks change |
| 32 | + useEffect(() => { |
| 33 | + onTextRecognizedRef.current = onTextRecognized; |
| 34 | + onErrorRef.current = onError; |
| 35 | + }, [onTextRecognized, onError]); |
| 36 | + |
| 37 | + // Initialize speech recognition and reinitialize when language changes |
| 38 | + useEffect(() => { |
| 39 | + let mounted = true; |
| 40 | + let initializationStarted = false; |
| 41 | + |
| 42 | + // Async initialization |
| 43 | + (async () => { |
| 44 | + try { |
| 45 | + // Cleanup previous instance and wait for it to complete |
| 46 | + await cleanupSpeechRecognition(); |
| 47 | + |
| 48 | + // Check if still mounted after cleanup |
| 49 | + if (!mounted) return; |
| 50 | + |
| 51 | + // Select model based on current language |
| 52 | + const modelType = language === 'zh-CN' ? 'small-cn' : 'small-en'; |
| 53 | + |
| 54 | + const config: SpeechRecognitionConfig = { |
| 55 | + provider: 'vosk', |
| 56 | + modelType |
| 57 | + }; |
| 58 | + |
| 59 | + initializationStarted = true; |
| 60 | + await initSpeechRecognitionWithProvider(config, (text: string) => { |
| 61 | + if (onTextRecognizedRef.current) { |
| 62 | + onTextRecognizedRef.current(text); |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + if (mounted) { |
| 67 | + isInitialized.current = true; |
| 68 | + } |
| 69 | + } catch (error) { |
| 70 | + logger.error('Failed to initialize speech recognition', error, 'VoiceInput'); |
| 71 | + if (mounted && onErrorRef.current) { |
| 72 | + onErrorRef.current('Speech recognition initialization failed'); |
| 73 | + } |
| 74 | + } |
| 75 | + })(); |
| 76 | + |
| 77 | + // Cleanup on unmount or language change |
| 78 | + return () => { |
| 79 | + mounted = false; |
| 80 | + isRecordingRef.current = false; |
| 81 | + if (initializationStarted) { |
| 82 | + cleanupSpeechRecognition(); |
| 83 | + } |
| 84 | + }; |
| 85 | + }, [language]); // Reinitialize when language changes |
| 86 | + |
| 87 | + /** |
| 88 | + * Start voice recording |
| 89 | + */ |
| 90 | + const startRecording = useCallback(async () => { |
| 91 | + if (isRecordingRef.current) return; |
| 92 | + |
| 93 | + if (!isInitialized.current) { |
| 94 | + const errorMsg = 'Speech recognition not initialized'; |
| 95 | + logger.error(errorMsg, undefined, 'VoiceInput'); |
| 96 | + if (onErrorRef.current) { |
| 97 | + onErrorRef.current(errorMsg); |
| 98 | + } |
| 99 | + setStatus('error'); |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + try { |
| 104 | + isRecordingRef.current = true; |
| 105 | + setStatus('recording'); |
| 106 | + await startSpeechRecognition(); |
| 107 | + } catch (error) { |
| 108 | + logger.error('Failed to start recording', error, 'VoiceInput'); |
| 109 | + isRecordingRef.current = false; |
| 110 | + setStatus('idle'); |
| 111 | + if (onErrorRef.current) { |
| 112 | + onErrorRef.current('Failed to start voice recording'); |
| 113 | + } |
| 114 | + } |
| 115 | + }, []); |
| 116 | + |
| 117 | + /** |
| 118 | + * Stop voice recording |
| 119 | + */ |
| 120 | + const stopRecording = useCallback(async () => { |
| 121 | + if (!isRecordingRef.current) return; |
| 122 | + |
| 123 | + try { |
| 124 | + isRecordingRef.current = false; |
| 125 | + await stopSpeechRecognition(); |
| 126 | + setStatus('idle'); |
| 127 | + } catch (error) { |
| 128 | + logger.error('Failed to stop recording', error, 'VoiceInput'); |
| 129 | + isRecordingRef.current = false; |
| 130 | + setStatus('idle'); |
| 131 | + if (onErrorRef.current) { |
| 132 | + onErrorRef.current('Failed to stop voice recording'); |
| 133 | + } |
| 134 | + } |
| 135 | + }, []); |
| 136 | + |
| 137 | + /** |
| 138 | + * Toggle recording state |
| 139 | + */ |
| 140 | + const toggleRecording = useCallback(async () => { |
| 141 | + if (isRecordingRef.current) { |
| 142 | + await stopRecording(); |
| 143 | + } else { |
| 144 | + await startRecording(); |
| 145 | + } |
| 146 | + }, [startRecording, stopRecording]); |
| 147 | + |
| 148 | + return { |
| 149 | + status, |
| 150 | + isRecording: status === 'recording', |
| 151 | + startRecording, |
| 152 | + stopRecording, |
| 153 | + toggleRecording, |
| 154 | + }; |
| 155 | +}; |
0 commit comments