Share via


Note

Please see Azure Cognitive Services for Speech documentation for the latest supported speech solutions.

RecognizedAudio.GetRange Method

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

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

returnValue = instance.GetRange(audioPosition, _
    duration)
public RecognizedAudio GetRange(
    TimeSpan audioPosition,
    TimeSpan duration
)

Parameters

  • audioPosition
    Type: System.TimeSpan
    The starting point of the audio data 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.

Exceptions

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.

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.

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(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.

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

Reference

RecognizedAudio Class

RecognizedAudio Members

Microsoft.Speech.Recognition Namespace

RecognizedAudio

WriteToWaveStream

WriteToAudioStream