Windows Speech Recognization Engine and Narrator utility comes with Windows XP service pack 2 or Office applications. Windows Narrator uses various windows items and speaks instantly. If you have never tried with it, it is time to try it first.
Open Start - > Programs -> Accessories - >Accessibility -> Narrator
After opening you will hear that a person is speaking out some weird terms. Actually this is nothing but your narrator speaking to help you. Actually the checkboxes determines various options like, reading events, Read typed characters etc. You can even change the narrator just by clicking on the Voice Button.
If you are not using .NET Framework 3.0 or above, this must be your only way to do this. First in this case you need to install Speech SDK 5.1. The speech SDK gives you the interface to work with SPVoice commands. The Speech SDK provides you two interfaces. One is for Text to Speech Conversion, while the other is for Speech Recognisation. In this article I am going to discuss only Text to Speech Conversion.
To do this, first create the Interop to the COM dll that comes with SAPI named speechlib.dll. You need to right click on your project, add Reference and add "Microsoft Speech Object Library" from COM tab.
After you add Interop.Speechlib.dll in your application you need to use some simple lines of code as below to run your application :
using SpeechLib; SpVoice speech = new SpVoice(); speech.Speak("Hello World",SpeechVoiceSpeakFlags.SVSFlagsAsync); speech.WaitUntilDone(Timeout.Infinite);
Thus if you run the above application, you will find that Microsoft Sam will say you "Hello World".
With the Introduction of .NET Framework 3.0, Microsoft released Managed API to handle these through your program and it doesnt need anything to be installed. Let us look how we can invoke using the new way.
First Add Reference to System.Speech.dll. You will find that in GAC if you have installed .NET 3.5 to your system.
As shown in the figure, after you add the dll you need to use some simple methods to invoke Speech To Text for your application.
To invoke this, you need to know about a class called SpeechSynthesizer. This is the main class to handle speech requests from the application and invokes the Narrator utility itself with configured narrator.
You can configure voice, speech, pitch of the narrator, and change the Narrator voice itself using the object of SpeechSynthesizer class.
Let us see how the code look like :
using System.Speech.Synthesis; SpeechSynthesizer spsynthesizer = new SpeechSynthesizer(); List<voiceinfo> lstVoice = this.LoadSelectVoiceMenu();Now when you want to invoke the Speaking utility just call :
spsynthesizer.SelectVoice(lstVoice.Name); //Invoke the one which you want spsynthesizer.Volume = 50; // Can be 1 - 100 spsynthesizer.Rate = 3; // can be 1 - 10 spsynthesizer.SpeakAsync("Hello World");
The SelectVoice method is used to choose the appropriate Narrator. If you have more than one narrator, you can use this method to SelectVoice. I have used lstVoice which should point to Microsoft Sam. The volume determines the volume of the voice, and Rate determines how fast the narrator speaks.
To Speak out the text, you have two method. I am using SpeakAsync, to call it asynchronously. You can also use Speak itself, which will wait until the text is spoken. While the text is been spoken using SpeakAsync, you can all SpeakAsyncCancel or SpeakAsyncCancelAll anytime to end the narrator instantly.
spsynthesizer.SpeakAsyncCancel(new Prompt("Lets End"));
if invoked will end the SpeakAsync and prompt Lets End.
Thus invoking a Text to Speech for your application become easiest like never before. You can use this without any heck.
In my next entry, I will be discussing about Speech Recognisation. So stay tune. Read Disclaimer Notice