Skip to content

Express JS – Convert Given text to Voice using Node JS

Last updated on November 21, 2022

In this post, I would like to introduce ‘say’, which reads out the given text and we can even save it in audio format.

Requirements

Mac OS X (comes with say)
Linux with Festival installed, you can install it with sudo apt-get install festival festvox-kallpc16k
Windows (comes with SAPI.SpVoice).

Convert Given text to Voice using Node JS

Limitations

In windows systems, voice parameter is not yet available. Uses whatever default system voice is set, ignoring voice parameter. The speed parameter is not yet available. for more information please visit click here

Below is the complete working rest API, which is designed with the express js framework.

{
  "name": "express-text-to-voice",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "directories": {
    "test": "test"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.17.2",
    "express": "^4.15.4",
    "say": "^0.11.0"
  }
}

Run the following command to install the dependencies.

npm install

Here is our Server file which will convert Given text to voice using Node JS.

// require express module
const express = require('express');

// create express instance
const app = express();

// require say moudle
const say = require('say');

// require body parser, which will hahlde the user inputs
const bodyParser = require('body-parser');

// useing body parser middlewares, which will handle the user intput streams.
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));

// rest api call, POST http://localhost:8080/speak 
app.post('/speak', function (req, res) {
   let text = req.body.text; // input text
   // here Alex is the voice 
   // 1.0 is speed
   // text is content 
   say.speak(text, 'Alex', 1.0, function (err) {
      if (err) {
        return res.json({ message: err, error: true });
      }
     return res.json({ message: 'Text has been spoken.', error: false });
   });
});

app.listen(8080);

Running the application

node Server.js

To test our rest API, you have to send a post request with ‘text’ field to http://localhost:8080/.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments