a blog for those who code

Monday 26 October 2015

Simple Text to Speech Application in Node.js

In this post we will show you how to create a text-to-speech module in Node.js using Edge.js. Text to Speech application actually means that you will provide text to the application and it will convert that to a speech. Edge.js connects Node.js and .Net together, where you will actually write the code in C# but it will be ran from Node.js.

If you are not aware of Edge.js, read Write .Net and Node.js code together using Edge.js. Using Edge.js you connect Node.js and .Net on Windows, MacOS, and Linux. You need to at first install Edge before proceeding with the code.

Code of Text to Speech:

var edge = require('edge');
var value = edge.func(function() {/*

  #r "System.Speech.dll"

  using System;
  using System.Threading.Tasks;
  using System.Speech.Synthesis;

  public class Startup
  {
    public async Task<object> Invoke(object input)
    {
      SpeechSynthesizer reader = new SpeechSynthesizer();
      Prompt prompt = new Prompt(input.ToString());
      reader.Speak(prompt);
      return null;
    }
  }
*/});

value("Hello World", function (error, result) {
  if(error) throw error;
  console.log(result);
});

Edge.js only reference System and System.Threading.Tasks, so for any other references in Edge.js we need to include the reference as #r "DLLName.dll". Like a Console Application in C# where you need to have a static main method, Edge.js needs a Class Startup and this class must invoke method that matches the Func<object, Task<object>> delegate. Inside that delegate we are using SpeechSynthesizer Class to convert the text to speech.

If your system did not have Visual Studio installed then you need to download the System.Speech.dll from the web and keep it in the same folder as your server.js file as shown below.


Please Like and Share the CodingDefined Blog, if you find it knowledgeable and helpful.

No comments:

Post a Comment