compatible for voice

This commit is contained in:
lanvent
2023-03-13 00:12:34 +08:00
parent cee57e4ffc
commit 8d2e81815c
6 changed files with 64 additions and 40 deletions

View File

@@ -30,7 +30,8 @@ class BaiduVoice(Voice):
with open(fileName, 'wb') as f:
f.write(result)
logger.info('[Baidu] textToVoice text={} voice file name={}'.format(text, fileName))
return fileName
reply = {"type": "VOICE", "content": fileName}
else:
logger.error('[Baidu] textToVoice error={}'.format(result))
return None
reply = {"type": "ERROR", "content": "抱歉,语音合成失败"}
return reply

View File

@@ -32,20 +32,27 @@ class GoogleVoice(Voice):
' -acodec pcm_s16le -ac 1 -ar 16000 ' + new_file, shell=True)
with speech_recognition.AudioFile(new_file) as source:
audio = self.recognizer.record(source)
reply = {}
try:
text = self.recognizer.recognize_google(audio, language='zh-CN')
logger.info(
'[Google] voiceToText text={} voice file name={}'.format(text, voice_file))
return text
reply = {"type": "TEXT", "content": text}
except speech_recognition.UnknownValueError:
return "抱歉,我听不懂"
reply = {"type": "ERROR", "content": "抱歉,我听不懂"}
except speech_recognition.RequestError as e:
return "抱歉,无法连接到 Google 语音识别服务;{0}".format(e)
reply = {"type": "ERROR", "content": "抱歉,无法连接到 Google 语音识别服务;{0}".format(e)}
finally:
return reply
def textToVoice(self, text):
textFile = TmpDir().path() + '语音回复_' + str(int(time.time())) + '.mp3'
self.engine.save_to_file(text, textFile)
self.engine.runAndWait()
logger.info(
'[Google] textToVoice text={} voice file name={}'.format(text, textFile))
return textFile
try:
textFile = TmpDir().path() + '语音回复_' + str(int(time.time())) + '.mp3'
self.engine.save_to_file(text, textFile)
self.engine.runAndWait()
logger.info(
'[Google] textToVoice text={} voice file name={}'.format(text, textFile))
reply = {"type": "VOICE", "content": textFile}
except Exception as e:
reply = {"type": "ERROR", "content": str(e)}
finally:
return reply

View File

@@ -16,12 +16,18 @@ class OpenaiVoice(Voice):
def voiceToText(self, voice_file):
logger.debug(
'[Openai] voice file name={}'.format(voice_file))
file = open(voice_file, "rb")
reply = openai.Audio.transcribe("whisper-1", file)
text = reply["text"]
logger.info(
'[Openai] voiceToText text={} voice file name={}'.format(text, voice_file))
return text
reply={}
try:
file = open(voice_file, "rb")
result = openai.Audio.transcribe("whisper-1", file)
text = result["text"]
reply = {"type": "TEXT", "content": text}
logger.info(
'[Openai] voiceToText text={} voice file name={}'.format(text, voice_file))
except Exception as e:
reply = {"type": "ERROR", "content": str(e)}
finally:
return reply
def textToVoice(self, text):
pass