Translation and Text to Speech with Amazon Polly and .NET

Want to learn more about AWS Lambda and .NET? Check out my A Cloud Guru course on ASP.NET Web API and Lambda.

Download full source code.

A few days ago, I wrote a blog post about converting text to speech with Amazon Polly. Using a .NET console application you could type in a sentence, and hear the sentence spoken.

This post builds on that by adding translation. You type your text in English, and it is translated into Spanish, and spoken in the voice of a Spanish speaker.

If you want to learn another language, you can choose the vocabulary you want to learn, and then listen to it spoken in a natural sounding voice.

Prerequisites

An AWS account with the permissions “AmazonPollyReadOnlyAccess”, and “TranslateReadOnly”

The Code

Create a new .NET console application.

dotnet new console -o TranslateAndTextToSpeech

Add three NuGet packages to the project:

dotnet add package AWSSDK.Polly
dotnet add package AWSSDK.Translate
dotnet add package NetCoreAudio

Open the Program.cs file and replace it with the following -

using Amazon.Polly;
using Amazon.Polly.Model;
using Amazon.Translate;
using Amazon.Translate.Model;
using NetCoreAudio;

var translateClient = new AmazonTranslateClient();
var translateRequest = new TranslateTextRequest
{
    SourceLanguageCode = "en",
    TargetLanguageCode = "es",
};

var pollyClient = new AmazonPollyClient();
var pollyRequest = new SynthesizeSpeechRequest
{
    OutputFormat = OutputFormat.Mp3,
    VoiceId = VoiceId.Lucia,
};

var player = new Player();

while (true)
{
    Console.WriteLine("Enter text to translate (CTRL-C to exit):");
    var text = Console.ReadLine();
    translateRequest.Text = text;
    var translateResponse = await translateClient.TranslateTextAsync(translateRequest);
    Console.WriteLine($"Translated text: {translateResponse.TranslatedText}\n");
    await TextToSpeech(translateResponse.TranslatedText); 
}

async Task TextToSpeech(string translatedText)
{
    pollyRequest.Text = translatedText;
    
    var response = await pollyClient.SynthesizeSpeechAsync(pollyRequest);

    using (var fileStream = new FileStream("speech.mp3", FileMode.Create, FileAccess.Write))
    {
        response.AudioStream.CopyTo(fileStream);
    }

    await player.Play("speech.mp3");
}

Run it using dotnet run and enter some text. You should hear the text spoken in Spanish, in the voice of Lucia.

On Linux, you might need to install an audio player. On Ubuntu, you can install mpg123 using sudo apt install mpg123.

That’s it. You are using AI to convert text from English to Spanish, then to spoken Spanish!

Download full source code.

comments powered by Disqus

Related