Python: Speech Synthesis

The objective of this post is to explain how to synthesis a simple speech using Python and the pyttsx module.


Installing the module

We will use pip to install the pyttsx module. Nevertheless, as indicated here, we first need to install the the pywin32-extensions package. Although we can do it using an installer available here, the easiest way is also installing via pip, using the following command:

pip install pypiwin32

Just as a quick explanation, pywin32 provides access to part of the Win32 API [1].

After this installation, we just need to install pyttsx with the following pip command:

pip install pyttsx

If after the installation, upon running an example, you get the error bellow, just restart the Python shell / editor and try to run it again [2].

ImportError: no module named win32api


Hello world program

First, we start by importing the previously installed pyttsx module.

import pyttsx

After the initial import, we call the init function to get an engine instance for the speech synthesis [3]. If we don’t pass any argument as input of the function, it will use the best driver for the operating system we are using [3].

voiceEngine = pyttsx.init()

Then, we call the say method on the engine instance, passing as input the text to be spoken.

voiceEngine.say('Hello World! This is a speech synthesis test.')

Finally, we call the runAndWait method, also on the engine instance, to process the voice commands [4]. Check the full code, already with this call, bellow.

import pyttsx

voiceEngine = pyttsx.init()
voiceEngine.say('Hello World! This is a speech synthesis test.')

voiceEngine.runAndWait()


Testing the code

To test the code, just run it, for example, on IDLE. The string defined in the code should now be synthesized and spoken, as indicated in the video bellow. Please note that different versions of the operating system may result in different voices, depending on the underlying speech synthesis engine. Also, in the video bellow, the voice is kind of robotized, probably due to my screen recording software. On my computer, the output is very clean.


Related content


References

[1] https://pypi.python.org/pypi/pypiwin32

[2] http://stackoverflow.com/questions/21343774/importerror-no-module-named-win32api

[3] https://pyttsx.readthedocs.io/en/latest/engine.html?highlight=init#pyttsx.init

[4] https://pyttsx.readthedocs.io/en/latest/engine.html?highlight=say#pyttsx.engine.Engine.runAndWait


Technical details

  • Python version: 2.7.8
  • Pyttsx version: 1.1
Advertisements
This entry was posted in Python and tagged , , . Bookmark the permalink.

One Response to Python: Speech Synthesis

  1. Pingback: Python pyttsx: Changing speech rate and volume | techtutorialsx

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s