Tag Archives: Node-Red speech with MPG123

Node Red Speech – The Sequel

Some time ago, I wrote about speech output (text to speech) on the Raspberry Pi using Node Red. Well, that’s been running just fine but like most things – it works until it doesn’t.

In this case the nice people at Google have decided it is time to make money out of the people they offered free language translation APIs to and the API behind the excellent NORMIT program no longer works – I feel sorry for the fellow who spent ages putting it together. Here’s the original article.. http://tech.scargill.net/talking-raspberry/

So, I’ve checked and the best I can come up with for text-to-speech that doesn’t once again rely on someone else's servers, is “eSpeak”. This is simply installed on your computer as “apt-get install espeak”. I can only vouch for it running on the Raspberry Pi. I tried it on the Radxa Rock yesterday and it failed miserably.

So – I’ve come up with another route.

A program I used which works well, to sound alarms is called mpg123.  (npm install mpg123).  You simply hand it the name of an MP3 file on the command line – and it plays it! http://www.mpg123.de/ for more info.

So yesterday I discovered another service called voicerss (http://www.voicerss.org/) – and you can fire some text at them – and get a streaming MP3 file back!

For example…

http://api.voicerss.org/?key=xxxxxx&src=hello

where xxxxx is the API key they’ll freely give you on application.

So – I wanted this to work in Node-Red from a simple input.  I decided to use msg.payload to carry the text – and reserve msg.topic for the word “alert” if I wanted to prefix the sound with an alarm.

In this version I’ve not implemented complicated queues – I just play the message. See my previous article on queues. These might be needed if you wanted to fire messages out in rapid succession because I’m using Node-Red’s EXEC function and as far as I know – as it fires off things asynchronously – there’s no way to know when jobs are done. I guess you could have it run a batch file which sets and clears some kind of flag – I’ll leave that to others.

Here’s the basic setup.

tmpEAD1

What you see above – is a simple inject node – with the word “alert” in the topic (optional) and some text in the payload – for example “hello how are you”.

The function in the middle does all the work – the EXEC node at the end simply has “mpg123” as the command – other boxes are defaults.

Here are the internals of “THING” in the middle.

if (msg.payload.indexOf(".mp3")!=-1) return (msg); // if payload is an mp3 just play it, nothing else
    if (msg.topic.indexOf("alert")!=-1)
    {
    var msg2 = {
                payload : "",
                topic : ""
                };
        msg2.topic=msg.topic;
        msg2.payload='/usr/audio/alert02.mp3';
        node.send(msg2); // possibly play an alert
    }
    var moment=context.global.moment;
    var timeadd="";
    var dateadd="";
    if (msg.topic.indexOf("time")!=-1)
      timeadd= moment().format("h:mm a ")+ ', ';
    if (msg.topic.indexOf("date")!=-1)
      dateadd= moment().format("dddd, MMMM Do YYYY")+ ', ';
     
    msg.payload = msg.payload.replace(/''/gi, "'");
    msg.payload = msg.payload.replace(/'/gi, "''");   
    msg.payload= " -q 'http://api.voicerss.org/?key=XXXXXXXX&src=" + dateadd + timeadd + msg.payload + "'";
    node.send(msg); // synth   

Where you see XXXXXX above – put your API key from api.voicerss.org

You’ll note I have a file called /usr/audio/alert02.mp3 in there as well -  you can change the name and location but you should grab a suitable short alert mp3 sound from somewhere. I got it off a trekkie site.

As detailed in the original blog you’ll need to install and include MOMENT  if you want to be able to put “time” or “date” into the topic and have that spoken – if not just miss that stuff offf.

Below is a version with no time and date stuff.

 

if (msg.payload.indexOf(".mp3")!=-1) return (msg); // if payload is an mp3 just play it, nothing else
    if (msg.topic.indexOf("alert")!=-1)
    {
    var msg2 = {
                payload : "",
                topic : ""
                };
        msg2.topic=msg.topic;
        msg2.payload='/usr/audio/alert02.mp3';
        node.send(msg2); // possibly play an alert
    }
     
    msg.payload = msg.payload.replace(/''/gi, "'");
    msg.payload = msg.payload.replace(/'/gi, "''");   
    msg.payload= " -q 'http://api.voicerss.org/?key=XXXXXXXX&src="  + msg.payload + "'";
    node.send(msg); // synth   

Have fun.

Facebooktwittergoogle_pluspinterestlinkedin