Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Exploring the SpVoice class of MS SAPI 5.1 to use the various available features for TTS

4.52/5 (16 votes)
11 Dec 2006CPOL3 min read 1   4K  
This article explores how to use the different features of the TTS engine of MS SAPI in your applications, using C#.

Sample Image - TTSFeaturesOfSAPI.jpg

Introduction

This is a simple example that will elaborate how to use the different features of the SpVoive class of SAPI 5.1 to make your applications speak out. Once, I needed to use Microsoft SAPI in a project while helping a dearest one. I was really amazed at how you could make use of the TTS within a very short time. This example will show you the following features of the TTS of Microsoft SAPI 5.1 in C#:

  • Play
  • Pause
  • Rate (Speed of speech / speaker)
  • Volume (Volume of the speaker )
  • Using all of the available voices of SAPI
  • Convert the provided text to a Wav file and save it

Using the code

To make use of the code, you will need to install the Microsoft Speech API 5.1, and you can download it from the Microsoft website freely.

After installing Microsoft SAPI 5.1, just download the source and open it in Visual Studio 2005. Before compiling or building the project, add a reference to Interop.SpeechLib.dll that will provide you the speechLib namespace that holds all the classes for TTS and SR. You can find this DLL in the bin directory of the source project.

Now, I will just give you the snippets of code that will provide the different features.

Play / Speak

To play/speak a text, create an object of the SpVoice class and call its Speak method that will speak out the text provided to it. Here is an example:

C#
SpVoice speech = new SpVoice();
speech.Speak("Hello It is a Microsoft Speech API test application", 
             SpeechVoiceSpeakFlags.SVSFlagsAsync);

Pause and Resume

To pause your current stream playing, just call the Pause method, and to resume the current stream, you can use the Resume method of the SpVoice class, like:

C#
speech.Pause();
speech.Resume();

Rate

To set the speed of the speaker, you can use the Rate method of the SpVoice class. You can use a track bar to modify the speech rate, and set the following properties of the track bar: Minimum = - 10 and Maximum = 10. You can capture the changed value on the Scroll event of the track bar.

C#
speech.Rate = speechRate;  // speechRate ranges from -10 to 10.

Voume

You can use the Volume property of SpVoice to set the volume of the speech, like:

C#
speech.Volume = volume; // volume ranging from 0 to 100

GetVoices

You can use the getVoices method of the SpVoice class to get the available voices in MS SAPI 5.1. It will return tokens as objects of ISpeechObjectToken. You can populate them in a combo box so that you can change it at runtime from the combo box. You should populate this combo box while initializing the components for the Windows Form or in the onLoad() method of the Windows form. An example of this is given below:

C#
foreach (ISpeechObjectToken Token in speech.GetVoices(string.Empty, string.Empty))
{
  // Populate the ComboBox Entries ..
  cmbVoices.Items.Add(Token.GetDescription(49));
  // Here cmbVoices is an ComboBox object.

}

cmbVoices.SelectedIndex = 0;

Converting to a WAV file

You can convert the text into a WAV file rather than speaking it out. The following code snippet will provide you the complete functionality for making a WAV file from a text:

C#
try
{
   SaveFileDialog sfd = new SaveFileDialog();
   sfd.Filter = "All files (*.*)|*.*|wav files (*.wav)|*.wav";
   sfd.Title = "Save to a wave file";
   sfd.FilterIndex = 2;
   sfd.RestoreDirectory = true;

   if (sfd.ShowDialog() == DialogResult.OK)
   {
     SpeechStreamFileMode SpFileMode = 
            SpeechStreamFileMode.SSFMCreateForWrite;
     SpFileStream SpFileStream = new SpFileStream();
     SpFileStream.Open(sfd.FileName, SpFileMode, false);
     speech.AudioOutputStream = SpFileStream;
     speech.Rate = speechRate;
     speech.Volume = volume;
     speech.Speak(tbspeech.Text, SpeechVoiceSpeakFlags.SVSFlagsAsync);
     speech.WaitUntilDone(Timeout.Infinite);
     SpFileStream.Close();
   }
}
catch
{
  MessageBox.Show("There is some error in converting to Wav file.");
}

That's it. You are done!!

History

I will try to keep a running update of any changes or improvements. Thanks.

License

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