Aldair - OH
Aldair - OH

Reputation: 66

Pyttsx Voice Gender

Good afternoon, I'm learning and using pyttsx for speech, the thing is that I want to use it as a "female" voice but I can not do it using this code:

import pyttsx as pt
from pyttsx import voice
engine = pt.init()
voices = engine.getProperty('voices')
#engine.setProperty('gender', 'female') # also does not work
engine.setProperty('female', voice.Voice.gender) #not even
engine.setProperty('female', voice.gender) #does not work
engine.setProperty('voice', voices[4].id)
engine.say("Hello World")
engine.runAndWait()


class Voice(object):
    def __init__(self, id, name=None, languages=[], gender=None, age=None):
        self.id = id
        self.name = name
        self.languages = languages
        self.gender = gender
        self.age = age

Upvotes: 2

Views: 20521

Answers (7)

sergej
sergej

Reputation: 1227

I use this helper function, that iterates over the voices. If a voice for a specific language and gender exists, it will change to this voice otherwise an exception will be raised.

# language  : en_US, de_DE, ...
# gender    : VoiceGenderFemale, VoiceGenderMale
def change_voice(engine, language, gender='VoiceGenderFemale'):
    for voice in engine.getProperty('voices'):
        if language in voice.languages and gender == voice.gender:
            engine.setProperty('voice', voice.id)
            return True

    raise RuntimeError("Language '{}' for gender '{}' not found".format(language, gender))

And finally it will be used like this:

engine = pt.init()
change_voice(engine, "en_US", "VoiceGenderFemale")
engine.say("Hello World")
engine.runAndWait()

Upvotes: 0

Khushi Pitroda
Khushi Pitroda

Reputation: 1

You have to check whether your computer has other narrator options or not for that go to control panel -> Ease of Access center -> Narrator options there will be option how many narrators you have. other option go to windows search for narrator. if you don't have other narrator that you have written in code, default narrator will work only.

Upvotes: 0

Chaitanya Lakhchaura
Chaitanya Lakhchaura

Reputation: 55

Use sound=getProperty ('voices'); engine.setProperty('voice','sound [1].id') This will definitely work. 0 for male and 1 for female.

Upvotes: 1

ABHAY
ABHAY

Reputation: 1

import pyttsx as pt
from pyttsx import voice
engine = pt.init()
voices = engine.getProperty('voices')
#engine.setProperty('gender', 'female') # also does not work
#engine.setProperty('female', voice.Voice.gender) #not even
#engine.setProperty('female', voice.gender) #does not work
engine.setProperty('voice', voices[1].id)
engine.say("Hello World")
engine.runAndWait

Upvotes: -1

Huzefa Mandviwala
Huzefa Mandviwala

Reputation: 112

This is a simpler solution:

engine = pyttsx.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)

Upvotes: 3

user3041896
user3041896

Reputation: 51

if you use linux/espeak...

change the code here engine.setProperty('voice', 'english+f1')

you can change the voice by adding f1 until f4

Upvotes: 5

Shirley Singleton
Shirley Singleton

Reputation: 51

I used the following code to iterate through the voices to find the female voice

import pyttsx
engine = pyttsx.init()
voices = engine.getProperty('voices')
for voice in voices:
   engine.setProperty('voice', voice.id)
   print voice.id
   engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait()

On my Windows 10 machine the female voice was HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_ZIRA_11.0

So I changed my code to look like this

import pyttsx
engine = pyttsx.init()
engine.setProperty('voice', 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_ZIRA_11.0')
engine.say('The quick brown fox jumped over the lazy dog.')
engine.runAndWait() 

Upvotes: 5

Related Questions