Text to Speech Converter

Raj.Trivedi
Posted by in C# category on for Intermediate level | Points: 250 | Views : 5510 red flag
Rating: 3.5 out of 5  
 2 vote(s)

Here we will look into creating a simple app to make our app talk.

Introduction

There are circumstances that we might have to make our app more interactive with users, and one of the way is to make our app to talk i.e. it will speak what we write.
This is a simple Text to Speech Converter.


Objective

To attain and understand Text to Speech Conversion.


Using the code


Open MS Visual Studio -> Create New Project -> Windows Application.

Over here in this app we will take on Mutliline Textbox and 4 Buttons.
Button 1 :- To make the app speak the text in Textbox
Button 2 :- To Pause 
Button 3 :- To Resume
Button 4 :- To Stop
Button 5 :- To open a file Dialog control

Now first we will have to add the reference of the system.speech dll provided by Microsoft .NET Framework in our references

Right Click Reference Folder Go to .NET Tab and select System.Speech

After that we need to import name space of System.Speech.Synthesis.

This class will provide us the ways and methods to make our app speak.



Now we will create an object of Speech Synthesizer class provided in speech.synthesis and write down the following code


To read the text from notepad we will be using the IO class to read the data from notepad,for that we need to import the System.IO namespace

Now we drag a file dialog control so that we can browse for the text file.



Block of code should be set style as "Code" like below.
// Code behind
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Speech.Synthesis;
using System.IO;


namespace text_to_speech
{
    public partial class Form1 : Form
    {
        SpeechSynthesizer reader;
        public Form1()
        {
            InitializeComponent();

        }
        private void Form1_Load(object sender, EventArgs e)
        {
            reader = new SpeechSynthesizer();
            btnPause.Enabled = false;
            btnResume.Enabled = false;
            btnStop.Enabled = false;
            xtxtContent.ScrollBars = ScrollBars.Both;
            
            volumebar.Maximum = 100;
            speedbar.Maximum = 10;
            speedbar.Minimum = -5;
            volumebar.Value = 50;
           
        }

        //SPEAK TEXT
        private void button1_Click(object sender, EventArgs e)
        {
            reader.Dispose();
            if (xtxtContent.Text != "")
            {

                reader = new SpeechSynthesizer();
                reader.Volume = volumebar.Value;
                reader.Rate = speedbar.Value;
                
                reader.SpeakAsync(xtxtContent.Text);
                label2.Text = "SPEAKING";
                btnPause.Enabled = true;
                btnStop.Enabled = true;
                reader.SpeakCompleted += new EventHandler<SpeakCompletedEventArgs>(reader_SpeakCompleted);
            }
            else
            {
                MessageBox.Show("Please enter some text in the textbox", "Message", MessageBoxButtons.OK);
            }
        }

        void reader_SpeakCompleted(object sender, SpeakCompletedEventArgs e)
        {
            label2.Text = "IDLE";
        }

        //PAUSE
        private void button2_Click(object sender, EventArgs e)
        {
            if (reader != null)
            {
                if (reader.State == SynthesizerState.Speaking)
                {
                    reader.Pause();
                    label2.Text = "PAUSED";
                    btnResume.Enabled = true;

                }
            }
        }

