use std::sync::Arc;
use numr::runtime::Runtime;
use numr::tensor::Tensor;
pub struct Reference<R: Runtime> {
pub(super) feat: Arc<Tensor<R>>,
pub(super) engine: u64,
}
impl<R: Runtime> Reference<R> {
pub fn patches(&self) -> usize {
self.feat.shape().first().copied().unwrap_or(0)
}
}
impl<R: Runtime> Clone for Reference<R> {
fn clone(&self) -> Self {
Self {
feat: Arc::clone(&self.feat),
engine: self.engine,
}
}
}
pub struct Prompt<R: Runtime> {
pub(super) feat: Arc<Tensor<R>>,
pub(super) engine: u64,
}
impl<R: Runtime> Prompt<R> {
pub fn patches(&self) -> usize {
self.feat.shape().first().copied().unwrap_or(0)
}
}
impl<R: Runtime> Clone for Prompt<R> {
fn clone(&self) -> Self {
Self {
feat: Arc::clone(&self.feat),
engine: self.engine,
}
}
}
pub struct PromptSource<'a, R: Runtime> {
pub prompt: &'a Prompt<R>,
pub text: &'a str,
}
impl<'a, R: Runtime> PromptSource<'a, R> {
pub fn new(prompt: &'a Prompt<R>, text: &'a str) -> Self {
Self { prompt, text }
}
}
impl<R: Runtime> Clone for PromptSource<'_, R> {
fn clone(&self) -> Self {
*self
}
}
impl<R: Runtime> Copy for PromptSource<'_, R> {}
pub enum VoiceSource<'a, R: Runtime> {
Named(&'a str),
Reference(&'a Reference<R>),
ZeroShot,
}
impl<R: Runtime> Clone for VoiceSource<'_, R> {
fn clone(&self) -> Self {
*self
}
}
impl<R: Runtime> Copy for VoiceSource<'_, R> {}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Badcase {
pub max_attempts: usize,
pub ratio: f64,
}
impl Badcase {
pub const MAX_ATTEMPTS: usize = 3;
pub const RATIO: f64 = 6.0;
pub fn no_retry(ratio: f64) -> Self {
Self {
max_attempts: 1,
ratio,
}
}
}
impl Default for Badcase {
fn default() -> Self {
Self {
max_attempts: Self::MAX_ATTEMPTS,
ratio: Self::RATIO,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Rendered {
pub samples: Vec<f32>,
pub attempts: usize,
}
pub struct SynthRequest<'a, R: Runtime> {
pub text: &'a str,
pub voice: VoiceSource<'a, R>,
pub prompt: Option<PromptSource<'a, R>>,
pub seed: Option<u64>,
pub cfg_value: Option<f32>,
pub n_timesteps: Option<usize>,
pub badcase: Badcase,
}
impl<'a, R: Runtime> SynthRequest<'a, R> {
pub fn new(text: &'a str, voice: VoiceSource<'a, R>) -> Self {
Self {
text,
voice,
prompt: None,
seed: None,
cfg_value: None,
n_timesteps: None,
badcase: Badcase::default(),
}
}
pub fn with_prompt(self, prompt: &'a Prompt<R>, prompt_text: &'a str) -> Self {
Self {
prompt: Some(PromptSource::new(prompt, prompt_text)),
..self
}
}
pub fn with_seed(self, seed: u64) -> Self {
Self {
seed: Some(seed),
..self
}
}
pub fn with_cfg(self, cfg_value: f32) -> Self {
Self {
cfg_value: Some(cfg_value),
..self
}
}
pub fn with_badcase(self, badcase: Badcase) -> Self {
Self { badcase, ..self }
}
pub fn with_steps(self, n_timesteps: usize) -> Self {
Self {
n_timesteps: Some(n_timesteps),
..self
}
}
}
impl<R: Runtime> Clone for SynthRequest<'_, R> {
fn clone(&self) -> Self {
*self
}
}
impl<R: Runtime> Copy for SynthRequest<'_, R> {}