Thursday, October 8, 2020

Text to Speech using Google TTS APIs

using Google.Cloud.TextToSpeech.V1;
using System;
using System.IO;
using System.Linq;
using System.Threading;

namespace QuickStart
{
    class Program
    {
        static void Main(string[] args)
        {
            // Check whether the environment variable exists.
            var value = Environment.GetEnvironmentVariable(@"GOOGLE_APPLICATION_CREDENTIALS");
            // If necessary, create it.
            if (value == null)
            {
                Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", @"C:\Google\My First Project-22f4c7c8ac46.json");

                // Now retrieve it.
                value = Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS");
            }

            GetListOfVoices();

            // Instantiate a client
            TextToSpeechClient client = TextToSpeechClient.Create();

            // Set the text input to be synthesized.
            SynthesisInput input = new SynthesisInput
            {
                Text = "hi!!!!......    it's a nice day"
            };

            // Build the voice request, select the language code ("en-US"),
            // and the SSML voice gender ("neutral").
            VoiceSelectionParams voice = new VoiceSelectionParams
            {
                LanguageCode = "en-IN",
                SsmlGender = SsmlVoiceGender.Male,    
                Name = "en-IN-Wavenet-C"
            };

            // Select the type of audio file you want returned.
            AudioConfig config = new AudioConfig
            {
                AudioEncoding = AudioEncoding.Linear16
            };

            // Perform the Text-to-Speech request, passing the text input
            // with the selected voice parameters and audio file type
            var response = client.SynthesizeSpeech(new SynthesizeSpeechRequest
            {
                Input = input,
                Voice = voice,
                AudioConfig = config,
            });

            // Write the binary AudioContent of the response to an Wave file.
            string outPutFile = @"C:\Google\ItsaNiceDay.wav";
            using (Stream output = File.Create(outPutFile))
            {
                response.AudioContent.WriteTo(output);
                Console.WriteLine($"Audio content written to file '{outPutFile}'");
            }


            new Thread(() => {
                System.Media.SoundPlayer player = new System.Media.SoundPlayer(outPutFile);
                player.Play();
            }).Start();

            Console.ReadLine();
        }

        static void GetListOfVoices()
        {
            // Create client
            TextToSpeechClient textToSpeechClient = TextToSpeechClient.Create();
            // Initialize request argument(s)
            ListVoicesRequest request = new ListVoicesRequest { LanguageCode = "", };
            // Make the request
            var response = textToSpeechClient.ListVoices(request);

            Console.WriteLine("List of voices supported");

            response.Voices.ToList().ForEach( v => Console.WriteLine($"Name:{v.Name} Gender:{v.SsmlGender}, LanguageCodes:{v.LanguageCodes}"));

        }
    }
}