Building Speech Applications with the Azure Speech SDK
This lesson explains how a lightweight Python application uses the Speech SDK of Azure Speech in Foundry Tools, which provides dedicated recognition and synthesis features that general models do not offer, such as SSML, custom speech and diarization.
Learning outcomes
After completing this lesson, you will be able to:
- Configure the Speech SDK with SpeechConfig and an audio configuration.
- Recognize speech with a SpeechRecognizer.
- Synthesize speech with a SpeechSynthesizer and SSML.
- Identify the other Speech capabilities, such as batch transcription and speech translation.
Configuring the Speech SDK
The Python Speech SDK is the azure-cognitiveservices-speech package, imported as speechsdk. A SpeechConfig object tells the SDK which Foundry resource to use, through its endpoint or region, and how to authenticate, with a key or a Microsoft Entra ID token. The same object sets speech_recognition_language (such as en-US) and speech_synthesis_voice_name (such as en-US-AvaMultilingualNeural). An AudioConfig chooses where audio comes from or goes to: the default microphone or speaker, a WAV file or a stream.
Speech recognition
A SpeechRecognizer combines the speech configuration and an audio configuration. recognize_once_async().get() recognizes a single utterance, up to a pause or a maximum of 15 seconds of audio, and returns a result whose reason is RecognizedSpeech on success and whose text is the transcript. For longer audio, start_continuous_recognition() raises recognizing events with interim results and recognized events with final phrases.
import azure.cognitiveservices.speech as speechsdk
speech_config = speechsdk.SpeechConfig(endpoint=endpoint, subscription=key) # or an Entra ID token
speech_config.speech_recognition_language = "en-US"
audio_config = speechsdk.audio.AudioConfig(filename="voicemail.wav") # or use_default_microphone=True
recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)
result = recognizer.recognize_once_async().get()
if result.reason == speechsdk.ResultReason.RecognizedSpeech:
print("Transcript:", result.text)Recognizing speech with the SDK
Speech synthesis
A SpeechSynthesizer converts text to audio. speak_text_async(text) uses the configured voice; speak_ssml_async(ssml) takes an SSML document that can set the voice, speaking style, rate, pauses and pronunciation. An AudioOutputConfig with a file name saves a WAV file, and the default speaker plays the audio directly. A result reason of SynthesizingAudioCompleted confirms success.
speech_config.speech_synthesis_voice_name = "en-US-AvaMultilingualNeural"
output = speechsdk.audio.AudioOutputConfig(filename="reply.wav")
synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=output)
result = synthesizer.speak_text_async("Your order has shipped and will arrive on Friday.").get()
print(result.reason)A reply that is never spoken
Other Speech capabilities
Azure Speech also offers capabilities that a lightweight application can add as needed.
- Fast and batch transcription REST APIs for recorded files and large archives, with diarization.
- Speech translation with a
TranslationRecognizerthat returns text in several target languages. - Phrase lists and custom speech models for domain vocabulary.
- Voice Live for real-time voice agents, as described in the previous topic.
The speech playgrounds in the Foundry portal let you try recognition, synthesis with different voices and Voice Live before writing code.
Speech SDK classes
Reading a Speech SDK snippet
A developer wants a script to save a spoken greeting as a WAV file. The code creates a SpeechRecognizer with AudioConfig(filename="greeting.wav") and calls recognize_once_async(), but no file is produced.
A recognizer reads audio and produces text, the opposite direction. The script needs a SpeechSynthesizer with AudioOutputConfig(filename="greeting.wav") and a call to speak_text_async() or speak_ssml_async().
Which Speech SDK call recognizes a single utterance?
Show answer
SpeechRecognizer.recognize_once_async(). Long audio uses continuous recognition.
Exam Tips
Remember for the exam
- SpeechConfig identifies the resource and authentication; AudioConfig chooses microphone, speaker, file or stream.
- SpeechRecognizer.recognize_once_async() for one utterance; continuous recognition for long audio.
- SpeechSynthesizer.speak_text_async() or speak_ssml_async() produces audio.