Click here to Skip to main content
11,705,473 members (43,320 online)
Click here to Skip to main content

To Convert your Text or .txt File to Speech or AudioFile in .wav Format

, 18 Jun 2009 CPOL 31.1K 4.3K 26
Rate this:
Please Sign up or sign in to vote.
A converter to Convert user's Text or any .txt File into Speech or .wav AudioFile


This is an application to convert the user's Text or Text file of .txt format to audio file of .wav format. Here the application identifies the words and punctuation marks from the text file and breaks it accordingly with the letters and phonemes concerned. The user will get the audio output of whatever text he/she has written in the richtextbox or whatever text file he/she has introduced in the textbox concerned.


I am trying to develop a Friend (an application) who will understand your words, remember it and will also talk as a friend. This Text to Speech Converter is another module of the Application, through which the application understands the user's words and reapplies them accordingly (Goal).

Using the Code

    //This is a converter to convert your .txt file or your Text 
    //to voice or .wav file.
using System;                      //Adding System namespace through keyword using.
using System.Collections.Generic;  //Adding namespace Collections.Generic of System 
using System.ComponentModel;       //Adding namespace ComponentModel of System
using System.Data;                 //Adding namespace Data
using System.Drawing;              //Adding namespace Drawing
using System.Text;                 //Adding namespace Text
using System.Windows.Forms;        //Adding namespace Windows Forms
using SpeechLib;                   //Adding namespace/dll file SpeechLib

Here in the above code the most importance difference you will notice is the namespace SpeechLib. This is the Dynamic Link Library of SAPI5.1, i.e. Speech Application programming Interphase5.1.

namespace WindowsApplication1            //Namespace WindowApplication1 Started
    public partial class Form1 : Form    //Class Form1 is extended to Class Form (system)
        public Form1()                   //Constructor for Form1
            InitializeComponent();       //Method to initialize the Form1 Component.
        SpFileStream spFileStream = new SpFileStream();   //declaring and Initializing 
							//fileStream obj
        SpeechStreamFileMode spFileMode = 
	SpeechStreamFileMode.SSFMCreateForWrite; //declaring fileStreamMode as to 
						//Create or Write
        SpObjectTokenClass my_SpObjToken = new SpObjectTokenClass();   //declaring and 
					//initializing SpObject Token Class
        SpeechVoiceSpeakFlags my_Spflag = 
	SpeechVoiceSpeakFlags.SVSFlagsAsync;   // declaring and initializing 
						//Speech Voice Flags
        SpVoice my_Voice = new SpVoice();   //declaring and initializing SpVoice Class
        int sp_Rate=0,sp_Volume=70;     //integral variables declaration initialization

        private void Form1_Load(object sender, EventArgs e)


Here in the above code, I am defining a few objects as "spFileStream" of class SpFileStream,"spFilemode" of Class SpeechStreamFileMode,"my_SpObjToken" of SpObjectTokenClass,"my_Voice" of SpVoice Class. I will discuss every object in later sections. A few integral variables are there, they are sp_Rate initialized by "0" and sp_Volume initialized by "70". These sp_Rate and sp_Volume will pass their values to different sections.

In Form1_Load event, i am hiding a few objects which will be used as per need below.

//Speak Button to Convert Text to Audio 
private void Speak_Btn_Click(object sender, EventArgs e)
            if (TxtArea_Chk.Checked == true)
                my_Voice.Speak(TextArea_User.Text, my_Spflag);
                my_Voice.Volume = sp_Volume;
                my_Voice.Rate = sp_Rate;
            else if (TxtFilePath_Chk.Checked == true)
		SpeechVoiceSpeakFlags.SVSFIsFilename); //to update with Browse_Btn
                my_Voice.Volume = sp_Volume;
                my_Voice.Rate = sp_Rate;

