【问题标题】:Changing the voice with PYTTSX module in python在 python 中使用 PYTTSX 模块改变声音
【发布时间】:2020-04-23 07:39:47
【问题描述】:

在 python 中使用 Pyttsx 模块时,如何更改播放文本时使用的语音 ID?

所提供的文档说明了如何循环显示所有可用的声音,但没有明确说明如何选择特定的声音。

【问题讨论】:

    标签: python voice


    【解决方案1】:
    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()
    

    【讨论】:

    • 如何添加新声音? b/c 它只给了我 3 个选项
    【解决方案2】:

    嗯,你应该使用engine.setProperty('voice', voice_id)voice_id 是系统中语音的 ID;你可以从engine.getProperty('voices') 获取可用语音列表),如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()
    

    您不必循环,您可以在没有for 循环的情况下设置语音ID。
    就这样做吧:

    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.')
    

    【讨论】:

    • 谢谢,这是我试过的,但是所有不同的语音 ID 的声音都完全一样,想知道我是否遗漏了一些明显的东西。
    • 如何添加新声音? b/c 它只给了我 3 个选项
    【解决方案3】:

    这里是一个pyttsx3模块使用示例:

    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')
    

    请参阅docs 了解更多信息。

    【讨论】:

    • 不同PC上的声音不同。在您的情况下,voice[0] 是男性,voice[1] 是女性,但是在另一台 PC 上,可以有更多声音,它们可以是男性/女性等(例如,在我的 PC 上两个声音都是女性)。
    猜你喜欢
    • 2018-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-19
    • 2018-01-06
    • 1970-01-01
    相关资源
    最近更新 更多