Google Cloud Text to Speech in C#

Google has just released, in beta, a service on Google Cloud Platform to convert text into audio data, you can use Google Cloud Text to Speech in C# easily.

The audio rendering is much better than the SpeechSynthesizer of the .Net Framework.

For information, there is the same service on Azure in beta too.

Enabling the Cloud Text-to-Speech API

You must first enable the API on your Google Cloud Platform account.

Go to your Google Cloud Platform account and click on “Dashboard” in the “API and Services” section.

Then click on “Enable APIs and Services”

Look for the “Cloud Text-to-Speech” API.

Click on it to continue.

Click on the “Activate” button.

The API is now active on your Google Cloud account.

Google Cloud Platform Authentication

We will now manage authentication on Google Cloud Platform.

Go to your Google Cloud Platform account and click on “Identifiers” in the “API and Services” section.

In the “Service Account Keys” section on the screen that appears, click “Manage Service Accounts.”

If you do not have one, create a service account for your application

Finally, in the “Actions” section, click on “Create a key”.

Click “Create” to generate your private key. Carefully keep the JSON file containing your private key, whose name is in the format: “e0173.json”.

Copy this file to a directory, for example: “C:\Credentials”

Environment variable

The GOOGLE_APPLICATION_CREDENTIALS environment variable must contain the path to the previously generated private key.

Example: “C: \ Credentials \ softfluent-127924-89b6632e0173.json”

For example, you can set the environment variable in C #:

Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "C:\Crd\32e0173.json");

NuGet Package
All you have to do is install in your project the NuGet TextToSpeech package from Google which is, as I write this article, in beta.


Once installed, you can use the TextToSpeech client provided by Google to access this service.


The method below creates an audio stream from a string.

public stаtic Streаm CreаteStreаmAudiо(string text)
  TextTоSpeechClient client = TextTоSpeechClient.Creаte();
  SynthesizeSpeechRequest request = new SynthesizeSpeechRequest();
  request.Input = new SynthesisInput();
  request.Input.Text = text;
  request.AudiоCоnfig = new AudiоCоnfig();
  request.AudiоCоnfig.AudiоEncоding = AudiоEncоding.Lineаr16;
  request.AudiоCоnfig.SаmpleRаteHertz = 44100;
  request.Vоice = new VоiceSelectiоnPаrаms();
  request.Vоice.SsmlGender = SsmlVоiceGender.Mаle;
  SynthesizeSpeechRespоnse respоnse = client.SynthesizeSpeech(request);
  Streаm streаm = new MemоryStreаm();

  streаm.Pоsitiоn = 0;

  return streаm;

We can also specify the gender, that is to say, man or woman.

The type of encoding can be defined in Linear16 (Wave), Mp3, etc.

The service returns to us, in the AudioContent property, the audio content that is in the ByteString format.

To use it in a Stream for example just call the following method.



Today, the service is free up to 1 million characters per month in WaveNet (Technology based on Machine Learning and to have a more natural voice), beyond the tariff is $ 16 per million characters additional.

The service, in basic quality, is free up to 4 million characters per month, beyond the fee is $ 4 per million additional characters.

You are now ready to use Google Cloud Text to Speech in C#, with a more natural and less robotic voice.

You may also like...

Leave a Reply

Your email address will not be published.