<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Speech Synthesis</title>

  <link rel="stylesheet" href="./styles.css"/>
</head>
<body>
  <div class="text-to-voice">
    <header><h1>Text-to-Voice</h1></header>

    <div class="controls">
      <select class="voices" name="voices">
        <option value="">Select a voice</option>
      </select>

      <label>Rate: <input type="range" name="rate" value="1" min=0 max=5></label>
      <label>Pitch: <input type="range" name="pitch" value="1" min=0 max=2 step=0.5></label>
    </div>

    <textarea name="text">Bonbon caramels jelly beans. Dessert wafer chocolate cake sugar plum marshmallow cake powder ice cream. 🌹</textarea>

    <footer>
      <input class="btn stop" type="button" name="stop" value="Stop">
      <input class="btn start" type="button" name="start" value="Start">
    </footer>
  </div>

  <script src="./scripts.js"></script>
</body>
</html>
@import url('https://fonts.googleapis.com/css?family=Merienda+One');

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  background: url('./img/bg.png');
}

header,
footer {
  background: #AE439E;
  padding: 30px;
  width: 100%;
  text-align: center;
  height: 95px;
  display: flex;
  justify-content: center;
}

header {
  border-radius: 10px 10px 0 0;
  color: #611C57;
  font-family: 'Merienda One', cursive;
}

footer {
  position: relative;
  top: 1px;
  align-items: center;
}

form {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-direction: column;
  margin-top: 20px;
}

.controls {
  width: 70%;
  text-align: center;
}

.controls select {
  width: 100%;
  height: 25px;
  margin-bottom: 20px;
  outline: none;
  border: none;
}

.controls label {
  margin-bottom: 10px;
  display: block;
}

.controls input {
  vertical-align: middle;
  margin-left: 10px;
}

.text-to-voice {
  background: rgba(255, 255, 255, 0.7);
  width: 400px;
  height: 500px;
  border-radius: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-direction: column;
  box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.5);
}

.text-to-voice textarea {
  width: 80%;
  height: 150px;
  border-radius: 10px;
  resize: none;
  border: none;
  outline: none;
  padding: 20px;
  font-size: 20px;
  box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.2);
}

footer {
  border-radius: 0 0 10px 10px;
}

footer .btn {
  width: 150px;
  height: 40px;
  border-radius: 5px;
  background: #D8E140;
  outline: none;
  border: none;
  font-family: 'Merienda One', cursive;
  font-size: 22px;
  color: #fff;
  letter-spacing: 1px;
  text-shadow: 1px 1px 1px rgba(0, 0, 0, 0.4);
  box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.3);
}

footer .btn:first-child {
  margin-right: 20px;
}

@media screen and (max-height: 550px) {
  .text-to-voice {
    height: 350px;
  }

  header,
  footer {
    height: 75px;
    align-items: center;
  }

  footer .btn {
    width: 120px;
    height: 38px;
    font-size: 18px;
  }

  .text-to-voice textarea {
    width: 70%;
    height: 90px;
    font-size: 12px;
  }

  .controls label:first-of-type {
    display: inline-block;
    margin-right: 10px;
  }

  .controls label {
    display: inline-block;
  }

  .controls input {
    width: 80px;
    margin-left: 5px;
  }
}
let select = document.querySelector('select.voices'),
    startButton = document.querySelector('.start'),
    stopButton = document.querySelector('.stop'),
    textarea = document.querySelector('textarea'),
    ranges = document.querySelectorAll('input[type="range"]'),
    synth = window.speechSynthesis,
    text = new SpeechSynthesisUtterance(textarea.value),
    voices = [];

function populateVoiceList() {
  voices = synth.getVoices();

  select.innerHTML = voices.map(voice => {
    return `<option value="${voice.name}">${voice.name} - ${voice.lang}</option>`;
  }).join('');
}

function setVoice() {
  let voiceName = select.selectedOptions[0].value;
  text.voice = voices.find(voice => voice.name === voiceName);
}

function setOptions(e) {
  text[this.name] = this.value;
  toggleSpeak();
}

function toggleSpeak(startOver = true) {
  synth.cancel();
  startOver && synth.speak(text);
}

speechSynthesis.addEventListener('voiceschanged', populateVoiceList);
startButton.addEventListener('click', toggleSpeak);
stopButton.addEventListener('click', toggleSpeak.bind(null, false));
ranges.forEach(range => range.addEventListener('change', setOptions));
select.addEventListener('change', setVoice);