Export (0) Print
Expand All

SpeechSynthesisConnector

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

A SpeechSynthesisConnector instance provides a Stream interface to feed audio data to an attached AudioVideoFlow instance. The Stream can be given to a SpeechSynthesizer (in System.Speech.Synthesis) as an output destination, in order to play text-to-speech (TTS) over the wire.

Before an application can call the Start method on a SpeechSynthesisConnector instance, a AudioVideoFlow instance must have been previously attached to the connector, and the State of the AudioVideoFlow instance must be Active. If both conditions are not met, Start throws an exception.

To use SpeechSynthesisConnector

  1. The application uses the constructor to create a SpeechSynthesisConnector instance.

    SpeechSynthesisConnector synthConnector = new SpeechSynthesisConnector();
    
  2. The application then calls AttachFlow to bind the SpeechSynthesisConnector instance to an AudioVideoFlow instance. In this code example, it is assumed that AVFlow has already been constructed.

    synthConnector.AttachFlow(AVFlow);
    
  3. The application then calls SetOutputToAudioStream on the SpeechSynthesizer instance (assumed to have been previously constructed) to identify the stream to use as well as how the audio data will be formatted.

    synthesizer.SetOutputToAudioStream(synthConnector.Stream, AudioFormat);
    
  4. The application then calls Start to on a SpeechSynthesizer instance. There must not be another sending device already operating.

    synthConnector.Start()
    
  5. The application can then use Speak to utter a prompt.

    synthesizer.Speak(prompt);
    
  6. When the Speak operation is complete or after the SpeakCompleted event is raised, the application uses Stop.

    synthConnector.Stop();
    

    If another prompt is to be played, call Start again to restart the SpeechSynthesisConnector, and then call Speak again.

The SpeechSynthesisConnector class has the following constructor.

// Creates a new instance of the SpeechSynthesisConnector class.
public SpeechSynthesisConnector()

The following are the public properties on the SpeechSynthesisConnector class.

// Gets the stream corresponding to this connector, for use as a parameter to SpeechSynthesizer.SetOutputToAudioStream().
public Stream Stream {get;}

// Gets the format in which SpeechSynthesisConnector expects audio data to be written to the stream.
public AudioFormat ExpectedAudioFormat {get;}

// Gets whether or not the SpeechSynthesisConnector is currently started.
public bool IsActive {get;}

// Gets whether the SpeechSynthesisConnector is currently attached to an AudioVideoFlow.
public bool IsAttached {get;}

// Gets the attached AudioVideoFlow instance.
public AudioVideoFlow AudioVideoFlow {get;}

// Gets a value indicating whether the current stream supports reading.
public override bool CanRead {get;}

// Gets a value indicating whether the current stream supports seeking.
// The current implementation always returns false.
public override bool CanSeek {get;}

// Gets a value that determines whether the current stream can time out.
// The current implementation always returns false.
public override bool CanTimeout {get;}

// Gets a value indicating whether the current stream supports writing.
// The current implementation always returns false.
public override bool CanWrite {get;}

// Gets the length in bytes of the stream. Not supported.
public override long Length {get;}

// Gets or sets the position within the current stream.
// The set accessor is not supported.
public override long Position {get; set;}

// Gets or sets a value that determines how long the stream will attempt to read before timing out.
// Not supported.
public override int ReadTimeout {get; set;}

// Gets or sets a value that determines how long the stream will attempt to write before timing out.
// Not supported.
public override int WriteTimeout {get; set;}

The following are the public methods on the SpeechSynthesisConnector class.

// Prepares the connector to accept input from the Synthesizer and transmits it to the AudioVideoFlow.
public void Start();

// Stops sending data to the AVFlow. After Stop() returns, it is safe to use another output device on the AudioVideoFlow.
// Will no-op if SSC is already stopped, or is not attached to an AVFlow.
public void Stop();

// Specifies which AudioVideoFlow will provide audio data for speech recognition.
// Only one AudioVideoFlow may be bound at a time.
public void AttachFlow(AudioVideoFlow audioVideoFlow);

// Detaches the current AudioVideoFlow, freeing the connector to use another.
// Will NO-OP if no flow is attached.
public void DetachFlow();

// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
// Blocks until the data has been (almost) sent on the wire.
public override void Write(byte[] buffer, int offset, int count);

// Clears all buffers for this stream and causes any buffered data to be written to the underlying device.
public override void Flush();

// Sets the position within the current stream.  Not supported.
public override long Seek(long offset, SeekOrigin origin);

// Sets the length of the current stream.  Not supported.
public override void SetLength(long value);

// Reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes.
// Not supported.
public override int Read(byte[] buffer, int offset, int count);

Start throws InvalidOperationException under the following circumstances:

  • Another device is already sending on the AudioVideoFlow.

  • The SpeechSynthesisConnector is not attached to an AudioVideoFlow.

  • The SpeechSynthesisConnector has already been started.

Attach throws InvalidOperationException if an AudioVideoFlow is already attached.

Write throws the following exceptions:

  • ArgumentException if the sum of offset and count is larger than the buffer length.

  • ArgumentOutOfRangeException if offset or count is negative.

  • ObjectDisposedException if the stream was already closed.

  • ArgumentNullException if buffer is null.

  • InvalidOperationException if the connector is not attached to a flow, or is not active, or has been suspended.

There are no events on the SpeechSynthesisConnector class.

Show:
© 2014 Microsoft