Speech Synthesis Program Text to Voice

Text to Voice Speech Synthesis Program

Speech Synthesis Program in C Sharp Console application is what you write it would speak.

It’s a text to the voice conversion basic program.

Step No, 01:  Create a new project in visual studio by pressing (ctrl + shift + N) Or follow

Go to File à New à Project… then click.

Choose the project type here you should choose Console.

  1. Select Visual C#
  2. Click on Console Application because we want to do basic practice on console then we will move on windows forms or UWP or WPF.
  3. Provide the name of what you want.
  4. Then click OK.

First of all, before writing the speech program, we need to add the Speech Synthesizer reference in our project.

Right click on you references in solution explorer windows of the project.

Then a window popup and

  1. Because we are using. NET framework and it provides many built-in libraries, and we just want to add. So click on Framework or You can search in search for Speech
  2. Now add the Class reference Speech
  3. Click OK.

We have added the Reference, but we don’t specify in our Program.cs namespace

So add the following.

using System.Speech.Synthesis;

Now it’s time to complete our basic program, but in this tutorial, we also write the entire loop based and user based program to wait unit user want to exit the program.

  1. Create the object of SpeechSynthesizer type
  2. Prompt to the user to input something. .Speak() method will take that string entered by the user and return the audio of that string.

The output of the above program.

In the above, I enter the string that Hi! Junaid Ahmed welcome to our speech program. And then press enter the program return the audio sound of the above string.

Now expand the program for many time input until the user wants to exit.

The Complete Code of Speech Synthesis Program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Speech.Synthesis;
using System.Text;
using System.Threading.Tasks;

namespace SpeechSynth
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to Our Speech Program!");
            SpeechSynthesizer speak = new SpeechSynthesizer();

            string response = "";
            do
            {
                //prompt to the user
                Console.WriteLine("Write something! ");
                speak.Speak(Console.ReadLine());

                //for more execution
                Console.WriteLine("Do you want to continue ? y/n");
                response = Console.ReadLine();

                //User response
                if (response == "y" || response == "Y")
                {
                    continue;
                }
                else
                {
                    Console.WriteLine("Bye! You press n/N.");
                    break;
                }

            } while (response == "y" || response == "Y");
            
            //Wait untill user press any key to continue
            Console.ReadKey();

        }
    }
}

When I execute the program by pressing F5 the program’s output screen popup and wait to input the string so in my case I give the Hi! My name is Junaid Ahmed then press enter the program speak that line of code. Then it prompts that do you want to continue. I press y then program continue so until I press n program will continue to execute.