This repository was archived by the owner on Nov 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 269
/
Copy pathindex.js
executable file
·145 lines (126 loc) · 4.31 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*eslint-env es6 */
(function () {
'use strict';
// Testing for browser support
const speechSynthesisSupported = 'speechSynthesis' in window;
let isPaused = false;
let isPlaying = false;
// Getting html elements
const supportMessageEle = document.getElementById('support-message');
const speakBtn = document.getElementById('speak-btn');
const pauseresumeBtn = document.getElementById('pauseresume-btn');
const cancelBtn = document.getElementById('cancel-btn');
const textToSpeechEle = document.getElementById('text-to-speech');
const voiceSelect = document.getElementById('voice');
const langSelect = document.getElementById('language');
const volumeRange = document.getElementById('volume');
const rateRange = document.getElementById('rate');
// var pitchRange = document.getElementById('pitch');
const speechStatus = document.getElementById('speech-status');
const log = function (message) {
console.log(`${message}<br/>`);
};
if (speechSynthesisSupported) {
supportMessageEle.innerHTML = 'Your browser <strong>supports</strong> the speech synthesis.';
} else {
supportMessageEle.innerHTML = 'Your browser <strong>does not support</strong> speech synthesis.';
supportMessageEle.classList.add('unSupported');
}
// Generic function to remove all options from a select
const clearSelect = function(selectObject) {
while (selectObject.length > 0) {
selectObject.remove(selectObject.length - 1);
}
};
// Clear combobox then add voice options
const displayVoices = function(voices) {
const lastSelectedVoiceName = voiceSelect.value;
clearSelect(voiceSelect);
voices.forEach((voice) => {
const option = document.createElement('option');
option.value = voice.name;
option.innerHTML = voice.name;
option.id = voice.name;
voiceSelect.appendChild(option);
});
if (lastSelectedVoiceName && voiceSelect.options.namedItem(lastSelectedVoiceName)) {
voiceSelect.value = lastSelectedVoiceName;
}
};
// Loading available voices for this browser/platform
// And displaying them into the combobox
const loadVoices = function () {
const voices = speechSynthesis.getVoices();
if (voices.length > 0) {
displayVoices(voices);
}
};
const speak = function (textToSpeech) {
const synUtterance = new SpeechSynthesisUtterance();
synUtterance.text = textToSpeech;
if (voiceSelect.value) {
synUtterance.voice = speechSynthesis
.getVoices()
.filter(function (voice) {
return voice.name === voiceSelect.value;
})[0];
}
synUtterance.lang = langSelect.value;
synUtterance.volume = parseFloat(volumeRange.value);
synUtterance.rate = parseFloat(rateRange.value);
// synUtterance.pitch = parseFloat(pitchRange.value);
const eventList = ['start', 'end', 'mark', 'pause', 'resume', 'error', 'boundary'];
eventList.forEach((event) => {
synUtterance.addEventListener(event, (speechSynthesisEvent) => {
log(`Fired '${speechSynthesisEvent.type}' event named '${speechSynthesisEvent.name}' at time '${speechSynthesisEvent.elapsedTime}' and character '${speechSynthesisEvent.charIndex} of length '${speechSynthesisEvent.charLength}.`);
});
});
window.speechSynthesis.speak(synUtterance);
};
if (speechSynthesisSupported) {
loadVoices();
// Chrome loads voices asynchronously.
window.speechSynthesis.onvoiceschanged = () => {
loadVoices();
};
}
if (speechSynthesisSupported) {
speakBtn.addEventListener('click', () => {
if (textToSpeechEle.value.length > 0) {
speak(textToSpeechEle.value);
}
});
pauseresumeBtn.addEventListener('click', () => {
if (!isPaused) {
window.speechSynthesis.pause();
isPaused = true;
pauseresumeBtn.textContent = 'Resume';
} else {
window.speechSynthesis.resume();
isPaused = false;
pauseresumeBtn.textContent = 'Pause';
}
});
cancelBtn.addEventListener('click', () => {
window.speechSynthesis.cancel();
});
setInterval(() => {
if (speechSynthesis.speaking) {
speechStatus.style.visibility = 'visible';
speechStatus.style.width = '32px';
if (!isPlaying) {
speechStatus.src = 'images/playingsound.gif';
isPlaying = true;
}
if (speechSynthesis.paused) {
speechStatus.src = 'images/pauseicon.png';
isPlaying = false;
}
} else {
isPlaying = false;
speechStatus.src = '';
speechStatus.style.visibility = 'hidden';
}
}, 100);
}
}());