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.
- Select Visual C#
- Click on Console Application because we want to do basic practice on console then we will move on windows forms or UWP or WPF.
- Provide the name of what you want.
- 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
- 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
- Now add the Class reference Speech
- Click OK.
We have added the Reference, but we don’t specify in our Program.cs namespace
So add the following.
1 |
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.
- Create the object of SpeechSynthesizer type
- 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
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.