Click here to Skip to main content
Click here to Skip to main content
Go to top

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

, 11 Dec 2006
Rate this:
Please Sign up or sign in to vote.
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:

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:

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.

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:

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:

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:

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)

Share

About the Author

Sabah u Din Irfan

United States United States
No Biography provided

Comments and Discussions

 
QuestionChange Language Pinmembermurat887-Sep-13 1:47 
Generalpls help me am searching for kannada or tamil language PinmemberMember 762726419-Jan-11 23:53 
QuestionHow to stop the Spvoice working ? Pinmemberxcstone13-Aug-09 3:40 
AnswerGetTranscript PinmemberMember 25569753-Apr-09 14:31 
GeneralFor all of you trying to use SAPI 5.1 in web application... PinmemberPredrag Tomasevic5-Mar-09 10:02 
GeneralUsing Multi-lingual TTS in SAPI 5.1 PinmemberVenkatesh.P2-Jul-08 0:58 
Questioni need speech application in asp.net Pinmembermagunta17-Apr-08 18:50 
GeneralAccess is denied. (Exception from HRESULT: 0X80070005 (E_ACCESSDENIED)) PinmemberMember 234429720-Feb-08 6:58 
GeneralRe: Access is denied. (Exception from HRESULT: 0X80070005 (E_ACCESSDENIED)) Pinmemberzirosman27-Apr-08 23:10 
GeneralWav file not playable in windows media player Pinmembermjmim24-Jul-07 12:25 
GeneralRe: Wav file not playable in windows media player PinmemberSabah u Din Irfan30-Jul-07 8:10 
QuestionMore than one voice? PinmemberGraham Dean18-Feb-07 7:45 
Questionis it possible to use the text2speech on web application? Pinmembersuriya_hoo8-Jan-07 3:14 
AnswerRe: is it possible to use the text2speech on web application? PinmemberSabah u Din Irfan12-Jan-07 11:27 
GeneralRe: is it possible to use the text2speech on web application? PinmemberSabah u Din Irfan12-Jan-07 11:38 
GeneralRe: is it possible to use the text2speech on web application? PinmemberSabah u Din Irfan12-Jan-07 11:41 
QuestionRe: is it possible to use the text2speech on web application? Pinmembersuriya_hoo18-Jan-07 2:09 
AnswerRe: is it possible to use the text2speech on web application? PinmemberAhmad Zebdieh18-Jan-07 11:55 
GeneralRe: is it possible to use the text2speech on web application? Pinmembermintos200018-Oct-07 4:47 
GeneralRe: is it possible to use the text2speech on web application? PinmemberNevin Morrison30-Oct-07 18:41 
GeneralRe: is it possible to use the text2speech on web application? Pinmemberdstclair13-Nov-07 6:16 
AnswerRe: is it possible to use the text2speech on web application? PinmemberNevin Morrison30-Oct-07 18:48 
GeneralRe: is it possible to use the text2speech on web application? Pinmembergrossste11-Sep-08 1:05 
GeneralSAPI 5.1 Pinmemberdafar22-Dec-06 3:17 
GeneralRe: SAPI 5.1 PinmemberSabah u Din Irfan26-Dec-06 11:24 
Questionspeech recognition Pinmemberdafar16-Dec-06 22:11 
AnswerRe: speech recognition PinmemberAcidCow18-Dec-06 19:18 
GeneralRe: speech recognition PinmemberSabah u Din Irfan20-Dec-06 12:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web02 | 2.8.140922.1 | Last Updated 11 Dec 2006
Article Copyright 2006 by Sabah u Din Irfan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid