Month: October 2011

Speaking and Listening Applications

Introduction

After a long period of silence, I decided to write my next blog post. I know that this is very interesting area and lots of new experiments going on. So in here I’m going to tell how to integrate text-to-speech and voice recognition capabilities to your application. This will help you to set your first step in this area.

This time I selected Visual C# as the language, because it provides very easy way to implement those. It is also essential to have .NET framework 3.0 or later version. With those implementing speaking /listening application is only a matter of few lines of code.

How to start

First open Visual C sharp and select a new project which allows building Console Application.

Create new project

For our purpose we have to add Speech API for our References. (By default it is not added)

For that Go to Solution Explorer, Right click on References and Click Add References.

Add Reference

From the Add reference window go to .NET tab, Click System.Speech and click OK.

Now the environment has settled. It’s time to code.

Text to speech application

First you need to add the following using statement.

using System.Speech.Synthesis;

Then add the following code where you want to implement the Text to speech capability.

SpeechSynthesizer synth = new SpeechSynthesizer();

synth.Speak("Hello from Student Guru!");

First it creates a SpeechSynthesizer object and then calls its Speak method. The text you want to read should be passed as the argument. But Speak method holds the program until its finish. This won’t create a program interactive. So instead Speak() you can use SpeakAsync(), which allows you to  execute your program while speaking is going on.

In addition to that SpeechSynthesizer alllow you to change voice using SelectVoice() method and save voice to .wav file using SetOutputToWaveFile() method.

Speech Recognition application

First you need to add

using System.Speech.Recognition;

Then add following code to a method you wish to do speech recognising,

SpeechRecognitionEngine recognitionEngine = new SpeechRecognitionEngine();

recognitionEngine.SetInputToDefaultAudioDevice();

recognitionEngine.LoadGrammar(new DictationGrammar());

RecognitionResult result = recognitionEngine.Recognize(new TimeSpan(0, 0, 20));

foreach (RecognizedWordUnit word in result.Words)

{

Console.Write("{0} ", word.Text);

}

After executing program, you have to speak to computer and it will detect your voice within first 20 seconds. If it fails to recognize any word it will show an error.

Note that this won’t give 100% accurate outputs, but this is for new comers.

In here I used DictationGrammer object to LoadGrammer, which is provided by windows desktop speech technology. But for special tasks you can build a new Grammer object using GrammerBuilder.

Recognition engine also allows you to read from a wave file and do the same.

References:

  1. Dr.dobbs web site:  http://drdobbs.com/windows/223101480?pgno=1
  2. StudentGuru: http://studentguru.gr/b/jupiter/archive/2010/02/01/speech-recognition-with-c-dictation-and-custom-grammar.aspx
  3. Microsoft Speech SDK: http://www.nextup.com/sapi5doc/Getting_Started.htm#Programmers_Guide
Advertisements