If you’re working on a Node.js application and need to add text-to-speech capabilities, there are several libraries you can use. These libraries provide the ability to convert written text into spoken words, allowing you to create more interactive experiences for your users. Here are the four best options for Node.js text-to-speech libraries:
say.js
say is a TTS library for node.js. It can send text from node.js to your speakers.
// automatically pick platform const say = require('say') // or, override the platform const Say = require('say').Say const say = new Say('darwin' || 'win32' || 'linux') // Use default system voice and speed say.speak('Hello!') // Stop the text currently being spoken say.stop() // More complex example (with an OS X voice) and slow speed say.speak("What's up, dog?", 'Alex', 0.5) // Fire a callback once the text has completed being spoken say.speak("What's up, dog?", 'Good News', 1.0, (err) => { if (err) { return console.error(err) } console.log('Text has been spoken.') }); // Export spoken audio to a WAV file say.export("I'm sorry, Dave.", 'Cellos', 0.75, 'hal.wav', (err) => { if (err) { return console.error(err) } console.log('Text has been saved to hal.wav.') })
say.js has great documentation and is easy to use.
nodejs-text-to-speech
This is a cloud Text-to-Speech API client for Node.js.
// Imports the Google Cloud client library const textToSpeech = require('@google-cloud/text-to-speech'); // Import other required libraries const fs = require('fs'); const util = require('util'); // Creates a client const client = new textToSpeech.TextToSpeechClient(); async function quickStart() { // The text to synthesize const text = 'hello, world!'; // Construct the request const request = { input: {text: text}, // Select the language and SSML voice gender (optional) voice: {languageCode: 'en-US', ssmlGender: 'NEUTRAL'}, // select the type of audio encoding audioConfig: {audioEncoding: 'MP3'}, }; // Performs the text-to-speech request const [response] = await client.synthesizeSpeech(request); // Write the binary audio content to a local file const writeFile = util.promisify(fs.writeFile); await writeFile('output.mp3', response.audioContent, 'binary'); console.log('Audio content written to file: output.mp3'); } quickStart();
winsay
This is a TTS module for Node.js on windows using Edge.js.
var winsay = require('winsay'); // no callback, fire and forget winsay.speak("GLaDOS", "Hello, and welcome to the Aperture Science Enrichment Center"); // use default voice in System Preferences winsay.speak(null, "Hello!"); // output some text to the console after the callback has completed winsay.speak("Good News', 'You've won the internet!", function () { console.log("text to speech complete"); }); console.log("This text is printed before you win the internet."); // syncronous speak function also available winsay.speakSync("Red", "A wild Pikachu appears."); console.log("This text is printed after you encounter the wild Pikachu.");
text-to-speech-nodejs
This is a sample Node.js application for the IBM Watson Text-to-Speech Service.
You just need to drop in the key as follows then run the app.
TEXT_TO_SPEECH_IAM_APIKEY=X4rbi8vwZmKpXfowaS3GAsA7vdy17Qh7km5D6EzKLHL2 TEXT_TO_SPEECH_URL=https://gateway-wdc.watsonplatform.net/text-to-speech/api