How to convert text to speech with Azure AI Speech
Text-to-speech turns written text into natural-sounding audio and is handy for creating video voice-overs, making applications more accessible, or giving a spoken reply in a virtual assistant. With Azure AI Speech and a few lines of Python you can convert text to high-quality speech in dozens of languages without training any model.
Prerequisites
- An Azure subscription with a Speech resource created — note the key and the region (for example,
westeurope). - Python 3.8 or later installed on your machine.
- Basic knowledge of the terminal and Python.
- Speakers or headphones to hear the result.
Step 1: Install the Azure AI Speech SDK
To convert text to speech you will use Microsoft's official library. Open the terminal and install the package with pip:
pip install azure-cognitiveservices-speechThis package includes everything needed to talk to the service. If you use a virtual environment (recommended), activate it before running the command to keep your dependencies tidy.
Step 2: Configure the key and region
The service identifies you through the key and region of your Speech resource. Find both in the Azure portal, on the resource page, under Keys and Endpoint. To avoid leaving secrets in your code, store them in environment variables and read them like this:
import os
import azure.cognitiveservices.speech as speechsdk
speech_key = os.environ["SPEECH_KEY"]
service_region = os.environ["SPEECH_REGION"]
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)The speech_config object holds the configuration you will reuse in the next steps.
Step 3: Choose the voice and language
Azure offers very realistic neural voices in many languages. For European Portuguese you can use pt-PT-RaquelNeural; for Brazilian Portuguese, pt-BR-FranciscaNeural. Just set the voice name in the configuration:
speech_config.speech_synthesis_voice_name = "pt-PT-RaquelNeural"If you don't set a voice, the service uses a default one. Switching voices is as easy as changing this name, so it's worth trying a few until you find the one that sounds best for your case.
Step 4: Generate the audio and save it to a file
Now you will connect the audio output to a WAV file and request the synthesis. The speak_text_async method returns a result object when it finishes:
audio_config = speechsdk.audio.AudioOutputConfig(filename="saida.wav")
synthesizer = speechsdk.SpeechSynthesizer(
speech_config=speech_config,
audio_config=audio_config)
texto = "Olá! Este áudio foi criado com o Azure AI Speech."
result = synthesizer.speak_text_async(texto).get()If you prefer to hear the sound straight from the speakers instead of saving a file, replace the audio_config line with speechsdk.audio.AudioOutputConfig(use_default_speaker=True).
Step 5: Check the status and handle errors
It is good practice to verify that the synthesis finished successfully and, if it fails, show the reason. This saves a lot of time diagnosing key or region problems:
if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
print("Áudio gravado com sucesso em saida.wav")
elif result.reason == speechsdk.ResultReason.Canceled:
details = result.cancellation_details
print("Síntese cancelada:", details.reason)
if details.reason == speechsdk.CancellationReason.Error:
print("Detalhes do erro:", details.error_details)The most common errors are an invalid key or a wrong region; the message in error_details usually tells you exactly what to fix.
Check the result
Run the script and look for the saida.wav file in the same folder. Open it in an audio player and you should hear the sentence read in a natural voice. If you hear the full sentence without cuts, the integration is working. If the file is empty or missing, go back to Step 5 and read the error message — it is almost always the key or the region.
Conclusion
In five steps you connected Python to Azure AI Speech and turned text into natural audio. From here, the next leap is to use SSML to control pauses, intonation, and speaking rate, try voices in other languages, or integrate this function into an API or web app. What will be the first sentence you give a voice to in your project?