Speech engines with python tutorial


Text To Speech (TTS)
Text To Speech (TTS)
A computer system used to create artificial speech is called a speech synthesizer, and can be implemented in software or hardware products.

A text-to-speech (TTS) system converts normal language text into speech. How can we use speech synthesis in Python?

Pyttsx

Pyttsx is a cross-platform speech (Mac OSX, Windows, and Linux) library. You can set voice metadata such as age, gender, id, language and name. Thee speech engine comes with a large amount of voices.

Text to speech sample:
 

Install with:

sudo pip install pyttsx

Create the code speech1.py

import pyttsx
engine = pyttsx.init()
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()

And execute it with python.

Espeak

eSpeak is a compact open source software speech synthesizer for English and other languages, for Linux and Windows.

Text to speech sample:
 

We can install using:

sudo apt-get install espeak

Create the code speech2.py:

import os
os.system("espeak 'The quick brown fox'")

It is very easy to use, but like pyttsx it sounds very robotic.

gTTS

The gtts module no longer works.

I found a script on Github that uses the Google speech engine. The script comes with many options and does not speak, instead it saves to an mp3. We added a command to play the mp3 automatically:

os.system("mpg321 out.mp3 -quiet")

Run with:

python gtts.py -s 'Python programming example'

The voice is extremely natural. The only disadvantage is that you need to be connected with the Internet when running this script.

Links

You might like: