Linux Blog

Add Voice to your Shell Scripts

Filed under: Shell Script Sundays — TheLinuxBlog.com at 3:52 pm on Sunday, October 12, 2008

Text to speech has been around for a number of years. While you may not want to add text to speech to all of your shell scripts you may have a particular script that would be nice to get some audio feedback from. There are a couple of text-to-speech engines that run in the shell, but my favorite is flite. It is really simple to install and use and has a decent set of voices. You can add other voices or compile your own with FestVox.

Here is how to use flite from the shell to directly play the text:

[owen@TheLinuxBlog ~]$ flite -t "Thanks for reading The Linochs Blog"

Notice the misspelling of the word Linux. If flite doesn’t correctly output the words, you can sometimes use phonetics to get a better result.

You can also play text files by using the -f option.

[owen@TheLinuxBlog ~]$ flite -t "Thanks for reading The Linochs Blog"
flite: pcm_params.c:2333: sndrv_pcm_hw_params: Assertion `err >= 0' failed.

If you get a result like above don’t despair. You may be able to google around to fix the problem, or you could use a simple workaround. Try putting an output wave at the end of the command, playing the file and then removing the wave file when you are finished like so:

/usr/bin/flite -t "See you next week" /tmp/play.wav; play /tmp/play.wav; rm /tmp/play.wav

Here are all of flites options:

flite: a small simple speech synthesizer
version: flite-1.3-release October 2005
CMU Copyright 1999-2005, all rights reserved
usage: flite TEXT/FILE [WAVEFILE]
Converts text in TEXTFILE to a waveform in WAVEFILE
If text contains a space the it is treated as a literal
textstring and spoken, and not as a file name
if WAVEFILE is unspecified or “play” the result is
played on the current systems audio device. If WAVEFILE
is “none” the waveform is discarded (good for benchmarking)
Other options must appear before these options
–version Output flite version number
–help Output usage string
-o WAVEFILE Explicitly set output filename
-f TEXTFILE Explicitly set input filename
-t TEXT Explicitly set input textstring
-p PHONES Explicitly set input textstring and synthesize as phones
–set F=V Set feature (guesses type)
-s F=V Set feature (guesses type)
–seti F=V Set int feature
–setf F=V Set float feature
–sets F=V Set string feature
-b Benchmark mode
-l Loop endlessly
-pw Print words
-ps Print segments
-pr RelName Print relation RelName
-v Verbose mode