问题描述
我创建了一个基本的虚拟助手。它有效,但听起来很糟糕,就像你从诈骗电话中听到的机器人声音。我为此使用的软件包是 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
但它不起作用。声音还是一样。有什么办法可以改变吗?
解决方法
您可以使用 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 为男声