Here in Speak_Btn_Click event, I am checking whether the TxtArea_Chk (checkBox) is clicked or not, if it is clicked then the text of the text of TextArea_User (richTextBox) will be passed to the Speak method of SpVoice class through concerned object my_Voice with the my_Spflag variable which is defined as "SVSFlagsAsync" (i.e. to speak asynchronously). If user had selected the TxtFilePath_Chk (checkBox), then the text file from openDialogBox gets passed to Speak method of SpVoice class through concerned object "my_Voice", with second argument as "SVSFIsFilename" of enum SpeechVoiceSpeakFlags. Here in both cases, the Volume and Rate of SpVoice class is initialised with the variable "sp_Volume" and "sp_Rate" through the same object as my_Voice.

//Decrement Rate Button to define Rate value
private void DecRate_Btn_Click(object sender, EventArgs e)
            if (sp_Rate > 0)
                MessageBox.Show("It is minimum Level.");

Here in DecRate_Btn_Click event, sp_Rate gets decremented by one if value is greater than 0 or it will show the message of minimum Limit.

//Increment Rate Button to define Rate value
private void IncRate_Btn_Click(object sender, EventArgs e)
            if (sp_Rate <= 20)
                MessageBox.Show("It is maximum Level.");

Here in IncRate_Btn_Click event, sp_Rate gets incremented by one if value is less than 21 or it will show the message of maximum Limit.

//Decrement Volume Button to define Volume value
private void DecVolume_Btn_Click(object sender, EventArgs e)
            if (sp_Volume > 60)
                MessageBox.Show("It is minimum Limit"); 

Here in DecVolume_Btn_Click event, sp_Volume gets decremented by two if value is greater than 60 or it will show the message of minimum Limit.

//Increment Volume Button to define Volume value
private void IncVolume_Btn_Click(object sender, EventArgs e)
            if (sp_Volume <= 100)
                MessageBox.Show("It is Maximum Limit.");

Here in IncVolume_Btn_Click event, sp_Volume gets incremented by two if value is less than 100 or it will show the message of maximum Limit.

//Button to Exit from Application
private void Exit_Btn_Click(object sender, EventArgs e)

Here in Exit_Btn_Click event, the application gets disposed.

//Save Button To preferred user's location
private void SaveFile_Btn_Click(object sender, EventArgs e)
            SaveFileDialog my_Sfd = new SaveFileDialog();
            my_Sfd.Filter = "All files (*.*)|*.*|wav files (*.wav)|*.wav";
            my_Sfd.Title = "Save to a wav file.";
            my_Sfd.FilterIndex = 2;
            my_Sfd.RestoreDirectory = true;
            if ((my_Sfd.ShowDialog() == DialogResult.OK)&&(TxtArea_Chk.Checked == true))
                spFileStream.Open(my_Sfd.FileName, spFileMode, false);
                my_Voice.AudioOutputStream = spFileStream;
                my_Voice.Speak(TextArea_User.Text, my_Spflag);
            else if((my_Sfd.ShowDialog() == 
		DialogResult.OK)&&(TxtFilePath_Chk.Checked == true))
                spFileStream.Open(my_Sfd.FileName, spFileMode, false);
                my_Voice.AudioOutputStream = spFileStream;
                my_Voice.Speak(my_fDlg.FileName, SpeechVoiceSpeakFlags.SVSFIsFilename);

Here in SaveFile_Btn_Click event, I had opened the saveDialogBox where the filter is ".wav" and after checking the checked mode of TxtArea_Chk or TxtFilePath_Chk, we are converting the text into audio file and saves it as per user's preferred name and location.

private void TxtArea_Chk_CheckedChanged(object sender, EventArgs e)
                TxtFilePath_Chk.Enabled = false;
                UserChoice_GrpBox.Text = "User Text Area";

Here in TxtArea_Chk of checked changed event, I am disabling the TxtFilePath_Chk, and changing text of GroupBox with calling show method for TextArea_User and Hide method for FilePath_Lbl, FileBrowse_TxtBox and Browse_Btn.

private void TxtFilePath_Chk_CheckedChanged(object sender, EventArgs e)
                TxtArea_Chk.Enabled = false;
                UserChoice_GrpBox.Text = "User Text File";

Here in TxtFilePath_Chk of checked changed event, I am disabling the TxtArea_Chk, and changing text of GroupBox with calling hide method for TextArea_User and show method for FilePath_Lbl, FileBrowse_TxtBox and Browse_Btn.

