Last active
July 21, 2020 14:28
-
-
Save VasVV/1b87ca4a287e6ca9c408418fd8bf6b17 to your computer and use it in GitHub Desktop.
Voice synthesis - JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let voices = []; | |
const voicesDropdown = document.querySelector('[name="voice"]'); | |
const options = document.querySelectorAll('[type="range"], [name="text"]'); | |
const speakButton = document.querySelector('#speak'); | |
const stopButton = document.querySelector('#stop'); | |
let msg = new SpeechSynthesisUtterance(); | |
msg.text = document.querySelector('[name="text"]').value; | |
console.log(msg); | |
function vc() { | |
voices = this.getVoices(); | |
// console.log(voices); | |
const optionsList = voices.map(e => { | |
return `<option value="${e.name}">${e.name}(${e.lang})</option>` | |
}).join(''); | |
// console.log(optionsList); | |
voicesDropdown.innerHTML = optionsList; | |
} | |
function changeVoice() { | |
msg.voice= voices.find(e => e.name == this.value) | |
} | |
function toggle(startOver = true) { | |
speechSynthesis.cancel(); | |
if (startOver) { | |
speechSynthesis.speak(msg) | |
} | |
} | |
function optionFunction() { | |
console.log(this.name, this.value) | |
msg[this.name] = this.value; | |
toggle(); | |
} | |
options.forEach(e => e.addEventListener('change',optionFunction)); | |
voicesDropdown.addEventListener('change', changeVoice); | |
speechSynthesis.addEventListener('voiceschanged', vc) | |
speakButton.addEventListener('click', toggle); | |
stopButton.addEventListener('click', () => toggle(false)); //как сделать так, чтобы не запускалось сразу? | |
//speechSynthesis.speak(msg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
html { | |
font-size: 10px; | |
box-sizing: border-box; | |
} | |
*, *:before, *:after { | |
box-sizing: inherit; | |
} | |
body { | |
margin: 0; | |
padding: 0; | |
font-family: sans-serif; | |
background-color: #3BC1AC; | |
display: flex; | |
min-height: 100vh; | |
align-items: center; | |
} | |
.voiceinator { | |
padding: 2rem; | |
width: 50rem; | |
margin: 0 auto; | |
border-radius: 1rem; | |
position: relative; | |
background: white; | |
overflow: hidden; | |
z-index: 1; | |
box-shadow: 0 0 5px 5px rgba(0,0,0,0.1); | |
} | |
h1 { | |
width: calc(100% + 4rem); | |
margin: -2rem 0 2rem -2rem; | |
padding: .5rem; | |
background: #ffc600; | |
border-bottom: 5px solid #F3C010; | |
text-align: center; | |
font-size: 5rem; | |
font-weight: 100; | |
font-family: 'Pacifico', cursive; | |
text-shadow: 3px 3px 0 #F3C010; | |
} | |
.voiceinator input, | |
.voiceinator button, | |
.voiceinator select, | |
.voiceinator textarea { | |
width: 100%; | |
display: block; | |
margin: 10px 0; | |
padding: 10px; | |
border: 0; | |
font-size: 2rem; | |
background: #F7F7F7; | |
outline: 0; | |
} | |
textarea { | |
height: 20rem; | |
} | |
input[type="select"] { | |
} | |
.voiceinator button { | |
background: #ffc600; | |
border: 0; | |
width: 49%; | |
float: left; | |
font-family: 'Pacifico', cursive; | |
margin-bottom: 0; | |
font-size: 2rem; | |
border-bottom: 5px solid #F3C010; | |
cursor: pointer; | |
position: relative; | |
} | |
.voiceinator button:active { | |
top: 2px; | |
} | |
.voiceinator button:nth-of-type(1) { | |
margin-right: 2%; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="voiceinator"> | |
<h1>Voice synthesis</h1> | |
<select name="voice" id="voices"> | |
<option value="">Select A Voice</option> | |
</select> | |
<label for="rate">Rate:</label> | |
<input name="rate" type="range" min="0" max="3" value="1" step="0.1"> | |
<label for="pitch">Pitch:</label> | |
<input name="pitch" type="range" min="0" max="2" step="0.1"> | |
<textarea name="text">Hola! Me llamo Vasilii</textarea> | |
<button id="stop">Stop!</button> | |
<button id="speak">Speak</button> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment