Share via


RecognizedAudio.GetRange Method

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

Namespace: Microsoft.Speech.Recognition
Assembly: Microsoft.Speech (in microsoft.speech.dll)

Syntax

'Declaration
Public Function GetRange ( _
    audioPosition As TimeSpan, _
    duration As TimeSpan _
) As RecognizedAudio
public RecognizedAudio GetRange (
    TimeSpan audioPosition,
    TimeSpan duration
)
public:
RecognizedAudio^ GetRange (
    TimeSpan audioPosition, 
    TimeSpan duration
)
public RecognizedAudio GetRange (
    TimeSpan audioPosition, 
    TimeSpan duration
)
public function GetRange (
    audioPosition : TimeSpan, 
    duration : TimeSpan
) : RecognizedAudio

Parameters

  • audioPosition
    A System.TimeSpan indicating the starting point of the audio to be returned.
  • duration
    A System.TimeSpan indicating length of the segment following audioPosition to be returned.

Return Value

Returns an instance of RecognizedAudio containing the subset of the original input selected by the values of audioPosition and duration.

Remarks

A System.ArgumentOutOfRangeException event is generated if audioPosition and duration attempt to reference beyond the bound the recognized audio.

Example

The example below show a testing routine that uses WriteToWaveStream, and WriteToAudioStream to write all the recognized audio to a stream, as well as GetRange to write selected portions of that audio to a stream.

Note

Because of the granularity of ticks and the non-zero size of the wave header, the size of a write to stream generated from a call such as GetRange(newTimeSpan(0),newTimeSpan(audio.Duration.Ticks/2) may not be exactly half that of the write from calls such as WriteToWaveStream(waveStream) or WriteToAudioStream(audioStream). The example below takes this into account.

public void AudioRangeTest (RecognitionResult recognitionResult)
{
  RecognizedAudio audio = recognitionResult.Audio;
  
  // Write to stream using WriteToAudioStream
  MemoryStream audioStream = new MemoryStream ();
  audio.WriteToAudioStream (audioStream);
  audioStream.Flush ();

  // Write to stream using WriteToWave
  MemoryStream waveStream = new MemoryStream ();
  audio.WriteToWaveStream (waveStream);
  waveStream.Flush ();

  //Now check that the WriteToWave write is slightly bigger than the 
  //WriteToAudioStream, due to Wave Formatting.
if (audioStream.Length != waveStream.Length - 16 ) ){
    MessageBox.Show(String.Format("Error: length of stream write by WriteToWaveStream(waveStream) is not 16 bytes larger than that of GetRange(new TimeSpan (0), audio.Duration)"));

  //Now do the same with GetRange
  Stream rangeStreamFull = new MemoryStream ();
  RecognizedAudio range = audio.GetRange (new TimeSpan (0), audio.Duration);
  range.WriteToWaveStream (rangeStreamFull);
  rangeStreamFull.Flush ();
  rangeStreamFull.Close ();
  if (rangeStreamFull.Length != audioStream.Length){
    MessageBox.Show(String.Format("Error: length of stream write by WriteToAudioStream(waveStream) does not match that of GetRange(new TimeSpan (0), audio.Duration)"));
}
//Now we test retrieving the first half the audio
    rangeStreamHalf = new MemoryStream ();
    range = audio.GetRange (new TimeSpan (0), 
                   new TimeSpan (audio.Duration.Ticks / 2));
    range.WriteToWaveStream (rangeStreamHalf);
    rangeStreamHalf.Flush ();
    rangeStreamHalf.Close ();
    if ( ! (rangeStreamHalf.Length >= waveStream.Length / 2 - 20 
         && rangeStreamHalf.Length <= waveStream.Length / 2 + 50)){
      MessageBox.Show(String.Format("Error: When retrieving the first half of audio, length of audio.GetRange (new TimeSpan (0), new TimeSpan (audio.Duration.Ticks / 2)) is not approximately half that of WriteToWaveStream(waveStream)."));
    }
    //Now we test retrieving the second half the audio
    rangeStream = new MemoryStream ();
    range = audio.GetRange (new TimeSpan (audio.Duration.Ticks / 2), 
                   new TimeSpan (audio.Duration.Ticks / 2));
    range.WriteToWaveStream (rangeStream);
    rangeStream.Flush ();
    rangeStream.Close ();
    if ( ! (rangeStreamHalf.Length >= waveStream.Length / 2 - 20 
         && rangeStreamHalf.Length <= waveStream.Length / 2 + 50)){
      MessageBox.Show(String.Format("Error: When retrieving the first half of audio, length of audio.GetRange (new TimeSpan (0), new TimeSpan (audio.Duration.Ticks / 2)) is not approximately half that of WriteToWaveStream(waveStream)."));
    }
}

Thread Safety

All public static (Shared in Visual Basic) members of this type are thread-safe. Instance members are not guaranteed to be thread-safe.

Platforms

Development Platforms

Windows XP Professional with Service Pack 2 (SP2), Windows Server 2003, Windows Vista Ultimate Edition, Windows Vista Business Edition, Windows Vista Enterprise Edition

Target Platforms

See Also

Reference

RecognizedAudio Class
RecognizedAudio Members
Microsoft.Speech.Recognition Namespace
System.TimeSpan
RecognizedAudio Class
WriteToWaveStream
WriteToAudioStream