Get Started with Speech Recognition
This topic provides an overview and examples for implementing speech recognition in a Windows Forms application. See the other topics in this section for more information and examples.
A speech recognition application will typically perform the following basic operations:
Initialize the speech recognizer.
Create a speech 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.
Note
The above steps apply if using the SpeechRecognizer class, which controls shared speech recognition in Windows. If using the SpeechRecognitionEngine class, which is independent of Windows Speech Recognition, two additional steps are required to setup recognition:
-
Set the input for speech recognition. See Audio Input for Recognition.
-
Start recognition. See Initialize and Manage a Speech Recognition Engine
Initialize the Speech Recognizer
To initialize an instance of the shared recognizer in Windows, use the SpeechRecognizer class.
// Create a new SpeechRecognizer instance.
SpeechRecognizer sr = new SpeechRecognizer();
To start an in-process speech recognizer that is under control of the application, create a new SpeechRecognitionEngine instance. See Initialize and Manage a Speech Recognition Engine for more information.
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. The example adds the words as a string array that is the argument to the Add([]) 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.
Choices colors = new Choices();
colors.Add(new string[] {"red", "green", "blue"});
GrammarBuilder gb = new GrammarBuilder();
gb.Append(colors);
// Create the Grammar instance.
Grammar g = new Grammar(gb);
For more information about creating grammars, see Create Grammars.
Load the Grammar into the 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.
sr.LoadGrammar(g);
See Initialize and Manage a Speech Recognition Engine for more information.
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. 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, sr_SpeechRecognized, is the name of the developer-written event handler.
sr.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(sr_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 or phrase using the Result property on the SpeechRecognizedEventArgs parameter, e.
void sr_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.
Note
To use the System.Speech types in this example, you must first add a reference in your project to System.Speech. Also, make sure that you have completed all the steps described in Setup the Development Environment for System.Speech.
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.
When the form loads, it automatically starts Windows Speech Recognition and its graphical user interface. You can activate speech recognition by saying "start listening" or by pressing the Microphone button. The sr_SpeechRecognized method, which executes when the speech recognizer raises the SpeechRecognized event, displays the name of any of the three colors that are recognized in the speech input. 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.
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;
using System.Speech.Recognition;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Create a new SpeechRecognitionEngine instance.
SpeechRecognizer recognizer = new SpeechRecognizer();
// 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);
recognizer.LoadGrammar(g);
// Register a handler for the SpeechRecognized event.
recognizer.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized);
}
// Create a simple handler for the SpeechRecognized event.
void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
MessageBox.Show("Speech recognized: " + e.Result.Text);
}
}
}
The following example is autogenerated code for a Windows Forms application.
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());
}
}
}