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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#![allow(non_camel_case_types)]

#[cfg(feature = "sys")]
mod sys {
    use speexdsp_sys::echo::*;
    use std::ffi::c_void;
    use std::fmt;

    #[derive(Clone, Copy, Debug)]
    pub enum SpeexEchoConst {
        SPEEX_ECHO_GET_FRAME_SIZE = 3,
        SPEEX_ECHO_SET_SAMPLING_RATE = 24,
        SPEEX_ECHO_GET_SAMPLING_RATE = 25,
        SPEEX_ECHO_GET_IMPULSE_RESPONSE_SIZE = 27,
        SPEEX_ECHO_GET_IMPULSE_RESPONSE = 29,
    }

    #[derive(Clone, Copy, Debug)]
    pub enum Error {
        FailedInit,
        UnknownRequest,
    }

    impl fmt::Display for Error {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            let v = match self {
                Error::FailedInit => "Failed to initialize",
                Error::UnknownRequest => "The request is unknown",
            };

            write!(f, "{}", v)
        }
    }

    #[derive(Clone)]
    pub struct SpeexEcho {
        st: *mut SpeexEchoState,
    }

    impl SpeexEcho {
        pub fn new(
            frame_size: usize,
            filter_length: usize,
        ) -> Result<Self, Error> {
            let st = unsafe {
                speex_echo_state_init(frame_size as i32, filter_length as i32)
            };

            if st.is_null() {
                Err(Error::FailedInit)
            } else {
                Ok(SpeexEcho { st })
            }
        }

        pub fn echo_init(
            frame_size: usize,
            filter_length: usize,
            nb_mic: usize,
            nb_speakers: usize,
        ) -> Result<Self, Error> {
            let st = unsafe {
                speex_echo_state_init_mc(
                    frame_size as i32,
                    filter_length as i32,
                    nb_mic as i32,
                    nb_speakers as i32,
                )
            };

            if st.is_null() {
                Err(Error::FailedInit)
            } else {
                Ok(SpeexEcho { st })
            }
        }

        pub fn echo_cancellation(
            &mut self,
            rec: &[i16],
            play: &[i16],
            out: &mut [i16],
        ) {
            unsafe {
                speex_echo_cancellation(
                    self.st,
                    rec.as_ptr(),
                    play.as_ptr(),
                    out.as_mut_ptr(),
                )
            };
        }

        pub fn echo_cancel(
            &mut self,
            rec: &[i16],
            play: &[i16],
            out: &mut [i16],
            yout: &mut [i32],
        ) {
            unsafe {
                speex_echo_cancel(
                    self.st,
                    rec.as_ptr(),
                    play.as_ptr(),
                    out.as_mut_ptr(),
                    yout.as_mut_ptr(),
                )
            };
        }

        pub fn echo_capture(&mut self, rec: &[i16], out: &mut [i16]) {
            unsafe {
                speex_echo_capture(self.st, rec.as_ptr(), out.as_mut_ptr())
            };
        }

        pub fn echo_playback(&mut self, play: &[i16]) {
            unsafe { speex_echo_playback(self.st, play.as_ptr()) };
        }

        pub fn echo_reset(&mut self) {
            unsafe { speex_echo_state_reset(self.st) };
        }

        pub fn echo_ctl(
            &mut self,
            request: SpeexEchoConst,
            ptr: usize,
        ) -> Result<(), Error> {
            let ret = unsafe {
                speex_echo_ctl(self.st, request as i32, ptr as *mut c_void)
                    as usize
            };
            if ret != 0 {
                Err(Error::UnknownRequest)
            } else {
                Ok(())
            }
        }

        pub(crate) fn get_ptr(&self) -> *mut SpeexEchoState {
            self.st
        }
    }

    impl Drop for SpeexEcho {
        fn drop(&mut self) {
            unsafe { speex_echo_state_destroy(self.st) };
        }
    }

    pub struct SpeexDecorr {
        st: *mut SpeexDecorrState,
    }

    impl SpeexDecorr {
        pub fn new(
            rate: usize,
            channels: usize,
            frame_size: usize,
        ) -> Result<Self, Error> {
            let st = unsafe {
                speex_decorrelate_new(
                    rate as i32,
                    channels as i32,
                    frame_size as i32,
                )
            };

            if st.is_null() {
                Err(Error::FailedInit)
            } else {
                Ok(SpeexDecorr { st })
            }
        }

        pub fn decorrelate(
            &mut self,
            input: &[i16],
            out: &mut [i16],
            strength: usize,
        ) {
            unsafe {
                speex_decorrelate(
                    self.st,
                    input.as_ptr(),
                    out.as_mut_ptr(),
                    strength as i32,
                )
            };
        }
    }

    impl Drop for SpeexDecorr {
        fn drop(&mut self) {
            unsafe { speex_decorrelate_destroy(self.st) };
        }
    }
}

#[cfg(feature = "sys")]
pub use self::sys::{Error, SpeexDecorr, SpeexEcho, SpeexEchoConst};