-
Notifications
You must be signed in to change notification settings - Fork 0
/
voicerecognition.js
191 lines (159 loc) · 5.23 KB
/
voicerecognition.js
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
export default class VoiceRecognition {
constructor(elements, fetchCompanyData, options = {}) {
this.elements = elements;
this.fetchCompanyData = this.debounce(
this.retryFetch(fetchCompanyData, options.maxRetries || 3),
300
);
this.options = {
interimResults: true,
continuous: false,
language: 'en-US',
confidenceThreshold: 0.6,
...options,
};
this.isListening = false;
this.recognition = this.initializeRecognition();
this.setupEventListeners();
}
initializeRecognition() {
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
if (!SpeechRecognition) {
this.showFeedback('Speech Recognition not supported in this browser.', false);
throw new Error('Speech Recognition API is not available.');
}
const recognition = new SpeechRecognition();
recognition.lang = this.options.language;
recognition.continuous = this.options.continuous;
recognition.interimResults = this.options.interimResults;
recognition.onstart = this.onRecognitionStart.bind(this);
recognition.onresult = this.onRecognitionResult.bind(this);
recognition.onend = this.onRecognitionEnd.bind(this);
recognition.onerror = this.onRecognitionError.bind(this);
return recognition;
}
setupEventListeners() {
this.elements.voiceButton.addEventListener('click', () => this.toggleVoiceRecognition());
document.addEventListener('keydown', (event) => {
if (event.altKey && event.key === 'v') {
this.toggleVoiceRecognition();
}
});
}
startRecognition() {
if (!this.isListening) {
this.recognition.start();
}
}
stopRecognition() {
if (this.isListening) {
this.recognition.stop();
}
}
toggleVoiceRecognition() {
this.isListening ? this.stopRecognition() : this.startRecognition();
}
onRecognitionStart() {
this.isListening = true;
this.showFeedback('Listening... Speak now.', true);
this.toggleButtonState(true);
}
onRecognitionResult(event) {
const filteredResults = Array.from(event.results).filter(
(result) => result.isFinal && result[0].confidence >= this.options.confidenceThreshold
);
const finalTranscript = filteredResults.map((result) => result[0].transcript.trim()).join(' ');
const interimTranscript = Array.from(event.results)
.filter((result) => !result.isFinal)
.map((result) => result[0].transcript.trim())
.join(' ');
if (interimTranscript) {
this.updateSearchInput(interimTranscript);
}
if (finalTranscript) {
this.handleFinalTranscript(finalTranscript);
this.animateDetection(); // Add glowing effect on detection
}
}
async handleFinalTranscript(transcript) {
this.updateSearchInput(transcript);
try {
await this.fetchCompanyData(transcript);
this.animateSuccess();
} catch (error) {
this.showFeedback(`Error: ${error.message}`, false);
} finally {
this.stopRecognition();
}
}
updateSearchInput(transcript) {
if (this.elements.companySearch) {
this.elements.companySearch.value = transcript;
}
}
onRecognitionEnd() {
this.isListening = false;
this.showFeedback('Click to start speaking.', false);
this.toggleButtonState(false);
}
onRecognitionError(event) {
const errorMessages = {
'no-speech': 'No speech detected. Please try again.',
'audio-capture': 'Microphone unavailable. Check permissions.',
'network': 'Network error. Check your connection.',
'not-allowed': 'Microphone access denied. Update browser settings.',
};
this.showFeedback(errorMessages[event.error] || `Error: ${event.error}`, false);
this.stopRecognition();
}
showFeedback(message, isActive) {
const { feedbackText, voiceButton } = this.elements;
feedbackText.textContent = message;
if (isActive) {
feedbackText.classList.add('active');
voiceButton.classList.add('active');
} else {
feedbackText.classList.remove('active');
voiceButton.classList.remove('active');
}
}
toggleButtonState(isActive) {
this.elements.voiceButton.classList.toggle('listening', isActive);
}
animateSuccess() {
const { voiceButton } = this.elements;
voiceButton.classList.add('success');
setTimeout(() => voiceButton.classList.remove('success'), 1000);
}
animateDetection() {
const { voiceButton } = this.elements;
voiceButton.classList.add('detected'); // Add detection effect
setTimeout(() => voiceButton.classList.remove('detected'), 1000);
}
retryFetch(fetchFunction, maxRetries) {
return async (data) => {
let attempt = 0;
while (attempt < maxRetries) {
try {
return await fetchFunction(data);
} catch {
attempt++; // 'error' was unused; just omit it
if (attempt >= maxRetries) {
throw new Error('Maximum retry attempts reached.');
}
await this.delay(300 * attempt);
}
}
};
}
debounce(func, wait) {
let timeout;
return (...args) => {
clearTimeout(timeout);
timeout = setTimeout(() => func(...args), wait);
};
}
delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
}