SpeechRecognizer SpeechRecognizer SpeechRecognizer SpeechRecognizer Class

Definition

Provides access to the shared speech recognition service available on the Windows desktop.

public ref class SpeechRecognizer : IDisposable
public class SpeechRecognizer : IDisposable
type SpeechRecognizer = class
    interface IDisposable
Public Class SpeechRecognizer
Implements IDisposable
Inheritance
SpeechRecognizerSpeechRecognizerSpeechRecognizerSpeechRecognizer
Implements

Examples

The following example is part of a console application that loads a speech recognition grammar and demonstrates asynchronous emulated input, the associated recognition results, and the associated events raised by the speech recognizer. If Windows Speech Recognition is not running, then starting this application will also start Windows Speech Recognition. If Windows Speech Recognition is in the Sleeping state, then EmulateRecognizeAsync always returns null.

using System;  
using System.Speech.Recognition;  
using System.Threading;  

namespace SharedRecognizer  
{  
  class Program  
  {  

    // Indicate whether the asynchronous emulate recognition  
    // operation has completed.  
    static bool completed;  

    static void Main(string[] args)  
    {  

      // Initialize an instance of the shared recognizer.  
      using (SpeechRecognizer recognizer = new SpeechRecognizer())  
      {  

        // Create and load a sample grammar.  
        Grammar testGrammar =  
          new Grammar(new GrammarBuilder("testing testing"));  
        testGrammar.Name = "Test Grammar";  
        recognizer.LoadGrammar(testGrammar);  

        // Attach event handlers for recognition events.  
        recognizer.SpeechRecognized +=  
          new EventHandler<SpeechRecognizedEventArgs>(  
            SpeechRecognizedHandler);  
        recognizer.EmulateRecognizeCompleted +=  
          new EventHandler<EmulateRecognizeCompletedEventArgs>(  
            EmulateRecognizeCompletedHandler);  

        completed = false;  

        // Start asynchronous emulated recognition.   
        // This matches the grammar and generates a SpeechRecognized event.  
        recognizer.EmulateRecognizeAsync("testing testing");  

        // Wait for the asynchronous operation to complete.  
        while (!completed)  
        {  
          Thread.Sleep(333);  
        }  

        completed = false;  

        // Start asynchronous emulated recognition.  
        // This does not match the grammar or generate a SpeechRecognized event.  
        recognizer.EmulateRecognizeAsync("testing one two three");  

        // Wait for the asynchronous operation to complete.  
        while (!completed)  
        {  
          Thread.Sleep(333);  
        }  
      }  

      Console.WriteLine();  
      Console.WriteLine("Press any key to exit...");  
      Console.ReadKey();  
    }  

    // Handle the SpeechRecognized event.  
    static void SpeechRecognizedHandler(  
      object sender, SpeechRecognizedEventArgs e)  
    {  
      if (e.Result != null)  
      {  
        Console.WriteLine("Recognition result = {0}",  
          e.Result.Text ?? "<no text>");  
      }  
      else  
      {  
        Console.WriteLine("No recognition result");  
      }  
    }  

    // Handle the SpeechRecognizeCompleted event.  
    static void EmulateRecognizeCompletedHandler(  
      object sender, EmulateRecognizeCompletedEventArgs e)  
    {  
      if (e.Result == null)  
      {  
        Console.WriteLine("No result generated.");  
      }  

      // Indicate the asynchronous operation is complete.  
      completed = true;  
    }  
  }  
}  

Remarks

Applications use the shared recognizer to access Windows Speech Recognition. Use the SpeechRecognizer object to add to the Windows speech user experience.

This class provides control over various aspects of the speech recognition process:

The configuration of Windows Speech Recognition is managed by the use of the Speech Properties dialog in the Control Panel. This interface is used to select the default desktop speech recognition engine and language, the audio input device, and the sleep behavior of speech recognition. If the configuration of Windows Speech Recognition is changed while the application is running, (for instance, if speech recognition is disabled or the input language is changed), the change affects all SpeechRecognizer objects.

