您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

39 行
1.2KB

  1. """
  2. baidu voice service
  3. """
  4. import time
  5. from aip import AipSpeech
  6. from bridge.reply import Reply, ReplyType
  7. from common.log import logger
  8. from common.tmp_dir import TmpDir
  9. from voice.voice import Voice
  10. from config import conf
  11. class BaiduVoice(Voice):
  12. APP_ID = conf().get('baidu_app_id')
  13. API_KEY = conf().get('baidu_api_key')
  14. SECRET_KEY = conf().get('baidu_secret_key')
  15. client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
  16. def __init__(self):
  17. pass
  18. def voiceToText(self, voice_file):
  19. pass
  20. def textToVoice(self, text):
  21. result = self.client.synthesis(text, 'zh', 1, {
  22. 'spd': 5, 'pit': 5, 'vol': 5, 'per': 111
  23. })
  24. if not isinstance(result, dict):
  25. fileName = TmpDir().path() + '语音回复_' + str(int(time.time())) + '.mp3'
  26. with open(fileName, 'wb') as f:
  27. f.write(result)
  28. logger.info('[Baidu] textToVoice text={} voice file name={}'.format(text, fileName))
  29. reply = Reply(ReplyType.VOICE, fileName)
  30. else:
  31. logger.error('[Baidu] textToVoice error={}'.format(result))
  32. reply = Reply(ReplyType.ERROR, "抱歉,语音合成失败")
  33. return reply