Introduction
This is a simple example that will elaborate how to use the different feature of SpVoive class of SAPI 5.1 to make your applications speak out. Once I needed to use Microsoft SAPI in one of the project while helping a dearest one. I really ammazed you can make use of TTS with in very short time. This example will show you the following features of 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 wav file and save it.
Using the code
To make use of the code you will need to install the Microsoft Spech API 5.1 and you can download it from the Microsoft's site freely. This is the link of that :
http://www.microsoft.com/speech/download/old/sapi5.asp
After installing the Microsoft SAPI 5.1 just download the source and open it in Visual Studio 2005. Before compiling or builiding the project add refrence to the following DLL file (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 snipts of the code thaat is providing the different features.
Play / Speak
To play/Speak a text, create an object of SpVoice class and all its speak method that will speak out the text provided to it. Code example is following:
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 Resume method of SpVoice class like,
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 trackbar values :Minimum = - 10 and Maximum = 10. and you can capture the changed value on Scroll event of the trackbar.
speech.Rate = speechRate; // speechRate ranges from -10 to 10.
Voume
You can use the volume method 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 object of ISpeechObjectToken. You can populate them in a comboBox so that you can change it on rum time from the ComboBox. You should populate this comboBox while Initializing the components for the windows form or in onLoad() method of the windows form. The 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 WAV File
You can convert the text into WAV file rather than speaking it out. The following code snipt will provide you the complete functionality for making the WAV file for the spoken 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.");
}
Download Sample Code:
You can download the sample code form the following link:
http://www.codeproject.com/useritems/TTSFeaturesOfSAPI.asp
No comments:
Post a Comment