To create an in-process speech recognizer that is independent of Windows Speech Recognition, use the SpeechRecognitionEngine class.

Note

Always call Dispose before you release your last reference to the speech recognizer. Otherwise, the resources it is using will not be freed until the garbage collector calls the recognizer object's Finalize method.

Constructors

SpeechRecognizer() SpeechRecognizer() SpeechRecognizer() SpeechRecognizer()

Initializes a new instance of the SpeechRecognizer class.

Properties

AudioFormat AudioFormat AudioFormat AudioFormat

Gets the format of the audio being received by the speech recognizer.

AudioLevel AudioLevel AudioLevel AudioLevel

Gets the level of the audio being received by the speech recognizer.

AudioPosition AudioPosition AudioPosition AudioPosition

Gets the current location in the audio stream being generated by the device that is providing input to the speech recognizer.

AudioState AudioState AudioState AudioState

Gets the state of the audio being received by the speech recognizer.

Enabled Enabled Enabled Enabled

Gets or sets a value that indicates whether this SpeechRecognizer object is ready to process speech.

Grammars Grammars Grammars Grammars

Gets a collection of the Grammar objects that are loaded in this SpeechRecognizer instance.

MaxAlternates MaxAlternates MaxAlternates MaxAlternates

Gets or sets the maximum number of alternate recognition results that the shared recognizer returns for each recognition operation.

PauseRecognizerOnRecognition PauseRecognizerOnRecognition PauseRecognizerOnRecognition PauseRecognizerOnRecognition

Gets or sets a value that indicates whether the shared recognizer pauses recognition operations while an application is handling a SpeechRecognized event.

RecognizerAudioPosition RecognizerAudioPosition RecognizerAudioPosition RecognizerAudioPosition

Gets the current location of the recognizer in the audio input that it is processing.

RecognizerInfo RecognizerInfo RecognizerInfo RecognizerInfo

Gets information about the shared speech recognizer.

State State State State

Gets the state of a SpeechRecognizer object.

Methods

Dispose() Dispose() Dispose() Dispose()

Disposes the SpeechRecognizer object.

Dispose(Boolean) Dispose(Boolean) Dispose(Boolean) Dispose(Boolean)

Disposes the SpeechRecognizer object and releases resources used during the session.

EmulateRecognize(RecognizedWordUnit[], CompareOptions) EmulateRecognize(RecognizedWordUnit[], CompareOptions) EmulateRecognize(RecognizedWordUnit[], CompareOptions) EmulateRecognize(RecognizedWordUnit[], CompareOptions)

Emulates input of specific words to the shared speech recognizer, using text instead of audio for synchronous speech recognition, and specifies how the recognizer handles Unicode comparison between the words and the loaded speech recognition grammars.

EmulateRecognize(String) EmulateRecognize(String) EmulateRecognize(String) EmulateRecognize(String)

Emulates input of a phrase to the shared speech recognizer, using text instead of audio for synchronous speech recognition.

EmulateRecognize(String, CompareOptions) EmulateRecognize(String, CompareOptions) EmulateRecognize(String, CompareOptions) EmulateRecognize(String, CompareOptions)

Emulates input of a phrase to the shared speech recognizer, using text instead of audio for synchronous speech recognition, and specifies how the recognizer handles Unicode comparison between the phrase and the loaded speech recognition grammars.

EmulateRecognizeAsync(RecognizedWordUnit[], CompareOptions) EmulateRecognizeAsync(RecognizedWordUnit[], CompareOptions) EmulateRecognizeAsync(RecognizedWordUnit[], CompareOptions) EmulateRecognizeAsync(RecognizedWordUnit[], CompareOptions)

Emulates input of specific words to the shared speech recognizer, using text instead of audio for asynchronous speech recognition, and specifies how the recognizer handles Unicode comparison between the words and the loaded speech recognition grammars.

EmulateRecognizeAsync(String) EmulateRecognizeAsync(String) EmulateRecognizeAsync(String) EmulateRecognizeAsync(String)

Emulates input of a phrase to the shared speech recognizer, using text instead of audio for asynchronous speech recognition.

