-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathscript.js
56 lines (46 loc) · 1.7 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
const textInput = document.getElementById('text-input');
const convertBtn = document.getElementById('convert-btn');
const stopBtn = document.getElementById('stop-btn');
let speechSynthesis = window.speechSynthesis;
let speechSynthesisUtterance = new SpeechSynthesisUtterance();
convertBtn.addEventListener('click', () => {
let text = textInput.value.trim();
if (text !== '') {
speechSynthesisUtterance.text = text;
speechSynthesis.speak(speechSynthesisUtterance);
convertBtn.disabled = true;
stopBtn.disabled = false;
}
});
stopBtn.addEventListener('click', () => {
speechSynthesis.cancel();
convertBtn.disabled = false;
stopBtn.disabled = true;
});
// speech to text
document.addEventListener('DOMContentLoaded', () => {
const startBtn = document.getElementById('start-btn');
const stopBtn = document.getElementById('stop-btn');
const output = document.getElementById('output');
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
const recognition = new SpeechRecognition();
recognition.continuous = true;
recognition.interimResults = true;
recognition.lang = 'en-US';
recognition.onresult = (event) => {
let transcript = '';
for (let i = event.resultIndex; i < event.results.length; ++i) {
transcript += event.results[i][0].transcript;
}
output.value = transcript;
};
recognition.onerror = (event) => {
console.error('Speech recognition error', event.error);
};
startBtn.addEventListener('click', () => {
recognition.start();
});
stopBtn.addEventListener('click', () => {
recognition.stop();
});
});