StreamingSynthesisConnection
Reference for the StreamingSynthesisConnection class in the Python SDK
This class represents a full-duplex streaming connection with the server. The expected use is to call append_text
as text is produced
and to iterate over the object to read audio. Make sure to call finish()
when you’re done submitting the entire text snippet.
To create an instance of this class, use the synthesize_streaming
method on the Speech
class.
append_text
async append_text(text)
Sends additional text to synthesize to the server. The text can be split at any point. For example, the two snippets below are semantically equivalent:
Parameters
Some or all of the text to synthesize.
Note
The text is guaranteed to be synthesized in the order that it is sent.
You can access the synthesized data by using the async iterator on the connection object.
Async Iterator
async __aiter__()
async __anext__()
Iterates over data returned from the server. See the notes below for details on the object format. Here’s a short snippet that shows how to iterate over the data:
Each returned object contains the following keys:
A bytes object containing the audio data as 96kbps mono MP3 stream with a sampling rate of 24kHz.
An array of text duration objects. Only returned if return_extras
was set to true
in when the connection was created.
The durations array resets its start time for each chunk of audio.
If return_extras
was set to true
in when the connection was created, this will include any warnings about the synthesis.
flush
async flush()
Call this when you want to trigger the server to synthesize all the text it currently has and return the audio data. Audio will be returned via the async iterator. This is recommended to be used only sparingly, if at all, as it can result in a less natural sounding speech. This could be useful if you are sure you have sent text that comes at a natural stop and want all the audio returned without closing the connection.
finish
async finish()
Call this function when you’ve written all the text you’re expecting to submit. It will flush any remaining data on the server and return the last chunks of audio via the async iterator. The connection will also be closed.