1 /* Copyright (C) Jean-Marc Valin */
2 /**
3    @file speex_echo.h
4    @brief Echo cancellation
5 */
6 /*
7    Redistribution and use in source and binary forms, with or without
8    modification, are permitted provided that the following conditions are
9    met:
10 
11    1. Redistributions of source code must retain the above copyright notice,
12    this list of conditions and the following disclaimer.
13 
14    2. Redistributions in binary form must reproduce the above copyright
15    notice, this list of conditions and the following disclaimer in the
16    documentation and/or other materials provided with the distribution.
17 
18    3. The name of the author may not be used to endorse or promote products
19    derived from this software without specific prior written permission.
20 
21    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24    DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25    INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29    STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31    POSSIBILITY OF SUCH DAMAGE.
32 */
33 
34 #ifndef SPEEX_ECHO_H
35 #define SPEEX_ECHO_H
36 /** @defgroup SpeexEchoState SpeexEchoState: Acoustic echo canceller
37  *  This is the acoustic echo canceller module.
38  *  @{
39  */
40 #include "speex/speex_types.h"
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 /** Obtain frame size used by the AEC */
47 #define SPEEX_ECHO_GET_FRAME_SIZE 3
48 
49 /** Set sampling rate */
50 #define SPEEX_ECHO_SET_SAMPLING_RATE 24
51 /** Get sampling rate */
52 #define SPEEX_ECHO_GET_SAMPLING_RATE 25
53 
54 /* Can't set window sizes */
55 /** Get size of impulse response (int32) */
56 #define SPEEX_ECHO_GET_IMPULSE_RESPONSE_SIZE 27
57 
58 /* Can't set window content */
59 /** Get impulse response (int32[]) */
60 #define SPEEX_ECHO_GET_IMPULSE_RESPONSE 29
61 
62 /** Internal echo canceller state. Should never be accessed directly. */
63 struct SpeexEchoState_;
64 
65 /** @class SpeexEchoState
66  * This holds the state of the echo canceller. You need one per channel.
67 */
68 
69 /** Internal echo canceller state. Should never be accessed directly. */
70 typedef struct SpeexEchoState_ SpeexEchoState;
71 
72 /** Creates a new echo canceller state
73  * @param frame_size Number of samples to process at one time (should correspond to 10-20 ms)
74  * @param filter_length Number of samples of echo to cancel (should generally correspond to 100-500 ms)
75  * @return Newly-created echo canceller state
76  */
77 SpeexEchoState *speex_echo_state_init(int frame_size, int filter_length);
78 
79 /** Creates a new multi-channel echo canceller state
80  * @param frame_size Number of samples to process at one time (should correspond to 10-20 ms)
81  * @param filter_length Number of samples of echo to cancel (should generally correspond to 100-500 ms)
82  * @param nb_mic Number of microphone channels
83  * @param nb_speakers Number of speaker channels
84  * @return Newly-created echo canceller state
85  */
86 SpeexEchoState *speex_echo_state_init_mc(int frame_size, int filter_length, int nb_mic, int nb_speakers);
87 
88 /** Destroys an echo canceller state
89  * @param st Echo canceller state
90 */
91 void speex_echo_state_destroy(SpeexEchoState *st);
92 
93 /** Performs echo cancellation a frame, based on the audio sent to the speaker (no delay is added
94  * to playback in this form)
95  *
96  * @param st Echo canceller state
97  * @param rec Signal from the microphone (near end + far end echo)
98  * @param play Signal played to the speaker (received from far end)
99  * @param out Returns near-end signal with echo removed
100  */
101 void speex_echo_cancellation(SpeexEchoState *st, const spx_int16_t *rec, const spx_int16_t *play, spx_int16_t *out);
102 
103 /** Performs echo cancellation a frame (deprecated) */
104 void speex_echo_cancel(SpeexEchoState *st, const spx_int16_t *rec, const spx_int16_t *play, spx_int16_t *out, spx_int32_t *Yout);
105 
106 /** Perform echo cancellation using internal playback buffer, which is delayed by two frames
107  * to account for the delay introduced by most soundcards (but it could be off!)
108  * @param st Echo canceller state
109  * @param rec Signal from the microphone (near end + far end echo)
110  * @param out Returns near-end signal with echo removed
111 */
112 void speex_echo_capture(SpeexEchoState *st, const spx_int16_t *rec, spx_int16_t *out);
113 
114 /** Let the echo canceller know that a frame was just queued to the soundcard
115  * @param st Echo canceller state
116  * @param play Signal played to the speaker (received from far end)
117 */
118 void speex_echo_playback(SpeexEchoState *st, const spx_int16_t *play);
119 
120 /** Reset the echo canceller to its original state
121  * @param st Echo canceller state
122  */
123 void speex_echo_state_reset(SpeexEchoState *st);
124 
125 /** Used like the ioctl function to control the echo canceller parameters
126  *
127  * @param st Echo canceller state
128  * @param request ioctl-type request (one of the SPEEX_ECHO_* macros)
129  * @param ptr Data exchanged to-from function
130  * @return 0 if no error, -1 if request in unknown
131  */
132 int speex_echo_ctl(SpeexEchoState *st, int request, void *ptr);
133 
134 
135 
136 struct SpeexDecorrState_;
137 
138 typedef struct SpeexDecorrState_ SpeexDecorrState;
139 
140 
141 /** Create a state for the channel decorrelation algorithm
142     This is useful for multi-channel echo cancellation only
143  * @param rate Sampling rate
144  * @param channels Number of channels (it's a bit pointless if you don't have at least 2)
145  * @param frame_size Size of the frame to process at ones (counting samples *per* channel)
146 */
147 SpeexDecorrState *speex_decorrelate_new(int rate, int channels, int frame_size);
148 
149 /** Remove correlation between the channels by modifying the phase and possibly
150     adding noise in a way that is not (or little) perceptible.
151  * @param st Decorrelator state
152  * @param in Input audio in interleaved format
153  * @param out Result of the decorrelation (out *may* alias in)
154  * @param strength How much alteration of the audio to apply from 0 to 100.
155 */
156 void speex_decorrelate(SpeexDecorrState *st, const spx_int16_t *in, spx_int16_t *out, int strength);
157 
158 /** Destroy a Decorrelation state
159  * @param st State to destroy
160 */
161 void speex_decorrelate_destroy(SpeexDecorrState *st);
162 
163 
164 #ifdef __cplusplus
165 }
166 #endif
167 
168 
169 /** @}*/
170 #endif
171