Export (0) Print
Expand All

SpeechRecognitionEngine.EmulateRecognizeAsync Method (String)

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

Namespace:  System.Speech.Recognition
Assembly:  System.Speech (in System.Speech.dll)

public void EmulateRecognizeAsync(
	string inputText
)

Parameters

inputText
Type: System.String

The input for the recognition operation.

ExceptionCondition
InvalidOperationException

The recognizer has no speech recognition grammars loaded, or the recognizer has an asynchronous recognition operation that is not yet complete.

ArgumentNullException

inputText is null.

ArgumentException

inputText is the empty string ("").

The speech recognizer raises the SpeechDetected, SpeechHypothesized, SpeechRecognitionRejected, and SpeechRecognized events as if the recognition operation is not emulated. When the recognizer completes the asynchronous recognition operation, it raises the EmulateRecognizeCompleted event.

The recognizers that ship with Vista and Windows 7 ignore case and character width when applying grammar rules to the input phrase. For more information about this type of comparison, see the CompareOptions enumeration values OrdinalIgnoreCase and IgnoreWidth. The recognizers also ignore new lines and extra white space and treat punctuation as literal input.

The code example below is part of a console application that demonstrates asynchronous emulated input, the associated recognition results, and the associated events raised by the speech recognizer. The example generates the following output.

TestRecognizeAsync("Smith")...
 SpeechDetected event raised.
 SpeechRecognized event raised.
  Grammar = Smith; Text = Smith
 EmulateRecognizeCompleted event raised.
  Grammar = Smith; Text = Smith
 Done.

TestRecognizeAsync("Jones")...
 SpeechDetected event raised.
 SpeechRecognized event raised.
  Grammar = Jones; Text = Jones
 EmulateRecognizeCompleted event raised.
  Grammar = Jones; Text = Jones
 Done.

TestRecognizeAsync("Mister")...
 SpeechDetected event raised.
 SpeechHypothesized event raised.
  Grammar = Smith; Text = mister
 SpeechRecognitionRejected event raised.
  Grammar = <not available>; Text =
 EmulateRecognizeCompleted event raised.
  No recognition result available.
 Done.

TestRecognizeAsync("Mister Smith")...
 SpeechDetected event raised.
 SpeechRecognized event raised.
  Grammar = Smith; Text = mister Smith
 EmulateRecognizeCompleted event raised.
  Grammar = Smith; Text = mister Smith
 Done.

press any key to exit...
using System;
using System.Globalization;
using System.Speech.Recognition;
using System.Threading;

namespace SreEmulateRecognizeAsync
{
  class Program
  {
    // Indicate when an asynchronous operation is finished.
    static bool completed;

    static void Main(string[] args)
    {
      using (SpeechRecognitionEngine recognizer =
        new SpeechRecognitionEngine(new CultureInfo("en-US")))
      {
        // Load grammars.
        recognizer.LoadGrammar(CreateNameGrammar("Smith"));
        recognizer.LoadGrammar(CreateNameGrammar("Jones"));

        // Configure the audio input.
        recognizer.SetInputToNull();

        // Add event handlers for the events raised by the
        // EmulateRecognizeAsync method.
        recognizer.SpeechDetected +=
          new EventHandler<SpeechDetectedEventArgs>(
            SpeechDetectedHandler);
        recognizer.SpeechHypothesized +=
          new EventHandler<SpeechHypothesizedEventArgs>(
            SpeechHypothesizedHandler);
        recognizer.SpeechRecognitionRejected +=
          new EventHandler<SpeechRecognitionRejectedEventArgs>(
            SpeechRecognitionRejectedHandler);
        recognizer.SpeechRecognized +=
          new EventHandler<SpeechRecognizedEventArgs>(
            SpeechRecognizedHandler);
        recognizer.EmulateRecognizeCompleted +=
          new EventHandler<EmulateRecognizeCompletedEventArgs>(
            EmulateRecognizeCompletedHander);

        // Start four asynchronous emulated recognition operations.
        TestRecognizeAsync(recognizer, "Smith");
        TestRecognizeAsync(recognizer, "Jones");
        TestRecognizeAsync(recognizer, "Mister");
        TestRecognizeAsync(recognizer, "Mister Smith");
      }

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

    // Create a simple name grammar.
    // Set the grammar name to the surname.
    private static Grammar CreateNameGrammar(string surname)
    {
      GrammarBuilder builder = new GrammarBuilder("mister", 0, 1);
      builder.Append(surname);

      Grammar nameGrammar = new Grammar(builder);
      nameGrammar.Name = surname;

      return nameGrammar;
    }

    // Send emulated input to the recognizer for asynchronous
    // recognition.
    private static void TestRecognizeAsync(
      SpeechRecognitionEngine recognizer, string input)
    {
      completed = false;

      Console.WriteLine("TestRecognizeAsync(\"{0}\")...", input);
      recognizer.EmulateRecognizeAsync(input);

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

      Console.WriteLine(" Done.");
      Console.WriteLine();
    }

    static void SpeechDetectedHandler(
      object sender, SpeechDetectedEventArgs e)
    {
      Console.WriteLine(" SpeechDetected event raised.");
    }

    static void SpeechHypothesizedHandler(
      object sender, SpeechHypothesizedEventArgs e)
    {
      Console.WriteLine(" SpeechHypothesized event raised.");
      if (e.Result != null)
      {
        Console.WriteLine("  Grammar = {0}; Text = {1}",
          e.Result.Grammar.Name ?? "<none>", e.Result.Text);
      }
      else
      {
        Console.WriteLine("  No recognition result available.");
      }
    }

    // Handle events.
    static void SpeechRecognitionRejectedHandler(
      object sender, SpeechRecognitionRejectedEventArgs e)
    {
      Console.WriteLine(" SpeechRecognitionRejected event raised.");
      if (e.Result != null)
      {
        string grammarName;
        if (e.Result.Grammar != null)
        {
          grammarName = e.Result.Grammar.Name ?? "<none>";
        }
        else
        {
          grammarName = "<not available>";
        }
        Console.WriteLine("  Grammar = {0}; Text = {1}",
          grammarName, e.Result.Text);
      }
      else
      {
        Console.WriteLine("  No recognition result available.");
      }
    }

    static void SpeechRecognizedHandler(
      object sender, SpeechRecognizedEventArgs e)
    {
      Console.WriteLine(" SpeechRecognized event raised.");
      if (e.Result != null)
      {
        Console.WriteLine("  Grammar = {0}; Text = {1}",
          e.Result.Grammar.Name ?? "<none>", e.Result.Text );
      }
      else
      {
        Console.WriteLine("  No recognition result available.");
      }
    }

    static void EmulateRecognizeCompletedHander(
      object sender, EmulateRecognizeCompletedEventArgs e)
    {
      Console.WriteLine(" EmulateRecognizeCompleted event raised.");
      
      if (e.Error != null)
      {
        Console.WriteLine("  {0} exception encountered: {1}:",
          e.Error.GetType().Name, e.Error.Message);
      }
      else if (e.Cancelled)
      {
        Console.WriteLine("  Operation cancelled.");
      }
      else if (e.Result != null)
      {
        Console.WriteLine("  Grammar = {0}; Text = {1}",
          e.Result.Grammar.Name ?? "<none>", e.Result.Text);
      }
      else
      {
        Console.WriteLine("  No recognition result available.");
      }

      completed = true;
    }
  }
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft