Skip to main content

wavekat_tts/
traits.rs

1use wavekat_core::AudioFrame;
2
3use crate::error::TtsError;
4use crate::types::{SynthesizeRequest, VoiceInfo};
5
6/// Batch TTS backend: text in, `AudioFrame<'static>` out.
7///
8/// Every backend must implement this trait. The output `AudioFrame` carries
9/// its native sample rate (e.g. 24000 for Kokoro, 24000 for Edge-TTS).
10/// Callers should check `frame.sample_rate()` and resample if needed.
11///
12/// # Example
13///
14/// ```ignore
15/// use wavekat_tts::{TtsBackend, SynthesizeRequest};
16///
17/// let tts = SomeBackend::new()?;
18/// let request = SynthesizeRequest::new("Hello, world");
19/// let audio = tts.synthesize(&request)?;
20///
21/// println!("Generated {} samples at {} Hz ({:.2}s)",
22///     audio.len(), audio.sample_rate(), audio.duration_secs());
23/// ```
24pub trait TtsBackend: Send + Sync {
25    /// Synthesize text into audio.
26    ///
27    /// Returns an owned `AudioFrame` at the backend's native sample rate.
28    fn synthesize(&self, request: &SynthesizeRequest) -> Result<AudioFrame<'static>, TtsError>;
29
30    /// List available voices for this backend.
31    fn voices(&self) -> Result<Vec<VoiceInfo>, TtsError>;
32}
33
34/// Streaming TTS backend: text in, `AudioFrame<'static>` chunks out.
35///
36/// Extends [`TtsBackend`] with streaming support. Each chunk is a
37/// self-contained `AudioFrame` that can be played or forwarded immediately.
38pub trait StreamingTtsBackend: TtsBackend {
39    /// Start streaming synthesis.
40    ///
41    /// Returns an iterator of audio chunks. Each chunk is an owned
42    /// `AudioFrame` at the backend's native sample rate.
43    /// The last chunk can be detected by checking the iterator exhaustion.
44    fn stream(
45        &self,
46        request: &SynthesizeRequest,
47    ) -> Result<Box<dyn Iterator<Item = Result<AudioFrame<'static>, TtsError>> + Send>, TtsError>;
48}