Compartilhar via


Speech 101, Part 2 - Using C# to Recognize “Hello World”

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:

  1. Create a Speech Recognition Engine
  2. Load a Grammar
  3. Set the Engine’s input
  4. Handle Recognition Events
  5. 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:

srEngine.LoadGrammar(gram);
srEngine.SetInputToWaveFile("helloworld.wav");

Last thing to do is handle the recognition events and start recognition:

srEngine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(srEngine_SpeechRecognized);
srEngine.Recognize();

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.

What Next?

Try out some of the SAPI experiments listed in my first Speech 101 post. Also, look at the different recognition events and results. 

Resources

MSDN’s Documentation on the Speech Recognition Engine:
https://msdn.microsoft.com/en-us/library/hh361636.aspx

MSDN’s Starting Point for Speech Recognition:
https://msdn.microsoft.com/en-us/library/hh361633.aspx

Comments

  • Anonymous
    February 02, 2012
    Thank you Robert for this article, I am going to test it. Regards !

  • Anonymous
    February 03, 2012
    thaks

  • Anonymous
    February 06, 2012
    really good

  • Anonymous
    February 10, 2012
    Thanks a lot Robert, really helpful

  • Anonymous
    February 11, 2012
    can i use it as a trigger event,for user imput  is that what you mean

  • Anonymous
    February 14, 2012
    Muy bueno, haré mas pruebas!!!

  • Anonymous
    February 21, 2012
    More insight for u. Tx

  • Anonymous
    February 23, 2012
    Thanks for your visiting at www.sysnet.pe.kr/.../1228 Also, thanks to your post, I could write the article in Korean. :)

  • Anonymous
    February 23, 2012
    cool story bro, tell it again

  • Anonymous
    March 06, 2012
    can you give real time speach recoganization with code

  • Anonymous
    March 07, 2012
    nice

  • Anonymous
    March 14, 2012
    very good and useful

  • Anonymous
    March 18, 2012
    nice one

  • Anonymous
    March 29, 2012
    its useful

  • Anonymous
    March 29, 2012
    Thanks - will try.  Short and sweet.

  • Anonymous
    April 13, 2012
    are this program is applicable in visual studio 2008?

  • Anonymous
    April 13, 2012
    @Prateek, Yes I believe that this will work within VS2008.  Just install the required dependencies and the latest versions of .NET.