Congratulations to all monthly winners of May 2013 !!! They have won INR 2900 cash and INR 27497 worth prize.
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 27234 |  Welcome, Guest!   Register  Login
Home > Articles > .NET Framework > Build Your Talking Application

Build Your Talking Application

5 vote(s)
Rating: 3.6 out of 5
Article posted by Abhi2434 on 3/17/2010 | Views: 6390 | Category: .NET Framework | Level: Beginner red flag

Advertisements

Advertisements
In this application it is discussed how you can use Windows Narrator to create talking application.


Introduction


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 doesn't 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 lstVoice = this.LoadSelectVoiceMenu();



private List LoadSelectVoiceMenu()
{
List lst = new List();
foreach (InstalledVoice voice in spsynthesizer.GetInstalledVoices())
{

lst.Items.Add(voice.VoiceInfo);
}
}
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. I will discuss more about it in my next article in this topic.
Thank you for reading.

Advertisements

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About Abhishek Sur

Experience:3 year(s)
Home page:http://www.abhisheksur.com
Member since:Wednesday, December 02, 2009
Level:Silver
Status: [Member] [Microsoft_MVP] [MVP]
Biography:Working for last 2 and 1/2 years in .NET environment with profound knowledge on basics of most of the topics on it.
 Responses
Posted by: Vuyiswamb | Posted on: 19 Mar 2010 03:02:34 AM

Nice Article. Now can we convert it to Voice to Text ?

Posted by: Abhi2434 | Posted on: 19 Mar 2010 03:46:36 AM

Yes... I have already done the same thing. And it works as well..

I think you will get the complete article with Synthesis API usage very shortly.

Cheers.

>> Write Response - Respond to this post and get points
Related Posts

Here I am showing how you can invoke a method asynchronously using delegates and AsyncCallback

This article describes step by step optimizing XML Serialization using SGEN.exe utility provided by Microsoft in .Net SDK

Reflection is one of the main features of .Net framework . In short it is a powerful way of collecting and manipulate information present in application's assemblies and its metadata. Metadata contain all the Type information used by the application.(Metadata - Data about data). The ability to obtain information at runtime also makes it even more advantageous. When reflection is used along with system.type, it allows the developer to get the valuable information about all the types and about the assemblies. We can even create the instances and then invoke various types that are used across the application.

Collection initializers are series of object initializers thus providing a shorthand method for initializing collections. Its very useful feature provided by C# 3.0 and needs to be utilized in conjunction with object initializers.

The purpose of this article is to describe some of the practical uses of the Reflection.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 6/18/2013 3:36:31 AM