Pyttsx3 语音和语言配置

问题描述 投票:0回答:1

我正在使用 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()

还有什么办法可以配置语音的语言吗?

python audio lib pyttsx3
1个回答
0
投票

实际上,该文档并未针对所有操作系统进行更新和正确。

首先检查可用的声音中是否有女声

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 ...")
© www.soinside.com 2019 - 2024. All rights reserved.