Google TTS on Ubuntu

Posted on by

If you‘ve ever wanted to install a TTS (Text to Speech) and have not known how, I propose a simple way to use Google TTS on Linux. The most common is to install an addon on your browser, but have the limitation of not being able to use it in a PDF or a text file. This way you can use it regardless of which program you are using just highlighting the text you want to hear and pressing a key combination of your choice.

 First we will install the say.sh script, you can download it from here or make a clone with git. Before you create a folder for our script in our home.
mkdir script
cd scripts
git clone https://gist.github.com/873364.git

Once downloaded you we will enable execute permissions and move it to /usr/bin/ to run it in the terminal without having to put the path where it is.

cd 873364
chmod 755 say.sh
sudo mv say.sh /usr/bin/say.sh

Before running it we will install all the programs we need, mplayer and wget.

sudo apt-get install mplayer wget
Now we make a first test to confirm that runs the script, when run with a text as argument to the script, this will connect to the service Google TTS and audio downloaded will be played with mplayer .
say.sh "hello world"

Perfect as we can make our console say whatever we want, but to play highlight text in any program have to use xsel. First we will install it.

sudo apt-get install xsel

To test will be enough simply by highlight some text in any program and run it in the terminal xsel, it will return the  highlighted text.

xsel
 Now we have to combine say.sh script with xsel. You can do a test, highlighting a text and running on the terminal the say.sh script:
xsel | ./say.sh

I will create a script with this combination. Open gedit and paste the following text and save it to your home with xsay as name.

#!/bin/bash
xsel | say.sh

Enable execute permissions and change its location to the folder /usr/bin/.

chmod 755 xsay
sudo mv xsay /usr/bin/xsay
In Ubuntu keyboard settings and associate  one combianción key to run xsay.
Associating combination of keys on Ubuntu the keyboard menu to run xsay script.

Associating combination of keys on Ubuntu the keyboard menu to run xsay script.

I have chosen Ctrl+Alt+E, now when you highlight text and press this combination the xsay script will read it. As simple and as I said before it will work not only in the browser but any program that you can highlight text.

By the way if you want another language just have to edit  say.sh script to change LNG parameter. Here you have some examples:
Bulgarian: bg
English: en
French: fr
German: de
Italian: it
Japanese: ja
Portuguese (Portugal): pt-PT
Romanian: ro
Russian: ru
Spanish: es
Swedish: sv
Turkish: tr

Category: Software | Tags: , , , ,
Comments are disabled