Reputation:
I am using the pyttsx3 module for text-to-speech in for an ai python assistant, but I am not able to choose a male/female option for voices. I read the documentation given on https://pypi.org/project/pyttsx3/ where it says use voices[0].id/voices[1].id for male and female voices respectively. However, that does not seem to work as there is no significant difference between the 2 voices.
this is my code:
import pyttsx3
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)
engine.say("Hello World!")
engine.runAndWait()
also is there any way to configure the language of the voice?
Upvotes: 1
Views: 1326
Reputation: 1227
Actually, the documentation is not updated and correct for all operating systems.
First, check if there is a female voice among the available voices
:
import pyttsx3
engine = pyttsx3.init()
voices = engine.getProperty("voices")
for voice in voices:
print(voice)
<Voice id=afrikaans
name=afrikaans
languages=[b'\x05af']
gender=male
age=None>
<Voice id=aragonese
name=aragonese
languages=[b'\x05an']
gender=male
age=None>
<Voice id=bulgarian
name=bulgarian
languages=[b'\x05bg']
gender=None
age=None>
...
I couldn't find the female voice myself. If you are using Linux/espeak, it seems that there are no specific female voices. However, you can simulate them using +f1
to +f4
intonations. The following code loops through all available combinations to help you find a suitable sample:
import pyttsx3
engine = pyttsx3.init()
voices = engine.getProperty("voices")
for voice in voices:
for intonation in ["+f1", "+f2", "+f3", "+f4"]:
print(voice.id, intonation)
engine.setProperty('voice', voice.id + intonation)
engine.say("Hello World!")
engine.runAndWait()
input("Press enter to continue ...")
Upvotes: 0