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

73 行
3.1KB

  1. """
  2. azure voice service
  3. """
  4. import json
  5. import os
  6. import time
  7. import azure.cognitiveservices.speech as speechsdk
  8. from bridge.reply import Reply, ReplyType
  9. from common.log import logger
  10. from common.tmp_dir import TmpDir
  11. from config import conf
  12. from voice.voice import Voice
  13. """
  14. Azure voice
  15. 主目录设置文件中需填写azure_voice_api_key和azure_voice_region
  16. 查看可用的 voice: https://speech.microsoft.com/portal/voicegallery
  17. """
  18. class AzureVoice(Voice):
  19. def __init__(self):
  20. try:
  21. curdir = os.path.dirname(__file__)
  22. config_path = os.path.join(curdir, "config.json")
  23. config = None
  24. if not os.path.exists(config_path): # 如果没有配置文件,创建本地配置文件
  25. config = {
  26. "speech_synthesis_voice_name": "zh-CN-XiaoxiaoNeural",
  27. "speech_recognition_language": "zh-CN",
  28. }
  29. with open(config_path, "w") as fw:
  30. json.dump(config, fw, indent=4)
  31. else:
  32. with open(config_path, "r") as fr:
  33. config = json.load(fr)
  34. self.api_key = conf().get("azure_voice_api_key")
  35. self.api_region = conf().get("azure_voice_region")
  36. self.speech_config = speechsdk.SpeechConfig(subscription=self.api_key, region=self.api_region)
  37. self.speech_config.speech_synthesis_voice_name = config["speech_synthesis_voice_name"]
  38. self.speech_config.speech_recognition_language = config["speech_recognition_language"]
  39. except Exception as e:
  40. logger.warn("AzureVoice init failed: %s, ignore " % e)
  41. def voiceToText(self, voice_file):
  42. audio_config = speechsdk.AudioConfig(filename=voice_file)
  43. speech_recognizer = speechsdk.SpeechRecognizer(speech_config=self.speech_config, audio_config=audio_config)
  44. result = speech_recognizer.recognize_once()
  45. if result.reason == speechsdk.ResultReason.RecognizedSpeech:
  46. logger.info("[Azure] voiceToText voice file name={} text={}".format(voice_file, result.text))
  47. reply = Reply(ReplyType.TEXT, result.text)
  48. else:
  49. logger.error("[Azure] voiceToText error, result={}, canceldetails={}".format(result, result.cancellation_details))
  50. reply = Reply(ReplyType.ERROR, "抱歉,语音识别失败")
  51. return reply
  52. def textToVoice(self, text):
  53. fileName = TmpDir().path() + "reply-" + str(int(time.time())) + ".wav"
  54. audio_config = speechsdk.AudioConfig(filename=fileName)
  55. speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=self.speech_config, audio_config=audio_config)
  56. result = speech_synthesizer.speak_text(text)
  57. if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
  58. logger.info("[Azure] textToVoice text={} voice file name={}".format(text, fileName))
  59. reply = Reply(ReplyType.VOICE, fileName)
  60. else:
  61. logger.error("[Azure] textToVoice error, result={}, canceldetails={}".format(result, result.cancellation_details))
  62. reply = Reply(ReplyType.ERROR, "抱歉,语音合成失败")
  63. return reply