        //RESUME
        private void button3_Click(object sender, EventArgs e)
        {
            if (reader != null)
            {
                if (reader.State == SynthesizerState.Paused)
                {
                    reader.Resume();
                    label2.Text = "SPEAKING";
                }
                btnResume.Enabled = false;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (reader != null)
            {
                reader.Dispose();
                label2.Text = "IDLE";
                btnPause.Enabled = false;
                btnResume.Enabled = false;
                btnStop.Enabled = false;
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            xtxtContent.Text =  File.ReadAllText(openFileDialog1.FileName.ToString());

        }
    }
}



Output Screen shots:-

Speaking




Pausing



Resume



Stop and Track Bar



Conclusion

Hope this will help any developer with similar requirements.
Code updated for Volume bar and Speed  


Page copy protected against web site content infringement by Copyscape

About the Author

Raj.Trivedi
Full Name: Raj Trivedi
Member Level:
Member Status: Member,MVP
Member Since: 6/16/2012 2:04:41 AM
Country: India
Regard's Raj.Trivedi "Sharing is Caring" Please mark as answer if your Query is resolved
http://www.dotnetfunda.com/profile/raj.trivedi.aspx
Raj Trivedi i.e. me started my career as Support Professional and then moved on the Software development eventually reached at these skills Software Development | Enthusiastic Blogger | Content Writer | Technical Writer | Problem Solver | Lecturer on Technology Subjects | Runnerup Award Winner on www.dotnetfunda.com and firm believer in Sharing as a way of Caring Yet this much achieved its still a long way to go and there is biggest dream lying to be one of the best entrepreneurs of India in Technology Department. The Dream has just started and i hope it follows. Highlights are mentioned in details in my profile at http://in.linkedin.com/pub/raj-trivedi/30/61/b30/

Login to vote for this post.

Comments or Responses

Posted by: Laghaterohan on: 3/5/2013 | Points: 25
Hi,
This looks reat...Could you please format the code properly as it is difficult to understand and its more readable?

Thank You.
Posted by: Karthick41287 on: 3/6/2013 | Points: 25
Is any namespace u declared for reader.how to make the speaking text .On ur above code i have an error in reader.dispose() ----object reference not set to an instance.How to clear that error.

Posted by: Manikandan711 on: 3/7/2013 | Points: 25
More informative.. keep writing more...


Posted by: Raj.Trivedi on: 3/8/2013 | Points: 25
Hello Karthicj i have updated the code.

it had to be sreader.dispose();

not reader.dispose();

Updated snippet

private void button4_Click(object sender, EventArgs e)
{
if (sreader != null)
{
sreader.Dispose();
label2.Text = "IDLE";
button2.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
}
}

Posted by: Manoj323 on: 3/11/2013 | Points: 25
Hell... I m a beginner.
Can u tell me How to highlight the current reading text in tts.??
Posted by: Bh80prk on: 3/15/2013 | Points: 25
Article is very nice and impressive.
Great Job!
Posted by: Robin0413 on: 4/1/2013 | Points: 25
Dear Sir,
Your Article is really very - very awesome. I got new learning thing from you. Now I need status on label2 - user is typing......
If someone type something on text box or browse any file from notepad file from dialog control then status should be on label2-
Searching Document............


Thanks and Regards,
Robin

Posted by: Raj.Trivedi on: 4/1/2013 | Points: 25
Hello Robin,

On the Key press event of the textbox set the label text as Typing.....

Code

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
label1.Text = "Typing.........";
}
Posted by: Raj.Trivedi on: 4/1/2013 | Points: 25
Hello Robin

Here is the code for second query

private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
label2.Text = "Document Fetched";
}

private void button1_Click(object sender, EventArgs e)
{
label2.Text = "Searching Document to Read....";
openFileDialog1.ShowDialog();



}
Posted by: Robin0413 on: 4/2/2013 | Points: 25
Thanks a lot Sir!!!
Posted by: Robin0413 on: 4/2/2013 | Points: 25
Dear Sir,
Again I would like to give you trouble to help me. I need solution for making any set up for windows based project.
As I tried this article and it's going well so now I wanna set up file for this ,then I can install it anywhere. It's just to
learn .


Thanks and Regards,
Robin
Posted by: Raj.Trivedi on: 4/2/2013 | Points: 25
Refer to this article written by me

http://www.dotnetfunda.com/articles/article2213-creating-set-up-for-windows-application-using-icons.aspx
Posted by: Robin0413 on: 4/2/2013 | Points: 25
Dear Sir,
It is really cool , I followed all the steps and I got it well. Now for making it so featured I would like to have high-lighted text on its
current position reading by SpeakAsync method.


Example:- This is to convert text into voice.
Posted by: Jimmy on: 3/11/2015 | Points: 25
InitializeComponent(); I Can understood y u did this

Login to post response

Comment using Facebook(Author doesn't get notification)