首页 > 解决方案 > 来自 gTTS 的机器人声音更少

问题描述

我创建了一个基本的虚拟助手。它有效,但听起来很糟糕,就像你从诈骗电话中得到的机器人声音。我为此使用的软件包是 gTTS,效果很好,但声音不好听。我能做些什么来改变它吗?

我的脚本中的代码:

tts = gTTS(text=audio_string, lang="en")
r = random.randint(1, 10000000)
audio_file = "audio-" + str(r) + ".mp3"
tts.save(audio_file)
playsound.playsound(audio_file)
print(audio_string)
os.remove(audio_file)

我已经尝试过en-us,或者en-uk它不起作用。声音还是一样的。有没有办法改变它?

标签: pythonpython-3.xvoice-recognitiongtts

解决方案


您可以使用 pyttsx3 模块

import pyttsx3

#Starting microsoft speech api sapi5
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')#fetching different voices from the system
engine.setProperty('voice', voices[1].id)#setting voice properties
engine.setProperty('rate', 130)#sets speed of speech

#enables speech
def speak(audio):
    engine.say(audio)
    engine.runAndWait()

您可以在 engine.setProperty('voice', voices[1].id) 语句中将 1 替换为 0


推荐阅读