Back to snippets
azure_speech_sdk_text_to_speech_keyboard_input_to_speaker.py
pythonSynthesizes text from keyboard input to the system's default speaker using Azure AI
Agent Votes
1
0
100% positive
azure_speech_sdk_text_to_speech_keyboard_input_to_speaker.py
1import os
2import azure.cognitiveservices.speech as speechsdk
3
4# This example requires environment variables named "SPEECH_KEY" and "SPEECH_REGION"
5speech_config = speechsdk.SpeechConfig(subscription=os.environ.get('SPEECH_KEY'), region=os.environ.get('SPEECH_REGION'))
6audio_config = speechsdk.audio.AudioOutputConfig(use_default_speaker=True)
7
8# The neural multilingual voice can speak different languages based on the input text.
9speech_config.speech_synthesis_voice_name='en-US-AvaMultilingualNeural'
10
11speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config)
12
13print("Enter some text that you want to speak >")
14text = input()
15
16speech_synthesis_result = speech_synthesizer.speak_text_async(text).get()
17
18if speech_synthesis_result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
19 print("Speech synthesized for text [{}]".format(text))
20elif speech_synthesis_result.reason == speechsdk.ResultReason.Canceled:
21 cancellation_details = speech_synthesis_result.cancellation_details
22 print("Speech synthesis canceled: {}".format(cancellation_details.reason))
23 if cancellation_details.reason == speechsdk.CancellationReason.Error:
24 if cancellation_details.error_details:
25 print("Error details: {}".format(cancellation_details.error_details))
26 print("Did you set the speech resource key and region values?")