-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
170 lines (140 loc) · 4.6 KB
/
script.js
File metadata and controls
170 lines (140 loc) · 4.6 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
/*
With apologies to MDN's Chris Mills, whose speak easy synthesis demo I have hacked
Todo/idea list:
- Set cookie for voice pref
- Handle latency
–– 2nd sound doesn't fire if clicked before 1st is finished
–– Google (remote) voices seem less responsive
- Make it easier to test
–– Add test links next to inputs
–– Trigger speak() when rate and pitch change
One day
- Analytics to log morphemes?
- Sharable URLs with presets (yeah)
- Toggle back to intro view when all inputs are empty
*/
//---------------------------------
// Vars
var synth = window.speechSynthesis;
var form = document.querySelector('form');
var intro = document.querySelector('#intro');
var display = document.querySelector('#display');
var settings = document.querySelector('#settings');
var voiceSelect = document.querySelector('select');
var pitch = document.querySelector('#pitch');
var pitchValue = document.querySelector('.pitch-value');
var rate = document.querySelector('#rate');
var rateValue = document.querySelector('.rate-value');
// Need to create arrays from NodeLists (to call indexOf method later)
var buttons = Array.from(document.querySelectorAll('.play'));
var sounds = Array.from(document.querySelectorAll('.sound'));
var preferedVoice = 'Google UK English Female';
var voices = [];
//---------------------------------
// Enable/disable buttons
// Event handlers for text inputs (could instead use forEach?)
for(var i=0; i<=sounds.length; i++) {
if (typeof sounds[i] != 'undefined') {
sounds[i].addEventListener('input', toggleButtons);
}
}
// When text is added to an input:
// enable the corresponding button and add a click handler
// and when all text is deleted:
// disable the corresponding button
function toggleButtons(){
// find button with corresponding index in button array
var targetButton = buttons[sounds.indexOf(this)];
var targetSound = this;
if (this.value !== '') {
targetButton.removeAttribute('disabled');
targetButton.onclick = function(event) {
event.preventDefault();
speak(targetSound);
}
appReady();
} else {
targetButton.setAttribute('disabled','true');
}
}
// set class to show enabled buttons and hide disabled ones
function appReady() {
form.classList.add('ready');
}
//---------------------------------
// Getting started
function scrollTo(target){
target.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
}
document.querySelector('.display-link').onclick = function() {
event.preventDefault();
scrollTo(display);
}
document.querySelector('.settings-link').onclick = function() {
event.preventDefault();
scrollTo(settings);
}
//---------------------------------
// Audio settings
pitch.onchange = function() {
pitchValue.textContent = pitch.value;
}
rate.onchange = function() {
rateValue.textContent = rate.value;
}
//---------------------------------
// Voice list
// https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesisVoice
function populateVoiceList() {
voices = synth.getVoices().sort(function (a, b) {
const aname = a.name.toUpperCase(), bname = b.name.toUpperCase();
if ( aname < bname ) return -1;
else if ( aname == bname ) return 0;
else return +1;
});
voiceSelect.innerHTML = '';
for(i = 0; i < voices.length ; i++) {
var option = document.createElement('option');
option.textContent = voices[i].name + ' (' + voices[i].lang + ')';
if(voices[i].default) {
option.textContent += ' -- DEFAULT';
}
if(voices[i].name == preferedVoice) {
var preferedVoiceIndex = i;
}
option.setAttribute('data-lang', voices[i].lang);
option.setAttribute('data-name', voices[i].name);
voiceSelect.appendChild(option);
}
var selectedIndex = voiceSelect.selectedIndex < preferedVoiceIndex ? preferedVoiceIndex : voiceSelect.selectedIndex;
voiceSelect.selectedIndex = selectedIndex;
}
populateVoiceList();
if (speechSynthesis.onvoiceschanged !== undefined) {
speechSynthesis.onvoiceschanged = populateVoiceList;
}
//---------------------------------
// Speak
function speak(sound){
// Use this to prevent dead clicks on other buttons
if (synth.speaking) {
console.error('speechSynthesis.speaking');
return;
}
if (sound.value !== '') {
var utterThis = new SpeechSynthesisUtterance(sound.value);
var selectedOption = voiceSelect.selectedOptions[0].getAttribute('data-name');
for(i = 0; i < voices.length ; i++) {
if(voices[i].name === selectedOption) {
utterThis.voice = voices[i];
break;
}
}
utterThis.pitch = pitch.value;
utterThis.rate = rate.value;
synth.speak(utterThis);
}
}