1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
mod speex;

pub struct State {
    st: speex::SpeexResamplerState,
}

#[derive(Debug, Clone, Copy)]
pub enum Error {
    AllocFailed,
    InvalidArg,
}

impl State {
    pub fn new(
        channels: usize,
        in_rate: usize,
        out_rate: usize,
        quality: usize,
    ) -> Result<Self, Error> {
        let st = speex::SpeexResamplerState::new(channels, in_rate, out_rate, quality);

        Ok(State { st })
    }

    pub fn set_rate(&mut self, in_rate: usize, out_rate: usize) -> Result<(), Error> {
        if self.st.set_rate(in_rate, out_rate) != 0 {
            Err(Error::InvalidArg)
        } else {
            Ok(())
        }
    }

    pub fn get_rate(&self) -> (usize, usize) {
        self.st.get_rate()
    }

    pub fn get_ratio(&self) -> (usize, usize) {
        self.st.get_ratio()
    }

    pub fn process_float(
        &mut self,
        index: usize,
        input: &[f32],
        output: &mut [f32],
    ) -> Result<(usize, usize), Error> {
        let mut in_len = input.len() as u32;
        let mut out_len = output.len() as u32;
        let ret = self
            .st
            .process_float(index as u32, input, &mut in_len, output, &mut out_len);

        if ret != 0 {
            Err(Error::AllocFailed)
        } else {
            Ok((in_len as usize, out_len as usize))
        }
    }

    pub fn skip_zeros(&mut self) {
        self.st.skip_zeros();
    }

    pub fn reset(&mut self) {
        self.st.reset_mem();
    }

    pub fn get_input_latency(&self) -> usize {
        self.st.get_input_latency()
    }

    pub fn get_output_latency(&self) -> usize {
        self.st.get_output_latency()
    }

    pub fn set_quality(&mut self, quality: usize) -> Result<(), Error> {
        if self.st.set_quality(quality) != 0 {
            Err(Error::InvalidArg)
        } else {
            Ok(())
        }
    }

    pub fn get_quality(&self) -> usize {
        self.st.get_quality()
    }
}