StreamingSynthesisConnection
Reference for the StreamingSynthesisConnection class in the Node.js SDK
This class represents a full-duplex streaming connection with the server. The
expected use is to call appendText
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.
When you’re done with the StreamingSynthesisConnection
instance, you can explicitly clean up its
resource utilization by calling the close()
method.
To create an instance of this class, use the synthesizeStreaming
method on the Speech
class.
appendText
appendText(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.
Streaming Data Iterator
The connection object provides an async iterator that yields data from the server as it arrives. See the notes below for details on the return value. Here’s a short snippet that shows how to iterate over the data:
Each returned object contains the following properties:
A binary string containing the synthesized 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 returned.
If return_extras
was set to true
in when the connection was created, this will include any warnings about the synthesis.
close
close()
Releases resources associated with this instance.
flush
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
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, return the last chunks of audio as described above, and close the streaming connection.
Was this page helpful?