-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
350 lines (304 loc) · 10.6 KB
/
index.html
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<!DOCTYPE html>
<html>
<head>
<title>Text-to-Speech App</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
}
.container {
max-width: 600px;
margin: 0 auto;
padding: 20px;
background-color: #fff;
border-radius: 8px;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
}
h1 {
margin-top: 0;
margin-bottom: 20px;
color: #007bff;
text-align: center;
}
textarea {
width: 100%;
height: 200px;
margin-bottom: 20px;
border: 1px solid #ccc;
border-radius: 4px;
padding: 8px;
resize: none;
font-size: 14px;
}
select, input[type="range"] {
width: 100%;
margin-bottom: 20px;
border: 1px solid #ccc;
border-radius: 4px;
padding: 8px;
font-size: 14px;
}
.controls {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 20px;
}
.button {
padding: 10px 20px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
transition: background-color 0.3s;
}
.button:hover {
background-color: #0056b3;
}
.icon {
margin-right: 5px;
}
.highlight {
background-color: yellow;
}
.save-load {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 20px;
}
.save-load input[type="text"] {
width: 70%;
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 14px;
}
.save-load .button {
width: 25%;
}
</style>
</head>
<body>
<div class="container">
<h1>Text-to-Speech App</h1>
<textarea id="text-to-speech" placeholder="Enter text..."></textarea>
<div class="controls">
<select id="language-select"></select>
<select id="voice-select"></select>
<select id="speech-select">
<option value="normal">Normal Text</option>
<option value="ssml">SSML</option>
</select>
</div>
<div class="controls">
<button id="speak-button" class="button"><i class="fas fa-play icon"></i>Speak</button>
<button id="pause-button" class="button"><i class="fas fa-pause icon"></i>Pause</button>
<button id="resume-button" class="button"><i class="fas fa-play icon"></i>Resume</button>
<button id="stop-button" class="button"><i class="fas fa-stop icon"></i>Stop</button>
</div>
<div class="controls">
<label for="rate-input">Rate:</label>
<input type="range" id="rate-input" min="0.5" max="2" step="0.1" value="1" />
<label for="volume-input">Volume:</label>
<input type="range" id="volume-input" min="0" max="1" step="0.1" value="1" />
<label for="pitch-input">Pitch:</label>
<input type="range" id="pitch-input" min="0" max="2" step="0.1" value="1" />
</div>
<div class="save-load">
<input type="text" id="save-input" placeholder="Enter name to save settings" />
<button id="save-button" class="button"><i class="fas fa-save icon"></i>Save</button>
<button id="load-button" class="button"><i class="fas fa-download icon"></i>Load</button>
</div>
</div>
<script src="https://kit.fontawesome.com/your-font-awesome-kit.js" crossorigin="anonymous"></script>
<script>
// Get the necessary elements from the HTML
const textarea = document.getElementById('text-to-speech');
const languageSelect = document.getElementById('language-select');
const voiceSelect = document.getElementById('voice-select');
const speechSelect = document.getElementById('speech-select');
const speakButton = document.getElementById('speak-button');
const pauseButton = document.getElementById('pause-button');
const resumeButton = document.getElementById('resume-button');
const stopButton = document.getElementById('stop-button');
const rateInput = document.getElementById('rate-input');
const volumeInput = document.getElementById('volume-input');
const pitchInput = document.getElementById('pitch-input');
const saveInput = document.getElementById('save-input');
const saveButton = document.getElementById('save-button');
const loadButton = document.getElementById('load-button');
const highlightClass = 'highlight';
let isSpeaking = false;
let speechQueue = [];
let currentSentenceIndex = 0;
function generateOptions() {
const voices = speechSynthesis.getVoices();
languageSelect.innerHTML = '';
voiceSelect.innerHTML = '';
voices.forEach(voice => {
const option = document.createElement('option');
option.value = voice.lang;
option.textContent = voice.name;
voiceSelect.appendChild(option);
if (!languageSelect.querySelector(`option[value="${voice.lang}"]`)) {
const languageOption = document.createElement('option');
languageOption.value = voice.lang;
languageOption.textContent = voice.lang;
languageSelect.appendChild(languageOption);
}
});
}
function setSpeechOption() {
const selectedVoice = voiceSelect.value;
speechSelect.disabled = selectedVoice.startsWith('Microsoft') || selectedVoice.startsWith('Google');
}
function queueSentences() {
const text = textarea.value.trim();
const sentences = text.split(/[.!?]+/);
let start = 0;
speechQueue = [];
sentences.forEach(sentence => {
const trimmedSentence = sentence.trim();
if (trimmedSentence !== '') {
const end = start + trimmedSentence.length;
speechQueue.push({ text: trimmedSentence, start, end });
start = end + 1; // Account for the punctuation mark
}
});
}
function toggleSpeakButton() {
if (isSpeaking) {
speakButton.innerHTML = '<i class="fas fa-pause icon"></i> Pause';
} else {
speakButton.innerHTML = '<i class="fas fa-play icon"></i> Speak';
}
}
function highlightCurrentSentence() {
const currentSentence = speechQueue[currentSentenceIndex];
const { start, end } = currentSentence;
const textBefore = textarea.value.substring(0, start);
const textInside = textarea.value.substring(start, end);
const textAfter = textarea.value.substring(end);
textarea.innerHTML = `
${textBefore}<span class="${highlightClass}">${textInside}</span>${textAfter}
`;
}
function clearHighlightedSentences() {
const highlightedSentences = textarea.querySelectorAll(`span.${highlightClass}`);
highlightedSentences.forEach(sentence => {
sentence.outerHTML = sentence.innerHTML;
});
}
function handleSpeakButtonClick() {
if (!isSpeaking) {
isSpeaking = true;
toggleSpeakButton();
const selectedVoice = voiceSelect.value;
const selectedSpeech = speechSelect.value;
const speechText = selectedSpeech === 'ssml' ? `<speak>${textarea.value}</speak>` : textarea.value;
const utterance = new SpeechSynthesisUtterance();
utterance.text = speechText;
utterance.voice = speechSynthesis.getVoices().find(voice => voice.lang === selectedVoice);
utterance.rate = rateInput.value;
utterance.volume = volumeInput.value;
utterance.pitch = pitchInput.value;
utterance.addEventListener('start', () => {
currentSentenceIndex = 0;
queueSentences();
highlightCurrentSentence();
});
utterance.addEventListener('boundary', event => {
const { charIndex } = event;
currentSentenceIndex = speechQueue.findIndex(sentence => sentence.end > charIndex);
clearHighlightedSentences();
highlightCurrentSentence();
});
utterance.addEventListener('end', () => {
isSpeaking = false;
toggleSpeakButton();
clearHighlightedSentences();
});
speechSynthesis.speak(utterance);
} else {
isSpeaking = false;
toggleSpeakButton();
speechSynthesis.pause();
}
}
function handleResumeButtonClick() {
isSpeaking = true;
toggleSpeakButton();
speechSynthesis.resume();
}
function handleStopButtonClick() {
isSpeaking = false;
toggleSpeakButton();
speechSynthesis.cancel();
clearHighlightedSentences();
}
function handleRateChange() {
const rate = rateInput.value;
speechSynthesis.rate = rate;
}
function handleVolumeChange() {
const volume = volumeInput.value;
speechSynthesis.volume = volume;
}
function handlePitchChange() {
const pitch = pitchInput.value;
speechSynthesis.pitch = pitch;
}
function handleTextareaInput() {
queueSentences();
clearHighlightedSentences();
}
function handleVoicesChanged() {
generateOptions();
setSpeechOption();
}
function handleSaveButtonClick() {
const settings = {
voice: voiceSelect.value,
speech: speechSelect.value,
rate: rateInput.value,
volume: volumeInput.value,
pitch: pitchInput.value,
text: textarea.value
};
const saveName = saveInput.value;
localStorage.setItem(saveName, JSON.stringify(settings));
alert('Settings saved successfully!');
}
function handleLoadButtonClick() {
const loadName = saveInput.value;
const settings = JSON.parse(localStorage.getItem(loadName));
if (settings) {
voiceSelect.value = settings.voice;
speechSelect.value = settings.speech;
rateInput.value = settings.rate;
volumeInput.value = settings.volume;
pitchInput.value = settings.pitch;
textarea.value = settings.text;
} else {
alert('No saved settings found with the given name.');
}
}
// Attach event listeners
speakButton.addEventListener('click', handleSpeakButtonClick);
resumeButton.addEventListener('click', handleResumeButtonClick);
stopButton.addEventListener('click', handleStopButtonClick);
rateInput.addEventListener('input', handleRateChange);
volumeInput.addEventListener('input', handleVolumeChange);
pitchInput.addEventListener('input', handlePitchChange);
textarea.addEventListener('input', handleTextareaInput);
saveButton.addEventListener('click', handleSaveButtonClick);
loadButton.addEventListener('click', handleLoadButtonClick);
// Wait for the voices to be loaded before generating options and setting speech option
speechSynthesis.addEventListener('voiceschanged', handleVoicesChanged);
</script>
</body>
</html>