-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
681 lines (559 loc) · 19.1 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="">
<meta name="HandheldFriendly" content="True">
<meta name="MobileOptimized" content="320">
<meta name="robots" content="index,follow">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SpeechSynthesisUtterance</title>
<script src="./scripts/speechsynthesisutterance.js"></script>
<style>
html {
box-sizing: border-box;
}
body {
font-size: 16px;
background-color: #eeeeee;
margin: 0;
min-width: 350px;
}
*,
::before,
::after {
-webkit-box-sizing: inherit;
-moz-box-sizing: inherit;
box-sizing: inherit;
}
.container {
width: 100%;
margin-left: auto;
margin-right: auto;
padding-left: 10px;
padding-right: 10px;
}
.border-box {
box-sizing: border-box;
}
.o-flex {
display: flex;
}
.o-flex.center {
flex-direction: row;
justify-content: center;
align-items: center;
}
.c-btn {
display: inline-block;
vertical-align: middle;
font: inherit;
text-align: center;
margin: 0;
cursor: pointer;
padding: 12px 24px;
transition: background-color 120ms ease-in-out;
border-radius: 3px;
}
.mac {
margin-left: auto;
margin-right: auto;
}
.green {
background-color: #33dd44;
text-align: center;
}
.red {
background-color: #ee4455;
}
.top {
margin-top: 30px;
}
.show {
display: inline-block !important;
}
.hide {
display: none !important;
}
.isvisible {
visibility: visible !important;
}
.isinvisible {
visibility: hidden !important;
}
.disabled {
max-width: 350px;
visibility: hidden;
color: #ff1111;
}
h1 {
font-size: 1.4rem;
text-align: center;
background-color: #ffa000;
margin: 0;
padding: 15px;
font-family: arial, helvetica, sans-serif;
}
.btn {
width: 45px;
height: 45px;
padding: unset;
margin: 0 10px 10px 0;
border: 2px solid #886600;
color: #ffffff;
font-weight: 600;
/* font-size: 20px; */
border-radius: 50%;
}
button svg {
pointer-events: none;
padding: 5px;
}
button svg path {
fill: #ffffff;
}
.controls {
width: 350px;
margin-left: auto;
margin-right: auto;
background-color: #dddddd;
padding: 10px;
border: 4px solid #ffa000;
box-sizing: border-box;
}
.controls label {
display: inline-block;
/* width: 3.25rem; */
width: 55px;
font-size: 1rem;
margin: 10px auto;
position: relative;
top: -70%;
}
.gap {
display: inline-block;
width: 50px;
}
.controls__voices {
/* width: calc(90% - 55px); */
}
#voices {
width: calc(90% - 55px);
}
#voices option {
/* width: calc(90% - 55px); */
width: 100%;
}
input[type="range"] {
/* width: 100%; */
width: calc(90% - 55px);
height: 1rem;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
background: transparent;
font-size: 1rem;
border-width: 0px;
outline-offset: -1px;
line-height: normal;
padding: 5px;
}
input[type=range]::-moz-range-thumb {
background: radial-gradient(#ffa000 35%, white 45%) content-box;
}
input[type=range]::-moz-range-track {
border: solid 1px #ffa000;
border-radius: 25px;
height: 0.25em;
}
input[type=range]::-moz-range-track {
background-color: #ffa000;
}
input[type=range]::-moz-focus-outer {
border: 0;
}
/* Webkit */
input[type=range]::-webkit-slider-runnable-track {
position: relative;
box-sizing: border-box;
padding: 0.25em;
width: 32em;
height: 0.75em;
border-radius: 25px;
border: 1px solid #ffa000;
margin: 0;
padding: 0;
}
input[type=range]::-webkit-slider-runnable-track {
background: radial-gradient(#ffa000 35%, #ff7777 71%) content-box, linear-gradient(#ffffff, #ffffff) border-box;
}
input[type=range]::-webkit-slider-thumb {
margin-top: -0.5em;
border: none;
width: 1.5em;
height: 1.5em;
border-radius: 50%;
border: 1px solid #c8c8c8;
box-shadow: 0 1px 0.125em #585858, inset 0 1px 1px #f8f8f8;
background-size: 100%;
cursor: ew-resize;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
outline: none;
}
input[type=range]::-webkit-slider-thumb:active {
cursor: grabbing;
}
input[type=range]::-webkit-slider-thumb {
background: radial-gradient(#ffa000 35%, white 45%) content-box;
}
/* IE */
input[type=range]::-ms-thumb {
font-size: 0.65rem;
margin-top: 0em;
border: none;
padding: 0.25em;
width: 2em;
height: 2em;
border-radius: 50%;
box-shadow: 0 1px 0.125em #585858, inset 0 1px 1px #f8f8f8;
background: radial-gradient(#ffa000 35%, #cb8037 71%) content-box, linear-gradient(#d4d4d4, #bfbfbf) border-box;
cursor: ew-resize;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
}
.play {
display: flex;
justify-content: center;
margin-top: 30px;
margin-bottom: 5px;
}
textarea {
display: block;
margin-left: auto;
margin-right: auto;
background-color: #fff;
padding: 15px;
font-family: "Droid Sans Mono", monospace;
font-size: 1rem;
overflow: auto;
}
textarea::-moz-selection {
background: #ffa000;
}
textarea::selection {
background: #ffa000;
}
@media screen and (max-width: 440px) {
/* .controls,
.controls__voices {
width: 90%;
width: calc(90% - 55px);
} */
.flex--mobile {
display: flex;
flex-direction: column;
}
input,
label,
select {
flex: 1;
}
.controls label {
margin-bottom: 0px;
}
#voices,
.controls select,
.controls label,
.controls input[type="range"] {
width: 100%;
padding: 0px;
}
textarea {
width: 90% !important;
height: calc(100vh - 370px) !important;
}
}
</style>
</head>
<body>
<h1>Text to Speech</h1>
<div class="container top">
<div class="controls">
<div class="controls__voices flex--mobile">
<label for="voices">Voice</label>
<select id="voices">
</select>
</div>
<div class="flex--mobile">
<label for="speed">Speed</label>
<input id="speed" type="range" min="0.2" max="8" step="0.2" value="1">
</div>
<div class="flex--mobile">
<label for="pitch">Pitch</label>
<input id="pitch" type="range" min="0" max="2" step="0.1" value="1">
</div>
</div>
<div class="play mac">
<!-- <div class="play__btns"> -->
<button class="c-btn btn green">
<div class="o-flex center">
<svg class="show" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" aria-hidden="true"
fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"
stroke-linejoin="round">
<path d="M20 20 80 50 20 80z" fill="#ffffff" />
</svg>
<svg class="hide" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" aria-hidden="true"
fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"
stroke-linejoin="round">
<path d="M25 20 40 20 40 80 25 80z" fill="#ffffff" />
<path d="M60 20 75 20 75 80 60 80z" fill="#ffffff" />
</svg>
</div>
</button>
<button class="c-btn btn red stop">
<div class="o-flex center">
<svg class="" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" aria-hidden="true"
fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"
stroke-linejoin="round">
<path d="M30,30 70,30 70,70 30,70 z" fill="#ffffff" />
</svg>
</div>
</button>
<div class="gap"></div>
<button class="c-btn btn green prev">
<div class="o-flex center">
<svg class="" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" aria-hidden="true"
fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"
stroke-linejoin="round">
<path d="M15,20 25,20 25,80 15,80 15,20 15,80z" fill="#ffffff" />
<path d="M25,50 85,20 85,80 25,50z" fill="#ffffff" />
</svg>
</div>
</button>
<button class="c-btn btn green next">
<div class="o-flex center">
<svg class="" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" aria-hidden="true"
fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"
stroke-linejoin="round">
<path d="M75,50 15,20 15,80 75,50z" fill="#ffffff" />
<path d="M85,20 75,20 75,80 85,80 85,20 85,80z" fill="#ffffff" />
</svg>
</div>
</button>
<!-- </div> -->
</div>
<textarea
placeholder="Type or copy and Paste something in here and press Play. A voice will read it out aloud for you. Enjoy."
style="font-family: sans-serif;outline: none;border-color: #ffa000;border-radius: 10px;resize: none;width:80vw;height: calc(100vh - 350px); min-width: 350px;"></textarea>
</div>
</body>
<script>
const synth = window.speechSynthesis
const elVoices = document.querySelector("#voices")
const elSpeed = document.querySelector("#speed")
const elPitch = document.querySelector("#pitch")
const elBtn = document.querySelector(".c-btn")
const elBtnSVG = document.querySelectorAll(".play svg")
const elBtnStop = document.querySelector(".btn.stop")
const elBtnPrev = document.querySelector(".btn.prev")
const elBtnNext = document.querySelector(".btn.next")
const elText = document.querySelector("textarea")
//Global variables
let txtPos = 0;
let shouldSpeak = false
let wholeText = ''
let playing = false
let arrStart = []
if ('speechSynthesis' in window) {
// SpeechSynth.init has a this object passed to it which makes
// SpeechSynth.init run voiceList after the voice list is generated
SpeechSynth.init({
onVoiceList: function () {
voiceList()
}
});
} else {
alert('It seems that TTS is not supported by your browser. Try it on Chrome.');
}
elSpeed.addEventListener('change', function () {
SpeechSynth.setRate(this.value)
elText.focus()
}, false);
elPitch.addEventListener('change', function () {
SpeechSynth.setPitch(this.value)
elText.focus()
}, false);
elVoices.addEventListener('change', function () {
SpeechSynth.voice = SpeechSynth.setVoice(elVoices.value)
elText.focus()
}, false);
// Text is split into sentences. A new SpeechSynthesisUtterance is created
// for each sentence so that the user can change the speed, voice and pitch while
// text is being spoken.
// arrStart is the text start position of each of these Utterances so that it can
// highlight the text.
function textArrays() {
let arrText = elText.value.split(/[.|\n]/)
let acc = 0
wholeText = elText.value.split(/[.|\n]/).filter(cv => (cv !== "") && (cv !== null) && (cv !== undefined))
acc = 0
for (let i = 0; i < arrText.length; i++) {
if ((arrText[i] === "") || (arrText[i] === null) || (arrText[i] === undefined)) {
acc++
} else {
arrStart.push(acc)
acc += (arrText[i].length + 1)
}
}
}
function handleTalk() {
if (shouldSpeak === false) {
textArrays()
shouldSpeak = true
elText.focus()
startIteration()
}
if (playing) {
// Pause
playingBtnPause()
synth.pause()
} else {
// Play
playingBtnPlay()
if (window.speechSynthesis.paused) {
window.speechSynthesis.resume();
}
}
playing = !playing
elText.focus()
}
function playingBtnPause() {
elBtnSVG[0].classList.add("show")
elBtnSVG[0].classList.remove("hide")
elBtnSVG[1].classList.add("hide")
elBtnSVG[1].classList.remove("show")
}
function playingBtnPlay() {
elBtnSVG[0].classList.add("hide")
elBtnSVG[0].classList.remove("show")
elBtnSVG[1].classList.add("show")
elBtnSVG[1].classList.remove("hide")
}
function playingStop() {
txtPos = 0;
shouldSpeak = false
playingBtnPause()
playing = false
SpeechSynth.stop()
elText.focus()
}
function playPrevious() {
SpeechSynth.stop()
playing = false;
if (txtPos > 0) txtPos--
if (txtPos > 0) txtPos--
window.speechSynthesis.resume();
}
function playNext() {
SpeechSynth.stop()
playing = false;
if (txtPos < wholeText.len) txtPos++;
window.speechSynthesis.resume();
}
elBtnStop.addEventListener('click', function () {
playingStop()
elText.setSelectionRange(0, 0)
elText.focus()
}, false);
elBtnPrev.addEventListener('click', function () {
playPrevious()
elText.focus()
}, false);
elBtnNext.addEventListener('click', function () {
playNext()
elText.focus()
}, false);
if (!synth) {
console.log("Your browser doesn't support speech Synthesis")
elBtn.toggleAttribute("disabled")
elBtn.style.opacity = "0.6"
} else {
elBtn.addEventListener("click", handleTalk);
}
//The following function triggered when play/pause button is clicked
function startIteration() {
if (shouldSpeak) {
if (wholeText.length > txtPos) {
if (wholeText[txtPos].length > 0) {
// This tells the SpeechSynth library to run
// startIteration() when the 'end' event is triggered
// on the Utterance. startIteration tells it to read the next
// sentence until all sentences have been read.
SpeechSynth.utteranceSettings({
text: wholeText[txtPos],
onEnd: function () {
startIteration()
}
});
elText.focus();
elText.setSelectionRange(arrStart[txtPos], arrStart[txtPos] + wholeText[
txtPos].length)
txtPos++
} else {
alert(
'Please write something for me to read :)');
}
} else {
shouldSpeak = false;
playing = false;
SpeechSynth.stop();
txtPos = 0;
playingBtnPause()
elText.setSelectionRange(0, 0)
}
}
}
function voiceList() {
// voice list is put to the SpeechSynth.voices array
let voices = SpeechSynth.voices
voices.forEach(function (cv) {
let opt = document.createElement("option")
opt.textContent = cv.name
opt.dataset.lang = cv.lang
elVoices.appendChild(opt)
})
SpeechSynth.voice = voices[0]
}
let slogans = `You can type in anything you want in here. As a placeholder, I've put in some funny slogans.
If I agreed with you, we'd both be wrong.
Abandon the search for Truth; settle for a good fantasy.
When nothing goes right……..go left.
Its much easier to apologize then it is to get permission.
Behind every Great man is a woman rolling her eyes.
When all else fails, lower your standards.
We never really grow up, we only learn how to act in public.
An onion a day keeps everyone away.
A bartender is just a pharmacist with a limited inventory.
Nobody is ugly after 2 a m.
Smoking helps you relax in the graveyard.
I have PTSD and a handgun.Any Questions?
Age is a very high price to pay for maturity.
You don't know what you have until it's gone.For example, toilet paper.
A bank is a place that will lend you money, if you can prove that you don't need it.
Whenever I find the key to success, someone changes the lock.
Campers: Nature's way of feeding mosquitoes.
When there's a will, I want to be in it!
Dear math grow up and solve your Problems.
I hate math but I love counting money.`
function initializeDOM() {
elText.value = slogans
elText.focus()
elText.setSelectionRange(0, 0)
}
initializeDOM()
</script>
</html>