Giving my Pi a Voice

The Rasperry Pi is so small and portable, I like to take it with me so I can do development and play with projects on the go. The Raspberry Pi is nice and small but lugging a monitor around just isn't an option.  What to do?  I have a laptop with me most of the time so using Secure Shell or VNC works but I need to know what IP address the Rasberry is setup on. This becomes an issue if you are using DHCP and not on your own network.  

The solution... Make it talk and tell me the information I need to know. With a talking Raspberry, I only need to travel with ear buds or a small portable speaker. 

Once you get your Raspberry to talk, it can have a lot to say. The projects that can use a voice are endless. Here is what I did to install a text-to-speach package and the scripts that I use to get it to announce it's IP when it startsup or the IP changes.


Checking the Sound Support

On my Raspberry Pi, the alsa tool and sound modules was already installed but you should check to see if they are installed on your version and install them if they are not.

Check to see if the alsa utilities are installed.  If they are, apt will let you know. If not then this will ask you if you want to install them.

# apr-get install alsa-utils

Check the file /etc/modules for the bcm2835 sound module.  If it is not in the file, then it needs to be added.

# vi /etc/modules

# This file contains the name on kernel modules that should be loaded
# at boot time, one per line. Lines begining with "#" are ingored.
# Parameters can be specified after the module name.



Installing mplayer

Mplayer is used to play most types of audio and video file types.  In this project it will be used to play voice files returned from the Google API (see more below on Google) but even if you do not want to use the Google API voice then mplayer is still a good idea in your project as it can be used to play wav or mp3 files that you may want in the future.

# apt-get install mplayer

Mplayer will give you a few errors about devices that are not found when you run it.  To fix the errrors, we need to edit mplayer's configuration file and tell it not to look for the lirc IR program.

# vi /etc/mplayer/mplayer.conf

add the following line at the end of the file:


Installing Espeak.

Espeak is a good test-to-speach program.  This voice is a little robot like sounding but it is quick and does not require internet or other resources to convert text to speach.  The Google voice (see below) is far better quality but I do use espeak for most of my Raspberry Pi's voice because it will not always be connected to the internet.

Install the espeak package.

# apt-get install espeak


Google Speech

Google's speech engine is a little different to Espeak.  You test that your want spoken gets sent to Google's servers to generate the speech file that is then sent back to your Raspberry Pi and played using mplayer. This means that you will need an internet connection for it to work correctly, but the speech quality is excellent. I would like to use Google's speech all the time because of the excellent quality but I usually end up using Espeak because I don't always have internet.

 I created a script that I call when I want the system to say something.  In this script I check to see if the internet is up and working.  If there is an internet connection, then it will use the Google Speech API for talking and if not then it used ESpeak.  This works will but it does give your Pi two different voices. You may want to change this depending on your preference.

Here is the script I wrote to do the talking. I wrote the script to the file /usr/local/sbin/say on my Raspberry. You can use the script by adding text as the argument to the script for what you want it to say.

# This script will play the text given to it on the command line.
# Usage: ./say "This is a test"


say() {
    local IFS=+;/usr/bin/mplayer -ao alsa -really-quiet -noconsolecontrols "$*";

    while [ -r ${PIDFILE} ]; do
        sleep 1
        if [ $n -gt 30 ]; then
            /usr/bin/logger -t $0 "Giving Up. Possable stale lock file."
            exit 0

    echo $$ > ${PIDFILE}

    # Check for internet connectivity.
    ping -q -w 1 -n -c 1 >/dev/null 2>/dev/null
    if [ $? -eq 0 ]; then
        # If there is Internet then use Google's API
        say $*
        /usr/bin/espeak -ven-us -k5 -s130 "$*" 2>/dev/null
    rm -f ${PIDFILE}


exit 0

The above script works quite well.  I do get some pop sounds before it talks and after it talks.  I suspect it is when it turns on and off the audio device.  Leave me a comment if you know how to get rid of this.

Fun things you can now do.

As I stated at the top of this article, I want to run my Raspberry Pi without a monitor so there are a couple of things I want it to tell me.  If it can talk to me and tell me when it is booting and what it's IP address is, then I can use this to know when and how to remotly connect to it.  This first part of that is to get it to say something on startup and shutdown.  This can be done with a simple init script.

Copy the following script into the file /etc/init.d/talk2me

#!/bin/sh -e
# Init script for talking on bootup and shutdown.
# Written by Kirk Davis <>;

# Make sure we only run one of these at a time.

case "$1" in
        /usr/local/sbin/say "Welcome to Raspberry Pi."
        /usr/local/sbin/say "System going down. Bye, Bye."
        echo "Usage: $0 {start|stop}"
        exit 1

exit 0

Where you boot up or shutdown your system, it should now let you know.

I will add more cool things that you can do with a talking Raspberry Pi to the end of this article as get a chance.  Leave me a comment if you think of other cool ideas!