[voice] using baidu service to gen reply voice

This commit is contained in:
wanggang
2023-03-08 15:22:46 +08:00
parent d7a8854fa1
commit 3db452ef71
6 changed files with 48 additions and 17 deletions

View File

@@ -2,7 +2,10 @@
"""
baidu voice service
"""
import time
from aip import AipSpeech
from common.log import logger
from common.tmp_dir import TmpDir
from voice.voice import Voice
from config import conf
@@ -19,4 +22,15 @@ class BaiduVoice(Voice):
pass
def textToVoice(self, text):
pass
result = self.client.synthesis(text, 'zh', 1, {
'spd': 5, 'pit': 5, 'vol': 5, 'per': 111
})
if not isinstance(result, dict):
fileName = TmpDir().path() + '语音回复_' + str(int(time.time())) + '.mp3'
with open(fileName, 'wb') as f:
f.write(result)
logger.info('[Baidu] textToVoice text={} voice file name={}'.format(text, fileName))
return fileName
else:
logger.error('[Baidu] textToVoice error={}'.format(result))
return None

View File

@@ -9,6 +9,7 @@ import time
import speech_recognition
import pyttsx3
from common.log import logger
from common.tmp_dir import TmpDir
from voice.voice import Voice
@@ -42,7 +43,7 @@ class GoogleVoice(Voice):
return "抱歉,无法连接到 Google 语音识别服务;{0}".format(e)
def textToVoice(self, text):
textFile = self.tmpFilePath + '语音回复_' + str(int(time.time())) + '.mp3'
textFile = TmpDir().path() + '语音回复_' + str(int(time.time())) + '.mp3'
self.engine.save_to_file(text, textFile)
self.engine.runAndWait()
logger.info(