-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
63 lines (54 loc) · 1.98 KB
/
script.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
const output = document.getElementById("output");
const startButton = document.getElementById("startButton");
let recognizing = false;
let finalTranscript = ''; // Moved outside to maintain context
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
const recognition = new SpeechRecognition();
recognition.lang = "en-US";
recognition.interimResults = true;
recognition.continuous = true; // Set continuous to true for indefinite recognition
startButton.addEventListener('click', () => {
if (recognizing) {
recognition.stop();
recognizing = false;
startButton.textContent = 'Start Listening';
} else {
output.textContent = ''; // Clear output text when starting
finalTranscript = ''; // Clear finalTranscript when starting
recognition.start();
recognizing = true;
startButton.textContent = 'Listening...';
}
});
recognition.addEventListener('result', (e) => {
let interimTranscript = '';
// Process interim and final results
for (const result of e.results) {
if (result.isFinal) {
finalTranscript = result[0].transcript; // Use = to replace old text
} else {
interimTranscript += result[0].transcript;
}
}
// Display only the final transcript
output.textContent = finalTranscript;
// You can use interimTranscript if you want to display live updates
// output.textContent = interimTranscript;
});
recognition.addEventListener('end', () => {
if (recognizing) {
recognition.start(); // Restart the recognition
} else {
startButton.textContent = 'Start Listening';
}
});
recognition.addEventListener('start', () => {
recognizing = true;
});
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape') {
recognition.stop();
recognizing = false;
startButton.textContent = 'Start Listening';
}
});