-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpeech-to-text
More file actions
50 lines (43 loc) · 1.49 KB
/
Copy pathSpeech-to-text
File metadata and controls
50 lines (43 loc) · 1.49 KB
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
<!DOCTYPE html>
<html>
<head>
<title>Speech-to-Text Example</title>
</head>
<body>
<h1>Speech-to-Text Example</h1>
<button id="startButton">Start Recording</button>
<div id="output"></div>
<script>
const startButton = document.getElementById('startButton');
const outputDiv = document.getElementById('output');
let recognition = new webkitSpeechRecognition() || new SpeechRecognition();
recognition.continuous = true;
recognition.interimResults = true;
recognition.onstart = () => {
startButton.disabled = true;
startButton.textContent = 'Recording...';
};
recognition.onerror = (event) => {
console.error('Speech recognition error:', event.error);
startButton.disabled = false;
startButton.textContent = 'Start Recording';
};
recognition.onend = () => {
startButton.disabled = false;
startButton.textContent = 'Start Recording';
};
recognition.onresult = (event) => {
let transcript = '';
for (let i = event.resultIndex; i < event.results.length; i++) {
if (event.results[i].isFinal) {
transcript += event.results[i][0].transcript;
}
}
outputDiv.textContent = transcript;
};
startButton.addEventListener('click', () => {
recognition.start();
});
</script>
</body>
</html>