Try Text to Speech for your application

It is really easy and fun to create applications that speaks out for you. With the introduction with System.Speech, it seems to look like even better. It is to be noted, that Windows Environment comes with a unique feature that converts your voice commands to text equivalent long before the introduction to this in .NET. I will discuss now how you can easily use this in your application and get the work done quickly.

Background

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.
Here you can see Microsoft Sam is listed. This is the default .NET narrator. You can adjust speed, volume, pitch etc of the listed Narrators. You can also add some external Narrators for you if you wish. Now I am going to show you how you can invoke Narrator to be used in your own application.

Old Way
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".

NEW Way

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[0].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[0] 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.

Conclusion

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.
Shout it Submit this story to DotNetKicks Bookmark and Share
Read Disclaimer Notice

1 comments:

Good Work said...

Really appreciated.

Post a Comment

Please make sure that the question you ask is somehow related to the post you choose. Otherwise you post your general question in Forum section.

Author's new book

Abhishek authored one of the best selling book of .NET. It covers ASP.NET, WPF, Windows 8, Threading, Memory Management, Internals, Visual Studio, HTML5, JQuery and many more...
Grab it now !!!