Click here to Skip to main content
12,554,623 members (61,218 online)
Click here to Skip to main content
Add your own
alternative version


46 bookmarked

A Text to WAV Converter

, 7 Aug 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
Makes use of Windows Forms and the Speech API to create WAV files from text
Sample Image - Main Window.jpg


Normally, you would create a document using something like Microsoft Word or Notepad. Providing it follows certain formatting conventions, it should be accessible to most people. However, what would you do if you are creating something that might need to be accessed by the partially sighted or blind?

It would be either difficult or impossible to convey something using text. With this program, you can convert a text file, or something you type in the program's text area, into WAV audio so the information it contains can also be heard.


To achieve this program's goal, you need to make use of the Speech API and I have chosen to use it in conjunction with C#. While the voice produced may have its limitations, the most important thing is the information contained in the original text. If the text is not suitable, then no amount of improvements to the converter can make the file that's produced more useful.

The Source Code

Finding a Text File to Convert

The first stage. As I stated in the introduction, you can convert anything you type in the program's text area, but there are limited capabilities here so it might be better to use an external program to create a text file.

private void Browse_Click(object sender, EventArgs e)
        string fileName;
        OpenFileDialog OpenFile = new OpenFileDialog();
            OpenFile.CheckFileExists = true;
            OpenFile.CheckPathExists = true;
            OpenFile.DefaultExt = "txt";
            OpenFile.DereferenceLinks = true;
            OpenFile.Filter = "Text files (*.txt)|*.txt|" +    
				"RTF files (*.rtf)|*.rtf|" + 
                              "Word Documents (*.doc)|*.doc|" +    
				"Works 6 and 7 (*.wps)|*.wps|" + 
                              "Windows Write (*.wri)|*.wri|" + 
				"WordPerfect document (*.wpd)|*.wpd";
            OpenFile.Multiselect = false;
            OpenFile.RestoreDirectory = true;
            OpenFile.ShowHelp = true;
            OpenFile.ShowReadOnly = false;
            OpenFile.Title = "Select a file to convert";
            OpenFile.ValidateNames = true;

            if (OpenFile.ShowDialog() == DialogResult.OK)
                fileName = OpenFile.FileName;
                StreamReader sr = new StreamReader(OpenFile.OpenFile());
                aText.Text = sr.ReadToEnd();
            MessageBox.Show("Something is wrong here", "Text2Audio");
            if(sr != null)

When dealing with filestreams, it is essential you use a try-catch, with an optional finally. This means you can minimize problems at runtime caused by e.g. files being corrupted. While I have used a rather trivial and ambiguous error message in the catch section, it will be better to make this more specific if you were to create this for an average user. Having a message that is either too technical or too ambiguous will only limit the possible audience.

Converting to Audio

private void Convert_Click(object sender, EventArgs e)
        SaveFileDialog cfile = new SaveFileDialog();

            SpeechVoiceSpeakFlags SpFlags = SpeechVoiceSpeakFlags.SVSFlagsAsync;
            SpVoice speech = new SpVoice();
            cfile.Filter = "wav files (*.wav)|*.wav";
            cfile.Title = "Save to a wave file";
            cfile.FilterIndex = 2;
            cfile.RestoreDirectory = true;
            if (cfile.ShowDialog()== DialogResult.OK) 
                SpeechStreamFileMode SpFileMode = 
                SpFileStream SpFileStream = new SpFileStream();
                SpFileStream.Open(cfile.FileName, SpFileMode, false);
                speech.AudioOutputStream = SpFileStream;
                speech.Speak(aText.Text, SpFlags);
            MessageBox.Show("Something's gone wrong here.");
        MessageBox.Show("Conversion successful!", "Text2Audio");

This is perhaps the most complicated bit of the process. While the filestreaming here isn't difficult (because we used the reverse of this earlier), the conversion to audio is. Again, you must make use of the try-catch facility to minimize problems.

This method sets the audio output stream as the filestream that we created. I have also made use of threading so this section stops when the conversion is complete and never earlier than that. It would be pointless if it only turned half the text into a WAV file!

Points of Interest

Perhaps the most annoying thing about the Speech API is the voice. It has severe limitations and does things such as saying 'bracket', instead of just changing the emphasis. This could perhaps be improved by doing some automated alterations to the text before it is sent to be converted. Also, there is only one voice, which limits the program to those of us who speak English.


I will be happy to accept any suggestions for improvements to this code. All you have to do is send them to my email address and I will reply as soon as possible.

Use of this Project

Please note that if you want to use any part of this code in one of your own projects/solutions, you are free to do so providing you include a comment stating that it was me who created the code, my email address and also put where you got it from.

This is completely open-source. If you would like to use any of this for a commercial project, then you must send me an email with a request and I will decide whether I will allow you to go ahead. Any emails sent to me will be read and replied to as quickly as possible.


  • 7th August, 2005 - First edition of the program


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


About the Author

The Ladies' Man
Web Developer
United Kingdom United Kingdom
My real name is David Morris - I would have used that as my display name but it was already taken when I registered!

I graduated with an honours degree in Computer Science with Business Information Engineering from the University of Hull in July of 2006. Before I started university, I got a BTEC in Computer Engineering. I am also a full member of the British Computer Society (since August 2006).

You may also be interested in...


Comments and Discussions

QuestionConvert Button Pin
Member 1065307221-Jun-14 15:13
memberMember 1065307221-Jun-14 15:13 
GeneralMy vote of 3 Pin
ProgramFOX22-Jan-13 7:34
member ProgramFOX22-Jan-13 7:34 
GeneralLibrary source code Pin
selonda21-Jan-10 20:57
memberselonda21-Jan-10 20:57 
GeneralHey David Pin
codesperm7-Jan-10 6:15
membercodesperm7-Jan-10 6:15 
Generalthank you Pin
*Jori*24-May-08 18:13
member*Jori*24-May-08 18:13 
Generalneed your help urgently Pin
shilpag108-Oct-07 18:40
membershilpag108-Oct-07 18:40 
GeneralRe: need your help urgently Pin
The Ladies' Man9-Oct-07 11:08
memberThe Ladies' Man9-Oct-07 11:08 
GeneralWav File Header Pin
mjmim25-Jul-07 11:27
membermjmim25-Jul-07 11:27 
GeneralAudio Output is not Understandable Pin
Nupur Gupta15-Jun-07 22:07
memberNupur Gupta15-Jun-07 22:07 
Generaltext to speech Pin
beathellout4-Feb-07 8:01
memberbeathellout4-Feb-07 8:01 
GeneralRe: text to speech Pin
The Ladies' Man4-Feb-07 11:09
memberThe Ladies' Man4-Feb-07 11:09 
GeneralRe: text to speech Pin
Dali Hammadi3-May-07 0:38
memberDali Hammadi3-May-07 0:38 
Generalplease reply me as soon as possible Pin
amany_zizo11-Jul-06 22:53
memberamany_zizo11-Jul-06 22:53 
GeneralRe: please reply me as soon as possible Pin
The Ladies' Man13-Jul-06 6:54
memberThe Ladies' Man13-Jul-06 6:54 
GeneralPlease Need Your Help Pin
amany_zizo11-Jul-06 1:51
memberamany_zizo11-Jul-06 1:51 
GeneralRe: Please Need Your Help Pin
The Ladies' Man11-Jul-06 8:02
memberThe Ladies' Man11-Jul-06 8:02 
GeneralRe: Please Need Your Help [modified] Pin
amany_zizo11-Jul-06 8:29
memberamany_zizo11-Jul-06 8:29 
GeneralRe: Please Need Your Help Pin
Dali Hammadi3-May-07 0:22
memberDali Hammadi3-May-07 0:22 
Generalawesome Pin
_jobo_14-Nov-05 14:12
member_jobo_14-Nov-05 14:12 
GeneralNeed Guidance in your code Pin
Sana Farooq14-Sep-05 3:33
memberSana Farooq14-Sep-05 3:33 
GeneralRe: Need Guidance in your code Pin
The Ladies' Man14-Sep-05 6:58
memberThe Ladies' Man14-Sep-05 6:58 
GeneralRe: Need Guidance in your code Pin
Sana Farooq14-Sep-05 8:13
memberSana Farooq14-Sep-05 8:13 
Generalclose output stream in finally Pin
Anonymous8-Aug-05 16:05
sussAnonymous8-Aug-05 16:05 
GeneralRe: close output stream in finally Pin
The Ladies' Man14-Sep-05 6:56
memberThe Ladies' Man14-Sep-05 6:56 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161021.1 | Last Updated 7 Aug 2005
Article Copyright 2005 by The Ladies' Man
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid