You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

baidu_voice.py 1.2KB

1 jaar geleden
1234567891011121314151617181920212223242526272829303132333435363738
  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