Skip to main content

HOW TO MAKE A SIMPLE TEXT TO SPEECH(TTS) WINDOWS PROGRAM USING C# PROGRAMMING LANGUAGE.

This post is a beginner's guide on how to get started with speech programming in visual studio using c# (c_sharp) programming language. For those who don't know what a programming language is, in a nut shell, a programming language is simply a command based computer language used for instructing a computer to do a particular job. When I say job, I mean very complex job. Write the above definition in an exam and stand a chance of losing marks. The definition isn't all there is about what programming language is, so I suggest you make a good search to learn what programming language really is. Though, this article is for beginners but I will say "BEGINNERS ARE CLASSIFIED", am a beginner. If you are a beginner who hasn't tasted code in his or her life before, I suggest you go start something. A good learning source is "tutorial point", they taught me a lot. Now for you who have tasted code, you will need the following:
  1. Computer System running windows 7 or XP ( of course you can't do it with phone).
  2. Visual Studio 2010 or 2012 (You can get the express edition free, they are available to download on microsoft websites) and sharpDevelop is not bad at all.
  3. The most important thing you need now is concentration.
  4. To help us get the real idea of how speech programming works, we are going to be developing a real program, one I call "RM (Reading Machine)". This little program of ours will read any text you type into its RichTextBox or one you load from file into its RichTextBox. at the end of this walkthrough, you should have a reading machine that looks like this:




I am doing this with visual studio 2012 express. open visual studio navigate to File>>New>>Project expand the Visual C# node and choose windows then select Windows Forms Application and name it speechee or whatever you want (for the sake of this walkthrough, name it speechee), am using .Net FrameWork 4.5 you can use 4 if you want, then click Ok.





 Notice that visual studio created the project for you with an empty form like this:





We are going to design our user interface on this form. start by dragging SplitContainer from the tools explorer on to the form, it should automatically fill the form but, if it doesn't, open the glyph and click on Dock in Parent container.


 //Im4 



Add a TableLayoutPanel on the right panel of the SplitContainer. Click the glyph of the TableLayoutContainer and click Remove Last Column. Click on the glyph again so that it closes, open the property explorer and change the Dock property to Fill. Drag the border seperating the up and down so that the downside occupies more of the available space.


 //Im5 


Add a RichTextBox in the downside and let it dock (am sure you are no more a learner on how to do this). Make Form1 gain focus by clicking on its tab and add a MenuStrip control. Type in File, click on the File you just typed and type in Load and Exit Our form will now look like this:



//Im6



 Add three buttons in the left panel of the SplitContainer and change their text property like this: button1=READ, button2=PAUSE and button3=RESUME. (To learn how to arrange controls on a form, check my post "Easy Life With Visual Studio"). On the upper side of the TableLayoutPanel in the right panel of the SplitContainer, add a TableLayoutPanel with three Rows and two Columns add labels and a TextBox in them so that it looks like this:


 



 TIME FOR DESIGN IS OVER! Its time to make it work. THE CODE In your Solution Explorer, right click reference and click Add Reference, expand the Assemblies node and choose Framework, look for System.Speech, check the box next to it and click ok.






Go to view and click on code, add this using statement:
using System.Speech.Synthesis; 

Just before the form's constructor, add this:

SpeechSynthesizer ss = new SpeechSynthesizer();

Switch back to your design window by going to the view again. Double click the READ button and add the following chunck of code in it:

if (richTextBox1.Text == "") 
 { label4.Text = "Nothing To Read!"; } else { label4.ForeColor = Color.Green; label4.Text = "Reading..."; } ss.Rate = -2; ss.SpeakAsync(richTextBox1.Text.ToString());

Double click the PAUSE button and add this:

  if (richTextBox1.Text == "")
 { label4.Text = "Pause what? You have no file typed or loaded."; } else { label4.ForeColor = Color.Blue; label4.Text = "PAUSED"; } ss.Pause();

Add the following in the event method of the RESUME button:

if (richTextBox1.Text == "") { label4.Text = "Nothing to resume to."; } else { label4.ForeColor = Color.Green; label4.Text = "RESUMED Reading..."; }
 ss.Resume();

Double click the Load menuItem in the MenuStrip Control





 and add the following code:

  try { OpenFileDialog ofd = new OpenFileDialog(); ofd.InitialDirectory = "c:\\"; ofd.Filter = "Text files(*.txt)|*.txt|All Files(*.*)|*.*"; ofd.FilterIndex = 2; ofd.RestoreDirectory = true; if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { richTextBox1.LoadFile(ofd.FileName, RichTextBoxStreamType.PlainText); textBox1.Text = ofd.FileName; label5.Text = "Loaded"; label5.ForeColor = Color.Green; } } 
 catch (Exception ex) { MessageBox.Show(ex.Message); }

Add this in the Exit Event method(Create it if you have not created it already):

ss.Dispose(); Close(); 
Double click on the Form to open the Load event method put this line in it:
  ss.SpeakAsync("Hello, My Name is reading Machine. I can read all your text and documents!");

Run the program by press F5 or ctrl+F5. Whooo! You have a reading machine. Play around with it and add many features. If there is anything you want to ask, add, improve or correct in this post, please add your comment for I will really appreciate it. Thanks for reading.

Comments

Post a Comment

Popular posts from this blog

HOW TO WRITE, COMPILE AND RUN C++ CODE ON LINUX KALI

This article is for hackers who want to develop a c++ program on Kali Linux without having to install any additional software. While some would tell you that you need to install an additional software or a compiler in order to develop a simple program in c++ on Kali Linux, I am going to show you how to develop a c++ program on this distro right out of box. Your Linux Kali comes pre-installed with a c++ compiler called g++ so we are going to write a C++ hello word code and compile it with this compiler. Before we get started, first open up terminal and run to verify if this compiler is installed on you machine: g++ -v if the compiler is pre-installed, you should get the version information of the compiler, otherwise, you should get an error. Now let's jump right in. In your terminal window, type in: nano MyCpp.cpp to create a c++ file and lunch it in nano editor for editing. When nano opens, type in the following c++ code and press Ctrl+x then y and then R...

LOAN MANAGEMENT SYSTEM (My Project Idea)

There are many companies that specializes on giving loan to people; they make their profits by collecting interest on any money they lend out, and before a loan can be given to any customer, the customer must provide a collateral and some details such as:  contact details and a reference/guarantor. When the loan is finally given to the customer, they charge interest based on the the amount given to the customer for a given period of time which could be daily, weekly, monthly or yearly.  For example, a company could be collecting interest based on a particular amount for a given period of time like: $200,000 loan would have an interest tag of $2,000  per month until the money is returned, and $100,000 would have an interest of $1,000 per month until the money is returned.  In  addition, the customers need to know when the time for them to pay their interest comes e.g.  at the end of the month or the timing model with which the calculation is being made. Usua...

How to execute eject command on an ATM Machine using CoreXfs lib on .NET

 In this tutorial, I'm going to demonstrate how to run an eject command on an ATM using CoreXfs, a library I developed.  You can download the library from  here  and follow up with tutorial. First we need to add reference to the downloaded dll. Screen One Once the reference to the dll has been added, you can proceed to coding the application. What I did in the screen below is very simple, I opened the device on the atm using it's logical name so that I can pass in commands. Screen Two So, that's it. thanks for dropping by. You can watch the video below to see how I coded it. Note that this code can only run on an ATM machine. NCR, WINCOR, DIEBOLD, HYOSUNG