Today we’re going to play around with 4 different ways to make your computer talk. Free text to speech (TTS) options for Linux are not the greatest, but they’re fun to experiment with.
To start it off, let’s talk about Espeak. You can install it with this.
Now try it out.
Pretty terrible isn’t it? lol. You can adjust the sound by changing various options. In this example, we use a different male voice and we adjust the speed and pitch.
There is an equally terrible version of a female voice too.
You can store your text in a variable. This variable will be set until you change it or close the shell.
You can also read text from a text file.
Next up, we’re gonna check out Festival.
You can test it with this.
Sounds quite a bit better, right? Now let’s check out a female voice. Install it with this.
We’re going to set this newly installed female voice to be the default voice for festival. This command will open a file called .festivalrc in your home directory. If it doesn’t already exist, it will create it.
Now add this line to the file and save it.
(set! voice_default voice_cmu_us_slt_arctic_hts)
You can change your default voice back by removing this line from the file or deleting the file entirely. Now test your new default female voice.
You can store your text in a variable.
You can read text from a text file with this.
And if you’d like to convert your text file to .mp3, you can do that with lame. If you do not have lame installed, the terminal will tell you how to install it when you run this command. This will record the voice with the default festival voice.
Moving right along. It’s time for a look at Pico. Start by installing it with this.
And give it a whirl with this command.
This actually creates a temporary .wav file, plays the voice, and then deletes the .wav file. If you would like to keep the .wav file, just remove the last part of that command like so…
You can store your text in a variable.
And read from a text file.
And last but not least, Google TTS. There are some pros and cons with this one. On the plus side, the voice is arguably the best of these 4 and there is no installation necessary. However, you must be online to use this and there is a 100 character limit per playback. If you try to use more than 100 characters, it simply won’t play. Not a big deal if you are using short sentences, but kind of a deal breaker if you need it for more than that. In this first example we add the text directly into the command. If you use this method, you must put hyphens between the words.
You can store your text in a variable. This will not require the hyphens between the words.
And you can read from a text file with this. The text file does not need the hyphens between the words.
You can also use any of these TTS methods to play text that you highlight with your mouse in a web browser, text file, terminal or whatever. The way we achieve this is by creating a small script and setting a keyboard combination to run it.
First we will install xsel. This is what grabs the info from whatever you highlight.
Next, choose the method of TTS you’d like to use from the list below. Copy the snippet from the chosen method and paste it into a bash script. If you don’t know how to make a bash script, check out this tutorial -> Bash Scripting Basics. Make sure your script is executable. Now open your Settings application and click on Devices -> Keyboard and scroll to the bottom to create a keyboard shortcut. Set the path to your script as the command and set up your desired keyboard combo. Once this is complete, you can highlight text just about anywhere, hit the keyboard combo you set, and it will read the text with the TTS method you chose.
Espeak
sayit=$(xsel); espeak -s 125 -v en+f5 "$sayit"
Festival
xsel | festival --tts --pipe
Pico
sayit=$(xsel); pico2wave -w tmp.wav "$sayit" && aplay tmp.wav && rm tmp.wav
Google TTS – Keep in mind that you must be online and there is a 100 character limit.
contents=$(xsel); sayit=$(echo $contents | xxd -plain | tr -d '\n' | sed 's/\(..\)/%\1/g'); mpg123 -q "http://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&q=$sayit&tl=En-us"