1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- import os
- import azure.cognitiveservices.speech as speechsdk
- import win32clipboard as w
- def get_clipboard_text():
- w.OpenClipboard()
- try:
- text = w.GetClipboardData()
- except TypeError:
- text = ""
- w.CloseClipboard()
- return text
- class SPEECH:
- def __init__(self):
-
-
- self.output_file = "r:\\output.wav"
-
- self.speech_config = speechsdk.SpeechConfig(subscription=os.environ.get('SPEECH_KEY'),
- region=os.environ.get('SPEECH_REGION'))
-
- self.speech_config.speech_synthesis_language = "zh-CN"
-
- self.speech_config.speech_synthesis_voice_name = 'zh-CN-XiaoxiaoNeural'
- def speak_txt(self):
-
- audio_config = speechsdk.audio.AudioOutputConfig(use_default_speaker=True)
- return audio_config
- def down_audio(self):
-
- audio_config = speechsdk.audio.AudioOutputConfig(filename=self.output_file)
- return audio_config
- def run(self, text, audio_config):
-
- speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=self.speech_config, audio_config=audio_config)
-
- speech_synthesis_result = speech_synthesizer.speak_text_async(text).get()
-
- if speech_synthesis_result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
- print("Speech synthesized for text [{}]".format(text))
- elif speech_synthesis_result.reason == speechsdk.ResultReason.Canceled:
- cancellation_details = speech_synthesis_result.cancellation_details
- print("Speech synthesis canceled: {}".format(cancellation_details.reason))
- if cancellation_details.reason == speechsdk.CancellationReason.Error:
- if cancellation_details.error_details:
- print("Error details: {}".format(cancellation_details.error_details))
- print("Did you set the speech resource key and region values?")
- if __name__ == "__main__":
- my_speech = SPEECH()
-
- text = get_clipboard_text()
-
- if not text:
- print("剪贴板没有内容,请复制一些文本。")
- else:
-
-
- print(text)
-
- my_speech.run(text, my_speech.speak_txt())
|