nateshmbhat / pyttsx3

Offline Text To Speech synthesis for python
Mozilla Public License 2.0
2.09k stars 330 forks source link

Male Voice Not Working #299

Open Lonehunter16 opened 10 months ago

Lonehunter16 commented 10 months ago

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?""")