Skip to main content

Text to speech converter

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.


         


Comments

Popular posts from this blog

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

Developing a C++ Program on Kali Linux Without Installing Additional Software This article is for hackers who want to develop a C++ program on Kali Linux without installing any additional software. Some might say you need to install a separate compiler or extra tools to write and run a simple C++ program on Kali Linux. However, I’ll show you how to do it right out of the box. Pre-installed C++ Compiler in Kali Linux Kali Linux comes with a pre-installed C++ compiler called g++ . We will use this to write and compile a basic "Hello, World!" program. Step 1: Check if g++ is Installed Open your terminal and run the following command: g++ -v If the compiler is installed, you should see version details. If not, you will get an error message. Step 2: Create a C++ File In your terminal, type: nano MyCpp.cpp This will create a C++ file and open it in the Nano editor. Step 3: Write the C++ Code Once Nano opens, enter the following C++ code: #include <...

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: Computer System running wind...

HOW TO APPLY FALLING TEXT EFFECT IN A CONSOLE PROGRAM WITH THREADING IN C#

"A thread is defined as the execution part of a program. Each thread defines a unique flow of control. If your application involves complicated and time consuming operations then it is often helpful to set different execution paths or threads, with each thread performing a particular job.", says the smart guys behind Tutorials Point.Well, our program is not going to be complicated and we are not going to be performing any time consuming task, we are rather going be creating a camouflage application that gives us the effect similar to what you get when you ping a site or an IP address with the "-t" option in cmd or the effect you see in movies like Blacklist, 24, Person Of Interest and the likes.Now let's stop the ranting and get started.We are going be building this application upon our existing code, one we started in the previous article "How to grammatically Change the background and foreground color of a console Application".Now open up the code in...