DEV Community

Asaolu Elijah šŸ§™ā€ā™‚ļø
Asaolu Elijah šŸ§™ā€ā™‚ļø

Posted on • Edited on

Text To Speech In 3 Lines Of JavaScript

If you're curious about trying this out, that's the 3 lines of code below šŸ‘‡

var msg = new SpeechSynthesisUtterance();
msg.text = "Hello World";
window.speechSynthesis.speak(msg);
Enter fullscreen mode Exit fullscreen mode

But if you're not in a hurry, this article explains everything you need to know about converting text to speech (spoken words) on the web with JavaScript.

Introduction

In a previous article, we'd explored the Web Speech API and also how to convert Speech To Text
Another amazing feature of the Web Speech API is to convert Text to Speech.

Note: Text To Speech != Speech To Text 🧐

  • Text To Speech is when we give the computer some words and the computer will say this words out loud in some robotic/human voice. While
  • Speech To Text is when we say some words to the computer, and what we'd just said will be converted to text (I guess this is explanatory enough)

Getting Started

The first thing we'll need to do is check if our browser supports the speech synthesis API. And the code below does that:

if ('speechSynthesis' in window) {
Ā // Speech Synthesis supported šŸŽ‰
}else{
  // Speech Synthesis Not Supported 😣
  alert("Sorry, your browser doesn't support text to speech!");
}
Enter fullscreen mode Exit fullscreen mode

Next step is to create a new speechSynthesis object, add required property and make our app talk šŸ‘‡

var msg = new SpeechSynthesisUtterance();
msg.text = "Good Morning";
window.speechSynthesis.speak(msg);
Enter fullscreen mode Exit fullscreen mode

Code Explanation

  • Line 1: We created a variable msg, and the value assigned to it is a new instance of the speechSynthesis class.
  • Line 2: The .text property is used to specify the text we want to convert to speech
  • And finally, the code on the 3rd(last) line is what actually make our browser talks.

Altering Default Output

The speechSynthesis API gives room to also change alter the default output like changing the voice, volume, speech rate, language, pitch and more:

var msg = new SpeechSynthesisUtterance();
var voices = window.speechSynthesis.getVoices();
msg.voice = voices[10]; 
msg.volume = 1; // From 0 to 1
msg.rate = 1; // From 0.1 to 10
msg.pitch = 2; // From 0 to 2
msg.text = "Como estas Joel";
msg.lang = 'es';
speechSynthesis.speak(msg);
Enter fullscreen mode Exit fullscreen mode

Getting Supported Voices

The code below helps you retrieve the list of all supported voices:

speechSynthesis.getVoices().forEach(function(voice) {
Ā  console.log(voice.name, voice.default ? voice.default :'');
});
Enter fullscreen mode Exit fullscreen mode

Conclusion

Well, there's nothing to conclude here I guess šŸ™ƒ
But you could follow me on Twitter, I tweet and retweet interesting technical stuffs; you sure want to see em.

Thanks for reading šŸ‘

Latest comments (31)

Collapse
 
isagive profile image
Imri Sagive

Wow, what a find - thank you very much

Collapse
 
kristijanlazarev profile image
Kristijan Lazarev

I didn't know it is built in. Awesome

Collapse
 
maadah profile image
maadah

Great, how can download the voice

Collapse
 
simplifycomplexity profile image
Kiran Randhawa

I can do it one in one line of code:

window.speechSynthesis.speak(new SpeechSynthesisUtterance("Hello World"));
šŸ™Š

Collapse
 
jcubic profile image
Jakub T. Jankiewicz • Edited

NOTE: getVoices doesn't work on page load in Chrome. See StackOverflow: Getting the list of voices in speechSynthesis (Web Speech API)

Collapse
 
abduallahnoorsayket profile image
abdullahnoorsayket • Edited
Collapse
 
sachin123np profile image
sachin123np • Edited

How to read the web page content? suppose in html page we have

hii gud morning

how do we take reference for the p tag in order to speak the content inside the p tag?
Collapse
 
nayyhah profile image
Neha Jha • Edited

You can add an id in p tag
say, id="content" is added in p tag

now access the content inside the p tag using this id and replace in above code like this:
msg.text = document.getElementById("content").textContent;

Collapse
 
mktcode profile image
mkt

Mind blown. Thanks! That's what Stephen Hawking used, right? :D

Collapse
 
asaoluelijah profile image
Asaolu Elijah šŸ§™ā€ā™‚ļø

Might be! :D

Collapse
 
mattmundell profile image
Matt Mundell

Nice. Any idea how to make it sound more human?

Collapse
 
phyrosalpha profile image
Felipe Matheus Ribeiro

I am not getting make the code to work.

I am using firefox in linux OS
The browser can detect the SpeechSynthesisUtterance() but there is no voice or
any sound. I noticed that window.speechSynthesis.getVoices() don't return any voice.

Collapse
 
vukky profile image
Vukky

You will need to have a text-to-speech dispatcher installed first, otherwise there's no TTS to use. See wiki.archlinux.org/title/List_of_a... for reference.