Skip to content

Commit f2531a0

Browse files
committed
done 23
1 parent 18605db commit f2531a0

File tree

1 file changed

+56
-22
lines changed

1 file changed

+56
-22
lines changed

23 - Speech Synthesis/index.html

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,74 @@
11
<!DOCTYPE html>
22
<html lang="en">
33
<head>
4-
<meta charset="UTF-8">
5-
<title>Speech Synthesis</title>
6-
<link href='https://fonts.googleapis.com/css?family=Pacifico' rel='stylesheet' type='text/css'>
7-
<link rel="stylesheet" href="style.css">
4+
<meta charset="UTF-8">
5+
<title>Speech Synthesis</title>
6+
<link href='https://fonts.googleapis.com/css?family=Pacifico' rel='stylesheet' type='text/css'>
7+
<link rel="stylesheet" href="style.css">
88
</head>
99
<body>
1010

11-
<div class="voiceinator">
11+
<div class="voiceinator">
1212

13-
<h1>The Voiceinator 5000</h1>
13+
<h1>The Voiceinator 5000</h1>
1414

15-
<select name="voice" id="voices">
15+
<select name="voice" id="voices">
1616
<option value="">Select A Voice</option>
17-
</select>
17+
</select>
1818

19-
<label for="rate">Rate:</label>
20-
<input name="rate" type="range" min="0" max="3" value="1" step="0.1">
19+
<label for="rate">Rate:</label>
20+
<input name="rate" type="range" min="0" max="3" value="1" step="0.1">
2121

22-
<label for="pitch">Pitch:</label>
22+
<label for="pitch">Pitch:</label>
2323

24-
<input name="pitch" type="range" min="0" max="2" step="0.1">
25-
<textarea name="text">Hello! I love JavaScript 👍</textarea>
26-
<button id="stop">Stop!</button>
27-
<button id="speak">Speak</button>
24+
<input name="pitch" type="range" min="0" max="2" step="0.1">
25+
<textarea name="text">Hello! I love JavaScript 👍</textarea>
26+
<button id="stop">Stop!</button>
27+
<button id="speak">Speak</button>
2828

29-
</div>
29+
</div>
3030

3131
<script>
32-
const msg = new SpeechSynthesisUtterance();
33-
let voices = [];
34-
const voicesDropdown = document.querySelector('[name="voice"]');
35-
const options = document.querySelectorAll('[type="range"], [name="text"]');
36-
const speakButton = document.querySelector('#speak');
37-
const stopButton = document.querySelector('#stop');
32+
const msg = new SpeechSynthesisUtterance();
33+
let voices = [];
34+
const voicesDropdown = document.querySelector('[name="voice"]');
35+
const options = document.querySelectorAll('[type="range"], [name="text"]');
36+
const speakButton = document.querySelector('#speak');
37+
const stopButton = document.querySelector('#stop');
38+
msg.text = document.querySelector('[name="text"]').value;
39+
40+
function populateVoices() {
41+
voices = this.getVoices();
42+
voicesDropdown.innerHTML = voices
43+
// .filter(voice => voice.lang.includes('en'))
44+
.map(voice => `<option value="${voice.name}">${voice.name} (${voice.lang})</option>`)
45+
.join('');
46+
}
47+
48+
function setVoice() {
49+
msg.voice = voices.find(voice => voice.name === this.value);
50+
toggle();
51+
}
52+
53+
function toggle(startOver = true) {
54+
speechSynthesis.cancel();
55+
if (startOver) {
56+
speechSynthesis.speak(msg);
57+
}
58+
}
59+
60+
function setOption() {
61+
console.log(this.name, this.value);
62+
msg[this.name] = this.value;
63+
toggle();
64+
}
65+
66+
speechSynthesis.addEventListener('voiceschanged', populateVoices);
67+
voicesDropdown.addEventListener('change', setVoice);
68+
options.forEach(option => option.addEventListener('change', setOption));
69+
speakButton.addEventListener('click', toggle);
70+
stopButton.addEventListener('click', () => toggle(false));
71+
3872
</script>
3973

4074
</body>

0 commit comments

Comments
 (0)