use crate::generator::SpeechGenerator;
use crate::provider::{AudioError, AudioFormat, SpeechResponse, VoiceId, VoicePreview};
use std::future::{Future, IntoFuture};
use std::pin::Pin;
#[derive(Debug, Clone)]
#[must_use = "a speech request does nothing until it is awaited"]
pub struct SpeechRequest<'a> {
generator: &'a SpeechGenerator,
text: String,
voice: Option<VoiceId>,
model: Option<String>,
format: Option<AudioFormat>,
speed: Option<f64>,
language: Option<String>,
instructions: Option<String>,
timestamps: bool,
name: Option<String>,
}
impl<'a> SpeechRequest<'a> {
pub(crate) fn new(generator: &'a SpeechGenerator, text: String) -> Self {
Self {
generator,
text,
voice: None,
model: None,
format: None,
speed: None,
language: None,
instructions: None,
timestamps: false,
name: None,
}
}
pub fn with_voice(mut self, voice: impl Into<VoiceId>) -> Self {
self.voice = Some(voice.into());
self
}
pub fn with_model(mut self, model: impl Into<String>) -> Self {
self.model = Some(model.into());
self
}
pub fn with_format(mut self, format: AudioFormat) -> Self {
self.format = Some(format);
self
}
pub fn with_speed(mut self, speed: f64) -> Self {
self.speed = Some(speed);
self
}
pub fn with_language(mut self, language: impl Into<String>) -> Self {
self.language = Some(language.into());
self
}
pub fn with_instructions(mut self, instructions: impl Into<String>) -> Self {
self.instructions = Some(instructions.into());
self
}
pub fn with_timestamps(mut self, timestamps: bool) -> Self {
self.timestamps = timestamps;
self
}
pub fn with_name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
pub fn text(&self) -> &str {
&self.text
}
pub fn voice(&self) -> Option<&str> {
self.voice.as_deref()
}
pub fn model(&self) -> Option<&str> {
self.model.as_deref()
}
pub fn format(&self) -> Option<AudioFormat> {
self.format
}
pub fn speed(&self) -> Option<f64> {
self.speed
}
pub fn language(&self) -> Option<&str> {
self.language.as_deref()
}
pub fn instructions(&self) -> Option<&str> {
self.instructions.as_deref()
}
pub fn timestamps(&self) -> bool {
self.timestamps
}
pub(crate) fn name(&self) -> Option<&str> {
self.name.as_deref()
}
pub(crate) fn without_timestamps(&self) -> Result<(), AudioError> {
without_timestamps(self.timestamps)
}
}
impl<'a> IntoFuture for SpeechRequest<'a> {
type Output = Result<SpeechResponse, AudioError>;
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move { self.generator.speak(&self).await })
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct DialogueTurn {
pub voice: VoiceId,
pub text: String,
}
impl DialogueTurn {
pub fn new(voice: impl Into<VoiceId>, text: impl Into<String>) -> Self {
Self {
voice: voice.into(),
text: text.into(),
}
}
}
#[derive(Debug, Clone)]
#[must_use = "a dialogue request does nothing until it is awaited"]
pub struct DialogueRequest<'a> {
generator: &'a SpeechGenerator,
voices: Vec<VoiceId>,
turns: Vec<(usize, String)>,
model: Option<String>,
format: Option<AudioFormat>,
speed: Option<f64>,
language: Option<String>,
timestamps: bool,
name: Option<String>,
}
impl<'a> DialogueRequest<'a> {
pub(crate) fn new(
generator: &'a SpeechGenerator,
dialogue: impl IntoIterator<Item = DialogueTurn>,
) -> Self {
let mut voices: Vec<VoiceId> = Vec::new();
let turns = dialogue
.into_iter()
.map(|turn| {
let speaker = match voices.iter().position(|voice| *voice == turn.voice) {
Some(speaker) => speaker,
None => {
voices.push(turn.voice);
voices.len() - 1
}
};
(speaker, turn.text)
})
.collect();
Self {
generator,
voices,
turns,
model: None,
format: None,
speed: None,
language: None,
timestamps: false,
name: None,
}
}
pub fn with_model(mut self, model: impl Into<String>) -> Self {
self.model = Some(model.into());
self
}
pub fn with_format(mut self, format: AudioFormat) -> Self {
self.format = Some(format);
self
}
pub fn with_speed(mut self, speed: f64) -> Self {
self.speed = Some(speed);
self
}
pub fn with_language(mut self, language: impl Into<String>) -> Self {
self.language = Some(language.into());
self
}
pub fn with_timestamps(mut self, timestamps: bool) -> Self {
self.timestamps = timestamps;
self
}
pub fn with_name(mut self, name: impl Into<String>) -> Self {
self.name = Some(name.into());
self
}
pub fn voices(&self) -> &[VoiceId] {
&self.voices
}
pub fn turns(&self) -> &[(usize, String)] {
&self.turns
}
pub fn speed(&self) -> Option<f64> {
self.speed
}
pub fn model(&self) -> Option<&str> {
self.model.as_deref()
}
pub fn format(&self) -> Option<AudioFormat> {
self.format
}
pub fn language(&self) -> Option<&str> {
self.language.as_deref()
}
pub fn timestamps(&self) -> bool {
self.timestamps
}
pub(crate) fn name(&self) -> Option<&str> {
self.name.as_deref()
}
pub(crate) fn without_timestamps(&self) -> Result<(), AudioError> {
without_timestamps(self.timestamps)
}
}
impl<'a> IntoFuture for DialogueRequest<'a> {
type Output = Result<SpeechResponse, AudioError>;
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move { self.generator.speak_dialogue(&self).await })
}
}
#[derive(Debug, Clone)]
#[must_use = "a voice design request does nothing until it is awaited"]
pub struct DesignVoiceRequest<'a> {
generator: &'a SpeechGenerator,
description: String,
text: Option<String>,
language: Option<String>,
count: usize,
model: Option<String>,
}
impl<'a> DesignVoiceRequest<'a> {
pub(crate) fn new(generator: &'a SpeechGenerator, description: String) -> Self {
Self {
generator,
description,
text: None,
language: None,
count: 1,
model: None,
}
}
pub fn with_text(mut self, text: impl Into<String>) -> Self {
self.text = Some(text.into());
self
}
pub fn with_language(mut self, language: impl Into<String>) -> Self {
self.language = Some(language.into());
self
}
pub fn with_count(mut self, count: usize) -> Self {
self.count = count;
self
}
pub fn with_model(mut self, model: impl Into<String>) -> Self {
self.model = Some(model.into());
self
}
pub fn description(&self) -> &str {
&self.description
}
pub fn text(&self) -> Option<&str> {
self.text.as_deref()
}
pub fn language(&self) -> Option<&str> {
self.language.as_deref()
}
pub fn count(&self) -> usize {
self.count
}
pub fn model(&self) -> Option<&str> {
self.model.as_deref()
}
}
impl<'a> IntoFuture for DesignVoiceRequest<'a> {
type Output = Result<Vec<VoicePreview>, AudioError>;
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move { self.generator.design(&self).await })
}
}
fn without_timestamps(timestamps: bool) -> Result<(), AudioError> {
if timestamps {
Err(AudioError::Unsupported("word timestamps"))
} else {
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::provider::SpeechProvider;
use async_trait::async_trait;
struct NoProvider;
#[async_trait]
impl SpeechProvider for NoProvider {
async fn generate(&self, _: &SpeechRequest<'_>) -> Result<SpeechResponse, AudioError> {
unreachable!()
}
}
#[test]
fn dialogue_voices_are_distinct_in_order_of_first_turn() {
let generator = SpeechGenerator::from_provider(NoProvider);
let request = generator.generate_dialogue([
DialogueTurn::new("b", "1"),
DialogueTurn::new("a", "2"),
DialogueTurn::new("b", "3"),
]);
assert_eq!(request.voices(), ["b", "a"]);
assert_eq!(
request.turns(),
[
(0, "1".to_string()),
(1, "2".to_string()),
(0, "3".to_string())
]
);
}
}