Skip to main content

adk_audio/traits/
tts.rs

1//! Text-to-speech provider trait and request types.
2
3use std::pin::Pin;
4
5use async_trait::async_trait;
6use futures::Stream;
7
8use crate::codec::AudioFormat;
9use crate::error::AudioResult;
10use crate::frame::AudioFrame;
11use crate::metering::AudioBilling;
12
13/// Emotion hint for TTS synthesis.
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15pub enum Emotion {
16    /// Neutral tone.
17    Neutral,
18    /// Happy / upbeat tone.
19    Happy,
20    /// Sad / somber tone.
21    Sad,
22    /// Angry / forceful tone.
23    Angry,
24    /// Whispered / quiet tone.
25    Whisper,
26    /// Excited / energetic tone.
27    Excited,
28    /// Calm / soothing tone.
29    Calm,
30}
31
32/// Descriptor for an available voice.
33#[derive(Debug, Clone)]
34pub struct Voice {
35    /// Provider-specific voice identifier.
36    pub id: String,
37    /// Human-readable voice name.
38    pub name: String,
39    /// BCP-47 language code.
40    pub language: String,
41    /// Optional gender label.
42    pub gender: Option<String>,
43}
44
45/// Request parameters for TTS synthesis.
46#[derive(Debug, Clone)]
47pub struct TtsRequest {
48    /// Text to synthesize.
49    pub text: String,
50    /// Voice identifier.
51    pub voice: String,
52    /// Optional BCP-47 language code.
53    pub language: Option<String>,
54    /// Speaking speed multiplier (0.5–2.0, default 1.0).
55    pub speed: f32,
56    /// Optional pitch adjustment.
57    pub pitch: Option<f32>,
58    /// Optional emotion hint.
59    pub emotion: Option<Emotion>,
60    /// Desired output format (internal use; providers output PCM16).
61    pub output_format: AudioFormat,
62}
63
64impl Default for TtsRequest {
65    fn default() -> Self {
66        Self {
67            text: String::new(),
68            voice: String::new(),
69            language: None,
70            speed: 1.0,
71            pitch: None,
72            emotion: None,
73            output_format: AudioFormat::Pcm16,
74        }
75    }
76}
77
78/// Unified trait for text-to-speech providers.
79///
80/// Implementors include cloud services (ElevenLabs, OpenAI, Gemini, Cartesia)
81/// and local models (MLX Kokoro, ONNX Kokoro).
82#[async_trait]
83pub trait TtsProvider: Send + Sync {
84    /// Synthesize text to a single audio frame (batch mode).
85    async fn synthesize(&self, request: &TtsRequest) -> AudioResult<AudioFrame>;
86
87    /// Synthesize text as a stream of audio frames (streaming mode).
88    async fn synthesize_stream(
89        &self,
90        request: &TtsRequest,
91    ) -> AudioResult<Pin<Box<dyn Stream<Item = AudioResult<AudioFrame>> + Send>>>;
92
93    /// List available voices for this provider.
94    fn voice_catalog(&self) -> &[Voice];
95
96    /// The vendor and model this provider's calls are billed under, for metering them
97    /// against the current run ([`crate::metering`]). `None`, the default, for a provider
98    /// that bills nothing, such as a local model.
99    fn billing(&self) -> Option<AudioBilling> {
100        None
101    }
102}