Tooling Tuesday - gTTS

Another Tuesday and another tool to add to your Python toolkit.

This time we take a look at a Python library that can provide exceptional text to speech. I used this library in Linux Format issue 222 to create an "Information Butler" powered by Raspberry Pi, which retrieved weather and news headlines from BBC news just by waving my hand over the box.

So what is it?

gTTS, Google Text-To-Speech is a Python library from Pierre-Nick Durette that converts a string into an audio file. It does not automatically speak, rather it creates an mp3 file that we can then play.

So how can I install it?

On Mac and Linux

sudo pip3 install gtts

On Windows

pip3.exe install gtts

You will also need to install the VLC app, which can be found via their website for Windows users. but for Linux users it is best to install using your package manager / software centre.

So how can I use it?

The most basic test is to say hello world! So in your favourite Python3 editor, write the following code.

from gtts import gTTS
tts = gTTS("hello world")
tts.save("hello_world.mp3")

This code will import the gTTS library, then we create an object called tts which will store the string that we wish to "speak". The final line saves the string as an audio file called hello_world.mp3 in the same directory as where the code is ran.

Is that it?

Well no as we can also provide an argument when using the library, that will change the accent and language of the speaker. Note: This does not translate the text from say English to French, rather you get a French voice reading English text. But it is different for numbers, for example the time, as it will read the text in the chosen language.

To use another accent / language, we pass the language as an argument when calling the object.

So before we did this

tts = gTTS("hello world")

But to have the text read in an American accent we use

tts = gTTS("hello world", lang="en-us")

Quick project

This is a remix of my inputs blog post and of my VLC for Python post.

Note: The input library requires running as root / sudo on Linux as it needs to access the keyboard directly. So I ran the code in a terminal, rather than the IDLE Python shell.

The goal of the project is to speak the time when the user presses the space bar. So let's start by importing the libraries needed.

  • gTTS for the Google Text-To-Speech.
  • get_key from inputs for detecting when a key is pressed.
  • gmtime, strftime to get the current time and format so that we can read it.
  • vlc to play the audio file.
from gtts import gTTS
from inputs import get_key
from time import gmtime, strftime
import vlc

To continuously run the code we need a loop, and for this basic test while True will do the job. The first line of code in the loop will store the key presses into a variable called events

while True:
    events = get_key()

Still inside the loop and we next create a variable called current_time which stores the current time, but only the Hours and Minutes %H:%M.

    current_time = strftime("%H:%M", gmtime())

So now we use a for loop to check for a key press event, i.e we press a key! If the event is KEY_SPACE which is how inputs refers to the space bar, and the event.state == 1 which means the key has been pressed, then it activates the code indented below it.

    for event in events:
        if event.code == "KEY_SPACE" and event.state == 1:

So inside the for loop and we start by printing the current time to the Python shell, for debug.

            print(current_time)

Then we use gTTS to read the current time in a UK voice / accent (in the video I change it to American, UK, Italian and French.)

            tts = gTTS(current_time,lang="en-uk")

Then I save the audio to my home directory.

            tts.save("/home/les/current_time.mp3")

The next step is to tell VLC where to the audio file is, and we save that information to an object called media.

            media = vlc.MediaPlayer("/home/les/current_time.mp3")

The last step is to play the media object.

            media.play()

Complete Code Listing

from gtts import gTTS
from inputs import get_key
from time import gmtime, strftime
import vlc


while True:
    events = get_key()
    current_time = strftime("%H:%M", gmtime())
    for event in events:
        if event.code == "KEY_SPACE" and event.state == 1:
            print(current_time)
            tts = gTTS(current_time,lang="en-uk")
            tts.save("/home/les/current_time.mp3")
            media = vlc.MediaPlayer("/home/les/current_time.mp3")
            media.play()

For Linux users, you will need to run the code from the Terminal using sudo. Windows users can run from their Python editor.

There we go!

So that was gTTS an interesting tool for your Python toolkit