voirs_cloning/
preprocessing.rs1use crate::{types::VoiceSample, Result};
4
5#[derive(Debug, Clone)]
7pub struct AudioPreprocessor {
8 pub target_sample_rate: u32,
10 pub normalize: bool,
12}
13
14impl AudioPreprocessor {
15 pub fn new(target_sample_rate: u32) -> Self {
17 Self {
18 target_sample_rate,
19 normalize: true,
20 }
21 }
22
23 pub async fn preprocess(&self, sample: &VoiceSample) -> Result<VoiceSample> {
25 let mut processed = sample.clone();
26
27 if sample.sample_rate != self.target_sample_rate {
29 processed = processed.resample(self.target_sample_rate)?;
30 }
31
32 if self.normalize {
34 processed.audio = processed.get_normalized_audio();
35 }
36
37 Ok(processed)
38 }
39}
40
41#[derive(Debug, Clone)]
43pub struct PreprocessingPipeline {
44 pub processors: Vec<AudioPreprocessor>,
46}
47
48impl PreprocessingPipeline {
49 pub fn new() -> Self {
51 Self {
52 processors: vec![AudioPreprocessor::new(22050)],
53 }
54 }
55
56 pub async fn process(&self, sample: &VoiceSample) -> Result<VoiceSample> {
58 let mut result = sample.clone();
59 for processor in &self.processors {
60 result = processor.preprocess(&result).await?;
61 }
62 Ok(result)
63 }
64}
65
66impl Default for PreprocessingPipeline {
67 fn default() -> Self {
68 Self::new()
69 }
70}