File size: 2,725 Bytes
4fd3a34
a966957
c5fde49
 
 
6dd9bad
4fd3a34
 
 
c5fde49
 
6dd9bad
c5fde49
4fd3a34
c5fde49
4fd3a34
 
 
 
 
 
5634afc
c5fde49
4fd3a34
 
 
 
c5fde49
4fd3a34
 
 
 
5634afc
 
a966957
5634afc
4fd3a34
c5fde49
 
4fd3a34
 
c5fde49
 
4fd3a34
 
 
 
 
 
 
 
 
 
 
 
6dd9bad
4fd3a34
 
 
 
c5fde49
 
a966957
4fd3a34
 
c5fde49
 
 
 
4fd3a34
 
c5fde49
 
 
 
 
 
4fd3a34
c5fde49
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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; // On garde le contrôle manuel de l'arrêt

            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) => {
                // On ignore l'erreur 'aborted' qui est déclenchée lors d'un stop() manuel
                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);
            // If already started, just set state
            setIsRecording(true);
        }
    };

    const stopRecording = () => {
        if (recognitionRef.current && isRecording) {
            recognitionRef.current.stop();
            setIsRecording(false);
        }
    };

    return {
        isRecording,
        isTranscribing: false, // No longer needed as it's real-time
        startRecording,
        stopRecording
    };
};