summaryrefslogtreecommitdiff
path: root/libcodec2/src/lib.rs
blob: 04c2717df69dc58dff2d7fdeeb6bf81ac2e8c1ee (plain)
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
extern crate codec2_sys as ffi;

pub enum Modes {
    Mode1400 = ffi::CODEC2_MODE_1400 as isize,
}

pub struct Codec2(*mut ffi::CODEC2);

impl Codec2 {
    pub fn new() -> Self {
        let mode = ffi::CODEC2_MODE_1400.try_into().unwrap();
        let c = unsafe { ffi::codec2_create(mode) };
        if c.is_null() {
            panic!("error")
        }
        Codec2(c)
    }

    pub fn destroy(&self) {
        unsafe { ffi::codec2_destroy(self.0) }
    }

    /// Encode an array of speech samples into an array of bits
    pub fn encode(self, speech: &[i16], bits: &mut [u8]) {
        unsafe {
            ffi::codec2_encode(self.0, bits.as_ptr() as *mut _, speech.as_ptr() as *mut _);
        }
    }
}

impl Drop for Codec2 {
    fn drop(&mut self) {
        self.destroy();
    }
}

#[cfg(test)]
mod tests {
    use super::Codec2;
    #[test]
    fn it_works() {
        let _c = Codec2::new();
        let result = 2 + 2;
        assert_eq!(result, 4);
    }
}