Skip to content

Text-to-Speech in C#

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 the Azure Text-to-Speech API in a C# console application.

Program.cs
TextToSpeechClass.cs

How to generate this sample
Command
ai dev new text-to-speech --csharp
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-cs' (3 files)...

TextToSpeech.csproj
TextToSpeechClass.cs
Program.cs

Generating 'text-to-speech' in 'text-to-speech-cs' (3 files)... DONE!

Program.cs

STEP 1: Read the configuration settings from environment variables:

Program.cs
var azureAPIKey = Environment.GetEnvironmentVariable("AZURE_API_KEY") ?? "<insert your Azure API key here>";
var azureRegion = Environment.GetEnvironmentVariable("AZURE_REGION") ?? "<insert your Azure region here>";

STEP 2: Initialize the helper class with the configuration settings:

Program.cs
var tts = new TextToSpeechClass(azureAPIKey, azureRegion);

STEP 3: Obtain user input, use the helper class to synthesize speech, and play the audio output:

Program.cs
while (true)
{
    Console.Write("Text to synthesize: ");
    var text = Console.ReadLine();
    if (string.IsNullOrEmpty(text) || text == "exit") break;

    var audioData = await tts.SynthesizeSpeechAsync(text);
    // Code to play audioData
}

TextToSpeechClass.cs

STEP 1: Create the client and initialize the speech synthesis service:

TextToSpeechClass.cs
public TextToSpeechClass(string azureAPIKey, string azureRegion)
{
    var config = SpeechConfig.FromSubscription(azureAPIKey, azureRegion);
    _synthesizer = new SpeechSynthesizer(config);
}

STEP 2: Synthesize speech from text input:

TextToSpeechClass.cs
public async Task<byte[]> SynthesizeSpeechAsync(string text)
{
    using var result = await _synthesizer.SpeakTextAsync(text);
    if (result.Reason == ResultReason.SynthesizingAudioCompleted)
    {
        return result.AudioData;
    }
    else
    {
        throw new Exception("Speech synthesis failed.");
    }
}