pub struct TtsStream { /* private fields */ }Expand description
A streaming text-to-speech session.
TtsStream provides fine-grained control over the TTS process, allowing you to:
- Send text incrementally
- Receive audio chunks as they are generated
- Access timing information for each audio segment
§Example
use gradium::{Client, TtsStream, protocol::tts::Setup};
let client = Client::new("your-api-key");
let setup = Setup::new("m86j6D7UZpGzHsNu");
let mut stream = TtsStream::new(setup, &client).await?;
stream.send_text("Hello, world!").await?;
stream.send_eos().await?;
while let Some(msg) = stream.next_message().await? {
// Process audio/text messages
}Implementations§
Source§impl TtsStream
impl TtsStream
pub fn split(self) -> (TtsStreamSender, TtsStreamReceiver)
Sourcepub async fn new(setup: Setup, client: &Client) -> Result<Self>
pub async fn new(setup: Setup, client: &Client) -> Result<Self>
Creates a new TTS streaming session.
This establishes a WebSocket connection to the Gradium API and initializes the TTS session with the provided configuration.
§Arguments
setup- The TTS configuration (model, voice, output format)client- The Gradium API client
§Returns
A new TtsStream ready to receive text input
§Errors
Returns an error if:
- The WebSocket connection fails
- The server returns an error during setup
- The server sends an unexpected response
Sourcepub async fn send_eos(&mut self) -> Result<()>
pub async fn send_eos(&mut self) -> Result<()>
Signals the end of the text input stream.
After calling this, the server will finish processing any remaining text and send the final audio chunks before closing the stream.
§Errors
Returns an error if the message cannot be sent
Sourcepub async fn next_message(&mut self) -> Result<Option<Response>>
pub async fn next_message(&mut self) -> Result<Option<Response>>
Receives the next message from the server.
This can be an audio chunk, text with timing information, or end-of-stream signal.
§Returns
Ok(Some(response))- A message was receivedOk(None)- The stream has endedErr(_)- An error occurred
§Errors
Returns an error if:
- The WebSocket encounters an error
- The server returns an error response
- Message deserialization fails
Sourcepub fn sample_rate(&self) -> u32
pub fn sample_rate(&self) -> u32
Returns the audio sample rate in Hz.
Sourcepub fn frame_size(&self) -> u32
pub fn frame_size(&self) -> u32
Returns the audio frame size in samples.
Sourcepub fn audio_stream_names(&self) -> &[String]
pub fn audio_stream_names(&self) -> &[String]
Returns the names of available audio streams.
Sourcepub fn text_stream_names(&self) -> &[String]
pub fn text_stream_names(&self) -> &[String]
Returns the names of available text streams.
Sourcepub fn request_id(&self) -> &str
pub fn request_id(&self) -> &str
Returns the request ID for this session.