How To Convert Text To Speech & Stream In node.js

How To Convert Text To Speech & Stream In node.js

The speech synthesis is utilized to change over composed data into sound where it is more helpful for people. Albeit such a highlights is extraordinarily utilized in portable applications, for example, voice-empowered email and bound together informing applications, you can carry out an extremely basic TTS system in Node.js utilizing the 'gtts' module.

There are various method of TTS like Say, text-to-speech-js, gtts and many other , Gtts is a interface to google's text to speech API. It takes into account limitless lengths of spoken content by tokenizing long sentences where the discourse would normally stop.

Installation

You may reach at gtts.js, You can use this module by using below command

npm install gtts

There are two method to use this module

Save Method

It will permit you to save you record according to given location, Create index.js and save below code

const gTTS = require('gtts');

var speech = 'Welcome to the world';
var gtts = new gTTS(speech, 'en');

gtts.save('speech.mp3', function (err, result){
  if(err) { throw new Error(err); }
  console.log("Text convered into speech");
});

To run use node index.js command

Stream Method

This strategy will permit you to run continuous , However you will pass text paramter in URL, it will change text over to speech. For which you have to install express module as well

npm install express

Create an route , As hear it's defined here

const express = require('express');
const app = express();
const Gtts = require('gtts');

app.get('/hear', function (req, res) {
  const gtts = new Gtts(req.query.text, req.query.lang);
  gtts.stream().pipe(res);
});

app.listen(3000, function () {
  console.log('Open url to hear voice http://localhost:3000/hear?lang=en&text=My Name is shantun, Nice meeting you');
});

So this is the way by which you can utilize the gtts (Google Text to Speech) module for changing text over to speech in Node.js.

Thanks for reading , See you in next article

Take care

Did you find this article valuable?

Support Shantun Parmar by becoming a sponsor. Any amount is appreciated!