Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active August 6, 2023 06:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save code-boxx/1e4361e00f1b100079b1b320078ac87a to your computer and use it in GitHub Desktop.
Save code-boxx/1e4361e00f1b100079b1b320078ac87a to your computer and use it in GitHub Desktop.
Javascript Text To Speech

JAVASCRIPT TEXT TO SPEECH

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>Text To Speech Demo</title>
<script defer src="1-simple.js"></script>
</head>
<body>
<button id="demoA" disabled>Text To Speech</button>
</body>
</html>
if ("speechSynthesis" in window) {
let demo = document.getElementById("demoA");
demo.onclick = () => {
let msg = new SpeechSynthesisUtterance("fus ro dah!");
speechSynthesis.speak(msg);
};
demo.disabled = false;
}
<!DOCTYPE html>
<html>
<head>
<title>Text To Speech Demo</title>
<style>
#demoB, #demoB * { padding: 10px; }
#demoB {
max-width: 400px;
background: #f2f2f2;
}
#demoB * {
font-family: arial, sans-serif;
box-sizing: border-box;
display: block;
width: 100%;
}
</style>
<script defer src="2-choose-voice.js"></script>
</head>
<body>
<form id="demoB">
<!-- (A) VOICE PICKER -->
<label>Choose A Voice</label>
<select id="demoB-voice" disabled></select>
<!-- (B) SAY SOMETHING -->
<label>Message</label>
<input type="text" id="demoB-msg" disabled required>
<label>Go!</label>
<input type="submit" id="demoB-go" disabled value="Speak">
</form>
</body>
</html>
if ("speechSynthesis" in window) {
// (A) GET HTML ELEMENTS
let demo = document.getElementById("demoB"),
vlist = document.getElementById("demoB-voice"),
vmsg = document.getElementById("demoB-msg"),
vgo = document.getElementById("demoB-go");
// (B) POPULATE AVAILABLE VOICES
// CHROME LOADS VOICES ASYNCHRONOUSLY
// THUS THIS "STUPID" WAY TO ATTACH AVAILABLE VOICES
var voices = () => {
speechSynthesis.getVoices().forEach((v, i) => {
let opt = document.createElement("option");
opt.value = i;
opt.innerHTML = v.name;
vlist.appendChild(opt);
});
};
voices();
speechSynthesis.onvoiceschanged = voices;
// (C) SPEAK
var speak = () => {
let msg = new SpeechSynthesisUtterance();
msg.voice = speechSynthesis.getVoices()[vlist.value];
msg.text = vmsg.value;
speechSynthesis.speak(msg);
return false;
};
// (D) ENABLE FORM
demo.onsubmit = speak;
vlist.disabled = false;
vmsg.disabled = false;
vgo.disabled = false;
}
<!DOCTYPE html>
<html>
<head>
<title>Text To Speech Demo</title>
<style>
#demoC, #demoC * { padding: 10px; }
#demoC {
max-width: 400px;
background: #f2f2f2;
}
#demoC * {
font-family: arial, sans-serif;
box-sizing: border-box;
display: block;
width: 100%;
}
</style>
<script defer src="3-more-controls.js"></script>
</head>
<body>
<form id="demoC">
<!-- (A) VOLUME + PITCH + RATE -->
<label>Volume</label>
<input type="range" id="demoC-vol" min="0" max="1" step="0.1" value="1">
<label>Pitch</label>
<input type="range" id="demoC-pitch" min="0" max="2" step="0.1" value="1">
<label>Rate</label>
<input type="range" id="demoC-rate" min="0.1" max="10" step="0.1" value="1">
<!-- (B) SAY SOMETHING -->
<label>Message</label>
<input type="text" id="demoC-msg" disabled required>
<label>Go!</label>
<input type="submit" id="demoC-go" disabled value="Speak">
</form>
</body>
</html>
if ("speechSynthesis" in window) {
// (A) GET HTML ELEMENTS
let demo = document.getElementById("demoC"),
vvol = document.getElementById("demoC-vol"),
vpitch = document.getElementById("demoC-pitch"),
vrate = document.getElementById("demoC-rate"),
vmsg = document.getElementById("demoC-msg"),
vgo = document.getElementById("demoC-go");
// (B) SPEAK
var speak = () => {
let msg = new SpeechSynthesisUtterance();
msg.voice = speechSynthesis.getVoices()[0];
msg.text = vmsg.value;
msg.volume = +vvol.value;
msg.pitch = +vpitch.value;
msg.rate = +vrate.value;
speechSynthesis.speak(msg);
return false;
};
// (C) ENABLE FORM
demo.onsubmit = speak;
vmsg.disabled = false;
vgo.disabled = false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment