niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
andrew@webrtc.org | b9d7d93 | 2012-01-25 19:21:13 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Henrik Kjellander | 9b72af9 | 2015-11-11 20:16:11 +0100 | [diff] [blame] | 11 | #ifndef WEBRTC_MODULES_AUDIO_PROCESSING_AEC_ECHO_CANCELLATION_H_ |
| 12 | #define WEBRTC_MODULES_AUDIO_PROCESSING_AEC_ECHO_CANCELLATION_H_ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | |
peah | 44c8a37 | 2016-05-05 13:34:29 -0700 | [diff] [blame] | 14 | #include <memory> |
| 15 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 16 | #include <stddef.h> |
| 17 | |
peah | 44c8a37 | 2016-05-05 13:34:29 -0700 | [diff] [blame] | 18 | extern "C" { |
| 19 | #include "webrtc/common_audio/ring_buffer.h" |
| 20 | } |
| 21 | #include "webrtc/modules/audio_processing/aec/aec_core.h" |
bjornv@webrtc.org | 21a2fc9 | 2013-02-15 17:01:03 +0000 | [diff] [blame] | 22 | #include "webrtc/typedefs.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 23 | |
peah | 50e21bd | 2016-03-05 08:39:21 -0800 | [diff] [blame] | 24 | namespace webrtc { |
| 25 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 26 | // Errors |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 27 | #define AEC_UNSPECIFIED_ERROR 12000 |
| 28 | #define AEC_UNSUPPORTED_FUNCTION_ERROR 12001 |
| 29 | #define AEC_UNINITIALIZED_ERROR 12002 |
| 30 | #define AEC_NULL_POINTER_ERROR 12003 |
| 31 | #define AEC_BAD_PARAMETER_ERROR 12004 |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 32 | |
| 33 | // Warnings |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 34 | #define AEC_BAD_PARAMETER_WARNING 12050 |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 35 | |
peah | ff63ed2 | 2016-01-29 07:46:13 -0800 | [diff] [blame] | 36 | enum { kAecNlpConservative = 0, kAecNlpModerate, kAecNlpAggressive }; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 37 | |
peah | ff63ed2 | 2016-01-29 07:46:13 -0800 | [diff] [blame] | 38 | enum { kAecFalse = 0, kAecTrue }; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 39 | |
| 40 | typedef struct { |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 41 | int16_t nlpMode; // default kAecNlpModerate |
| 42 | int16_t skewMode; // default kAecFalse |
| 43 | int16_t metricsMode; // default kAecFalse |
| 44 | int delay_logging; // default kAecFalse |
| 45 | // float realSkew; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 46 | } AecConfig; |
| 47 | |
| 48 | typedef struct { |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 49 | int instant; |
| 50 | int average; |
| 51 | int max; |
| 52 | int min; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 53 | } AecLevel; |
| 54 | |
| 55 | typedef struct { |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 56 | AecLevel rerl; |
| 57 | AecLevel erl; |
| 58 | AecLevel erle; |
| 59 | AecLevel aNlp; |
minyue | 5045337 | 2016-04-07 06:36:43 -0700 | [diff] [blame] | 60 | float divergent_filter_fraction; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 61 | } AecMetrics; |
| 62 | |
bjornv@webrtc.org | 132c15d | 2013-02-27 21:03:41 +0000 | [diff] [blame] | 63 | struct AecCore; |
| 64 | |
peah | 44c8a37 | 2016-05-05 13:34:29 -0700 | [diff] [blame] | 65 | class ApmDataDumper; |
| 66 | |
| 67 | typedef struct Aec { |
| 68 | std::unique_ptr<ApmDataDumper> data_dumper; |
| 69 | |
| 70 | int delayCtr; |
| 71 | int sampFreq; |
| 72 | int splitSampFreq; |
| 73 | int scSampFreq; |
| 74 | float sampFactor; // scSampRate / sampFreq |
| 75 | short skewMode; |
| 76 | int bufSizeStart; |
| 77 | int knownDelay; |
| 78 | int rate_factor; |
| 79 | |
| 80 | short initFlag; // indicates if AEC has been initialized |
| 81 | |
| 82 | // Variables used for averaging far end buffer size |
| 83 | short counter; |
| 84 | int sum; |
| 85 | short firstVal; |
| 86 | short checkBufSizeCtr; |
| 87 | |
| 88 | // Variables used for delay shifts |
| 89 | short msInSndCardBuf; |
| 90 | short filtDelay; // Filtered delay estimate. |
| 91 | int timeForDelayChange; |
| 92 | int startup_phase; |
| 93 | int checkBuffSize; |
| 94 | short lastDelayDiff; |
| 95 | |
| 96 | // Structures |
| 97 | void* resampler; |
| 98 | |
| 99 | int skewFrCtr; |
| 100 | int resample; // if the skew is small enough we don't resample |
| 101 | int highSkewCtr; |
| 102 | float skew; |
| 103 | |
| 104 | RingBuffer* far_pre_buf; // Time domain far-end pre-buffer. |
| 105 | |
| 106 | int farend_started; |
| 107 | |
| 108 | // Aec instance counter. |
| 109 | static int instance_count; |
| 110 | AecCore* aec; |
| 111 | } Aec; |
| 112 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 113 | /* |
| 114 | * Allocates the memory needed by the AEC. The memory needs to be initialized |
Bjorn Volcker | 9345e86 | 2015-06-10 21:43:36 +0200 | [diff] [blame] | 115 | * separately using the WebRtcAec_Init() function. Returns a pointer to the |
| 116 | * object or NULL on error. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 117 | */ |
Bjorn Volcker | 9345e86 | 2015-06-10 21:43:36 +0200 | [diff] [blame] | 118 | void* WebRtcAec_Create(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 119 | |
| 120 | /* |
| 121 | * This function releases the memory allocated by WebRtcAec_Create(). |
| 122 | * |
| 123 | * Inputs Description |
| 124 | * ------------------------------------------------------------------- |
kwiberg@webrtc.org | 924e81f | 2014-05-12 09:55:19 +0000 | [diff] [blame] | 125 | * void* aecInst Pointer to the AEC instance |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 126 | */ |
Bjorn Volcker | f6a99e6 | 2015-04-10 07:56:57 +0200 | [diff] [blame] | 127 | void WebRtcAec_Free(void* aecInst); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 128 | |
| 129 | /* |
| 130 | * Initializes an AEC instance. |
| 131 | * |
| 132 | * Inputs Description |
| 133 | * ------------------------------------------------------------------- |
kwiberg@webrtc.org | 924e81f | 2014-05-12 09:55:19 +0000 | [diff] [blame] | 134 | * void* aecInst Pointer to the AEC instance |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 135 | * int32_t sampFreq Sampling frequency of data |
| 136 | * int32_t scSampFreq Soundcard sampling frequency |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 137 | * |
| 138 | * Outputs Description |
| 139 | * ------------------------------------------------------------------- |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 140 | * int32_t return 0: OK |
| 141 | * -1: error |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 142 | */ |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 143 | int32_t WebRtcAec_Init(void* aecInst, int32_t sampFreq, int32_t scSampFreq); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 144 | |
| 145 | /* |
| 146 | * Inserts an 80 or 160 sample block of data into the farend buffer. |
| 147 | * |
| 148 | * Inputs Description |
| 149 | * ------------------------------------------------------------------- |
kwiberg@webrtc.org | 924e81f | 2014-05-12 09:55:19 +0000 | [diff] [blame] | 150 | * void* aecInst Pointer to the AEC instance |
kwiberg@webrtc.org | 38214d5 | 2014-07-03 09:47:33 +0000 | [diff] [blame] | 151 | * const float* farend In buffer containing one frame of |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 152 | * farend signal for L band |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 153 | * int16_t nrOfSamples Number of samples in farend buffer |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 154 | * |
| 155 | * Outputs Description |
| 156 | * ------------------------------------------------------------------- |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 157 | * int32_t return 0: OK |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame] | 158 | * 12000-12050: error code |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 159 | */ |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 160 | int32_t WebRtcAec_BufferFarend(void* aecInst, |
kwiberg@webrtc.org | 38214d5 | 2014-07-03 09:47:33 +0000 | [diff] [blame] | 161 | const float* farend, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 162 | size_t nrOfSamples); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 163 | |
| 164 | /* |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame] | 165 | * Reports any errors that would arise if buffering a farend buffer |
| 166 | * |
| 167 | * Inputs Description |
| 168 | * ------------------------------------------------------------------- |
| 169 | * void* aecInst Pointer to the AEC instance |
| 170 | * const float* farend In buffer containing one frame of |
| 171 | * farend signal for L band |
| 172 | * int16_t nrOfSamples Number of samples in farend buffer |
| 173 | * |
| 174 | * Outputs Description |
| 175 | * ------------------------------------------------------------------- |
| 176 | * int32_t return 0: OK |
| 177 | * 12000-12050: error code |
| 178 | */ |
| 179 | int32_t WebRtcAec_GetBufferFarendError(void* aecInst, |
| 180 | const float* farend, |
| 181 | size_t nrOfSamples); |
| 182 | |
| 183 | /* |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 184 | * Runs the echo canceller on an 80 or 160 sample blocks of data. |
| 185 | * |
| 186 | * Inputs Description |
| 187 | * ------------------------------------------------------------------- |
kwiberg@webrtc.org | 924e81f | 2014-05-12 09:55:19 +0000 | [diff] [blame] | 188 | * void* aecInst Pointer to the AEC instance |
aluebs@webrtc.org | c78d81a | 2015-01-21 19:10:55 +0000 | [diff] [blame] | 189 | * float* const* nearend In buffer containing one frame of |
| 190 | * nearend+echo signal for each band |
| 191 | * int num_bands Number of bands in nearend buffer |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 192 | * int16_t nrOfSamples Number of samples in nearend buffer |
| 193 | * int16_t msInSndCardBuf Delay estimate for sound card and |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 194 | * system buffers |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 195 | * int16_t skew Difference between number of samples played |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 196 | * and recorded at the soundcard (for clock skew |
| 197 | * compensation) |
| 198 | * |
| 199 | * Outputs Description |
| 200 | * ------------------------------------------------------------------- |
aluebs@webrtc.org | c78d81a | 2015-01-21 19:10:55 +0000 | [diff] [blame] | 201 | * float* const* out Out buffer, one frame of processed nearend |
| 202 | * for each band |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 203 | * int32_t return 0: OK |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame] | 204 | * 12000-12050: error code |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 205 | */ |
andrew@webrtc.org | 13b2d46 | 2013-10-08 23:41:42 +0000 | [diff] [blame] | 206 | int32_t WebRtcAec_Process(void* aecInst, |
aluebs@webrtc.org | c78d81a | 2015-01-21 19:10:55 +0000 | [diff] [blame] | 207 | const float* const* nearend, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 208 | size_t num_bands, |
aluebs@webrtc.org | c78d81a | 2015-01-21 19:10:55 +0000 | [diff] [blame] | 209 | float* const* out, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 210 | size_t nrOfSamples, |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 211 | int16_t msInSndCardBuf, |
| 212 | int32_t skew); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 213 | |
| 214 | /* |
| 215 | * This function enables the user to set certain parameters on-the-fly. |
| 216 | * |
| 217 | * Inputs Description |
| 218 | * ------------------------------------------------------------------- |
kwiberg@webrtc.org | 924e81f | 2014-05-12 09:55:19 +0000 | [diff] [blame] | 219 | * void* handle Pointer to the AEC instance |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 220 | * AecConfig config Config instance that contains all |
| 221 | * properties to be set |
| 222 | * |
| 223 | * Outputs Description |
| 224 | * ------------------------------------------------------------------- |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame] | 225 | * int return 0: OK |
| 226 | * 12000-12050: error code |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 227 | */ |
bjornv@webrtc.org | 47b274d | 2013-02-20 17:09:47 +0000 | [diff] [blame] | 228 | int WebRtcAec_set_config(void* handle, AecConfig config); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 229 | |
| 230 | /* |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 231 | * Gets the current echo status of the nearend signal. |
| 232 | * |
| 233 | * Inputs Description |
| 234 | * ------------------------------------------------------------------- |
kwiberg@webrtc.org | 924e81f | 2014-05-12 09:55:19 +0000 | [diff] [blame] | 235 | * void* handle Pointer to the AEC instance |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 236 | * |
| 237 | * Outputs Description |
| 238 | * ------------------------------------------------------------------- |
kwiberg@webrtc.org | 924e81f | 2014-05-12 09:55:19 +0000 | [diff] [blame] | 239 | * int* status 0: Almost certainly nearend single-talk |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 240 | * 1: Might not be neared single-talk |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame] | 241 | * int return 0: OK |
| 242 | * 12000-12050: error code |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 243 | */ |
bjornv@webrtc.org | 21a2fc9 | 2013-02-15 17:01:03 +0000 | [diff] [blame] | 244 | int WebRtcAec_get_echo_status(void* handle, int* status); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 245 | |
| 246 | /* |
| 247 | * Gets the current echo metrics for the session. |
| 248 | * |
| 249 | * Inputs Description |
| 250 | * ------------------------------------------------------------------- |
kwiberg@webrtc.org | 924e81f | 2014-05-12 09:55:19 +0000 | [diff] [blame] | 251 | * void* handle Pointer to the AEC instance |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 252 | * |
| 253 | * Outputs Description |
| 254 | * ------------------------------------------------------------------- |
kwiberg@webrtc.org | 924e81f | 2014-05-12 09:55:19 +0000 | [diff] [blame] | 255 | * AecMetrics* metrics Struct which will be filled out with the |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 256 | * current echo metrics. |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame] | 257 | * int return 0: OK |
| 258 | * 12000-12050: error code |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 259 | */ |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 260 | int WebRtcAec_GetMetrics(void* handle, AecMetrics* metrics); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 261 | |
| 262 | /* |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 263 | * Gets the current delay metrics for the session. |
| 264 | * |
| 265 | * Inputs Description |
| 266 | * ------------------------------------------------------------------- |
bjornv@webrtc.org | b1786db | 2015-02-03 06:06:26 +0000 | [diff] [blame] | 267 | * void* handle Pointer to the AEC instance |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 268 | * |
| 269 | * Outputs Description |
| 270 | * ------------------------------------------------------------------- |
bjornv@webrtc.org | b1786db | 2015-02-03 06:06:26 +0000 | [diff] [blame] | 271 | * int* median Delay median value. |
| 272 | * int* std Delay standard deviation. |
| 273 | * float* fraction_poor_delays Fraction of the delay estimates that may |
| 274 | * cause the AEC to perform poorly. |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 275 | * |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame] | 276 | * int return 0: OK |
| 277 | * 12000-12050: error code |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 278 | */ |
bjornv@webrtc.org | b1786db | 2015-02-03 06:06:26 +0000 | [diff] [blame] | 279 | int WebRtcAec_GetDelayMetrics(void* handle, |
| 280 | int* median, |
| 281 | int* std, |
| 282 | float* fraction_poor_delays); |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 283 | |
bjornv@webrtc.org | 132c15d | 2013-02-27 21:03:41 +0000 | [diff] [blame] | 284 | // Returns a pointer to the low level AEC handle. |
| 285 | // |
| 286 | // Input: |
| 287 | // - handle : Pointer to the AEC instance. |
| 288 | // |
| 289 | // Return value: |
| 290 | // - AecCore pointer : NULL for error. |
| 291 | // |
| 292 | struct AecCore* WebRtcAec_aec_core(void* handle); |
| 293 | |
peah | 50e21bd | 2016-03-05 08:39:21 -0800 | [diff] [blame] | 294 | } // namespace webrtc |
| 295 | |
Henrik Kjellander | 9b72af9 | 2015-11-11 20:16:11 +0100 | [diff] [blame] | 296 | #endif // WEBRTC_MODULES_AUDIO_PROCESSING_AEC_ECHO_CANCELLATION_H_ |