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

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

Rate me:
Please Sign up or sign in to vote.
4.52/5 (16 votes)
11 Dec 2006CPOL3 min read 171.3K   4K   35   28
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionChange Language Pin
murat887-Sep-13 1:47
murat887-Sep-13 1:47 
Generalpls help me am searching for kannada or tamil language Pin
Member 762726419-Jan-11 23:53
Member 762726419-Jan-11 23:53 
QuestionHow to stop the Spvoice working ? Pin
xcstone13-Aug-09 3:40
xcstone13-Aug-09 3:40 
AnswerGetTranscript Pin
Member 25569753-Apr-09 14:31
Member 25569753-Apr-09 14:31 
GeneralFor all of you trying to use SAPI 5.1 in web application... Pin
lepipele5-Mar-09 10:02
lepipele5-Mar-09 10:02 
GeneralUsing Multi-lingual TTS in SAPI 5.1 Pin
Venkatesh.P2-Jul-08 0:58
Venkatesh.P2-Jul-08 0:58 
Questioni need speech application in asp.net Pin
magunta17-Apr-08 18:50
magunta17-Apr-08 18:50 
GeneralAccess is denied. (Exception from HRESULT: 0X80070005 (E_ACCESSDENIED)) Pin
Member 234429720-Feb-08 6:58
Member 234429720-Feb-08 6:58 
GeneralRe: Access is denied. (Exception from HRESULT: 0X80070005 (E_ACCESSDENIED)) Pin
zirosman27-Apr-08 23:10
zirosman27-Apr-08 23:10 
GeneralWav file not playable in windows media player Pin
mjmim24-Jul-07 12:25
mjmim24-Jul-07 12:25 
GeneralRe: Wav file not playable in windows media player Pin
Sabah u Din Irfan30-Jul-07 8:10
Sabah u Din Irfan30-Jul-07 8:10 
QuestionMore than one voice? Pin
Graham Dean18-Feb-07 7:45
Graham Dean18-Feb-07 7:45 
Questionis it possible to use the text2speech on web application? Pin
suriya_hoo8-Jan-07 3:14
suriya_hoo8-Jan-07 3:14 
AnswerRe: is it possible to use the text2speech on web application? Pin
Sabah u Din Irfan12-Jan-07 11:27
Sabah u Din Irfan12-Jan-07 11:27 
GeneralRe: is it possible to use the text2speech on web application? Pin
Sabah u Din Irfan12-Jan-07 11:38
Sabah u Din Irfan12-Jan-07 11:38 
GeneralRe: is it possible to use the text2speech on web application? Pin
Sabah u Din Irfan12-Jan-07 11:41
Sabah u Din Irfan12-Jan-07 11:41 
QuestionRe: is it possible to use the text2speech on web application? Pin
suriya_hoo18-Jan-07 2:09
suriya_hoo18-Jan-07 2:09 
AnswerRe: is it possible to use the text2speech on web application? Pin
Ahmad Zebdieh18-Jan-07 11:55
Ahmad Zebdieh18-Jan-07 11:55 
GeneralRe: is it possible to use the text2speech on web application? Pin
mintos200018-Oct-07 4:47
mintos200018-Oct-07 4:47 
GeneralRe: is it possible to use the text2speech on web application? Pin
Nevin Morrison30-Oct-07 18:41
Nevin Morrison30-Oct-07 18:41 
GeneralRe: is it possible to use the text2speech on web application? Pin
dstclair13-Nov-07 6:16
dstclair13-Nov-07 6:16 
AnswerRe: is it possible to use the text2speech on web application? Pin
Nevin Morrison30-Oct-07 18:48
Nevin Morrison30-Oct-07 18:48 
GeneralRe: is it possible to use the text2speech on web application? Pin
grossste11-Sep-08 1:05
grossste11-Sep-08 1:05 
GeneralSAPI 5.1 Pin
dafar22-Dec-06 3:17
dafar22-Dec-06 3:17 
GeneralRe: SAPI 5.1 Pin
Sabah u Din Irfan26-Dec-06 11:24
Sabah u Din Irfan26-Dec-06 11:24 

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.