|
| 1 | +/** |
| 2 | + * JavaScript30 by Wes Bos, https://javascript30.com/ |
| 3 | + * TypeScript implementation by Will Wager |
| 4 | + * Project: Speech Synthesis |
| 5 | + * Concepts: Web Speech API |
| 6 | + * Key takeaways: Specifying this in a handler function signature; |
| 7 | + * Set options with element name pattern (not very TS friendly). |
| 8 | + * Sidenotes: https://developer.mozilla.org/en-US/docs/Web/API/Web_Speech_API |
| 9 | + * Chrome required. |
| 10 | + * Targeted es6 to get Array.find; Allowed implicit any for msg[input.name]; |
| 11 | + * Compilation command: |
| 12 | + * tsc --removeComments --strictNullChecks --target es6 typescripts.ts |
| 13 | + */ |
| 14 | + |
| 15 | +const msg = new SpeechSynthesisUtterance(); |
| 16 | +let voices: SpeechSynthesisVoice[] = []; |
| 17 | +const voicesDropdown = document.querySelector('[name="voice"]')! as HTMLSelectElement; |
| 18 | +const options = document.querySelectorAll('[type="range"], [name="text"]'); |
| 19 | +const speakButton = document.querySelector('#speak')! as HTMLButtonElement; |
| 20 | +const stopButton = document.querySelector('#stop')! as HTMLButtonElement; |
| 21 | +msg.text = (document.querySelector('[name="text"]')! as HTMLInputElement).value; |
| 22 | + |
| 23 | +function populateVoices(this: SpeechSynthesis) { |
| 24 | + voices = this.getVoices(); |
| 25 | + voicesDropdown.innerHTML = (voices |
| 26 | + .map(voice => `<option value="${voice.name}">${voice.name} (${voice.lang})</option>`) |
| 27 | + .join('')); |
| 28 | +} |
| 29 | + |
| 30 | +function setVoice() { |
| 31 | + const newVoice = voices.find(voice => voice.name === this.value); |
| 32 | + if (newVoice) { |
| 33 | + msg.voice = newVoice; |
| 34 | + toggle(); |
| 35 | + return true; |
| 36 | + } else { |
| 37 | + console.error('Selected voice not found'); |
| 38 | + return false; |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +function toggle(e?: Event, startOver = true) { |
| 43 | + speechSynthesis.cancel(); |
| 44 | + if (startOver) { |
| 45 | + speechSynthesis.speak(msg); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +function setOption() { |
| 50 | + msg[this.name] = Number(this.value); |
| 51 | + toggle(); |
| 52 | +} |
| 53 | + |
| 54 | +speechSynthesis.addEventListener('voiceschanged', populateVoices); |
| 55 | +voicesDropdown.addEventListener('change', setVoice); |
| 56 | +options.forEach(option => option.addEventListener('change', setOption)); |
| 57 | +speakButton.addEventListener('click', toggle); |
| 58 | +stopButton.addEventListener('click', e => toggle(e, false)); |
0 commit comments