Speech Synthesis
const msg = new SpeechSynthesisUtterance();// ๊ฐ์ฒด ์ ์ธ (๋ธ๋ผ์ฐ์ ์ ๋ด์ฅ ๋์ด์๋ ์ฃผ์ด์ง ํ
์คํธ๋ฅผ ์๋ฆฌ๋ก ๋ฐ๊ฟ์ฃผ๋ TTS API)
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');
msg.text = document.querySelector('[name="text"]').value;
function populateVoices() {
voices = this.getVoices();
voicesDropdown.innerHTML = voices
.filter(voice => voice.lang.includes('ko'))
.map(voice => `<option value="${voice.name}">${voice.name} (${voice.lang})</option>`)
.join(''); // ์ต์
์์ฑ
}
function setVoice() {
msg.voice - voices.find(voice => voice.name === this.valye);
toggle();
}
/*์๋ฆฌ๋ฅผ ์ค์ ์ด๋ฒคํธ๋ฅผ ๋ชฉ์๋ฆฌ๋ฅผ ์ค์ ํ๋ ํจ์
TTS ์ ๋ณด๋ฅผ ๊ด๋ฆฌํ๋ msg ๊ฐ์ฒด์ voice ๊ฐ์ ๋ฐ๊ฟ์ค ํ ์ง์ ์์ฑํ toggle() ํจ์๋ก ๋ชฉ์๋ฆฌ๋ฅผ ๋ค์ ์ฌ์ํ๋ค. */
function toggle(startOver = true) {
speechSynthesis.cancel();
if(startOver){
speechSynthesis.speak(msg);
}
}
/* ์์ฑ์ ์ฌ์ํ๋ toggle() ์์ฑ ์ธ์๋ก startOver๋ฅผ ๋๊ธฐ๊ณ , ๊ธฐ๋ณธ ์ค์ ์ธ์์ธ true์ธ ๊ฒฝ์ฐ
์ด์ ์์ฑ์ ์ทจ์ํ๊ณ ์๋ก์ด ์์ฑ์ ์ฌ์ํ๋ฉฐ, false๋ฅผ ์ธ์๋ก ์ฃผ๋ฉด ์์ฑ๋ง ์ทจ์ํ๋ค. */
function setOption(){
console.log(this.name, this.value);
msg[this.name] = this.value;
toggle();
}
/* ๋ค๋ฅธ ์ค์ ์ ๋ฐ๊ฟ ์ ์๋ setOption() ์์ฑํ๊ธฐ
input ๊ฐ์ฒด์ name์ ๋ณ๊ฒฝํ ์์ฑ์ ์ด๋ฆ๊ณผ ์ผ์น์ํค๋ฉด input ๊ฐ์ฒด์ value๋ก ๋ณ๋๋ก ์ต์
์ ๊ตฌ๋ณํ์ฌ ์์ฑํ ํ์์์ด ํ๋ฒ์ ์ฌ์ฉํ ์ ์๋ค.*/
// -> ํจ์๋ค์ ์ด๋ฒคํธ๋ก ๊ฑธ์ด์ค๋ค.
speechSynthesis.addEventListener('voiceschanged', populateVoices);
voicesDropdown.addEventListener('change', setVoice);
options.forEach(option => option.addEventListener('change', setOption)); // ์ต์
์ด ๋ณ๊ฒฝ๋ ๋ ๋ง๋ค ์คํ
speakButton.addEventListener('click', toggle);
stopButton.addEventListener('click', ()=> toggle(false));
<div class="voiceinator">
<h1>speak ๋ฒํผ์ ๋๋ฌ๋ณด์ธ์!</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">์๋
ํ์ธ์. ๋ฐ๊ฐ์์.๐</textarea>
<button id="stop">Stop!</button>
<button id="speak">Speak</button>
</div>
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;
background-image:
radial-gradient(circle at 100% 150%, #3BC1AC 24%, #42D2BB 25%, #42D2BB 28%, #3BC1AC 29%, #3BC1AC 36%, #42D2BB 36%, #42D2BB 40%, transparent 40%, transparent),
radial-gradient(circle at 0 150%, #3BC1AC 24%, #42D2BB 25%, #42D2BB 28%, #3BC1AC 29%, #3BC1AC 36%, #42D2BB 36%, #42D2BB 40%, transparent 40%, transparent),
radial-gradient(circle at 50% 100%, #42D2BB 10%, #3BC1AC 11%, #3BC1AC 23%, #42D2BB 24%, #42D2BB 30%, #3BC1AC 31%, #3BC1AC 43%, #42D2BB 44%, #42D2BB 50%, #3BC1AC 51%, #3BC1AC 63%, #42D2BB 64%, #42D2BB 71%, transparent 71%, transparent),
radial-gradient(circle at 100% 50%, #42D2BB 5%, #3BC1AC 6%, #3BC1AC 15%, #42D2BB 16%, #42D2BB 20%, #3BC1AC 21%, #3BC1AC 30%, #42D2BB 31%, #42D2BB 35%, #3BC1AC 36%, #3BC1AC 45%, #42D2BB 46%, #42D2BB 49%, transparent 50%, transparent),
radial-gradient(circle at 0 50%, #42D2BB 5%, #3BC1AC 6%, #3BC1AC 15%, #42D2BB 16%, #42D2BB 20%, #3BC1AC 21%, #3BC1AC 30%, #42D2BB 31%, #42D2BB 35%, #3BC1AC 36%, #3BC1AC 45%, #42D2BB 46%, #42D2BB 49%, transparent 50%, transparent);
background-size:100px 50px;
}
.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% + 4vw);
margin: -2vw 0 2vw -2vw;
padding: 20px;
background: #ffc600;
border-bottom: 5px solid #F3C010;
text-align: center;
font-size: 2.5vw;
font-weight: 500;
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;
}
.voiceinator button {
background: #ffc600;
border: 0;
width: 49%;
float: left;
margin-bottom: 0;
font-size: 1.6vw;
border-bottom: 5px solid #F3C010;
cursor: pointer;
position: relative;
}
.voiceinator button:active {
top: 2px;
}
.voiceinator button:nth-of-type(1) {
margin-right: 2%;
}
Last updated
Was this helpful?