Want the Pi to do be able to do text to speech (tts) from the command line? Easy…
Eventually we’re going to have sound files (MP3) to play so I’ve installed mpg321 to do that.
Tell the Pi to use the 3.5mm audio out:
sudo amixer cset numid=3 1
For the conversion of text to speech, there are a few different options. for now I’m just gonna take advantage of a Google API call to their translate service which has a slightly hidden TTS built in. Obviously this will only work if you have a network connection. If not you’re gonna need something like eSpeak or Festival.
The call is as simple as:
For UK users that gives a male voice despite the .com address. I believe US users get a female voice. To get that in the UK, it’s possible to make the request through Google Mexico…
So to use that to say something it’s just:
wget -U Mozilla "http://translate.google.com.mx/translate_tts?tl=en&q=hello+world" -O helloworld.mp3
(Note we need to persuade Google we’re a web browser so we add a User-Agent)
As a bonus, an easy way to achieve this without the request to Google is to get flite (based on Festival above).
sudo apt-get install flite
…and then use -t to get it to speak strings…
flite -t “hello jon. how are you today?”
(a female voice is available using the -voice option: flite -voice slt -t “hello jon. how are you today?”)Tags: raspberry pi