Raspberry Pi – Text to Speech

Hey.
So 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 splits the line into words, and sends sections to the service, under 100 characters, then plays them automatically…

it only relies on mpg123 – a simple mp3 player which takes a URL as an input.
either get this with:
apt-get install mpg123
or replace that line (near the bottom) with another program.

the code:

#!/bin/bash
#################################
# Speech Script by Dan Fountain #
# TalkToDanF@gmail.com #
#################################

INPUT=$*
STRINGNUM=0

ary=($INPUT)
echo "---------------------------"
echo "Speech Script by Dan Fountain"
echo "TalkToDanF@gmail.com"
echo "---------------------------"
for key in "${!ary[@]}"
do
SHORTTMP[$STRINGNUM]="${SHORTTMP[$STRINGNUM]} ${ary[$key]}"
LENGTH=$(echo ${#SHORTTMP[$STRINGNUM]})
#echo "word:$key, ${ary[$key]}"
#echo "adding to: $STRINGNUM"
if [[ "$LENGTH" -lt "100" ]]; then
#echo starting new line
SHORT[$STRINGNUM]=${SHORTTMP[$STRINGNUM]}
else
STRINGNUM=$(($STRINGNUM+1))
SHORTTMP[$STRINGNUM]="${ary[$key]}"
SHORT[$STRINGNUM]="${ary[$key]}"
fi
done

for key in "${!SHORT[@]}"
do
#echo "line: $key is: ${SHORT[$key]}"

echo "Playing line: $(($key+1)) of $(($STRINGNUM+1))"
mpg123 -q "http://translate.google.com/translate_tts?tl=en&q=${SHORT[$key]}"
done

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

sudo chmod -x Speech.sh

pass any text like this:

./Speech.sh What is this great task for which I, Deep Thought, the second greatest computer in the Universe of Time and Space have been called into existence?

(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

21 Comments

  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. Dan

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

  4. Eric

    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. Dan

    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. For a male voice you can use –

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

    Cheers,
    Trevor

  8. 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

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

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

  11. 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

Trackbacks/Pingbacks

  1. Raspberry Pi Voice Recognition Works Like Siri - OscarLiang.net - [...] is no longer than 100 characters, and each parts can be played successfully. This is a script from here ...

Leave a Reply