Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

78 lines
3.3KB

  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. import json
  11. import os
  12. import time
  13. from bridge.reply import Reply, ReplyType
  14. from common.log import logger
  15. from common.tmp_dir import TmpDir
  16. from config import conf
  17. from voice.voice import Voice
  18. from .xunfei_asr import xunfei_asr
  19. from .xunfei_tts import xunfei_tts
  20. from voice.audio_convert import any_to_mp3
  21. import shutil
  22. from pydub import AudioSegment
  23. class XunfeiVoice(Voice):
  24. def __init__(self):
  25. try:
  26. curdir = os.path.dirname(__file__)
  27. config_path = os.path.join(curdir, "config.json")
  28. conf = None
  29. with open(config_path, "r") as fr:
  30. conf = json.load(fr)
  31. print(conf)
  32. self.APPID = str(conf.get("APPID"))
  33. self.APIKey = str(conf.get("APIKey"))
  34. self.APISecret = str(conf.get("APISecret"))
  35. self.BusinessArgsTTS = conf.get("BusinessArgsTTS")
  36. self.BusinessArgsASR= conf.get("BusinessArgsASR")
  37. except Exception as e:
  38. logger.warn("XunfeiVoice init failed: %s, ignore " % e)
  39. def voiceToText(self, voice_file):
  40. # 识别本地文件
  41. try:
  42. logger.debug("[Xunfei] voice file name={}".format(voice_file))
  43. #print("voice_file===========",voice_file)
  44. #print("voice_file_type===========",type(voice_file))
  45. #mp3_name, file_extension = os.path.splitext(voice_file)
  46. #mp3_file = mp3_name + ".mp3"
  47. #pcm_data=get_pcm_from_wav(voice_file)
  48. #mp3_name, file_extension = os.path.splitext(voice_file)
  49. #AudioSegment.from_wav(voice_file).export(mp3_file, format="mp3")
  50. #shutil.copy2(voice_file, 'tmp/test1.wav')
  51. #shutil.copy2(mp3_file, 'tmp/test1.mp3')
  52. #print("voice and mp3 file",voice_file,mp3_file)
  53. text = xunfei_asr(self.APPID,self.APISecret,self.APIKey,self.BusinessArgsASR,voice_file)
  54. logger.info("讯飞语音识别到了: {}".format(text))
  55. reply = Reply(ReplyType.TEXT, text)
  56. except Exception as e:
  57. logger.warn("XunfeiVoice init failed: %s, ignore " % e)
  58. reply = Reply(ReplyType.ERROR, "讯飞语音识别出错了;{0}")
  59. return reply
  60. def textToVoice(self, text):
  61. try:
  62. # Avoid the same filename under multithreading
  63. fileName = TmpDir().path() + "reply-" + str(int(time.time())) + "-" + str(hash(text) & 0x7FFFFFFF) + ".mp3"
  64. return_file = xunfei_tts(self.APPID,self.APIKey,self.APISecret,self.BusinessArgsTTS,text,fileName)
  65. logger.info("[Xunfei] textToVoice text={} voice file name={}".format(text, fileName))
  66. reply = Reply(ReplyType.VOICE, fileName)
  67. except Exception as e:
  68. logger.error("[Xunfei] textToVoice error={}".format(fileName))
  69. reply = Reply(ReplyType.ERROR, "抱歉,讯飞语音合成失败")
  70. return reply