int igCounter = 0;
private void Refresh_Btn_Click(object sender, EventArgs e)
            UserChoice_GrpBox.Text = "User Choice Area";
            TxtArea_Chk.Enabled = true;
            TxtFilePath_Chk.Enabled = true;
            TxtArea_Chk.Checked = false;
            TxtFilePath_Chk.Checked = false;
            if (igCounter == 0)
                MessageBox.Show("Single Click twice to Complete refresh.");

Here in Refresh_Btn Click event, what I am doing is just defining the boolean true value to both checkbox's enabled and hiding all the other members of the groupBox.

OpenFileDialog my_fDlg = new OpenFileDialog();
String user_WaveFile;
private void Browse_Btn_Click(object sender, EventArgs e)
            my_fDlg.Title = "Browse your .wav file.";
            my_fDlg.Filter = "Wav Files (*.txt)|*.txt";
            my_fDlg.RestoreDirectory = true;
            if (my_fDlg.ShowDialog() == DialogResult.OK)
                user_WaveFile = @my_fDlg.FileName;
                FileBrowse_TxtBox.Text = @my_fDlg.FileName;

Here in Browse_Btn Click event, I had opened an openDialogBox having filter ".txt" and initialize the text of the textBox with the user preferable filename.

What Does the Code Do?

This code belongs to the main form as "Form1.cs", here in User Choice group section the user will have two selection checkboxes - one for richTextbox and another to browse the text file from the system. Through these checkboxes, the user can choose the required way to convert his text to speech/audio file or user can browse his/her text file to Convert into the audio file. When user Clicks the "speak" button, then text from the richtextbox or the text from the .txt file passes to the speak method of spVoice class contained in the Speechlib.dll file. As through the SaveFile button, the procedure is the same as the speak method converts the text into audio format of .wav and a saveDialogbox opens for the preferred location to save. User can also set the volume and speaking rate as from the "Inc" and "Dec" button. Here the volume and rate is an integral variable which initializes the volume and rate of the Volume and Rate of the SpVoice class of Speechlib.dll file.

How Do I Use It?

Mark the checkbox that user needs. If user is going through the richtextbox, then the user has to write the text there in richtextbox and click the speak button or if user had marked the TextFilePath checkbox, then he/she will have to browse the textfile through browser button and then click the speak button. To save the audio file, the user will get a saveDialogbox through saveFile button and he/she can save the audio file generated at any location with a new name.

Why Would Someone Want to Use your Version?

It is possible to get any similar application, but its uniqueness is about the integrated pattern of converting text from richtextbox or even through textfile into audio file.

Environment of the Code to be Restricted?

This code is written in C#2.0 with Visual Studio 2005 and .NET 3.0 Framework.

Points of Interest

Getting the audio file from the user's simple text or his/her simple text file is most important in this application. The core concept that I am using here is (SAPI5.1) speak method of Spvoice class.


  • 18th June, 2009: Initial post


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


About the Author

Software Developer (Senior)
India India
Thinking is the way, implementation is the milestone.

You may also be interested in...

Comments and Discussions

Questionmy vote of 5 Pin
Member 1030385928-Jul-15 2:39
memberMember 1030385928-Jul-15 2:39 
GeneralMy vote of 5 Pin
Member 86517736-Oct-12 6:08
memberMember 86517736-Oct-12 6:08 
GeneralRe: My vote of 5 Pin
saketbit3-Dec-12 19:14
membersaketbit3-Dec-12 19:14 
GeneralMy vote of 3 Pin
bearking781123-Dec-11 18:33
memberbearking781123-Dec-11 18:33 
GeneralRe: My vote of 3 Pin
saketbit2-May-12 19:01
membersaketbit2-May-12 19:01 
GeneralMy vote of 1 Pin
Priyank Bolia18-Jun-09 19:25
memberPriyank Bolia18-Jun-09 19:25 
GeneralRe: My vote of 1 Pin
saketbit27-Jun-09 8:11
membersaketbit27-Jun-09 8:11 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150819.1 | Last Updated 18 Jun 2009
Article Copyright 2009 by saketbit
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid