Click here to Skip to main content
15,867,750 members
Articles / Programming Languages / C#
Article

A Text to WAV Converter

Rate me:
Please Sign up or sign in to vote.
3.94/5 (12 votes)
7 Aug 2005CPOL3 min read 96K   1.8K   47   24
Makes use of Windows Forms and the Speech API to create WAV files from text
Sample Image - Main Window.jpg

Introduction

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.

Background

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.

C#
private void Browse_Click(object sender, EventArgs e)
    {
        string fileName;
        OpenFileDialog OpenFile = new OpenFileDialog();
        
        try 
        {
            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();
            }
        }
        catch
        {
            MessageBox.Show("Something is wrong here", "Text2Audio");
        }
        finally
        {
            if(sr != null)
            {
                sr.Close();
            }
        }
    }

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

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

        try 
        {
            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 = 
			SpeechStreamFileMode.SSFMCreateForWrite;
                SpFileStream SpFileStream = new SpFileStream();
                SpFileStream.Open(cfile.FileName, SpFileMode, false);
                speech.AudioOutputStream = SpFileStream;
                speech.Speak(aText.Text, SpFlags);
                speech.WaitUntilDone(Timeout.Infinite);
                SpFileStream.Close();
            }
        }
        catch
        {
            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.

Suggestions

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.

History

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

License

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


Written By
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).

Comments and Discussions

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