-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext-to-speech.js
95 lines (72 loc) · 2.77 KB
/
text-to-speech.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
// Initialize new SpeechSynthesisUtterance object
let speech = new SpeechSynthesisUtterance();
// Set Speech Language
speech.lang = "en";
let voices = []; // global array of available voices
let speechRate;
function cancelSpeech() {
window.speechSynthesis.cancel();
}
function resumeSpeech() {
window.speechSynthesis.resume();
}
function pauseSpeech() {
window.speechSynthesis.pause();
}
function countwords(str) {
count = str.split(" ").length;
document.querySelector("#count").innerHTML = count;
}
window.speechSynthesis.onvoiceschanged = () => {
// Get List of Voices
voices = window.speechSynthesis.getVoices();
// Initially set the First Voice in the Array.
speech.voice = voices[0];
// Set the Voice Select List. (Set the Index as the value, which we'll use later when the user updates the Voice using the Select Menu.)
let voiceSelect = document.querySelector("#voices");
voices.forEach(
(voice, i) => (voiceSelect.options[i] = new Option(voice.name, i)),
);
};
document.querySelector("#rate").addEventListener("input", () => {
// Get rate Value from the input
const rate = document.querySelector("#rate").value;
// Set rate property of the SpeechSynthesisUtterance instance
speech.rate = rate;
speechRate = speech.rate;
// Update the rate label
document.querySelector("#rate-label").innerHTML = rate;
});
document.querySelector("#volume").addEventListener("input", () => {
// Get volume Value from the input
const volume = document.querySelector("#volume").value;
// Set volume property of the SpeechSynthesisUtterance instance
speech.volume = volume;
// Update the volume label
document.querySelector("#volume-label").innerHTML = volume;
});
document.querySelector("#voices").addEventListener("change", () => {
// On Voice change, use the value of the select menu (which is the index of the voice in the global voice array)
speech.voice = voices[document.querySelector("#voices").value];
});
// get words from textarea and count.
document.querySelector("textarea").addEventListener("input", () => {
// Get words from textarea
const words = document.querySelector("textarea").value;
if (words.length > 0) {
countwords(words);
} else {
document.querySelector("#count").innerHTML = '';
}
});
document.querySelector("#start").addEventListener("click", () => {
// cancel all speech synthesis process running
cancelSpeech();
// Set the text property with the value of the textarea
speech.text = document.querySelector("textarea").value;
// Start Speaking depending on the rate that has been set.
window.speechSynthesis.speak(speech);
});
document.querySelector("#pause").addEventListener("click", pauseSpeech);
document.querySelector("#resume").addEventListener("click", resumeSpeech);
document.querySelector("#cancel").addEventListener("click", cancelSpeech);