The objective of this post is to explain how to change the speech rate and volume on the pyttsx module.
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 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 . 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 . 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 .
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.
Figure 1 – Default values for speech rate, volume and voice.
Check in the video bellow the result for the speech synthesized.
- Python version: 2.7.8
- Pyttsx version: 1.1