python logo

Speech engines with python tutorial


Python hosting: Host, run, and code Python in the cloud!
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?

Related courses:

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:

BackNext





Leave a Reply:




Anjan Dudda Wed, 28 Dec 2016

How do i change the language in pyttsx from english to german? It would be great if you can send me some sample code for the same. Thanks in advance.

Frank Sun, 08 Jan 2017

It depends on the driver, see available drivers on: https://github.com/RapidWareTech/pyttsx/tree/1a84ee33971951b1ea18f2708061a5d19ef94018/pyttsx/drivers

If you use Espeak you could try the setProperty method and set 'languages'