Click here to Skip to main content
14,773,680 members
Articles » Multimedia » Audio and Video » Speech
Posted 17 Apr 2011


56 bookmarked

Text to Speech

Rate me:
Please Sign up or sign in to vote.
4.76/5 (32 votes)
17 Apr 2011CPOL
How to implement speech technology in our project


Text To Speech becomes very easy in C#. We know speech technology is very useful for the blind. Now, we are going to learn how to implement speech technology in our project.

text to speak


  1. Open Microsoft Visual Studio 2010 from Start | All Programs | Microsoft Visual Studio 2010 | Microsoft Visual Studio 2010.
  2. On the Microsoft Visual Studio Start Page, click the New Project icon.
  3. On the Microsoft Visual Studio Start Page, click the New Project icon.
  4. In C# projects Templates list, select Windows | WindowsApplication.
  5. In the Name field, enter the name “text to speak”
  6. In the upper right hand corner, ensure that version 4.0 of the .NET Framework is selected.
  7. Accept the default location and Solution Name. Click the OK button. The solution will be created.
  8. Right-click on the Text to Speak project node and select Add Reference.
  9. Under the .NET tab in the Add Reference dialog box, select System.Speech.
  10. Click the OK button.

Select a Text File

Select a text file to convert audio.selected text file display on rich text box.

private void Browse_Click(object sender, EventArgs e)
        OpenFileDialog Ofg = new OpenFileDialog();

            Ofg.CheckFileExists = true;
            Ofg.CheckPathExists = true;
            Ofg.DefaultExt = "txt";
            Ofg.DereferenceLinks = true;
            Ofg.Filter = "Text files (*.txt)|*.txt|" +
				"RTF files (*.rtf)|*.rtf|" +
                              " +
				Works 6 and 7 (*.wps)|*.wps|" +
                              "Windows Write (*.wri)|*.wri|" +
				"WordPerfect document (*.wpd)|*.wpd";
            Ofg.Multiselect = false;
            Ofg.RestoreDirectory = true;
            Ofg.ShowHelp = true;
            Ofg.ShowReadOnly = false;
            Ofg.Title = "Select a file ";
            OpenFile.ValidateNames = true;

            if (OpenFile.ShowDialog() == DialogResult.OK)
                StreamReader sr = new StreamReader(ofg.OpenFile());
                richTextBox1.Text = sr.ReadToEnd();
            MessageBox.Show("can not open the file", "Text two speech");

Here, first creating object instance for reader is used to read a text file.

Text to Speak

SpeakAsync method is used to speak a word. There are two properties used here; one is volume, another one is Rate. voice gender is also one of the properties. It is used to select voice gender (enum NotSet, Male, Female, Neutral). If any error occurred, message box shows error message.

private void speak_Click(object sender, EventArgs e)
    SpeechSynthesizer speak = new SpeechSynthesizer();

    switch (comboBox1.SelectedItem.ToString())
                    case "NotSet":
                    case "Male":
                    case "Female":
                    case "Neturl":

The above code can change voice gender. speakAsyn is an asynchronous method. It means return immediately and speak as a background process. If any error occurred, it will display on message box.

voice.Volume = trackBar1.Value;
voice.Rate = trackBar2.Value;

Save Audio File

Text converted to audio stream .audio stream can save wav file format. The following steps are used to save wav file.

  1. Create object instance for Save File Dialog.
  2. Set audio stream to select file.
SaveFileDialog sfd = new SaveFileDialog();

sfd.Filter = ""wav files (*.wav)|*.wav";
sfd.Title = "Save to a wave file";
sfd.FilterIndex = 2;
sfd.RestoreDirectory = true;

if (sfd.ShowDialog() == DialogResult.OK)
     FileStream fs = new FileStream(sfd.FileName,FileMode.Create,FileAccess.Write);

Points of Interest

If you want to know more about speech technology, click on this link on the Microsoft website.


  • 14th April, 2011: Initial version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
