| import { useState, useRef, useEffect } from 'react'; |
| import { logError } from '../lib/logger'; |
|
|
| interface UseAudioRecorderProps { |
| onTranscriptionComplete: (text: string) => void; |
| onError?: (message: string) => void; |
| apiUrl?: string; |
| token?: string; |
| organizationId?: string; |
| } |
|
|
| export const useAudioRecorder = ({ onTranscriptionComplete, onError }: UseAudioRecorderProps) => { |
| const [isRecording, setIsRecording] = useState(false); |
| const recognitionRef = useRef<any>(null); |
|
|
| useEffect(() => { |
| const SpeechRecognition = (window as any).SpeechRecognition || (window as any).webkitSpeechRecognition; |
| if (SpeechRecognition) { |
| const recognition = new SpeechRecognition(); |
| recognition.lang = 'fr-FR'; |
| recognition.interimResults = true; |
| recognition.continuous = true; |
|
|
| recognition.onresult = (event: any) => { |
| let currentTranscript = ""; |
| for (let i = 0; i < event.results.length; i++) { |
| currentTranscript += event.results[i][0].transcript; |
| } |
| onTranscriptionComplete(currentTranscript); |
| }; |
|
|
| recognition.onerror = (event: any) => { |
| |
| if (event.error !== 'aborted') { |
| logError("Speech recognition error:", event.error); |
| } |
| setIsRecording(false); |
| }; |
|
|
| recognition.onend = () => { |
| setIsRecording(false); |
| }; |
|
|
| recognitionRef.current = recognition; |
| } |
|
|
| return () => { |
| if (recognitionRef.current) { |
| recognitionRef.current.stop(); |
| } |
| }; |
| }, [onTranscriptionComplete]); |
|
|
| const startRecording = () => { |
| if (!recognitionRef.current) { |
| onError?.("La dictée vocale instantanée n'est pas supportée sur ce navigateur."); |
| return; |
| } |
| try { |
| recognitionRef.current.start(); |
| setIsRecording(true); |
| } catch (err) { |
| logError("Failed to start speech recognition:", err); |
| |
| setIsRecording(true); |
| } |
| }; |
|
|
| const stopRecording = () => { |
| if (recognitionRef.current && isRecording) { |
| recognitionRef.current.stop(); |
| setIsRecording(false); |
| } |
| }; |
|
|
| return { |
| isRecording, |
| isTranscribing: false, |
| startRecording, |
| stopRecording |
| }; |
| }; |
|
|