From 61865bc40834327dc09d3ccaef9562087ba996ab Mon Sep 17 00:00:00 2001 From: zwssunny Date: Mon, 27 Mar 2023 14:54:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9google=5Fvoice=E4=B8=BAgoogle?= =?UTF-8?q?=E5=90=88=E6=88=90=EF=BC=8C=E8=A7=A3=E5=86=B3=E7=B3=BB=E7=BB=9F?= =?UTF-8?q?=E5=85=BC=E5=AE=B9=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- voice/google/google_voice.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/voice/google/google_voice.py b/voice/google/google_voice.py index 74431db..2cc9851 100644 --- a/voice/google/google_voice.py +++ b/voice/google/google_voice.py @@ -9,9 +9,11 @@ import time from bridge.reply import Reply, ReplyType import speech_recognition import pyttsx3 +from gtts import gTTS from common.log import logger from common.tmp_dir import TmpDir from voice.voice import Voice +from voice.audio_convert import mp3_to_wav class GoogleVoice(Voice): @@ -29,8 +31,9 @@ class GoogleVoice(Voice): def voiceToText(self, voice_file): new_file = voice_file.replace('.mp3', '.wav') - subprocess.call('ffmpeg -i ' + voice_file + - ' -acodec pcm_s16le -ac 1 -ar 16000 ' + new_file, shell=True) + # subprocess.call('ffmpeg -i ' + voice_file + + # ' -acodec pcm_s16le -ac 1 -ar 16000 ' + new_file, shell=True) + mp3_to_wav(voice_file, new_file) with speech_recognition.AudioFile(new_file) as source: audio = self.recognizer.record(source) try: @@ -46,12 +49,14 @@ class GoogleVoice(Voice): return reply def textToVoice(self, text): try: - textFile = TmpDir().path() + '语音回复_' + str(int(time.time())) + '.mp3' - self.engine.save_to_file(text, textFile) - self.engine.runAndWait() + mp3File = TmpDir().path() + '语音回复_' + str(int(time.time())) + '.mp3' + # self.engine.save_to_file(text, textFile) + # self.engine.runAndWait() + tts = gTTS(text=text, lang='zh') + tts.save(mp3File) logger.info( - '[Google] textToVoice text={} voice file name={}'.format(text, textFile)) - reply = Reply(ReplyType.VOICE, textFile) + '[Google] textToVoice text={} voice file name={}'.format(text, mp3File)) + reply = Reply(ReplyType.VOICE, mp3File) except Exception as e: reply = Reply(ReplyType.ERROR, str(e)) finally: