Pi to 100 places with Amazon Polly and .NET
Here is a quick post for Pi day. With .NET and Amazon Polly, it’s very easy to convert text to speech. I’ve created a simple console application that reads Pi to 100 places.
If you want to see a little more about Amazon Polly, check out these other posts.
The code
Add two NuGet packages to the project -
dotnet add package AWSSDK.Polly
dotnet add package NetCoreAudio
The code is very simple.
using Amazon.Polly;
using Amazon.Polly.Model;
using NetCoreAudio;
var pollyClient = new AmazonPollyClient();
var request = new SynthesizeSpeechRequest
{
OutputFormat = OutputFormat.Mp3,
VoiceId = VoiceId.Matthew,
Text = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679"
};
var response = await pollyClient.SynthesizeSpeechAsync(request);
using (var fileStream = new FileStream("pi.mp3", FileMode.Create, FileAccess.Write))
{
response.AudioStream.CopyTo(fileStream);
}
var player = new Player();
await player.Play("pi.mp3");
Console.WriteLine("CTRL-C to exit");
Console.ReadLine();