英文:
Pyttsx3 voice tone and language configuration
问题
我正在使用 pyttsx3 模块为一个 AI Python 助手进行文本转语音,但我无法选择男声/女声的选项。我阅读了 https://pypi.org/project/pyttsx3/ 上提供的文档,其中说要使用 voices[0].id/voices[1].id 来分别选择男声和女声。然而,这似乎不起作用,因为这两个声音之间没有明显的区别。
这是我的代码:
import pyttsx3
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)
engine.say("Hello World!")
engine.runAndWait()
另外,是否有办法配置声音的语言?
英文:
I am using the pyttsx3 module for text-to-speech in for an ai python assistant, but I am not able to choose a male/female option for voices. I read the documentation given on https://pypi.org/project/pyttsx3/ where it says use voices[0].id/voices[1].id for male and female voices respectively. However, that does not seem to work as there is no significant difference between the 2 voices.
this is my code:
import pyttsx3
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[0].id)
engine.say("Hello World!")
engine.runAndWait()
also is there any way to configure the language of the voice?
答案1
得分: 1
实际上,文档未更新并且对所有操作系统都不正确。
首先,请检查在可用的“voices”中是否有女性声音:
import pyttsx3
engine = pyttsx3.init()
voices = engine.getProperty("voices")
for voice in voices:
print(voice)
<!-- 分隔符 -->
<Voice id=afrikaans
name=afrikaans
languages=[b'\x05af']
gender=male
age=None>
<Voice id=aragonese
name=aragonese
languages=[b'\x05an']
gender=male
age=None>
<Voice id=bulgarian
name=bulgarian
languages=[b'\x05bg']
gender=None
age=None>
...
我自己找不到女性声音。如果您使用的是Linux/espeak,似乎没有特定的女性声音。但是,您可以使用 +f1
到 +f4
的语调来模拟它们。以下代码循环遍历所有可用的组合,以帮助您找到合适的示例:
import pyttsx3
engine = pyttsx3.init()
voices = engine.getProperty("voices")
for voice in voices:
for intonation in ["+f1", "+f2", "+f3", "+f4"]:
print(voice.id, intonation)
engine.setProperty('voice', voice.id + intonation)
engine.say("Hello World!")
engine.runAndWait()
input("Press enter to continue ...")
英文:
Actually, the documentation is not updated and correct for all operating systems.
First, check if there is a female voice among the available voices
:
import pyttsx3
engine = pyttsx3.init()
voices = engine.getProperty("voices")
for voice in voices:
print(voice)
<!-- separator -->
<Voice id=afrikaans
name=afrikaans
languages=[b'\x05af']
gender=male
age=None>
<Voice id=aragonese
name=aragonese
languages=[b'\x05an']
gender=male
age=None>
<Voice id=bulgarian
name=bulgarian
languages=[b'\x05bg']
gender=None
age=None>
...
I couldn't find the female voice myself. If you are using Linux/espeak, it seems that there are no specific female voices. However, you can simulate them using +f1
to +f4
intonations. The following code loops through all available combinations to help you find a suitable sample:
import pyttsx3
engine = pyttsx3.init()
voices = engine.getProperty("voices")
for voice in voices:
for intonation in ["+f1", "+f2", "+f3", "+f4"]:
print(voice.id, intonation)
engine.setProperty('voice', voice.id + intonation)
engine.say("Hello World!")
engine.runAndWait()
input("Press enter to continue ...")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论