Making the Raspberry Pi Speak

This is a short post on getting audio setup on my Raspberry Pi and then making it speak.
For the audio setup, I followed instructions on this website:
http://cagewebdev.com/index.php/raspberry-pi-getting-audio-working/
After the above steps, the first time I plugged in a pair of powered speakers to the Pi, I got a whole bunch of “journal” errors from the kernel. Luckily, they went away after a reboot. I have to live with the fact that installing stuff on Linux will always be a “transcendental” experience for me. 😉
To test audio, you can try:
aplay /usr/share/sounds/alsa/*
Once you are happy with this, the next step is to install pyttsx, which is a Python text-to-speech library. You can install it as follows:
wget https://pypi.python.org/packages/source/p/pyttsx/pyttsx-1.1.tar.gz
gunzip pyttsx-1.1.tar.gz
tar -xf pyttsx-1.1.tar
cd pyttsx-1.1/
sudo python setup.py install
In addition to the above, I also needed to install espeak, which I did as follows:
sudo apt-get install espeak
Now, to get some quality speech out of our Pi. Try the Python code below:
###############################################################################
# speech-test.py
#
# Author: electronut.in
#
# Description:
#
# Testing Raspberry Pi audio using pyttsx - Python Cross-platform
# text-to-speech wrapper
#
# test run:
#
# python speech-test.py "hello there"
###############################################################################
import sys
import pyttsx
# main() function
def main():
# use sys.argv if needed
print 'running speech-test.py...'
engine = pyttsx.init()
str = "I speak. Therefore. I am. "
if len(sys.argv) > 1:
str = sys.argv[1]
engine.say(str)
engine.runAndWait()
# call main
if __name__ == '__main__':
main()
