-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathindex.html
More file actions
174 lines (148 loc) · 5.54 KB
/
Copy pathindex.html
File metadata and controls
174 lines (148 loc) · 5.54 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Text to speech demo</title>
<style>
html {
font-family: Arial, Helvetica, sans-serif;
margin: 0;
}
body {
width: 600px;
max-width: 90%;
margin: 20px auto;
}
label {
display: block;
}
textarea {
width: 100%;
height: 50vh;
padding: 10px;
margin: 4px 0 20px 0;
}
</style>
</head>
<body>
<h1>Text to speech</h1>
<noscript id="nosupport">
<p>Your browser doesn't support Text to Speech</p>
</noscript>
<label for="text">Text</label>
<textarea name="text" id="text" data-tts-text>This is a text</textarea>
<div data-tts>
<select name="voices" id="voices" hidden data-tts-select></select>
<button hidden data-tts-play>Play</button>
<button hidden data-tts-pause>Pause</button>
<button hidden data-tts-resume>Resume</button>
</div>
<script>
if ('speechSynthesis' in window) {
const synthesis = window.speechSynthesis;
const voiceSelect = document.querySelector('[data-tts-select]');
const playButton = document.querySelector('[data-tts-play]');
const pauseButton = document.querySelector('[data-tts-pause]');
const resumeButton = document.querySelector('[data-tts-resume]');
const textarea = document.querySelector('[data-tts-text]');
let text = false;
let chosenVoice = false;
let isPlaying = false;
let voices;
const getText = () => {
return textarea.value.replace(/\n\n/g, ', ');
};
const play = () => {
if (!text) {
text = getText()
}
const utterance = new SpeechSynthesisUtterance(text);
if (chosenVoice) {
utterance.voice = chosenVoice;
}
// Speak the utterance
synthesis.speak(utterance);
isPlaying = true;
pauseButton.hidden = false;
playButton.hidden = true;
resumeButton.hidden = true;
// somehow chrome stops after 14seconds, so resume from there
var r = setInterval(() => {
if (!isPlaying) {
clearInterval(r);
} else {
synthesis.resume();
}
}, 14000);
window.addEventListener('beforeunload', () => {
clearInterval(r);
synthesis.cancel();
});
utterance.onend = () => {
isPlaying = false;
playButton.hidden = false;
pauseButton.hidden = true;
resumeButton.hidden = true;
text = false;
}
};
const pause = () => {
synthesis.pause();
isPlaying = false;
pauseButton.hidden = true;
resumeButton.hidden = false;
playButton.hidden = true;
};
const resume = () => {
synthesis.resume();
isPlaying = true;
pauseButton.hidden = false;
resumeButton.hidden = true;
playButton.hidden = true;
}
const setVoices = () => {
voices = synthesis.getVoices();
// Log the properties of the voices in the list
if (voices && voices.length && voices.length > 0) {
voices.forEach(function(voice) {
const voiceOption = document.createElement('option');
voiceOption.innerText = voice.name;
voiceSelect.appendChild(voiceOption);
});
voiceSelect.hidden = false;
voiceSelect.addEventListener('change', (ev) => {
chosenVoice = voices.filter((voice) => {
return voice.name === ev.target.value;
})[0];
});
playButton.hidden = false;
playButton.addEventListener('click', () => {
play();
});
pauseButton.addEventListener('click', () => {
pause();
});
resumeButton.addEventListener('click', () => {
resume();
});
textarea.addEventListener('keyup', (ev) => {
text = ev.target.value.replace(/\n\n/g, ', ');
isPlaying = false;
synthesis.cancel();
playButton.hidden = false;
pauseButton.hidden = true;
resumeButton.hidden = true;
});
}
};
setTimeout(function() {
setVoices();
}, 50)
} else {
document.documentElement.innerHTML = document.getElementById('nosupport').innerHTML;
}
</script>
</body>
</html>