Friday, 1 November 2013

Text To Speech in C#

Text to speech converter in C#
Hi friends, today I am writing blog about how to make Text to speech C# application. 
Open your visual studio. I am using Visual studio 2012. Go to File -> New Project -> Windows Form Application, Select Visual C#. Name the project TextToSpeech.

Now, for text to speech application we need to add references.
Procedure to add references is
1. Open your solution explorer (Menu bar -> View - > Solution Explorer OR Ctrl+w,s ).

2. Right click on TextToSpeech , Add references . Now here is Reference Manager.

3. Search System.speech and add it

Now , To design a form, We will require a Tool box. Open Tool box just as you opened Solution Explorer.
Or just press Ctrl+w, x  to open Tool box.

Now we will make design part of an application. We will need four buttons and one RichTextBox. We will drag and drop that into our form.

Now we will rename button texts, So we are going to rename them as start, pause, resume and stop by using properties. Properties will open by clicking button only once. Just go to Text and rename it.
and save it.
Repeat the process for all four buttons.


Now we will add some namespaces to Form1.cs. Please note that Form1.cs and Form1.cs [Design] are two different files.

These Two namespaces are (Which we have to write on the top section):
using System. Speech;
using System.Speech.Synthesis;


Now declare the object of SpeechSynthesizer, we will name it reader. In Partial class Form1.
Object declaration: 



And copy the following code.


Here is the explanation of above code.

SpeekSyn
thesizer is a sealed class. And its method SpeakAsync is taking String as an argument. And that string is the string that we want to convert into a speech.
Refer the document provided by msdn.
Just Google SpeekSynthesizer and go to msdn link


Type of richTextBox1.Text
is string. So, you can take this in string variable and use it.
Now we will look at pause button event AND button resume event:

Explanation :
if reader is not null that is there is something in reader object(text in string format). Then go in inner if.
1       1.  Pause - If state of reader is speaking then pause it.
2       2. Resume - If state of reader is paused then resume it.

     Now the obvious question is how am I able to write those statements?
1.       State is a property which we have used in inner if. Again Google SynthesizerState  class, visit msdn link. Under property section, you will get state property.

If we check State property then we will get its syntax:   

        But it gets the SynthesizerState object. Lets check SynthesizerState.


       We came to know that SyntheSizerState is an enum and its members are 

1               1.  Paused
2               2. Ready
3               3. Speaking.
Hence,


Same in the case of Resume.
Now we will check stop event.

Explanation of above code:
If there is something in reader object, Dispose it. Dispose() method is public and of SpeechSynthesizer class. Which releases all recourses, that is it stops the process.


  Now we are ready to run it.

  Click the start button to run it.
1.      Type whatever you want text to be converted into speech into RichTextBox. Use buttons to    control 
.

******* Thank You *******













No comments:

Post a Comment