-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
147 lines (126 loc) · 3.61 KB
/
index.ts
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
export type OnUpdateFunc = ({
transcript,
interimTranscript,
isFinal,
}: {
transcript: string;
interimTranscript: string;
isFinal: boolean;
}) => void;
export type OnErrorFunc = ({
error,
}: {
error: SpeechRecognitionErrorEvent | null;
}) => void;
export interface Options {
lang?: string;
continuous?: boolean;
timeout?: number;
onUpdate?: OnUpdateFunc;
onError?: OnErrorFunc;
}
export function useSpeechRecognition({
lang,
continuous,
timeout,
onUpdate,
onError,
}: Options) {
const [transcript, setTranscript] = useState("");
const [interimTranscript, setInterimTranscript] = useState("");
const [isFinal, setIsFinal] = useState(false);
const [isListening, setIsListening] = useState(false);
const [isSupported, setIsSupported] = useState(false);
const [error, setError] = useState<SpeechRecognitionErrorEvent | null>(null);
const recognitionRef = useRef<SpeechRecognition | null>(null);
const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const SpeechRecognition = useMemo(() => {
if (typeof window === "undefined") {
return null;
}
return window.SpeechRecognition || window.webkitSpeechRecognition;
}, []);
useEffect(() => {
setIsSupported(!!SpeechRecognition);
}, [SpeechRecognition]);
useEffect(() => {
if (!SpeechRecognition) return;
const rec = new SpeechRecognition();
recognitionRef.current = rec;
rec.lang = lang ?? "en-US";
rec.continuous = continuous ?? timeout !== undefined;
rec.interimResults = true;
rec.onstart = () => {
setError(null);
setInterimTranscript("");
setTranscript("");
setIsFinal(false);
setIsListening(true);
};
rec.onend = () => {
setIsFinal(true);
setIsListening(false);
};
rec.onerror = (event) => {
console.error(`Speech recognition error: ${event.error}`);
setError(event);
};
rec.onresult = (event) => {
// Handle custom timeout
if (!continuous && timeout !== undefined) {
if (timeoutRef.current) clearTimeout(timeoutRef.current);
timeoutRef.current = setTimeout(() => rec.stop(), timeout);
}
// Parse the transcript from results
let final = "";
let interim = "";
for (let i = 0; i < event.results.length; i++) {
const s = event.results[i]?.[0]?.transcript.toLocaleLowerCase();
if (event.results[i]?.isFinal) {
final += s;
} else {
interim += s;
}
}
setInterimTranscript(interim);
setTranscript(final);
};
return () => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
timeoutRef.current = null;
}
if (recognitionRef.current) {
recognitionRef.current.abort();
recognitionRef.current = null;
}
};
}, [SpeechRecognition, continuous, lang, timeout]);
useEffect(() => {
if (!isSupported) return;
if (onUpdate) onUpdate({ transcript, interimTranscript, isFinal });
}, [onUpdate, transcript, interimTranscript, isFinal, isSupported]);
useEffect(() => {
if (!isSupported) return;
if (onError) onError({ error });
}, [onError, error, isSupported]);
const start = useCallback(() => {
if (!recognitionRef.current) return;
recognitionRef.current.start();
}, []);
const stop = useCallback(() => {
if (!recognitionRef.current) return;
recognitionRef.current.stop();
}, []);
return {
transcript,
interimTranscript,
isListening,
isFinal,
isSupported,
start,
stop,
error,
};
}