Reputation: 171
When trying to use pyttsx3 I can only use English voices. I would like to be able to use Dutch as well.
I have already installed the text to speech language package in the windows settings menu. But I can still only use the deafaut english voice.
How can I fix this?
Upvotes: 9
Views: 40044
Reputation: 755
The language property "zh" denotes Mandarin Chinese. You can loop through all the voices included in pyttsx3 and at the time of this writing there are 3 supported Chinese voices (on a MacBook): Taiwanese, Hong Kong, and Chinese (mainland).
engine = pyttsx3.init()
voices = engine.getProperty('voices')
for voice in voices:
engine.setProperty('voice', voice.id)
if "zh" in voice.id:
print(voice.id)
I'm using a Mac, so the results may be different for you. But here is my result:
com.apple.voice.compact.zh-TW.Meijia
com.apple.voice.compact.zh-HK.Sinji
com.apple.voice.compact.zh-CN.Tingting
Here is an example code to audibly speak Chinese:
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', "com.apple.voice.compact.zh-CN.Tingting")
engine.say('炒菜的时候,也可以放')
engine.runAndWait()
If you want a Chinese voice, for example, on a Windows computer, it is not included by default. You will have to first manually add one. Then you can run through the first code sample above and find the voice with "zh" in it. I did a test on a Windows machine and the name of the voice.id was "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_ZH-CN_HUIHUI_11.0"
Upvotes: 1
Reputation: 387
Installing another language on Windows is not enough. By default, windows installed new "speakers" are accessible only to windows official programs. You need to give access to use it within python code. This is done by changing couple of registry files(Don't do it without backup or if you are not sure what you are doing). here is a small example of using another language, hebrew in this case. as for a manual how to change the registries, go into machine_buddy.get_all_voices(ack=True) documentation and it's written in the comments.
import wizzi_utils as wu # pip install wizzi_utils
def tts():
# pip install pyttsx3 # needed
machine_buddy = wu.tts.MachineBuddy(rate=150)
all_voices = machine_buddy.get_all_voices(ack=True)
print('\taudio test')
for i, v in enumerate(all_voices):
machine_buddy.change_voice(new_voice_ind=i)
machine_buddy.say(text=v.name)
if 'Hebrew' in str(v.name):
t = 'שלום, מה קורה חברים?'
machine_buddy.say(text=t)
return
def main():
tts()
return
if __name__ == '__main__':
wu.main_wrapper(
main_function=main,
seed=42,
ipv4=False,
cuda_off=False,
torch_v=False,
tf_v=False,
cv2_v=False,
with_pip_list=False,
with_profiler=False
)
output:
<Voice id=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_DAVID_11.0
name=Microsoft David Desktop - English (United States)
languages=[]
gender=None
age=None>
<Voice id=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\MSTTS_V110_heIL_Asaf
name=Microsoft Asaf - Hebrew (Israel)
languages=[]
gender=None
age=None>
<Voice id=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_ZIRA_11.0
name=Microsoft Zira Desktop - English (United States)
languages=[]
gender=None
age=None>
audio test
Upvotes: 0
Reputation: 1227
If you want to change a language you need to change to another "voice" that supports your language.
import pyttsx3
engine = pyttsx3.init()
for voice in engine.getProperty('voices'):
print(voice)
engine.setProperty('voice', voice.id)
I personally use this helper function I mentioned here also
# 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, you can use it like this (if language and gender are installed):
import pyttsx3
engine = pyttsx3.init()
change_voice(engine, "nl_BE", "VoiceGenderFemale")
engine.say("Hello World")
engine.runAndWait()
Upvotes: 16