content through text-to-speech conversion

SpeechSynthesisUtterance: Convert Text to Speech with JavaScript

Learn how to convert text to speech using the SpeechSynthesisUtterance API in JavaScript. This powerful API allows you to control the voice, pitch, and speed of the synthesized speech, making it ideal for creating accessible web applications and adding a touch of personality to your websites.


Enhance your web applications and add a touch of personality with the SpeechSynthesisUtterance API, a JavaScript tool that converts text into spoken audio. This API provides granular control over the voice, pitch, and speed of the synthesized speech, enabling you to create accessible and engaging experiences for your users.


Code Example

Here’s a simple example of how to use the SpeechSynthesisUtterance API to convert the text “Hello, world!” into spoken audio:

const utterance = new SpeechSynthesisUtterance('Hello, world!');
utterance.pitch = 1.5; // Increase pitch
utterance.rate = 1.2; // Increase speech rate


For in-depth information about the SpeechSynthesisUtterance API, refer to the comprehensive documentation provided by MDN:

Usage Examples

  1. Read aloud instructions or notifications: Utilize the SpeechSynthesisUtterance API to provide audio feedback for user actions, guiding them through complex tasks or informing them about important notifications.
  2. Create accessible web applications: Make your web applications more accessible to individuals with hearing impairments by enabling them to hear the content through text-to-speech conversion.
  3. Enhance website navigation: Add a voice-controlled interface to your website, allowing users to navigate pages, select options, and perform actions using spoken commands.
  4. Create engaging educational content: Transform text-based educational materials into engaging audio lectures, making learning more accessible and enjoyable for students of all ages.
  5. Personalize user experiences: Tailor the voice and style of the synthesized speech to match the personality and preferences of your target audience, adding a unique touch to your web applications.

Browser Support

The SpeechSynthesisUtterance API enjoys broad support across major web browsers. Here’s a breakdown of the support status:

  • Chrome: Fully supported from version 4 and up.
  • Firefox: Fully supported from version 2 and up.
  • Safari: Fully supported from version 3.1 and up.
  • Edge: Fully supported from version 12 and up.
  • Opera: Fully supported from version 10 and up.

In recent versions of Chrome, the window.speechSynthesis.getVoices() method often returns an empty array immediately after the page loads. This is because Chrome fetches the list of available voices from Google’s servers asynchronously. To ensure the voices are available before attempting to synthesize text, you can use a callback function to wait for the voices to be loaded. Here’s an example of how to do this:

window.speechSynthesis.onvoiceschanged = () => {
  const voices = window.speechSynthesis.getVoices();
  console.log('Voices loaded:', voices);

This code will wait for the voices to be loaded and then log them to the console. You can then use the voices array to select the desired voice for your text-to-speech operation.




Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.