Share via


Note

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

RecognizedAudio.WriteToAudioStream Method

Writes the entire audio to a stream as raw data.

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

Syntax

'Declaration
Public Sub WriteToAudioStream ( _
    outputStream As Stream _
)
'Usage
Dim instance As RecognizedAudio
Dim outputStream As Stream

instance.WriteToAudioStream(outputStream)
public void WriteToAudioStream(
    Stream outputStream
)

Parameters

  • outputStream
    Type: System.IO.Stream
    The stream that will receive the audio data.

Remarks

Audio data is written to outputStream in binary form. No header information is included.

The WriteToAudioStream method uses the Wave format, but does not include the Wave header. To include the Wave header, use the WriteToWaveStream method.

Examples

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(new TimeSpan(0), new TimeSpan(audio.Duration.Ticks / 2)) 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

GetRange