Raspberry Pi – Text to Speech

Posted in: Projects | 37

I wanted to get some text to speech working with the raspberry pi.
I found a bunch of nice solutions, but the best quality one was Google’s online one, via their translate tool.

You can get an mp3 of whatever you wanted to say via a URL…
Only this is limited to 100 characters.
Someone produced a shell script which automatically requested and played this, but it refused to play anything above 100.

I created this shell script, which should work under any UNIX system… It works to ensure that the messages sent to the google API are under 100 characters. However it ensures that it only breaks the speech on a space. This it doesn’t break the word.

it only require mpg123 – a simple mp3 player which takes a URL as an input.
either get this with:

or replace the ‘mpg123′ line (near the bottom) with another program that can play a URL

the code:

Usage:
save as a shell script, eg: Speech.sh
set permissions:

pass any text like this:

(Speech marks “” are optional, but can help if it gets confused with strange punctuation.)

Let me know if you do anything nice with it.
Enjoy,
Dan

37 Responses

  1. Matrhint

    Great script! One small thing I had to change was with using sudo chmod -x — instead I used sudo chmod +x. I will try to use this in future projects. Thanks.

  2. Hi Dan,

    thank you for the script!
    I did something similar in Java, but will take your script and combine it with Icinga (a monitoring tool), my Pi and a loudspeaker.
    If something bad happens to the monitored hosts and services, my Pi will play an audio message.

  3. Very good!
    I’m glad this is serving people well!

  4. I was just wondering if there is anyway to change the sound of the voice, and how one would do so.

  5. Moose

    Thank you! This rocks!

  6. I heard that google might be adding different voices, (or rather dialects).
    Bearing in mind, this is a private script which is used in the middle of the pronunciation part of their translation API, there is no need for different voices, except maybe those future dialects.

  7. […] is no longer than 100 characters, and each parts can be played successfully. This is a script from here which I modified to be used in our […]

  8. For a male voice you can use –

    mpg123 -q “http://translate.google.co.uk/translate_tts?tl=en&q=${SHORT[$key]}”

    Cheers,
    Trevor

  9. Getting an error message with your script.

    —————————
    Speech Script by Dan Fountain
    TalkToDanF@gmail.com
    —————————
    Playing line: 1 of 1
    http_open: HTTP/1.0 400 Bad Request
    http://translate.google.com: No such file or directory

  10. Got it working. I had something running that was blocking my internet connection.

  11. Is there a way to have your script read the time on the quarter hour and IP on bootup?

  12. You can look in to crontab for intervals:
    http://blog.davidsingleton.org/raspberry-pi-webcam-a-gentle-intro-to-crontab/
    and startup:
    http://www.raspberrypi-spy.co.uk/2013/07/running-a-python-script-at-boot-using-cron/

    eg to schedule a SHELL script (as opposed to python in the example)
    you can run:
    */15 * * * * sudo sh /bin/speakTime.sh

    supposing you had copied the shell script there…

    You could make a shell script that calls mine, with the time as the input..
    dont know how to do that from shell, but you can look it up.

    also, i recommend using a dynamic ip service instead of reading and using your ip
    http://www.raspberrypi.org/phpBB3/viewtopic.php?f=30&t=20844

  13. I do not want just an audio “link”.
    So how do I download by audio sentence as an mp3 file to save it on my hard drive.?
    Yes, I have a gmail account.

    But no, there is no download button to save this audio sentence ? is there a way to “pipe” it in via mplayer and then output it as an mp3 file ?

    Thanks.

  14. massimo Bonomo

    hi thank you for the script!! ,
    i got this error unsing mpg123 on raspberryPi ; (i think because mpg123 use http1.0 ) the error is above .
    http_open: HTTP/1.0 400 Bad Request

    workaround :
    Emulating user-Agent with Mozilla the issue is fixed
    replacing this line :
    mpg123 -q “http://translate.google.com/translate_tts?tl=en&q=${SHORT[$key]}”
    with this one :
    mplayer -user-agent Mozilla “http://translate.google.com/translate_tts?tl=en&q={SHORT[$key]}”

    thanks
    Massimo

  15. Nobunaga1989

    Hi Dan,

    Good article, I apply it on my Linux and publish the steps here http://neenutna.blogspot.com/2013/12/use-text-to-speech-in-linux.html

  16. I’m getting a 400 error on the script, and when I tried a wget against the URL that you provided, I get a 403 Forbidden. Did Google change the URL that needs to be used? I copied the code straight from the website.

    pi@raspberrypi ~/scripts $ ./speak.sh “hello”
    —————————
    Speech Script by Dan Fountain
    TalkToDanF@gmail.com
    —————————
    Playing line: 1 of 1
    http_open: HTTP/1.0 400 Bad Request
    http://translate.google.com: No such file or directory
    pi@raspberrypi ~/scripts $ wget http://translate.google.com/translate_tts?tl=en&q=hello
    [1] 19774
    pi@raspberrypi ~/scripts $ –2014-01-07 15:18:03– http://translate.google.com/translate_tts?tl=en
    Resolving translate.google.com (translate.google.com)… 74.125.225.132, 74.125.225.130, 74.125.225.137, …
    Connecting to translate.google.com (translate.google.com)|74.125.225.132|:80… connected.
    HTTP request sent, awaiting response… 403 Forbidden
    2014-01-07 15:18:04 ERROR 403: Forbidden.

  17. Hmm, I’m getting the same error as David when running this off my Pi over ssh.

    http_open: HTTP/1.0 400 Bad Request
    http://translate.google.com: No such file or directory

    I can ping google (8.8.8.8) so access to the internet seems ok. Any thoughts???

  18. Thiago Neves

    Hey there !
    Im having just a hard time when the text is been read.
    Because of the break every 100 characters, the system pauses for 2 seconds and comeback to start reading the next line. But this little pause makes you loose the understanding of the computer is saying… I don’t know if that makes sense, but is there any trick to remove the little pause in between the 100 characters ?!

    Thank you very much !

    Thiago

  19. FunMon

    Nice script! I was wondering if anyone has any luck incorporating key binding like the one in this video:

    https://www.youtube.com/watch?v=4uKTamXonPs

    The key bindings are used to read selected text in a web page or file. I’ve tried a few things but they’re not working.

  20. […] are a few scripts that I found (including this one from Dan Fountain) that offer an interface to this API, however the majority of them just split the […]

  21. Awesome!!! Got my raspberry talking now!
    How would i go about and make it read the content of a file?
    I write the current weather status to a file, then i’d like to hear that over the speakers… Any idea on how to do that?

  22. Never mind, got it going on my own! FYI, i do it like this:

    #!/bin/bash

    #get weather status
    ./getweather.sh > currentweather.log

    #speak it out loud
    # ./talk.sh “cat currentweather.log”;

    file1=cat currentweather.log

    echo $file1

    #mpg123 -q “http://translate.google.com/translate_tts?tl=en&q=${SHORT[$key]}”
    mpg123 -q “http://translate.google.com/translate_tts?tl=en&q=$file1″

  23. Astryder

    Hi,

    i would like make a little contribute for french people (and language with accent in general).
    I just find a add to do.

    So:

    replace this line

    mpg123 -q “http://translate.google.com/translate_tts?tl=en&q=${SHORT[$key]}”

    to

    mpg123 -q “http://translate.google.com/translate_tts?tl=fr&ie=UTF-8&q=${SHORT[$key]}”

    We need only to add this thing: &ie=UTF-8

    Thx Dan for your script!!!

  24. Thanks for the script, Dan.
    However, it stops after every 100 characters.
    The only thing that will continue it is a keyboard Ctrl-C.
    Is this meant to be the case?
    If so, is there a command that can be inserted in the script to automate pressing Ctrl-C every 100 characters?

  25. […] code below relies on a few third party libraries and scripts, specifically: tweepy, vol and speech.sh. Here’s the […]

  26. Thanks. I am using this alongside Domoticz and some LUA scripts to analyse doors, lights, switches, sensors and people in my house and then speaking out various messages. Yes very geeky.

  27. Hey.
    Sounds like mpg123 isn’t returning?
    Try using a different media player- one that can run via command line and terminates itself when it is complete.

  28. Hello.
    I have made a modification to the script which should deal with accented characters.
    You can often change the language on the URL portion… try chaning ‘en’ to ‘es’ for spanish ect.

  29. You can send the contents of a file using xargs.
    do so like this:
    cat textfile.txt | xargs sh speech.sh

  30. Hey.
    Google doesn’t accept more than 100 characters. (at least when I wrote this script).
    I remember that google doesn’t do any speech if you have over 100.
    This tool decides to break the lines between words instead of in the middle of one.

  31. Seems like mpg123 is having trouble getting the url.
    Is it able to play other .mp3 files via http?
    Also try changing the mpg123 line to ‘echo’
    That should show you the URL that it is trying to run… see if that downloads in your desktop browser.

  32. Saw raspivt’s post about this first.. I replied:

    Seems like mpg123 is having trouble getting the url.
    Is it able to play other .mp3 files via http?
    Also try changing the mpg123 line to ‘echo’
    That should show you the URL that it is trying to run… see if that downloads in your desktop browser.

  33. Looks great.
    Thanks for the link back!

  34. Thanks for the work around.
    No idea of the source.
    Hopefully this can help people.

  35. The script passes a URL to mpg123.
    it should be possible to pass that url to wget or curl
    these can download files.
    do some research on those looks if you haven’t seen them before.
    All the best.

  36. […] Google Calendar in which i created reminders in the form of appointments and used the Google TTS, using the this script, to read them out loud. It was pretty cool, All I had to do was to add something in my calendar, […]

Leave a Reply