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.

speech = Speech('LMNT_API_KEY')
connection = await speech.synthesize_streaming('mara-wilson')

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:

await connection.append_text('This is a test of ')
await connection.append_text('the emergency broadcast system.')
await connection.append_text('This is a test of the eme')
await connection.append_text('rgency broadcast system.')

Parameters

text
str
required

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:

conn = await speech.synthesize_streaming('mara-wilson')
async for msg in conn:
  msg['audio']  # contains a binary string with the audio data

Each returned object contains the following keys:

audio
bytes

A bytes object containing the audio data as 96kbps mono MP3 stream with a sampling rate of 24kHz.

durations
list of duration objects

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.

warnings
string

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.