59 lines
2.0KB

  1. """
  2. google voice service
  3. """
  4. import pathlib
  5. import subprocess
  6. import time
  7. from bridge.reply import Reply, ReplyType
  8. import speech_recognition
  9. import pyttsx3
  10. from common.log import logger
  11. from common.tmp_dir import TmpDir
  12. from voice.voice import Voice
  13. class GoogleVoice(Voice):
  14. recognizer = speech_recognition.Recognizer()
  15. engine = pyttsx3.init()
  16. def __init__(self):
  17. # 语速
  18. self.engine.setProperty('rate', 125)
  19. # 音量
  20. self.engine.setProperty('volume', 1.0)
  21. # 0为男声,1为女声
  22. voices = self.engine.getProperty('voices')
  23. self.engine.setProperty('voice', voices[1].id)
  24. def voiceToText(self, voice_file):
  25. new_file = voice_file.replace('.mp3', '.wav')
  26. subprocess.call('ffmpeg -i ' + voice_file +
  27. ' -acodec pcm_s16le -ac 1 -ar 16000 ' + new_file, shell=True)
  28. with speech_recognition.AudioFile(new_file) as source:
  29. audio = self.recognizer.record(source)
  30. try:
  31. text = self.recognizer.recognize_google(audio, language='zh-CN')
  32. logger.info(
  33. '[Google] voiceToText text={} voice file name={}'.format(text, voice_file))
  34. reply = Reply(ReplyType.TEXT, text)
  35. except speech_recognition.UnknownValueError:
  36. reply = Reply(ReplyType.ERROR, "抱歉,我听不懂")
  37. except speech_recognition.RequestError as e:
  38. reply = Reply(ReplyType.ERROR, "抱歉,无法连接到 Google 语音识别服务;{0}".format(e))
  39. finally:
  40. return reply
  41. def textToVoice(self, text):
  42. try:
  43. textFile = TmpDir().path() + '语音回复_' + str(int(time.time())) + '.mp3'
  44. self.engine.save_to_file(text, textFile)
  45. self.engine.runAndWait()
  46. logger.info(
  47. '[Google] textToVoice text={} voice file name={}'.format(text, textFile))
  48. reply = Reply(ReplyType.VOICE, textFile)
  49. except Exception as e:
  50. reply = Reply(ReplyType.ERROR, str(e))
  51. finally:
  52. return reply