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.

38 line
1.2KB

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