Using C# to output TTS to a .wav

The purpose of this brief article is to demonstrate, step by step with screenshots, how you can use Visual Studio to write a C# application that will write TTS to a .wav file. In a nutshell, the process is to create a form with a button on in that writes a TTS sentence (embedded in the code for now) to a .wav file.The app itself is a bit crude - but hey, I'm assuming you have a better sense of design than me.

This brief article came about from a recent blurb on my blog regarding Matt Harrington's article on some code to enable speech. Somebody asked how to ouput TTS to a .wav... and with some specifics from Matt (thanks Matt!) I'm hoping that the following details can walk you through the entire process.

Without further delay...

 

1. Open Visual Studio 2003 and create a New Project under Visual C# Projects. Make it a Windows Application just for fun.

2. On the References folder on the Solution Explorer, right click and then select "Add Reference".

3. Add the Microsoft.Speech.Web component:

4. Next, on your form, drag a button onto your form.

5. Then, double click on your button to get to the callback function and add the following code:

which is in text form here:

string text = "Hi Patty, 5 minus 4 is one.";

SpVoice voice =

new SpVoice();

SpFileStream fileStream =

new SpFileStream();

fileStream.Open(@"c:\sample.wav", SpeechStreamFileMode.SSFMCreateForWrite,

false);

voice.AudioOutputStream = fileStream;

voice.Speak(text,SpeechVoiceSpeakFlags.SVSFDefault);

fileStream.Close();

6. At the top of the code, make sure to add the line, "using SpeechLib;".

7. Finally, simply Build and then Start your solution. Check your file path for the newly created .wav! Remember, if you want to share your exe with a friend (or your Mom), make sure that the .exe remains in the same folder with the "Interop.SpeechLib.dll" that was created since you referenced the SpeechLib.