iTranslated by AI
[C#] Sample Implementation of GCP Text-to-Speech
Introduction
I used to work with C# in my previous job.
Recently, I have been mostly writing React and ReactNative using JavaScript and TypeScript, and I've been starting to forget quite a bit about C#. However, a little while ago, I tried out GCP's Text-to-Speech out of curiosity, so I'll share the code from that time as a reminder for myself.
What is Text-to-Speech?
Powered by Google's AI technology, this API allows you to convert text into natural-sounding speech.
As explained above, it can convert text into speech.
Since the inflection and intonation are close to that of a human, combining it with services like Twilio allows for automatic responses with fluent pronunciation.
Setup
Let's install Google.Cloud.TextToSpeech using the command below.
You can probably do the same via the NuGet GUI.
Install-Package Google.Cloud.TextToSpeech.V1 -Pre
Obtaining a Service Account Key
Refer to the content of the official quickstart above to obtain a service account key.
While the documentation shows an example of placing the service account key path in the environment variable GOOGLE_APPLICATION_CREDENTIALS, this time we will use the method of passing the path to the module from the outside.
Code
Here is the actual code.
If it runs successfully, a result.mp3 file should be created.
using Grpc.Auth;
using Grpc.Core;
using Google.Apis.Auth.OAuth2;
using Google.Cloud.TextToSpeech.V1;
public class Sample
{
public static void Main(string[] args)
{
// Service account key file path
// You can also place the json in the environment variable [GOOGLE_APPLICATION_CREDENTIALS]
// In that case, the following GoogleCredential retrieval becomes unnecessary,
// and the constructor argument when creating the TextToSpeech client instance will not be required.
string credentialFile = "./credential.json";
// Get GoogleCredential
GoogleCredential gc = GoogleCredential.FromFile(credentialFile).CreateScoped(TextToSpeechClient.DefaultScopes);
Channel channel = new Channel(TextToSpeechClient.DefaultEndpoint.Host, gc.ToChannelCredentials());
// Generate an instance of the TextToSpeech client
TextToSpeechClient client = TextToSpeechClient.Create(channel);
// Generate the content to be read aloud
string body = "<speak>音声読み上げだよ!</speak>";
SynthesisInput input = new SynthesisInput();
input.Ssml = body;
// Voice settings (speaker, language, etc.)
VoiceSelectionParams voice = new VoiceSelectionParams
{
// Speaker (specifying ja-JP-Wavenet-A here)
Name = "ja-JP-Wavenet-A",
// Language
LanguageCode = "ja-JP",
// Gender
SsmlGender = SsmlVoiceGender.Neutral
};
// Audio file settings (format, speaking rate, etc.)
AudioConfig config = new AudioConfig
{
// File format
AudioEncoding = AudioEncoding.Mp3,
// Speaking rate (specify in the range of 0.25 to 4.0)
SpeakingRate = 1.0,
};
// Execute request
var response = client.SynthesizeSpeech(new SynthesizeSpeechRequest
{
Input = input,
Voice = voice,
AudioConfig = config
});
// Write to file
using (Stream output = File.Create("./result.mp3"))
{
response.AudioContent.WriteTo(output);
}
}
}
It might be interesting to adjust parameters such as speaker attributes and speaking rate and listen to the generated mp3 file while making various changes.
Summary
In this article, I introduced a sample of running GCP's Text-to-Speech in C#.
As I hadn't been involved with speech synthesis in either my work or personal development, it felt very fresh.
Although I mentioned it was fluent, to be honest, you can still tell it's a machine-generated voice, so I hope to see quality improvements in this area.
Discussion