Changing the voice with PYTTSX module in python
Asked Answered
C

3

1

When using the Pyttsx module within python, how do you change the voice ID that is used when playing out text?

The documentation provided illustrates how to cycle through all the available voices, but does not make clear how to choose a specific one.

Centurial answered 5/2, 2015 at 12:39 Comment(0)
S
7

Uh, you should use engine.setProperty('voice', voice_id) (with voice_id being an ID of the voice in your system; you can grab the list of available voices from engine.getProperty('voices')) as proposed in that example:

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

You don't have to cycle, you can set voice id without a for loop.
Just do it like that:

engine = pyttsx.init()
engine.setProperty('voice', voice_id)  # use whatever voice_id you'd like
engine.say('The quick brown fox jumped over the lazy dog.')
Santoyo answered 5/2, 2015 at 12:41 Comment(2)
Thanks, that was what I had tried, but all the differnet voice ID's sound exactly the same, was wondering if I had missed something obvious.Centurial
How can I add a new voice? b/c it gives me only 3 optionsHulky
N
8
import pyttsx

engine = pyttsx.init()
voices = engine.getProperty('voices')

engine.setProperty('voice', voices[0].id) #change index to change voices
engine.say('I\'m a little teapot...')

engine.runAndWait()
Natalya answered 7/7, 2015 at 0:18 Comment(1)
How can I add a new voice? b/c it gives me only 3 optionsHulky
S
7

Uh, you should use engine.setProperty('voice', voice_id) (with voice_id being an ID of the voice in your system; you can grab the list of available voices from engine.getProperty('voices')) as proposed in that example:

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

You don't have to cycle, you can set voice id without a for loop.
Just do it like that:

engine = pyttsx.init()
engine.setProperty('voice', voice_id)  # use whatever voice_id you'd like
engine.say('The quick brown fox jumped over the lazy dog.')
Santoyo answered 5/2, 2015 at 12:41 Comment(2)
Thanks, that was what I had tried, but all the differnet voice ID's sound exactly the same, was wondering if I had missed something obvious.Centurial
How can I add a new voice? b/c it gives me only 3 optionsHulky
O
1

Here is an example of pyttsx3 module usage:

import pyttsx3
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
def speak(audio):
    engine.say(audio)
    engine.runAndWait()
speak('Hello World')

See the docs for more information.

Octodecimo answered 7/4, 2020 at 12:43 Comment(1)
Voices are different on different PCs. In your case, voice[0] is a male one, and voice[1] is a female one, but on another PC, there can be more voices, they can be both male/female etc. (for example, on my PC both voices are female).Planer

© 2022 - 2024 — McMap. All rights reserved.