Pyttsx3声音音调和语言配置

huangapple go评论122阅读模式
英文:

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(&quot;voices&quot;)

for voice in voices:
    print(voice)

<!-- separator -->

&lt;Voice id=afrikaans
          name=afrikaans
          languages=[b&#39;\x05af&#39;]
          gender=male
          age=None&gt;
&lt;Voice id=aragonese
          name=aragonese
          languages=[b&#39;\x05an&#39;]
          gender=male
          age=None&gt;
&lt;Voice id=bulgarian
          name=bulgarian
          languages=[b&#39;\x05bg&#39;]
          gender=None
          age=None&gt;
...

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(&quot;voices&quot;)

for voice in voices:
    for intonation in [&quot;+f1&quot;, &quot;+f2&quot;, &quot;+f3&quot;, &quot;+f4&quot;]:
        print(voice.id, intonation)
        engine.setProperty(&#39;voice&#39;, voice.id + intonation)

        engine.say(&quot;Hello World!&quot;)
        engine.runAndWait()

        input(&quot;Press enter to continue ...&quot;)

huangapple
  • 本文由 发表于 2023年7月27日 23:31:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76781332.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定