Get Started with Speech Recognition (Microsoft.Speech)

Microsoft Speech Platform SDK 11

Collapse image Expand Image Copy image CopyHover image

A speech recognition application will typically perform the following basic operations:

  • Start the speech recognizer.

  • Create a recognition grammar.

  • Load the grammar into the speech recognizer.

  • Register for speech recognition event notification.

  • Create a handler for the speech recognition event.

The following provides information about how to program each of these operations.

Start the Speech Recognizer

To start the speech recognizer, create a new SpeechRecognitionEngine instance. The following example does not show the declaration of the sre object.

C#  Copy imageCopy Code
// Create a new SpeechRecognitionEngine instance.
sre = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US"));

Create a Speech Recognition Grammar

One way to create a speech recognition grammar is to use the constructors and methods on the GrammarBuilder and Choices classes. The following example creates a simple grammar that recognizes the words "red", "green", or "blue". The words are added using a Choices object. For a match between user speech and the grammar to occur, the user must speak exactly one of the elements added by the Choices instance. Each of the words is added by a call to the Add(array<String>[]()[][]) method.

After the Choices instance is created and set with the option strings, the example creates a GrammarBuilder instance. Using the Append(Choices) method, the example appends the colors object to the GrammarBuilder instance. In the last line, the example creates a Grammar instance and initializes it with the GrammarBuilder instance.

C#  Copy imageCopy Code
Choices colors = new Choices();
colors.Add(new string[] {"red"});
colors.Add(new string[] {"green"});
colors.Add(new string[] {"blue"});

GrammarBuilder gb = new GrammarBuilder();
gb.Append(colors);

// Create the Grammar instance.
Grammar g = new Grammar(gb);

Alternatively, the first four lines of code can be collapsed to a single line by using the Choices constructor to initialize the instance with an array of string options, as follows:

Choices colors = new Choices(new string[] {"red", "green", "blue"});

For more information about creating grammars, see Create Grammars (Microsoft.Speech).

Load the Grammar into a Speech Recognizer

After the grammar is created, it must be loaded into the speech recognizer. The following example loads the grammar by calling the LoadGrammar(Grammar) method, passing the grammar created in the previous operation.

C#  Copy imageCopy Code
sre.LoadGrammar(g);

Register for Speech Recognition Event Notification

The speech recognizer raises a number of events during its operation, including the SpeechRecognized event. For more information, see Use Speech Recognition Events (Microsoft.Speech). The speech recognizer raises the SpeechRecognized event when it matches a user utterance with a grammar. An application registers for notification of this event by appending an EventHandler instance as shown in the following example. The argument to the EventHandler constructor, sre_SpeechRecognized, is the name of the developer-written event handler.

C#  Copy imageCopy Code
sre.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized);

Create a Speech Recognition Event Handler

When you register a handler for a particular event, the Intellisense feature in Microsoft Visual Studio creates a skeleton event handler if you press the TAB key. This process ensures that parameters of the correct type are used. The handler for the SpeechRecognized event shown in the following example displays the text of the recognized word using the Result property on the SpeechRecognizedEventArgs parameter, e.

C#  Copy imageCopy Code
void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
  MessageBox.Show(e.Result.Text);
}

Speech Recognition Example

The following examples are components of a Windows System.Windows.Forms application that features speech recognition. Although the application displays a form when it starts, nothing of interest happens with the form. The first example contains the code for the handler for the form's Load event, which is raised when the form is loaded.

Almost everything of interest in this application occurs in the Form1_Load method. The method builds a grammar incrementally using a Choices instance to add the strings "red", "green", and "blue". It then creates a GrammarBuilder instance using the Choices object. The method then initializes a Grammar instance with the GrammarBuilder object created earlier. The grammar, which is capable of recognizing the words "red", "green", or "blue", is then loaded into the speech recognizer. Finally, the Form1_Load method registers an event handler for the SpeechRecognized()()()() event.

The sre_SpeechRecognized method, which executes when the speech recognizer raises the SpeechRecognized()()()() event, displays whichever of the three colors the user spoke. The following illustration shows the interaction between the user's speech and the speech recognizer with its grammar. When the utterance matches an element in the grammar, the speech recognizer makes a recognition, and produces a recognition result.

C#  Copy imageCopy Code
using Microsoft.Speech.Recognition;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

      // Create a new SpeechRecognitionEngine instance.
      sre = new SpeechRecognitionEngine();

      // Create a simple grammar that recognizes "red", "green", or "blue".
      Choices colors = new Choices();
      colors.Add(new string[] {"red", "green", "blue"});

      // Create a GrammarBuilder object and append the Choices object.
      GrammarBuilder gb = new GrammarBuilder();
      gb.Append(colors);

      // Create the Grammar instance and load it into the speech recognition engine.
      Grammar g = new Grammar(gb);
      sre.LoadGrammar(g);

      // Register a handler for the SpeechRecognized event.
      sre.SpeechRecognized +=
        new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized);
    }

    // Create a simple handler for the SpeechRecognized event.
    void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
      MessageBox.Show(e.Result.Text);
    }

    SpeechRecognitionEngine sre;
  }
}

The following example is autogenerated code for a Windows Forms application.

C#  Copy imageCopy Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
  static class Program
  {

    // The main entry point for the application.
    [STAThread]
    static void Main()
    {
      Application.EnableVisualStyles();
      Application.SetCompatibleTextRenderingDefault(false);
      Application.Run(new Form1());
    }
  }
}