India India
No Biography provided

Comments and Discussions

GeneralMy vote of 5 Pin
Member 1063581118-Aug-16 12:38
MemberMember 1063581118-Aug-16 12:38 
GeneralMy vote of 2 Pin
Member 96740184-Jul-15 7:28
MemberMember 96740184-Jul-15 7:28 
QuestionNeed to add new voice Pin
Member 1176311113-Jun-15 2:12
MemberMember 1176311113-Jun-15 2:12 
BugSmall problem Pin
KING_9123-Apr-14 1:24
MemberKING_9123-Apr-14 1:24 
GeneralRe: Small problem Pin
Member 129838525-Feb-17 7:00
MemberMember 129838525-Feb-17 7:00 
BugHelp me Pin
Xuân Tâm17-Feb-14 5:58
MemberXuân Tâm17-Feb-14 5:58 
GeneralRe: Help me Pin
karthik.B.E18-Feb-14 7:48
Memberkarthik.B.E18-Feb-14 7:48 
GeneralRe: Help me Pin
Xuân Tâm19-Feb-14 4:54
MemberXuân Tâm19-Feb-14 4:54 
Questionkarthik.B.E. Pin
Rahul Kumar Ghosh1-May-13 7:37
MemberRahul Kumar Ghosh1-May-13 7:37 
GeneralMy vote of 5 Pin
Jitesh R. Neve24-Jan-13 2:39
MemberJitesh R. Neve24-Jan-13 2:39 
QuestionMore voices Pin
kiquenet.com22-Jan-13 22:14
professionalkiquenet.com22-Jan-13 22:14 
GeneralMy vote of 5 Pin
Kr.Prakash5-Dec-12 1:23
MemberKr.Prakash5-Dec-12 1:23 
QuestionHow to add TTS engine for windows 7 Pin
Kiran Naveen11-Nov-12 19:32
MemberKiran Naveen11-Nov-12 19:32 
AnswerRe: How to add TTS engine for windows 7 Pin
Member 129838525-Feb-17 7:01
MemberMember 129838525-Feb-17 7:01 
AnswerCursor animation Pin
Christophe Bertrand4-Jun-11 0:47
MemberChristophe Bertrand4-Jun-11 0:47 
GeneralMy vote of 3 Pin
SercanOzdemir17-May-11 3:22
MemberSercanOzdemir17-May-11 3:22 
Generalsome ranting Pin
alxxl19-Apr-11 2:31
Memberalxxl19-Apr-11 2:31 
GeneralIf my PC has multipul soundcards, how to specify the output to which soundcard? Pin
zhangzq7118-Apr-11 3:29
Memberzhangzq7118-Apr-11 3:29 
AnswerRe: If my PC has multipul soundcards, how to specify the output to which soundcard? Pin
karthik.B.E19-Apr-11 5:01
Memberkarthik.B.E19-Apr-11 5:01 
GeneralRe: If my PC has multipul soundcards, how to specify the output to which soundcard? Pin
zhangzq7119-Apr-11 5:14
Memberzhangzq7119-Apr-11 5:14 
GeneralRe: If my PC has multipul soundcards, how to specify the output to which soundcard? Pin
kiquenet.com25-Apr-11 0:39
professionalkiquenet.com25-Apr-11 0:39 
GeneralRe: If my PC has multipul soundcards, how to specify the output to which soundcard? Pin
Seve6526-Apr-11 3:17
MemberSeve6526-Apr-11 3:17 
GeneralRe: If my PC has multipul soundcards, how to specify the output to which soundcard? Pin
zhangzq7127-Apr-11 16:38
Memberzhangzq7127-Apr-11 16:38 
GeneralMy vote of 5 Pin
Bizounours17-Apr-11 23:30
MemberBizounours17-Apr-11 23:30 
GeneralNice Pin
Toniyo Jackson17-Apr-11 21:40
MemberToniyo Jackson17-Apr-11 21:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.