Share via


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 _
)
public void WriteToAudioStream (
    Stream outputStream
)
public:
void WriteToAudioStream (
    Stream^ outputStream
)
public void WriteToAudioStream (
    Stream outputStream
)
public function WriteToAudioStream (
    outputStream : Stream
)

Parameters

  • outputStream
    Any valid System.IO.Stream object, to which an application wishes to write audio data in binary format.

Remarks

Audio data is written to outputStream in pure binary, no structure (such as Wave format) is applied.

The format of data written by WriteToAudioStream is in the same format as that written by

.WriteToWaveStream exclusive of the prepended Wave header.

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
RecognizedAudio.GetRange Method