In my last post I covered how to perform basic speech recognition with a simple grammar and a “Hello World” WAV recording in C++. This post will cover how to accomplish what was in the previous post, but this time using the managed APIs.
Sound Check, Round 2
The support files for this example can be found here. Both HelloWorld.xml and HelloWorld.wav are required.
Again, in addition to the helper files the Windows SDK is also required.
Hello, Hello? Is this C#?
The most basic Speech recognition work flow is this:
- Create a Speech Recognition Engine
- Load a Grammar
- Set the Engine’s input
- Handle Recognition Events
- Start Recognition
First, launch Visual Studio and create a new Visual C# Console Application. Copy the two helper files to the project root directory, add them to the project and set their properties to “Copy on Build”.
Next, it is time to create a SpeechRecognitionEngine and Grammar:
SpeechRecognitionEngine srEngine = new SpeechRecognitionEngine();Grammar gram = new Grammar("helloworld.xml");
Now, load the grammar into the engine and set the engine’s input to the sample wave file:
Last thing to do is handle the recognition events and start recognition:
srEngine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(srEngine_SpeechRecognized);
Here’s an easy example for the Recognition Handler:
static void srEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
Console.WriteLine("Detected: " + e.Result.Text);
That’s it. When the application runs, “Detected: Hello world” should be written to the console window.
Now you have a simple recognition example in just 10 lines of code.
Try out some of the SAPI experiments listed in my first Speech 101 post. Also, look at the different recognition events and results.
MSDN’s Documentation on the Speech Recognition Engine:
MSDN’s Starting Point for Speech Recognition: