My male voice sounds like a weird version of the female voice. Here's my code:
import pyttsx3
import pyttsx3.voice
class VoiceEngine(object):
voiceDict = {"male": 0, "female": 1}
def __init__(self, voice: str="female", speed: int=144):
"""
Initialize a VoiceEngine object.
:param voice: 'male' or 'female'
"""
try:
self.voice = VoiceEngine.voiceDict[voice]
self.speed = speed
except:
self.voice = 1
self.speed = 144
print("Defaulting to female voice due to an error in the input.")
self.engine = pyttsx3.init("sapi5")
voices = self.engine.getProperty("voices")
self.engine.setProperty("voice", voices[self.voice].id)
self.engine.setProperty("rate", self.speed)
def speak(self, text: str):
self.engine.say(text)
self.engine.runAndWait()
def set_speed(self, speed: int):
self.speed = speed
self.engine.setProperty("rate", self.speed)
def set_voice(self, id):
self.voice = id
self.engine.setProperty("voice", self.voice)
if __name__ == '__main__':
voice_engine = VoiceEngine("male")
voice_engine.set_speed(200)
voices = pyttsx3.init()
male_voice = pyttsx3.voice.Voice(2, name="male", languages=["english"], gender="male")
voices = voices.getProperty("voices")
voice_engine.set_voice(2)
voice_engine.speak("""When a doctor doctors a doctor, does the doctor doing the doctoring doctor as the doctor being doctored wants to be doctored or does the doctor doing the doctoring doctor as he wants to doctor?""")
My male voice sounds like a weird version of the female voice. Here's my code: