Dynamically Loading/Unloading Grammar – Kinect Speech Recognition Engine

Dynamically Loading/Unloading Grammar – Kinect Speech Recognition Engine

Microsoft Speech API provides the SpeechRecognitionEngine class, which works as a backbone for the speech-enabled application. With the Kinect SDK as well, the SpeechRecognitionEngine class accepts an audio stream from the Kinect sensor and processes it. The Recognition engines, requires set of grammars ( set of commands), that tell the recognizer what to match.  You can build the grammars either using Choices Class or using XML documentation approach.

Once you are done with defining the grammar,  you need to load the grammar to the speech recognizer as follows:

Grammar grammar = new Grammar(grammarBuilder);
speechRecognizer.LoadGrammar(grammar);

Speech Recognition Engine Flow

Fig: Building Grammar using Choices / XML and Loading into Speech Recognizer

To build an extensive speech enable application you need multiple set of commands, that enables you to need to multiple grammar module to load inside the speech recognizer.

Load Multiple Grammar in Speech Recognition Engine

Fig : Loading Multiple Grammar

Now, when you have the large set of grammar modules, you may required to unload some of them and load few other set of grammar ! that how you can enable the dynamic nature of a application.  The SpeechRecognitionEngine class has the UnloadGrammar method. Using that you can unload the grammars when required. To unload a grammar, you have to pass the grammar name as an argument in the UnloadGrammar method.

speechRecognizer.UnloadGrammar(grammar);

You can even use the UnloadAllGrammars method to unload all the grammars. Once the grammar is unloaded, you have to load it again for further use.

Load and Unload Grammar

Fig: Loading and Unloading the grammars
Couple of days back, one of my Kinect dev. friend was asked me this question. I answered him and while I looked back, I found one of my reply on MSDN Forum , which  was given almost one and half years back.  So, I thought to share over here with some more level of details for your further reference and understanding.

Hope you liked it!

Don’t forgot to check out the Kinect for Windows SDK Tips

Abhijit Jana

Abhijit runs the Daily .NET Tips. He started this site with a vision to have a single knowledge base of .NET tips and tricks and share post that can quickly help any developers . He is a Former Microsoft ASP.NET MVP, CodeProject MVP, Mentor, Speaker, Author, Technology Evangelist and presently working as a .NET Consultant. He blogs at http://abhijitjana.net , you can follow him @AbhijitJana . He is the author of book Kinect for Windows SDK Programming Guide.