1use crate::generator::SpeechGenerator;
4use crate::provider::{AudioError, AudioFormat, SpeechResponse, VoiceId, VoicePreview};
5use std::future::{Future, IntoFuture};
6use std::pin::Pin;
7
8#[derive(Debug, Clone)]
12#[must_use = "a speech request does nothing until it is awaited"]
13pub struct SpeechRequest<'a> {
14 generator: &'a SpeechGenerator,
15 text: String,
16 voice: Option<VoiceId>,
17 model: Option<String>,
18 format: Option<AudioFormat>,
19 speed: Option<f64>,
20 language: Option<String>,
21 instructions: Option<String>,
22 timestamps: bool,
23 name: Option<String>,
24}
25
26impl<'a> SpeechRequest<'a> {
27 pub(crate) fn new(generator: &'a SpeechGenerator, text: String) -> Self {
28 Self {
29 generator,
30 text,
31 voice: None,
32 model: None,
33 format: None,
34 speed: None,
35 language: None,
36 instructions: None,
37 timestamps: false,
38 name: None,
39 }
40 }
41
42 pub fn with_voice(mut self, voice: impl Into<VoiceId>) -> Self {
45 self.voice = Some(voice.into());
46 self
47 }
48
49 pub fn with_model(mut self, model: impl Into<String>) -> Self {
51 self.model = Some(model.into());
52 self
53 }
54
55 pub fn with_format(mut self, format: AudioFormat) -> Self {
57 self.format = Some(format);
58 self
59 }
60
61 pub fn with_speed(mut self, speed: f64) -> Self {
63 self.speed = Some(speed);
64 self
65 }
66
67 pub fn with_language(mut self, language: impl Into<String>) -> Self {
69 self.language = Some(language.into());
70 self
71 }
72
73 pub fn with_instructions(mut self, instructions: impl Into<String>) -> Self {
75 self.instructions = Some(instructions.into());
76 self
77 }
78
79 pub fn with_timestamps(mut self, timestamps: bool) -> Self {
82 self.timestamps = timestamps;
83 self
84 }
85
86 pub fn with_name(mut self, name: impl Into<String>) -> Self {
88 self.name = Some(name.into());
89 self
90 }
91
92 pub fn text(&self) -> &str {
94 &self.text
95 }
96
97 pub fn voice(&self) -> Option<&str> {
99 self.voice.as_deref()
100 }
101
102 pub fn model(&self) -> Option<&str> {
104 self.model.as_deref()
105 }
106
107 pub fn format(&self) -> Option<AudioFormat> {
109 self.format
110 }
111
112 pub fn speed(&self) -> Option<f64> {
114 self.speed
115 }
116
117 pub fn language(&self) -> Option<&str> {
119 self.language.as_deref()
120 }
121
122 pub fn instructions(&self) -> Option<&str> {
124 self.instructions.as_deref()
125 }
126
127 pub fn timestamps(&self) -> bool {
129 self.timestamps
130 }
131
132 pub(crate) fn name(&self) -> Option<&str> {
133 self.name.as_deref()
134 }
135
136 pub(crate) fn without_timestamps(&self) -> Result<(), AudioError> {
138 without_timestamps(self.timestamps)
139 }
140}
141
142impl<'a> IntoFuture for SpeechRequest<'a> {
143 type Output = Result<SpeechResponse, AudioError>;
144 type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>;
145
146 fn into_future(self) -> Self::IntoFuture {
147 Box::pin(async move { self.generator.speak(&self).await })
148 }
149}
150
151#[derive(Debug, Clone, PartialEq, Eq)]
153#[non_exhaustive]
154pub struct DialogueTurn {
155 pub voice: VoiceId,
157 pub text: String,
159}
160
161impl DialogueTurn {
162 pub fn new(voice: impl Into<VoiceId>, text: impl Into<String>) -> Self {
164 Self {
165 voice: voice.into(),
166 text: text.into(),
167 }
168 }
169}
170
171#[derive(Debug, Clone)]
174#[must_use = "a dialogue request does nothing until it is awaited"]
175pub struct DialogueRequest<'a> {
176 generator: &'a SpeechGenerator,
177 voices: Vec<VoiceId>,
178 turns: Vec<(usize, String)>,
179 model: Option<String>,
180 format: Option<AudioFormat>,
181 speed: Option<f64>,
182 language: Option<String>,
183 timestamps: bool,
184 name: Option<String>,
185}
186
187impl<'a> DialogueRequest<'a> {
188 pub(crate) fn new(
189 generator: &'a SpeechGenerator,
190 dialogue: impl IntoIterator<Item = DialogueTurn>,
191 ) -> Self {
192 let mut voices: Vec<VoiceId> = Vec::new();
193 let turns = dialogue
194 .into_iter()
195 .map(|turn| {
196 let speaker = match voices.iter().position(|voice| *voice == turn.voice) {
197 Some(speaker) => speaker,
198 None => {
199 voices.push(turn.voice);
200 voices.len() - 1
201 }
202 };
203 (speaker, turn.text)
204 })
205 .collect();
206 Self {
207 generator,
208 voices,
209 turns,
210 model: None,
211 format: None,
212 speed: None,
213 language: None,
214 timestamps: false,
215 name: None,
216 }
217 }
218
219 pub fn with_model(mut self, model: impl Into<String>) -> Self {
221 self.model = Some(model.into());
222 self
223 }
224
225 pub fn with_format(mut self, format: AudioFormat) -> Self {
227 self.format = Some(format);
228 self
229 }
230
231 pub fn with_speed(mut self, speed: f64) -> Self {
234 self.speed = Some(speed);
235 self
236 }
237
238 pub fn with_language(mut self, language: impl Into<String>) -> Self {
240 self.language = Some(language.into());
241 self
242 }
243
244 pub fn with_timestamps(mut self, timestamps: bool) -> Self {
247 self.timestamps = timestamps;
248 self
249 }
250
251 pub fn with_name(mut self, name: impl Into<String>) -> Self {
253 self.name = Some(name.into());
254 self
255 }
256
257 pub fn voices(&self) -> &[VoiceId] {
260 &self.voices
261 }
262
263 pub fn turns(&self) -> &[(usize, String)] {
265 &self.turns
266 }
267
268 pub fn speed(&self) -> Option<f64> {
270 self.speed
271 }
272
273 pub fn model(&self) -> Option<&str> {
275 self.model.as_deref()
276 }
277
278 pub fn format(&self) -> Option<AudioFormat> {
280 self.format
281 }
282
283 pub fn language(&self) -> Option<&str> {
285 self.language.as_deref()
286 }
287
288 pub fn timestamps(&self) -> bool {
290 self.timestamps
291 }
292
293 pub(crate) fn name(&self) -> Option<&str> {
294 self.name.as_deref()
295 }
296
297 pub(crate) fn without_timestamps(&self) -> Result<(), AudioError> {
299 without_timestamps(self.timestamps)
300 }
301}
302
303impl<'a> IntoFuture for DialogueRequest<'a> {
304 type Output = Result<SpeechResponse, AudioError>;
305 type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>;
306
307 fn into_future(self) -> Self::IntoFuture {
308 Box::pin(async move { self.generator.speak_dialogue(&self).await })
309 }
310}
311
312#[derive(Debug, Clone)]
315#[must_use = "a voice design request does nothing until it is awaited"]
316pub struct DesignVoiceRequest<'a> {
317 generator: &'a SpeechGenerator,
318 description: String,
319 text: Option<String>,
320 language: Option<String>,
321 count: usize,
322 model: Option<String>,
323}
324
325impl<'a> DesignVoiceRequest<'a> {
326 pub(crate) fn new(generator: &'a SpeechGenerator, description: String) -> Self {
327 Self {
328 generator,
329 description,
330 text: None,
331 language: None,
332 count: 1,
333 model: None,
334 }
335 }
336
337 pub fn with_text(mut self, text: impl Into<String>) -> Self {
339 self.text = Some(text.into());
340 self
341 }
342
343 pub fn with_language(mut self, language: impl Into<String>) -> Self {
346 self.language = Some(language.into());
347 self
348 }
349
350 pub fn with_count(mut self, count: usize) -> Self {
352 self.count = count;
353 self
354 }
355
356 pub fn with_model(mut self, model: impl Into<String>) -> Self {
359 self.model = Some(model.into());
360 self
361 }
362
363 pub fn description(&self) -> &str {
365 &self.description
366 }
367
368 pub fn text(&self) -> Option<&str> {
370 self.text.as_deref()
371 }
372
373 pub fn language(&self) -> Option<&str> {
375 self.language.as_deref()
376 }
377
378 pub fn count(&self) -> usize {
380 self.count
381 }
382
383 pub fn model(&self) -> Option<&str> {
385 self.model.as_deref()
386 }
387}
388
389impl<'a> IntoFuture for DesignVoiceRequest<'a> {
390 type Output = Result<Vec<VoicePreview>, AudioError>;
391 type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>;
392
393 fn into_future(self) -> Self::IntoFuture {
394 Box::pin(async move { self.generator.design(&self).await })
395 }
396}
397
398fn without_timestamps(timestamps: bool) -> Result<(), AudioError> {
399 if timestamps {
400 Err(AudioError::Unsupported("word timestamps"))
401 } else {
402 Ok(())
403 }
404}
405
406#[cfg(test)]
407mod tests {
408 use super::*;
409 use crate::provider::SpeechProvider;
410 use async_trait::async_trait;
411
412 struct NoProvider;
413
414 #[async_trait]
415 impl SpeechProvider for NoProvider {
416 async fn generate(&self, _: &SpeechRequest<'_>) -> Result<SpeechResponse, AudioError> {
417 unreachable!()
418 }
419 }
420
421 #[test]
422 fn dialogue_voices_are_distinct_in_order_of_first_turn() {
423 let generator = SpeechGenerator::from_provider(NoProvider);
424 let request = generator.generate_dialogue([
425 DialogueTurn::new("b", "1"),
426 DialogueTurn::new("a", "2"),
427 DialogueTurn::new("b", "3"),
428 ]);
429 assert_eq!(request.voices(), ["b", "a"]);
430 assert_eq!(
431 request.turns(),
432 [
433 (0, "1".to_string()),
434 (1, "2".to_string()),
435 (0, "3".to_string())
436 ]
437 );
438 }
439}