Saturday, March 1, 2014

Speech synthesis in Python with pyttsx

By Vasudev Ram

[ Click on the link below to hear a synthesized voice:

pyttsx is a library for cross-platform speech synthesis (a.k.a. text-to-speech) in Python. I saw it via this article on Dr. Dobbs Journal:

Voice Throwing with Python and Android.

I have an interest in speech synthesis and have tried out some tools for it in the past.

So I downloaded and installed pyttsx, and then tried it out a bit. Had to tweak the speech rate a bit, since the default "voice" was talking a bit too fast. Also, although going by its API, pyttsx seems to support more than one voice (if your underlying text-to-speech API supports that), the program only played the words in one voice (that of a woman), even though I ran it in a loop for all the voices. It could be that my OS doesn't support more than one voice, or maybe I need to use some other pyttsx option.

Here is the test program using pyttsx,
import pyttsx
engine = pyttsx.init()
engine.setProperty('rate', 70)

voices = engine.getProperty('voices')
for voice in voices:
    print "Using voice:", repr(voice)
    engine.say("Hi there, how's you ?")
    engine.say("A B C D E F G H I J K L M")
    engine.say("N O P Q R S T U V W X Y Z")
    engine.say("0 1 2 3 4 5 6 7 8 9")
    engine.say("Sunday Monday Tuesday Wednesday Thursday Friday Saturday")
    engine.say("Violet Indigo Blue Green Yellow Orange Red")
    engine.say("Apple Banana Cherry Date Guava")

You can run it with the command:

- Vasudev Ram - Dancing Bison Enterprises

Contact Page

No comments: