Implementing speech technology into your program is extremely easy, but, our aim in this post is to implement it in such a way that it would be reusable. The code am about to share here is a part of my Emris class library project that I built so that I do not re-write codes I have already writen before when I need them. Just like the project name which is named after the young War-Lock's ancient name in the Movie "Merlin" implies, the class library is a collection of magical classes that does all kinds of things, one of which is the one we are talking about.
The Speech class.
The class contains 3 private member variables and 1 public SpeechSynthesizer instance variable which are:
private int speechRate;
private string speechString;
private bool IsAsync=false;
public SpeechSynthesizer ss;
3 Public properties which is used to get and set values to the private member variables:
//properties
public int _speechRate
{
get{return speechRate;}
set{value=speechRate;}
}
public string _speechString
{
get{return speechString;}
set{value=speechString;}
}
public bool _isAsync
{
get { return IsAsync; }
set { value = _isAsync; }
}
One public construtor used to initialize value to the private data members:
public Speech(int speech_rate, string speech_string, bool is_async)
{
this.speechRate = speech_rate;
this.speechString = speech_string;
this.IsAsync = is_async;
}
And one method that processes all the collected data:
public void Talk()
{
ss = new SpeechSynthesizer();
ss.Rate = _speechRate;
if (_isAsync == true)
{
ss.SpeakAsync(_speechString);
}
else
{
ss.Speak(_speechString);
}
}
Usage: The usage of this class is just a matter of initialzing the data members using the constructor Speech() and calling the Talk() method.
Like this:
Speech emris = new Speech(1, "You just have to use your magic for good.", true);
emris.Talk();
Note: You need to add reference to System.Speech for this to work.
Create a form in visual studio and add 2 textboxes and one Button to it. name the first TextBox txtWhat_To_Say, name the second one txt_rate and name the button btn_Speak also change the text of the button to "Speak" without the quatation mark.
Once that is done, double click the Speak button, in the method that opens, type in:
Speech emris = new Speech(txt_rate,txtWhat_To_Say.Text, true);
emris.Talk();
Once that is done, add a class named Speech and copy the following code into the class:
public class Speech
{
//Kinsware Technologies
private int speechRate;
private string speechString;
private bool IsAsync=false;
public SpeechSynthesizer ss;
//properties
public int _speechRate
{
get{return speechRate;}
set{value=speechRate;}
}
public string _speechString
{
get{return speechString;}
set{value=speechString;}
}
public bool _isAsync
{
get { return IsAsync; }
set { value = _isAsync; }
}
public Speech(int speech_rate, string speech_string, bool is_async)
{
this.speechRate = speech_rate;
this.speechString = speech_string;
this.IsAsync = is_async;
}
public void Talk()
{
ss = new SpeechSynthesizer();
ss.Rate = _speechRate;
if (_isAsync == true)
{
ss.SpeakAsync(_speechString);
}
else
{
ss.Speak(_speechString);
}
}
}
Then run the program and type in what you want the program to say in the What_To_Textbox and type how fast or slow you want it to say it in the rate textbox.
Please take your time to play with this magical class and try to make it possible for users to be able to control whether it should speak Asynchronously or Synchronously by using a checkbox control to interface the IsAsync Variable. Just build something interesting with it.
Note that this tutorial is for any beginner C# Programmer who now understands how class works. If this class is good for you, you can add it to your library too.
Enjoy a chunk of my Emris Magical library. Hahahaha!
Comments and questions are welcome.
Thanks for reading.
The Speech class.
The class contains 3 private member variables and 1 public SpeechSynthesizer instance variable which are:
private int speechRate;
private string speechString;
private bool IsAsync=false;
public SpeechSynthesizer ss;
3 Public properties which is used to get and set values to the private member variables:
//properties
public int _speechRate
{
get{return speechRate;}
set{value=speechRate;}
}
public string _speechString
{
get{return speechString;}
set{value=speechString;}
}
public bool _isAsync
{
get { return IsAsync; }
set { value = _isAsync; }
}
One public construtor used to initialize value to the private data members:
public Speech(int speech_rate, string speech_string, bool is_async)
{
this.speechRate = speech_rate;
this.speechString = speech_string;
this.IsAsync = is_async;
}
And one method that processes all the collected data:
public void Talk()
{
ss = new SpeechSynthesizer();
ss.Rate = _speechRate;
if (_isAsync == true)
{
ss.SpeakAsync(_speechString);
}
else
{
ss.Speak(_speechString);
}
}
Usage: The usage of this class is just a matter of initialzing the data members using the constructor Speech() and calling the Talk() method.
Like this:
Speech emris = new Speech(1, "You just have to use your magic for good.", true);
emris.Talk();
Note: You need to add reference to System.Speech for this to work.
Create a form in visual studio and add 2 textboxes and one Button to it. name the first TextBox txtWhat_To_Say, name the second one txt_rate and name the button btn_Speak also change the text of the button to "Speak" without the quatation mark.
Once that is done, double click the Speak button, in the method that opens, type in:
Speech emris = new Speech(txt_rate,txtWhat_To_Say.Text, true);
emris.Talk();
Once that is done, add a class named Speech and copy the following code into the class:
public class Speech
{
//Kinsware Technologies
private int speechRate;
private string speechString;
private bool IsAsync=false;
public SpeechSynthesizer ss;
//properties
public int _speechRate
{
get{return speechRate;}
set{value=speechRate;}
}
public string _speechString
{
get{return speechString;}
set{value=speechString;}
}
public bool _isAsync
{
get { return IsAsync; }
set { value = _isAsync; }
}
public Speech(int speech_rate, string speech_string, bool is_async)
{
this.speechRate = speech_rate;
this.speechString = speech_string;
this.IsAsync = is_async;
}
public void Talk()
{
ss = new SpeechSynthesizer();
ss.Rate = _speechRate;
if (_isAsync == true)
{
ss.SpeakAsync(_speechString);
}
else
{
ss.Speak(_speechString);
}
}
}
Then run the program and type in what you want the program to say in the What_To_Textbox and type how fast or slow you want it to say it in the rate textbox.
Please take your time to play with this magical class and try to make it possible for users to be able to control whether it should speak Asynchronously or Synchronously by using a checkbox control to interface the IsAsync Variable. Just build something interesting with it.
Note that this tutorial is for any beginner C# Programmer who now understands how class works. If this class is good for you, you can add it to your library too.
Enjoy a chunk of my Emris Magical library. Hahahaha!
Comments and questions are welcome.
Thanks for reading.
Comments
Post a Comment