How to change voice in pyttsx3 in Python – Male to female

Hello programmers, we will see how to change voice to text using pyttsx3 in Python in this tutorial.

The library pyttsx3 is a text-to-speech conversion library in Python. This library works offline which makes it advantageous over others. This library works for both Python 2 and Python 3.

 

Installation

Using command prompt in your system, install the pyttsx3 library using the following command.

pip install pyttsx3

 

Basic Usage

Below given is an illustration of how to use this library with explanations.

import pyttsx3 #import the library

def textToVoice():
    eng = pyttsx3.init() #initialize an instance
    eng.say("This is a demonstration of how to convert text to voice using pyttsx3 library in python.") #say method for passing text to be spoken
    eng.runAndWait() #run and process the voice command

if __name__ == "__main__":
    textToVoice()

Output

---Expected Voice---

Explanation
On the run of the python file, the textToVoice() function is triggered. We need to import the pyttsx3 library first. Inside the function, an instance is initialized in the variable “eng”. The eng then converts it from text to speech and the compiler tells it on the call of runAndWait() method.

 

Understanding TTS engines

Pyttsx3 is a very easy-to-use module in python which converts the text into speech. This module supports two voices: a female voice, and a male voice.

It includes three TTS(Text-to-Speech) engines:

  • sapi5 – provides the male and female voice in Windows
  • nsss – provides the male and female voice in MAC-OS
  • espeak – provides the male and female voice in every other environment

 

Change voice from male to female in pyttsx3

Illustration of how to convert one form of voice to another with explanations

import pyttsx3 #import the library

def voiceChange():
    eng = pyttsx3.init() #initialize an instance
    voice = eng.getProperty('voices') #get the available voices
    # eng.setProperty('voice', voice[0].id) #set the voice to index 0 for male voice
    eng.setProperty('voice', voice[1].id) #changing voice to index 1 for female voice
    eng.say("This is a demonstration of how to convert index of voice using pyttsx3 library in python.") #say method for passing text to be spoken
    eng.runAndWait() #run and process the voice command

if __name__ == "__main__":
    voiceChange()

Output

---Expected Female Voice---

Explanation
On the run of the python file, the voiceChange() function is invoked. Inside the function, an instance is initialized of the module. In the voices library, we get the available voices, and then using the setProperty() method, we change the voice id accordingly to bring a male or female voice. The say method contains the text to be spoken. The runAndWait() method runs and processes the voice command.

 

Conclusion

There are many other functionalities of this module. We can change the rate of the voice, and volume of the voice and even save the voice to a file.

This library works offline and is very handy in various voice command-based projects.

Leave a Reply

Your email address will not be published.