EmulateRecognizeAsync(String, CompareOptions) EmulateRecognizeAsync(String, CompareOptions) EmulateRecognizeAsync(String, CompareOptions) EmulateRecognizeAsync(String, CompareOptions)

Emulates input of a phrase to the shared speech recognizer, using text instead of audio for asynchronous speech recognition, and specifies how the recognizer handles Unicode comparison between the phrase and the loaded speech recognition grammars.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

Gets the Type of the current instance.

(Inherited from Object)
LoadGrammar(Grammar) LoadGrammar(Grammar) LoadGrammar(Grammar) LoadGrammar(Grammar)

Loads a speech recognition grammar.

LoadGrammarAsync(Grammar) LoadGrammarAsync(Grammar) LoadGrammarAsync(Grammar) LoadGrammarAsync(Grammar)

Asynchronously loads a speech recognition grammar.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
RequestRecognizerUpdate() RequestRecognizerUpdate() RequestRecognizerUpdate() RequestRecognizerUpdate()

Requests that the shared recognizer pause and update its state.

RequestRecognizerUpdate(Object) RequestRecognizerUpdate(Object) RequestRecognizerUpdate(Object) RequestRecognizerUpdate(Object)

Requests that the shared recognizer pause and update its state and provides a user token for the associated event.

RequestRecognizerUpdate(Object, TimeSpan) RequestRecognizerUpdate(Object, TimeSpan) RequestRecognizerUpdate(Object, TimeSpan) RequestRecognizerUpdate(Object, TimeSpan)

Requests that the shared recognizer pause and update its state and provides an offset and a user token for the associated event.

ToString() ToString() ToString() ToString()

Returns a string that represents the current object.

(Inherited from Object)
UnloadAllGrammars() UnloadAllGrammars() UnloadAllGrammars() UnloadAllGrammars()

Unloads all speech recognition grammars from the shared recognizer.

UnloadGrammar(Grammar) UnloadGrammar(Grammar) UnloadGrammar(Grammar) UnloadGrammar(Grammar)

Unloads a specified speech recognition grammar from the shared recognizer.

Events

AudioLevelUpdated AudioLevelUpdated AudioLevelUpdated AudioLevelUpdated

Occurs when the shared recognizer reports the level of its audio input.

AudioSignalProblemOccurred AudioSignalProblemOccurred AudioSignalProblemOccurred AudioSignalProblemOccurred

Occurs when the recognizer encounters a problem in the audio signal.

AudioStateChanged AudioStateChanged AudioStateChanged AudioStateChanged

Occurs when the state changes in the audio being received by the recognizer.

EmulateRecognizeCompleted EmulateRecognizeCompleted EmulateRecognizeCompleted EmulateRecognizeCompleted

Occurs when the shared recognizer finalizes an asynchronous recognition operation for emulated input.

LoadGrammarCompleted LoadGrammarCompleted LoadGrammarCompleted LoadGrammarCompleted

Occurs when the recognizer finishes the asynchronous loading of a speech recognition grammar.

RecognizerUpdateReached RecognizerUpdateReached RecognizerUpdateReached RecognizerUpdateReached

Occurs when the recognizer pauses to synchronize recognition and other operations.

SpeechDetected SpeechDetected SpeechDetected SpeechDetected

Occurs when the recognizer detects input that it can identify as speech.

SpeechHypothesized SpeechHypothesized SpeechHypothesized SpeechHypothesized

Occurs when the recognizer has recognized a word or words that may be a component of multiple complete phrases in a grammar.

SpeechRecognitionRejected SpeechRecognitionRejected SpeechRecognitionRejected SpeechRecognitionRejected

Occurs when the recognizer receives input that does not match any of the speech recognition grammars it has loaded.

SpeechRecognized SpeechRecognized SpeechRecognized SpeechRecognized

Occurs when the recognizer receives input that matches one of its speech recognition grammars.

StateChanged StateChanged StateChanged StateChanged

Occurs when the running state of the Windows Desktop Speech Technology recognition engine changes.

Applies to

See also