|
1 | 1 | <!DOCTYPE html> |
2 | 2 | <html lang="en"> |
3 | 3 | <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"> |
8 | 8 | </head> |
9 | 9 | <body> |
10 | 10 |
|
11 | | - <div class="voiceinator"> |
| 11 | +<div class="voiceinator"> |
12 | 12 |
|
13 | | - <h1>The Voiceinator 5000</h1> |
| 13 | + <h1>The Voiceinator 5000</h1> |
14 | 14 |
|
15 | | - <select name="voice" id="voices"> |
| 15 | + <select name="voice" id="voices"> |
16 | 16 | <option value="">Select A Voice</option> |
17 | | - </select> |
| 17 | + </select> |
18 | 18 |
|
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"> |
21 | 21 |
|
22 | | - <label for="pitch">Pitch:</label> |
| 22 | + <label for="pitch">Pitch:</label> |
23 | 23 |
|
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> |
28 | 28 |
|
29 | | - </div> |
| 29 | +</div> |
30 | 30 |
|
31 | 31 | <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 | + |
38 | 72 | </script> |
39 | 73 |
|
40 | 74 | </body> |
|
0 commit comments