-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
95 lines (86 loc) · 3.01 KB
/
index.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
// This is so that if speech is still playing from previous session, it stops on page load
speechSynthesis.cancel();
var isSpeaking = false;
// Initialize the speech synthesis
var speech = new SpeechSynthesisUtterance();
speech.rate = 1;
speech.pitch = 1;
speech.volume = 1;
speech.voice = speechSynthesis.getVoices()[0];
function speakInputText() {
isSpeaking = true;
speech.text = document.getElementById("textInput").value;
speechSynthesis.speak(speech);
}
function pauseSpeech() {
if (isSpeaking) {
isSpeaking = false;
speechSynthesis.pause();
document.getElementById(
"pauseButton"
).innerHTML = `Resume<img class="icon-link-small"
src="./assets/icons/resumeIcon.svg">`;
} else {
isSpeaking = true;
speechSynthesis.resume();
document.getElementById(
"pauseButton"
).innerHTML = `Pause<img class="icon-link-small"
src="./assets/icons/pauseIcon.svg">`;
}
}
function stopSpeech() {
isSpeaking = false;
speechSynthesis.cancel();
}
function changeVoice(voice) {
if (voice == "voice1") {
// console.log((speech.voice = speechSynthesis.getVoices()[8]));
speech.voice = speechSynthesis.getVoices()[8];
} else if (voice == "voice2") {
// console.log((speech.voice = speechSynthesis.getVoices()[0]));
speech.voice = speechSynthesis.getVoices()[0];
} else if (voice == "voice3") {
// console.log((speech.voice = speechSynthesis.getVoices()[1]));
speech.voice = speechSynthesis.getVoices()[1];
} else if (voice == "voice4") {
// console.log((speech.voice = speechSynthesis.getVoices()[11]));
speech.voice = speechSynthesis.getVoices()[11];
} else if (voice == "voice5") {
// console.log((speech.voice = speechSynthesis.getVoices()[12]));
speech.voice = speechSynthesis.getVoices()[12];
} else if (voice == "voice6") {
// console.log((speech.voice = speechSynthesis.getVoices()[18]));
speech.voice = speechSynthesis.getVoices()[18];
} else if (voice == "voice7") {
// console.log((speech.voice = speechSynthesis.getVoices()[33]));
speech.voice = speechSynthesis.getVoices()[33];
} else if (voice == "voice8") {
// console.log((speech.voice = speechSynthesis.getVoices()[37]));
speech.voice = speechSynthesis.getVoices()[37];
} else if (voice == "voice9") {
// console.log((speech.voice = speechSynthesis.getVoices()[41]));
speech.voice = speechSynthesis.getVoices()[41];
}
// for (let i = 0; i < 100; i++) {
// console.log((speech.voice = speechSynthesis.getVoices()[i]));
// }
}
function changeVoiceSpeed(voiceSpeed) {
// For some reason, speed below 0.5 doesn't work
if (voiceSpeed == "speed2") {
speech.rate = 2;
} else if (voiceSpeed == "speed1.75") {
speech.rate = 1.75;
} else if (voiceSpeed == "speed1.5") {
speech.rate = 1.5;
} else if (voiceSpeed == "speed1.25") {
speech.rate = 1.25;
} else if (voiceSpeed == "speed1") {
speech.rate = 1;
} else if (voiceSpeed == "speed0.75") {
speech.rate = 0.75;
} else if (voiceSpeed == "speed0.5") {
speech.rate = 0.5;
}
}