RecognizedAudio.GetRange Method (TimeSpan, TimeSpan)

 

Selects and returns a section of the current recognized audio as binary data.

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

public RecognizedAudio GetRange(
	TimeSpan audioPosition,
	TimeSpan duration
)

Parameters

audioPosition
Type: System.TimeSpan

The starting point of the audio data to be returned.

duration
Type: System.TimeSpan

The length of the segment to be returned.

Return Value

Type: System.Speech.Recognition.RecognizedAudio

Returns a subsection of the recognized audio, as defined by audioPosition and duration.

Exception Condition
ArgumentOutOfRangeException

audioPosition and duration define a segment of audio outside the range of the current segment.

InvalidOperationException

The current recognized audio contains no data.

The following example creates a speech recognition grammar for name input, adds a handler for the SpeechRecognized event, and loads the grammar into an in-process speech recognizer. Then it writes the audio information for the name portion of the input to an audio file. The audio file is used as input to a SpeechSynthesizer object, which speaks a phrase that includes the recorded audio.

private static void AddNameGrammar(SpeechRecognitionEngine recognizer)
{
  GrammarBuilder builder = new GrammarBuilder();
  builder.Append("My name is");
  builder.AppendWildcard();

  Grammar nameGrammar = new Grammar(builder);
  nameGrammar.Name = "Name Grammar";
  nameGrammar.SpeechRecognized +=
    new EventHandler<SpeechRecognizedEventArgs>(
      NameSpeechRecognized);

  recognizer.LoadGrammar(nameGrammar);
}

// Handle the SpeechRecognized event of the name grammar.
private static void NameSpeechRecognized(
  object sender, SpeechRecognizedEventArgs e)
{
  Console.WriteLine("Grammar ({0}) recognized speech: {1}",
    e.Result.Grammar.Name, e.Result.Text);

  try
  {

    // The name phrase starts after the first three words.
    if (e.Result.Words.Count < 4)
    {

      // Add code to check for an alternate that contains the wildcard.
      return;
    }

    RecognizedAudio audio = e.Result.Audio;
    TimeSpan start = e.Result.Words[3].AudioPosition;
    TimeSpan duration = audio.Duration - start;

    // Add code to verify and persist the audio.
    string path = @"C:\temp\nameAudio.wav";
    using (Stream outputStream = new FileStream(path, FileMode.Create))
    {
      RecognizedAudio nameAudio = audio.GetRange(start, duration);
      nameAudio.WriteToWaveStream(outputStream);
      outputStream.Close();
    }

    Thread testThread =
      new Thread(new ParameterizedThreadStart(TestAudio));
    testThread.Start(path);
  }
  catch (Exception ex)
  {
    Console.WriteLine("Exception thrown while processing audio:");
    Console.WriteLine(ex.ToString());
  }
}

// Use the speech synthesizer to play back the .wav file
// that was created in the SpeechRecognized event handler.

private static void TestAudio(object item)
{
  string path = item as string;
  if (path != null && File.Exists(path))
  {
    SpeechSynthesizer synthesizer = new SpeechSynthesizer();
    PromptBuilder builder = new PromptBuilder();
    builder.AppendText("Hello");
    builder.AppendAudio(path);
    synthesizer.Speak(builder);
  }
}

.NET Framework
Available since 3.0
Return to top
Show: