Using Microsoft Speech Recognition, can I get a moment when it will start and when it will end?

I play with Microsoft's speech recognition engine. The code looks like this:

static ManualResetEvent _completed = null;
static void Main(string[] args)
{
     _completed = new ManualResetEvent(false);
     SpeechRecognitionEngine _recognizer = new SpeechRecognitionEngine();
     _recognizer.LoadGrammar(new Grammar(new GrammarBuilder("test")) Name = { "testGrammar" }); // load a grammar
     _recognizer.LoadGrammar(new Grammar(new GrammarBuilder("exit")) Name = { "exitGrammar" }); // load a "exit" grammar
     _recognizer.SpeechRecognized += _recognizer_SpeechRecognized; 
     _recognizer.SetInputToDefaultAudioDevice(); // set the input of the speech recognizer to the default audio device
     _recognizer.RecognizeAsync(RecognizeMode.Multiple); // recognize speech asynchronous
     _completed.WaitOne(); // wait until speech recognition is completed
     _recognizer.Dispose(); // dispose the speech recognition engine
} 
void _recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
     if (e.Result.Text == "test") // e.Result.Text contains the recognized text
     {
         Console.WriteLine("The test was successful!");
     } 
     else if (e.Result.Text == "exit")
     {
         _completed.Set();
     }
}

It seems to work very cool. And the program may appear when I say β€œtest” or β€œexit”. But can I get the exact moment when the program starts and when the program finishes testing and restarts to check another word?

+4
source share
2 answers

RecognitionResult.Audio has a start time and a sound duration.

void SpeechRecognizedHandler(object sender, SpeechRecognizedEventArgs e)
{
  if (e.Result == null) return;

  // Add event handler code here.

  // The following code illustrates some of the information available
  // in the recognition result.
      Console.WriteLine("Grammar({0}): {1}", e.Result.Grammar.Name, e.Result.Text);
      Console.WriteLine("Audio for result:");
      Console.WriteLine("  Start time: "+ e.Result.Audio.StartTime);
      Console.WriteLine("  Duration: " + e.Result.Audio.Duration);
      Console.WriteLine("  Format: " + e.Result.Audio.Format.EncodingFormat);
}
+1
source

There is an SpeechDetectedEvent in SpeechRecognitionEngine . You can use this to determine when it identifies the next word to be processed.

( ):

. SpeechRecognitionEngine , SpeechDetected, . AudioPosition SpeechDetectedEventArgs , . SpeechRecognitionEngine SpeechDetected , SpeechHypothesized, SpeechRecognized SpeechRecognitionRejected.

0

All Articles