tencent.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # -*- coding: utf-8 -*-
  2. """
  3. 腾讯API
  4. """
  5. from __future__ import division
  6. from ..__load__ import *
  7. from .base import Base
  8. class Tencent(Base):
  9. def __init__(self):
  10. super(self.__class__, self).__init__(True)
  11. def setting(self, appid, secret_id, secret_key, region='ap-guangzhou', voiceType=0, language=1, **args):
  12. self.engine = self.sdk.init(secret_id, secret_key)
  13. self.region = region
  14. self.voiceType = voiceType
  15. self.language = language
  16. def asr(self, fp):
  17. file = convertMp3ToWav(fp)
  18. r = self.engine.ASR(file, 'mp3', '1', self.region)
  19. File.remove(file)
  20. res = json.loads(r)
  21. print(file)
  22. print(res)
  23. if 'Response' in res and 'Result' in res['Response']:
  24. return self.asrLog(1, res['Response']['Result'])
  25. else:
  26. return self.asrLog(2, '')
  27. """
  28. 腾讯的语音合成
  29. region: 服务地域,挑个离自己最近的区域有助于提升速度。
  30. 有效值:https://cloud.tencent.com/document/api/441/17365#.E5.9C.B0.E5.9F.9F.E5.88.97.E8.A1.A8
  31. voiceType:
  32. - 0:女声1,亲和风格(默认)
  33. - 1:男声1,成熟风格
  34. - 2:男声2,成熟风格
  35. language:
  36. - 1: 中文,最大100个汉字(标点符号算一个汉子)
  37. - 2: 英文,最大支持400个字母(标点符号算一个字母)
  38. """
  39. def tts(self, phrase):
  40. result = self.engine.TTS(phrase, self.voiceType, self.language, self.region)
  41. if 'Response' in result and 'Audio' in result['Response']:
  42. audio = result['Response']['Audio']
  43. data = base64.b64decode(audio)
  44. tmpfile = writeTempFile(data, '.mp3')
  45. return self.ttsLog(1, tmpfile)
  46. else:
  47. return self.ttsLog(2, '')