Learn how to synthesize text into voice with Node.js

How to convert (synthesize) text to speech in Node.js

The speech synthesis is used to convert written information into sound where it is more convenient for humans. Although such a features is specially used in mobile applications such as voice-enabled e-mail and unified messaging apps, you can implement a very simple TTS system in Node.js using the say module.

The say module doesn't depend on any remote service as it will use the native TTS service of the system (Linux with Festival, MacOS with Mac OS X text to speech using AppleScript and Windows with SAPI).

Requirements

In order to convert text to speech, we'll depend of the say module. Say is a TTS (text to speech) library for node that sends text from node.js to your speakers.

To install this module, execute the following command in your terminal:

npm install say

After the installation, you'll be able to access this module using require("say"). You can read more about the say module in the official repository in Github here.

Implementation

The say module is an object that offers 3 methods (dependently of the OS some functions will not work as expected or may not be available):

Speak function

The speak function allows you to synthesize text easily. It expects as first parameter the text to speak and as second parameter a function to check for any error (in case there was) and the end event:

var say = require('say');

say.speak('Hello, how are you today?', (err) => {
    if (err) {
        return console.error(err);
    }

    console.log('Text has been spoken.');
});

In MacOS and Linux, you can specify the speed of the synthesization and the voice providing the first and third parameter:

var say = require('say');

say.speak('Hello, how are you', 'Alex', 0.5, (err) => {
    if (err) {
        return console.error(err);
    }

    console.log('Text has been spoken.');
});

Note that in Windows these options aren't available.

Stop function

You can stop any current speech utterance using the say.stop method:

var say = require('say');

say.speak('Hello, how are you', 'Alex', 0.5, (err) => {
    if (err) {
        return console.error(err);
    }

    console.log('Text has been spoken.');
});

say.stop();

Export function

The export function allows you to export the text synthesization into a .wav file.

var say = require("say");
var filename = "myaudio.wav"

say.export("I'm sorry, Dave.", 'Cellos', 0.75, filename, function(err) {
    if (err) {
        return console.error(err);
    }

    console.log(`Text has been saved to ${filename}`);
});

Notes for windows

The voice parameter and speed in the say function not available, that means it will use the default system voice, ignoring the voice and speed parameter.

The export function is neither available.

Notes for OSX

In mac OS you can change the voices to:

Feminine Voices

Agnes, Kathy, Princess, Vicki, Victoria

Masculine Voices

Albert, Alex, Bruce, Fred, Junior, Ralph

Miscellaneous Voices

Bad News, Bahh, Bells, Boing, Bubbles, Cellos, Deranged, Good News, Hysterical, Pipe Organ, Trinoids, Whisper, Zarvox.

Notes for Linux

To synthesize text using Node.js in Linux, you'll FestivalVoices for Festival sometimes need to be installed separately - you can check which voices are available by starting up Festival in interactive mode, typing (voice_, and pressing TAB. Then take the name of the voice you'd like to try, minus the parentheses, and pass it in to say.js, for example:

var say = require("say");
var voiceName = "voice_nick1"

say.speak("Luke, I am your father", voiceName, 0.75 , (err) => {
    if (err) {
        return console.error(err);
    }

    console.log(`Text with the voice ${voice}`);
});

Happy coding !


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors