Python pyttsx: Changing speech rate and volume

The objective of this post is to explain how to change the speech rate and volume on the pyttsx module.


Introduction

The objective of this post is to explain how to change the speech rate and volume on the pyttsx module. If you need help installing the module, please check this previous “Hello World” tutorial.

If you ran the mentioned “Hello World” program, which uses the default values for the speech rate and volume, you may have noticed that the speech is a little bit fast (although the volume is probably fine). So, we will check how to change these properties.


The code

The first portion of the code is similar to the “Hello World” example and consists on importing the pyttsx module and getting an instance of the voice engine.

import pyttsx
voiceEngine = pyttsx.init()

Then, we will get the default values for both the speech rate and the volume. To do so, we will call the getProperty method on the speech engine instance. This method receives as input the name of the property and returns its value [1]. You can check here the available properties.

So, we will get and print the values for the speech rate and volume and print then. We will also get the currently defined voice, although we will not change this property.

rate = voiceEngine.getProperty('rate')
volume = voiceEngine.getProperty('volume')
voice = voiceEngine.getProperty('voice')

print rate
print volume
print voice

Now, to change a property, we just call the setProperty module, which receives as input the name of the property and the value [2]. Note that the rate is an integer which corresponds to the number of words per minute and the volume a floating point between 0 and 1 [1].

You can check the full source code bellow, where we will iterate through multiple voice speech ratings and volumes. Note that we are increasing the speech rate by 50 words per minute in each iteration (starting by 50) until 300 words per minute. Then, we maintaint the speech rate at 125 words per minute and iterate the volume from 0.1 to 1, increasing 0.3 in each iteration. Also, don’t forget to call the runAndWait method in the end of each iteration, so the voice is synthesized.

import pyttsx
voiceEngine = pyttsx.init()

rate = voiceEngine.getProperty('rate')
volume = voiceEngine.getProperty('volume')
voice = voiceEngine.getProperty('voice')

print rate
print volume
print voice

newVoiceRate = 50
while newVoiceRate <= 300:
    voiceEngine.setProperty('rate', newVoiceRate)
    voiceEngine.say('Testing different voice rates.')
    voiceEngine.runAndWait()
    newVoiceRate = newVoiceRate+50

voiceEngine.setProperty('rate', 125)

newVolume = 0.1
while newVolume <= 1:
    voiceEngine.setProperty('volume', newVolume)
    voiceEngine.say('Testing different voice volumes.')
    voiceEngine.runAndWait()
    newVolume = newVolume+0.3


Testing the code

To test the code, just run it on your Python environment. In my case, I usually use IDLE. On the Python shell, you should get an output similar to figure 1, with the default values for the speech rate, volume and voice. Note that the voice will probably be different, depending on the Operating System, which may have different speech engines.

ttsx rate volume and voice default values

 Figure 1 – Default values for speech rate, volume and voice.

Check in the video bellow the result for the speech synthesized.

 

References

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

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

 

Technical details

  • Python version: 2.7.8
  • Pyttsx version: 1.1
Advertisements

4 Replies to “Python pyttsx: Changing speech rate and volume”

    1. Hi! Thank you 🙂

      I haven’t played with changing voices yet, but from what I’ve seen in the documents,you can iterate over the voices supported in your system:
      http://pyttsx.readthedocs.io/en/latest/engine.html#changing-voices

      I’ve just ran that example and it shows me a list of voice synthesizers available. Although none of them has the languages array defined, one of them just happens to have the following description (I’m on Windows 8):
      ” name=Microsoft Helena Desktop – Spanish (Spain)”

      So it seems like each synthesizer has a different voice rather that a single synthesizer having different choices.

      It also seems that we can install new voices if needed:
      https://support.office.com/en-us/article/How-to-download-Text-to-Speech-languages-for-Windows-4c83a8d8-7486-42f7-8e46-2b0fdf753130

      Let me know if it helps 🙂

      Like

      1. No, this unfortunally doesnt work. The new voices, which you can install and use for Windows TTS are not detected by pyttsx3, but work just fine with Windows TTS. I think they have to be added in the registry under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\
        But i did not managed to get them recognized by pyttsx3. Also when you use Speechangine.runandwait() no other actions can be fulfilled in python(like pushing a stop button, which could trigger Speechengine.stop().
        Maybe You have some Ideas.

        Greetings, and thanks for your tutorials(i liked espacially the esp32 server ones)

        Liked by 1 person

        1. Hi!

          Thank you very much for the feedback and for sharing your tests results 🙂

          Unfortunately I also don’t have any new idea to make it work, I’ve not been working with the pyttsx libs in a while.

          My only suggestion is to ask around the GitHub page of the project. If there’s a way of doing it, then someone there will most likely be able to answer.
          https://github.com/nateshmbhat/pyttsx3

          If you find a solution, please let us know since it will be very useful for other readers.

          You’re welcome 🙂 I’m planning on doing a couple more on the ESP32 server, it has many features yet to explore!

          Best regards,
          Nuno Santos

          Like

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s