Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="wrapper">
  <h1>Multilingual Text To Speech</h1>
  <div class="uiunit">
    <label for="speakerMenu">Voice: </label>
    <select id="speakerMenu"></select>  speaks 
    <span id="language">English.</span>
  </div>
  <p id="warning">Sorry, your browser does not support the Speech Synthesis API.</p>

  <textarea id="txtFld">Choose any voice. Type here in the chosen language.</textarea>
  <label for="txtFld">Type text above in the selected language (or copy from Google Translate). Then click the Speak button.</label>
  <div>
    <button type="button" id="speakBtn">Speak</button>
    <br>
    <p>Note: For best results on a Mac, use the latest version of Chrome, Safari, or FireFox. On Windows, use Chrome.</p>
  </div>

</div>
              
            
!

CSS

              
                @charset "UTF-8";
*, *:before, *:after {box-sizing: border-box;}
:root {
  --textcolor:black;
  --accentcolor:#269;
  --lightaccentcolor:#9df;
  --reversecolor:white;
}
html {
  font-size: 16px;
  font-family: Trebuchet,sans-serif;
}
body {
  background: var(--accentcolor);
  color: var(--reversecolor);
  margin: 0;
  padding: 0;
  line-height: 1.5;
}
strong, b {font-weight: bold;}
#wrapper {
  margin: 0 auto;
  padding: 1.2rem;
  width: 100%;
  height: auto;
  min-width: 320px;
  max-width: 960px;
  background: var(--reversecolor);
  color: var(--textcolor);
  text-align: left;
  border:2px solid var(--accentcolor);
  border-radius:0 0 16px 16px;
  box-shadow:4px 4px 16px #333;
}
h1 {
  color: var(--accentcolor);
  font-size: 3rem; 
  margin: 0;
}
p {
  display:block;
  font-size: 1rem; 
  margin: 1rem 0 1rem 0;
}
a:link, a:visited {color: var(--accentcolor);}
a:hover {text-decoration: none;}

#txtFld {
  width: 100%;
  height: 4.4rem;
  font-size: 1.6rem;
  font-family: inherit;
}
button {
  -webkit-appearance: none;
  font-size: 1.4rem;
  font-weight: normal;
  background: var(--lightaccentcolor);
  border-radius: 12px;
  border: 2px solid var(--accentcolor);
  color: var(--textcolor);
  padding: 0.3rem 0.5rem 0.3rem 0.5rem;
  cursor: pointer;
  margin: 10px 20px 10px 20px;
}
button:hover {
  color: var(--reversecolor);
  background: var(--accentcolor);
}
button:disabled{
  opacity:0.3;
  cursor: not-allowed;
}
.uiunit {
  display: inline-block;
  margin: 1rem;
  text-align: left;
}
#warning {
  color: red;
  font-size: 1.4rem;
  display: none;
}
              
            
!

JS

              
                let speakBtn, txtFld, speakerMenu, language;
let allVoices, langtags;
let voiceIndex = 0;

function init(){
  speakBtn = qs("#speakBtn");
  txtFld = qs("#txtFld"); 
  speakerMenu = qs("#speakerMenu");
  language = qs("#language");
  langtags = getLanguageTags();
  speakBtn.addEventListener("click",talk,false);
  speakerMenu.addEventListener("change",selectSpeaker,false);
  if (window.speechSynthesis) {
    if (speechSynthesis.onvoiceschanged !== undefined) {
      //Chrome gets the voices asynchronously so this is needed
      speechSynthesis.onvoiceschanged = setUpVoices;
    }
    setUpVoices(); //for all the other browsers
  }else{
    speakBtn.disabled = true;
    speakerMenu.disabled = true;
    qs("#warning").style.display = "block";
  }
}
function setUpVoices(){
  allVoices = getAllVoices();
  createSpeakerMenu(allVoices);
}
function getAllVoices() {
  let voicesall = speechSynthesis.getVoices();
  let vuris = [];
  let voices = [];
  voicesall.forEach(function(obj,index){
    let uri = obj.voiceURI;
    if (!vuris.includes(uri)){
      vuris.push(uri);
      voices.push(obj);
    }
  });
  voices.forEach(function(obj,index){obj.id = index;});
  return voices;
}
function createSpeakerMenu(voices){
  let code = ``;
  voices.forEach(function(vobj,i){
    code += `<option value=${vobj.id}>`;
    code += `${vobj.name} (${vobj.lang})`;
    code += vobj.voiceURI.includes(".premium") ? ' (premium)' : ``;
    code += `</option>`;
  });
  speakerMenu.innerHTML = code;
  speakerMenu.selectedIndex = voiceIndex;
}
//code for when the user selects a speaker
function selectSpeaker(){
  voiceIndex = speakerMenu.selectedIndex;
  let sval = Number(speakerMenu.value);
  let voice = allVoices[sval];
  let langcode = voice.lang.substring(0,2);
  let langcodeobj = searchObjects(langtags,"code",langcode);
  language.innerHTML = langcodeobj[0].name;
}
function talk(){
  let sval = Number(speakerMenu.value);
  let u = new SpeechSynthesisUtterance();
  u.voice = allVoices[sval];
  u.lang = u.voice.lang;
  u.text = txtFld.value;
  u.rate = 0.8;
  speechSynthesis.speak(u);
}
function getLanguageTags(){
  let langs = ["ar-Arabic","cs-Czech","da-Danish","de-German","el-Greek","en-English","eo-Esperanto","es-Spanish","et-Estonian","fi-Finnish","fr-French","he-Hebrew","hi-Hindi","hu-Hungarian","id-Indonesian","it-Italian","ja-Japanese","ko-Korean","la-Latin","lt-Lithuanian","lv-Latvian","nb-Norwegian Bokmal","nl-Dutch","nn-Norwegian Nynorsk","no-Norwegian","pl-Polish","pt-Portuguese","ro-Romanian","ru-Russian","sk-Slovak","sl-Slovenian","sq-Albanian","sr-Serbian","sv-Swedish","th-Thai","tr-Turkish","zh-Chinese"];
  let langobjects = [];
  for (let i=0;i<langs.length;i++){
    let langparts = langs[i].split("-");
    langobjects.push({"code":langparts[0],"name":langparts[1]});
  }
  return langobjects;
}
// Generic utility functions
function searchObjects(array,prop,term,casesensitive = false){
  //searches an array of objects for a given term in a given property
  //returns only those objects that test positive
  let regex = new RegExp(term, casesensitive ? "" : "i");
  let newArrayOfObjects = array.filter(obj => regex.test(obj[prop]));
  return newArrayOfObjects;
}
function qs(selectorText){
  //saves lots of typing for those who eschew Jquery
  return document.querySelector(selectorText);
}
document.addEventListener('DOMContentLoaded', function (e) {
  try {init();} catch (error){
    console.log("Data didn't load", error);}
});
              
            
!
999px

Console