DEV Community

Cover image for Speech Synthesis in JavaScript
Nikita Dmitriev
Nikita Dmitriev

Posted on • Edited on

3 1

Speech Synthesis in JavaScript

Backstory

Hi. I want to develop my personal assistant. I needed to implement speech synthesis. I had two ways:

  1. Use online speech synthesis like googleTTS
  2. Use offline npm modules and my system

So, I chose the second one.

And, here I’ll tell you everything about my adventure and how to use speech synthesis on your device using node.js

New era

I decided to use Say.js but the repository is abandoned by the author, and I needed to set “UTF-8” encoding for my voice. Originally I wanted to create a fork for Say.js module, but more convenient was to create my own module (I renamed variables, added encodings and created convenient documentation README). So, I developed my module based on Say.js named “speakertts”. If you want to contribute follow github

How it works

This module uses node.js module “child-process” to call powershell and execute built-in SpeechSynthesizer object (I talk about windows). You can use your powershell and paste this code:

Add-Type -AssemblyName System.speech
$speak = New-Object System.Speech.Synthesis.SpeechSynthesizer
$speak.Speak('Hello...')
Enter fullscreen mode Exit fullscreen mode

You can read more here

How to use

  1. Install speakertts module

    npm i speakertts
    
  2. Get your voices using getInstalledVoices() function

    const speaker = require('speakertts')
    
    speaker.getInstalledVoices((error, voices) => {
        if (error) {
            console.error('Error:', error);
        } else {
            console.log('Installed voices:', voices);
        }
    });
    
  3. Call SpeechSynthesizer using speak function

    speaker.speak('Hello, world', 'Your voice', 1, 'ASCII', 
    (error) => {
      if (error) {
        return console.error('Error speaking!', error)
      }
    
      console.log('text to speech complete')
    })
    

It was basics, other functions you can look up on github or npm

How to set your not system voice (for windows)

You just need to install SAPI format voice, and repeat previous chapter

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (1)

Collapse
 
vulcanwm profile image
Medea

nice article!

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs