Skip to content

Azure Speech Synthesis in Python

This page was automatically generated by AI; not yet reviewed for accuracy...

The content and code samples on this page were generated by using the ai CLI with customized prompts in this repository.

It's cool, but, it's experimental. 😁

Please review the content and code before using it in your application.

This sample demonstrates how to use Azure Speech Synthesis in a Python application.

main.py
requirements.txt

How to generate this sample
Command
ai dev new text-to-speech --python
Output
AI - Azure AI CLI, Version 1.0.0
Copyright (c) 2024 Microsoft Corporation. All Rights Reserved.

This PUBLIC PREVIEW version may change at any time.
See: https://aka.ms/azure-ai-cli-public-preview

Generating 'text-to-speech' in 'text-to-speech-py' (2 files)...

main.py
requirements.txt

Generating 'text-to-speech' in 'text-to-speech-py' (2 files)... DONE!

main.py

STEP 1: Import necessary modules and retrieve configuration from environment variables.

main.py
from azure.cognitiveservices.speech import SpeechConfig, SpeechSynthesizer, SpeechSynthesisResult, SpeechSynthesisCancellationDetails, CancellationReason, ResultReason
from azure.cognitiveservices.speech.audio import AudioOutputConfig
import os

speech_key = os.environ.get('AZURE_AI_SPEECH_KEY') or "<insert your Speech Service API key here>"
service_region = os.environ.get('AZURE_AI_SPEECH_REGION') or "<insert your Speech Service region here>"
voice_name = 'en-US-AndrewNeural'

STEP 2: Create instances of a speech config and audio config, and set the voice name to use.

main.py
speech_config = SpeechConfig(subscription=speech_key, region=service_region)
speech_config.speech_synthesis_voice_name = voice_name
audio_config = AudioOutputConfig(use_default_speaker=True)

STEP 3: Create the speech synthesizer from the above configuration information.

main.py
speech_synthesizer = SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config)

STEP 4: Get text from the user to synthesize.

main.py
text = input('Enter text: ')

STEP 5: Start speech synthesis, and return after it has completed.

main.py
result = speech_synthesizer.speak_text_async(text).get()

STEP 6: Check the result.

main.py
if result.reason == ResultReason.SynthesizingAudioCompleted:
    print('SYNTHESIZED: {} byte(s)'.format(len(result.audio_data)))
elif result.reason == ResultReason.Canceled:
    cancellation_details = result.cancellation_details
    print('CANCELED: Reason={}'.format(cancellation_details.reason))
    if cancellation_details.reason == CancellationReason.Error:
        print('CANCELED: ErrorDetails={}'.format(cancellation_details.error_details))
        print('CANCELED: Did you update the subscription info?')