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

baidu_voice.py 1.1KB

123456789101112131415161718192021222324252627282930313233343536
  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. return fileName
  29. else:
  30. logger.error('[Baidu] textToVoice error={}'.format(result))
  31. return None