GetRange Method

Microsoft Speech Platform SDK 11

Collapse imageExpand ImageCopy imageCopyHover image

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

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

Syntax

Visual Basic (Declaration)
Public Function GetRange ( _
	audioPosition As TimeSpan, _
	duration As TimeSpan _
) As RecognizedAudio
Visual Basic (Usage)
Dim instance As RecognizedAudio
Dim audioPosition As TimeSpan
Dim duration As TimeSpan
Dim returnValue As RecognizedAudio

returnValue = instance.GetRange(audioPosition, _
	duration)
C#
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: Microsoft.Speech.Recognition..::..RecognizedAudio

Returns a RecognizedAudio instance that contains the subset of the original input selected by the values of audioPosition and duration.

Remarks

An ArgumentOutOfRangeException exception is thrown if audioPosition and duration attempt to reference beyond the bounds of the recognized audio.

Examples

The following example shows a testing routine that uses WriteToWaveStream, and WriteToAudioStream to write all of the recognized audio to a stream. It also uses GetRange to write selected portions of that audio to a stream.

NoteNote

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(new TimeSpan (0), new TimeSpan (audio.Duration.Ticks / 2)0 may not be exactly half that of the write from calls such as WriteToWaveStream(waveStream) or WriteToAudioStream(audioStream). The following example takes this discrepancy into account.

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

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

  // Check that the result of WriteToWave is slightly larger than the 
  // result of 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)"));

  // Perform the same check for 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)"));
  }
  // 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)."));
  }
  // 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)."));
  }
}

See Also