Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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