|
|
@@ -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: |
|
|
|