Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #####################################################################
  2. # xunfei voice service
  3. # Auth: njnuko
  4. # Email: njnuko@163.com
  5. #
  6. # 要使用本模块, 首先到 xfyun.cn 注册一个开发者账号,
  7. # 之后创建一个新应用, 然后在应用管理的语音识别或者语音合同右边可以查看APPID API Key 和 Secret Key
  8. # 然后在 config.json 中填入这三个值
  9. #
  10. # 配置说明:
  11. # {
  12. # "APPID":"xxx71xxx",
  13. # "APIKey":"xxxx69058exxxxxx", #讯飞xfyun.cn控制台语音合成或者听写界面的APIKey
  14. # "APISecret":"xxxx697f0xxxxxx", #讯飞xfyun.cn控制台语音合成或者听写界面的APIKey
  15. # "BusinessArgsTTS":{"aue": "lame", "sfl": 1, "auf": "audio/L16;rate=16000", "vcn": "xiaoyan", "tte": "utf8"}, #语音合成的参数,具体可以参考xfyun.cn的文档
  16. # "BusinessArgsASR":{"domain": "iat", "language": "zh_cn", "accent": "mandarin", "vad_eos":10000, "dwa": "wpgs"} #语音听写的参数,具体可以参考xfyun.cn的文档
  17. # }
  18. #####################################################################
  19. import json
  20. import os
  21. import time
  22. from bridge.reply import Reply, ReplyType
  23. from common.log import logger
  24. from common.tmp_dir import TmpDir
  25. from config import conf
  26. from voice.voice import Voice
  27. from .xunfei_asr import xunfei_asr
  28. from .xunfei_tts import xunfei_tts
  29. from voice.audio_convert import any_to_mp3
  30. import shutil
  31. from pydub import AudioSegment
  32. class XunfeiVoice(Voice):
  33. def __init__(self):
  34. try:
  35. curdir = os.path.dirname(__file__)
  36. config_path = os.path.join(curdir, "config.json")
  37. conf = None
  38. with open(config_path, "r") as fr:
  39. conf = json.load(fr)
  40. print(conf)
  41. self.APPID = str(conf.get("APPID"))
  42. self.APIKey = str(conf.get("APIKey"))
  43. self.APISecret = str(conf.get("APISecret"))
  44. self.BusinessArgsTTS = conf.get("BusinessArgsTTS")
  45. self.BusinessArgsASR= conf.get("BusinessArgsASR")
  46. except Exception as e:
  47. logger.warn("XunfeiVoice init failed: %s, ignore " % e)
  48. def voiceToText(self, voice_file):
  49. # 识别本地文件
  50. try:
  51. logger.debug("[Xunfei] voice file name={}".format(voice_file))
  52. #print("voice_file===========",voice_file)
  53. #print("voice_file_type===========",type(voice_file))
  54. #mp3_name, file_extension = os.path.splitext(voice_file)
  55. #mp3_file = mp3_name + ".mp3"
  56. #pcm_data=get_pcm_from_wav(voice_file)
  57. #mp3_name, file_extension = os.path.splitext(voice_file)
  58. #AudioSegment.from_wav(voice_file).export(mp3_file, format="mp3")
  59. #shutil.copy2(voice_file, 'tmp/test1.wav')
  60. #shutil.copy2(mp3_file, 'tmp/test1.mp3')
  61. #print("voice and mp3 file",voice_file,mp3_file)
  62. text = xunfei_asr(self.APPID,self.APISecret,self.APIKey,self.BusinessArgsASR,voice_file)
  63. logger.info("讯飞语音识别到了: {}".format(text))
  64. reply = Reply(ReplyType.TEXT, text)
  65. except Exception as e:
  66. logger.warn("XunfeiVoice init failed: %s, ignore " % e)
  67. reply = Reply(ReplyType.ERROR, "讯飞语音识别出错了;{0}")
  68. return reply
  69. def textToVoice(self, text):
  70. try:
  71. # Avoid the same filename under multithreading
  72. fileName = TmpDir().path() + "reply-" + str(int(time.time())) + "-" + str(hash(text) & 0x7FFFFFFF) + ".mp3"
  73. return_file = xunfei_tts(self.APPID,self.APIKey,self.APISecret,self.BusinessArgsTTS,text,fileName)
  74. logger.info("[Xunfei] textToVoice text={} voice file name={}".format(text, fileName))
  75. reply = Reply(ReplyType.VOICE, fileName)
  76. except Exception as e:
  77. logger.error("[Xunfei] textToVoice error={}".format(fileName))
  78. reply = Reply(ReplyType.ERROR, "抱歉,讯飞语音合成失败")
  79. return reply