A SpeechSynthesisConnectorinstance provides a Streaminterface to feed audio data to an attached AudioVideoFlowinstance. The Streamcan 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 Startmethod on a SpeechSynthesisConnectorinstance, a AudioVideoFlowinstance must have been previously attached to the connector, and the Stateof the AudioVideoFlowinstance must be Active. If both conditions are not met, Startthrows an exception.

To use SpeechSynthesisConnector
  1. The application uses the constructor to create a SpeechSynthesisConnectorinstance.

    Copy Code
    SpeechSynthesisConnector synthConnector = new
    SpeechSynthesisConnector();
    
  2. The application then calls AttachFlowto bind the SpeechSynthesisConnectorinstance to an AudioVideoFlowinstance. In this code example, it is assumed that AVFlowhas already been constructed.

    Copy Code
    synthConnector.AttachFlow(AVFlow);
    
  3. The application then calls SetOutputToAudioStreamon the SpeechSynthesizerinstance (assumed to have been previously constructed) to identify the stream to use as well as how the audio data will be formatted.

    Copy Code
    synthesizer.SetOutputToAudioStream(synthConnector.Stream,
    AudioFormat);
    
  4. The application then calls Startto on a SpeechSynthesizerinstance. There must not be another sending device already operating.

    Copy Code
    synthConnector.Start()
    
  5. The application can then use Speakto utter a prompt.

    Copy Code
    synthesizer.Speak(prompt);
    
  6. When the Speakoperation is complete or after the SpeakCompletedevent is raised, the application uses Stop.

    Copy Code
    synthConnector.Stop();
    

    If another prompt is to be played, call Startagain to restart the SpeechSynthesisConnector, and then call Speakagain.

SpeechSynthesis Constructors

The SpeechSynthesisConnectorclass has the following constructor.

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

SpeechSynthesis Properties

The following are the public properties on the SpeechSynthesisConnectorclass.

Copy Code
// 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;}

SpeechSynthesis Methods

The following are the public methods on the SpeechSynthesisConnectorclass.

Copy Code
// 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);

Startthrows InvalidOperationExceptionunder the following circumstances:

  • Another device is already sending on the AudioVideoFlow.

  • The SpeechSynthesisConnectoris not attached to an AudioVideoFlow.

  • The SpeechSynthesisConnectorhas already been started.

Attachthrows InvalidOperationExceptionif an AudioVideoFlowis already attached.

Writethrows the following exceptions:

  • ArgumentExceptionif the sum of offsetand countis larger than the buffer length.

  • ArgumentOutOfRangeExceptionif offsetor countis negative.

  • ObjectDisposedExceptionif the stream was already closed.

  • ArgumentNullExceptionif bufferis null.

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

SpeechSynthesis Events

There are no events on the SpeechSynthesisConnectorclass.