niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
bjornv@webrtc.org | 0c6f931 | 2012-01-30 09:39:08 +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 | |
| 11 | /* |
| 12 | * Contains the API functions for the AEC. |
| 13 | */ |
bjornv@webrtc.org | 6f6acd9 | 2013-02-14 21:17:12 +0000 | [diff] [blame] | 14 | #include "webrtc/modules/audio_processing/aec/include/echo_cancellation.h" |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 15 | |
| 16 | #include <math.h> |
andrew@webrtc.org | 8594f76 | 2011-11-22 00:51:41 +0000 | [diff] [blame] | 17 | #ifdef WEBRTC_AEC_DEBUG_DUMP |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 18 | #include <stdio.h> |
| 19 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | |
bjornv@webrtc.org | 6f6acd9 | 2013-02-14 21:17:12 +0000 | [diff] [blame] | 23 | #include "webrtc/common_audio/signal_processing/include/signal_processing_library.h" |
| 24 | #include "webrtc/modules/audio_processing/aec/aec_core.h" |
| 25 | #include "webrtc/modules/audio_processing/aec/aec_resampler.h" |
| 26 | #include "webrtc/modules/audio_processing/aec/echo_cancellation_internal.h" |
| 27 | #include "webrtc/modules/audio_processing/utility/ring_buffer.h" |
| 28 | #include "webrtc/typedefs.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 29 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 30 | // Maximum length of resampled signal. Must be an integer multiple of frames |
| 31 | // (ceil(1/(1 + MIN_SKEW)*2) + 1)*FRAME_LEN |
| 32 | // The factor of 2 handles wb, and the + 1 is as a safety margin |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 33 | // TODO(bjornv): Replace with kResamplerBufferSize |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 34 | #define MAX_RESAMP_LEN (5 * FRAME_LEN) |
| 35 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 36 | static const int kMaxBufSizeStart = 62; // In partitions |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 37 | static const int sampMsNb = 8; // samples per ms in nb |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 38 | static const int initCheck = 42; |
| 39 | |
andrew@webrtc.org | 1e39bc8 | 2011-11-27 23:46:23 +0000 | [diff] [blame] | 40 | #ifdef WEBRTC_AEC_DEBUG_DUMP |
bjornv@webrtc.org | 7267ffd | 2013-02-14 17:56:23 +0000 | [diff] [blame] | 41 | int webrtc_aec_instance_count = 0; |
andrew@webrtc.org | 1e39bc8 | 2011-11-27 23:46:23 +0000 | [diff] [blame] | 42 | #endif |
| 43 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 44 | // Estimates delay to set the position of the far-end buffer read pointer |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 45 | // (controlled by knownDelay) |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 46 | static int EstBufDelay(aecpc_t *aecInst); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 47 | |
| 48 | WebRtc_Word32 WebRtcAec_Create(void **aecInst) |
| 49 | { |
| 50 | aecpc_t *aecpc; |
| 51 | if (aecInst == NULL) { |
| 52 | return -1; |
| 53 | } |
| 54 | |
| 55 | aecpc = malloc(sizeof(aecpc_t)); |
| 56 | *aecInst = aecpc; |
| 57 | if (aecpc == NULL) { |
| 58 | return -1; |
| 59 | } |
| 60 | |
| 61 | if (WebRtcAec_CreateAec(&aecpc->aec) == -1) { |
| 62 | WebRtcAec_Free(aecpc); |
| 63 | aecpc = NULL; |
| 64 | return -1; |
| 65 | } |
| 66 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 67 | if (WebRtcAec_CreateResampler(&aecpc->resampler) == -1) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 68 | WebRtcAec_Free(aecpc); |
| 69 | aecpc = NULL; |
| 70 | return -1; |
| 71 | } |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 72 | // Create far-end pre-buffer. The buffer size has to be large enough for |
| 73 | // largest possible drift compensation (kResamplerBufferSize) + "almost" an |
| 74 | // FFT buffer (PART_LEN2 - 1). |
| 75 | if (WebRtc_CreateBuffer(&aecpc->far_pre_buf, |
| 76 | PART_LEN2 + kResamplerBufferSize, |
| 77 | sizeof(float)) == -1) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 78 | WebRtcAec_Free(aecpc); |
| 79 | aecpc = NULL; |
| 80 | return -1; |
| 81 | } |
| 82 | |
| 83 | aecpc->initFlag = 0; |
| 84 | aecpc->lastError = 0; |
| 85 | |
andrew@webrtc.org | 8594f76 | 2011-11-22 00:51:41 +0000 | [diff] [blame] | 86 | #ifdef WEBRTC_AEC_DEBUG_DUMP |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 87 | if (WebRtc_CreateBuffer(&aecpc->far_pre_buf_s16, |
| 88 | PART_LEN2 + kResamplerBufferSize, |
| 89 | sizeof(int16_t)) == -1) { |
| 90 | WebRtcAec_Free(aecpc); |
| 91 | aecpc = NULL; |
| 92 | return -1; |
| 93 | } |
andrew@webrtc.org | 1e39bc8 | 2011-11-27 23:46:23 +0000 | [diff] [blame] | 94 | { |
| 95 | char filename[64]; |
bjornv@webrtc.org | 7267ffd | 2013-02-14 17:56:23 +0000 | [diff] [blame] | 96 | sprintf(filename, "aec_buf%d.dat", webrtc_aec_instance_count); |
andrew@webrtc.org | 1e39bc8 | 2011-11-27 23:46:23 +0000 | [diff] [blame] | 97 | aecpc->bufFile = fopen(filename, "wb"); |
bjornv@webrtc.org | 7267ffd | 2013-02-14 17:56:23 +0000 | [diff] [blame] | 98 | sprintf(filename, "aec_skew%d.dat", webrtc_aec_instance_count); |
andrew@webrtc.org | 1e39bc8 | 2011-11-27 23:46:23 +0000 | [diff] [blame] | 99 | aecpc->skewFile = fopen(filename, "wb"); |
bjornv@webrtc.org | 7267ffd | 2013-02-14 17:56:23 +0000 | [diff] [blame] | 100 | sprintf(filename, "aec_delay%d.dat", webrtc_aec_instance_count); |
andrew@webrtc.org | 1e39bc8 | 2011-11-27 23:46:23 +0000 | [diff] [blame] | 101 | aecpc->delayFile = fopen(filename, "wb"); |
bjornv@webrtc.org | 7267ffd | 2013-02-14 17:56:23 +0000 | [diff] [blame] | 102 | webrtc_aec_instance_count++; |
andrew@webrtc.org | 1e39bc8 | 2011-11-27 23:46:23 +0000 | [diff] [blame] | 103 | } |
andrew@webrtc.org | 8594f76 | 2011-11-22 00:51:41 +0000 | [diff] [blame] | 104 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | WebRtc_Word32 WebRtcAec_Free(void *aecInst) |
| 110 | { |
| 111 | aecpc_t *aecpc = aecInst; |
| 112 | |
| 113 | if (aecpc == NULL) { |
| 114 | return -1; |
| 115 | } |
| 116 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 117 | WebRtc_FreeBuffer(aecpc->far_pre_buf); |
| 118 | |
andrew@webrtc.org | 8594f76 | 2011-11-22 00:51:41 +0000 | [diff] [blame] | 119 | #ifdef WEBRTC_AEC_DEBUG_DUMP |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 120 | WebRtc_FreeBuffer(aecpc->far_pre_buf_s16); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 121 | fclose(aecpc->bufFile); |
| 122 | fclose(aecpc->skewFile); |
| 123 | fclose(aecpc->delayFile); |
andrew@webrtc.org | 8594f76 | 2011-11-22 00:51:41 +0000 | [diff] [blame] | 124 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 125 | |
| 126 | WebRtcAec_FreeAec(aecpc->aec); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 127 | WebRtcAec_FreeResampler(aecpc->resampler); |
| 128 | free(aecpc); |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | WebRtc_Word32 WebRtcAec_Init(void *aecInst, WebRtc_Word32 sampFreq, WebRtc_Word32 scSampFreq) |
| 134 | { |
| 135 | aecpc_t *aecpc = aecInst; |
| 136 | AecConfig aecConfig; |
| 137 | |
| 138 | if (aecpc == NULL) { |
| 139 | return -1; |
| 140 | } |
| 141 | |
| 142 | if (sampFreq != 8000 && sampFreq != 16000 && sampFreq != 32000) { |
| 143 | aecpc->lastError = AEC_BAD_PARAMETER_ERROR; |
| 144 | return -1; |
| 145 | } |
| 146 | aecpc->sampFreq = sampFreq; |
| 147 | |
| 148 | if (scSampFreq < 1 || scSampFreq > 96000) { |
| 149 | aecpc->lastError = AEC_BAD_PARAMETER_ERROR; |
| 150 | return -1; |
| 151 | } |
| 152 | aecpc->scSampFreq = scSampFreq; |
| 153 | |
| 154 | // Initialize echo canceller core |
| 155 | if (WebRtcAec_InitAec(aecpc->aec, aecpc->sampFreq) == -1) { |
| 156 | aecpc->lastError = AEC_UNSPECIFIED_ERROR; |
| 157 | return -1; |
| 158 | } |
| 159 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 160 | if (WebRtcAec_InitResampler(aecpc->resampler, aecpc->scSampFreq) == -1) { |
| 161 | aecpc->lastError = AEC_UNSPECIFIED_ERROR; |
| 162 | return -1; |
| 163 | } |
| 164 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 165 | if (WebRtc_InitBuffer(aecpc->far_pre_buf) == -1) { |
| 166 | aecpc->lastError = AEC_UNSPECIFIED_ERROR; |
| 167 | return -1; |
| 168 | } |
| 169 | WebRtc_MoveReadPtr(aecpc->far_pre_buf, -PART_LEN); // Start overlap. |
| 170 | |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 171 | aecpc->initFlag = initCheck; // indicates that initialization has been done |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 172 | |
| 173 | if (aecpc->sampFreq == 32000) { |
| 174 | aecpc->splitSampFreq = 16000; |
| 175 | } |
| 176 | else { |
| 177 | aecpc->splitSampFreq = sampFreq; |
| 178 | } |
| 179 | |
| 180 | aecpc->skewFrCtr = 0; |
| 181 | aecpc->activity = 0; |
| 182 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 183 | aecpc->delayCtr = 0; |
| 184 | |
| 185 | aecpc->sum = 0; |
| 186 | aecpc->counter = 0; |
| 187 | aecpc->checkBuffSize = 1; |
| 188 | aecpc->firstVal = 0; |
| 189 | |
| 190 | aecpc->ECstartup = 1; |
| 191 | aecpc->bufSizeStart = 0; |
| 192 | aecpc->checkBufSizeCtr = 0; |
| 193 | aecpc->filtDelay = 0; |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 194 | aecpc->timeForDelayChange = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 195 | aecpc->knownDelay = 0; |
| 196 | aecpc->lastDelayDiff = 0; |
| 197 | |
| 198 | aecpc->skew = 0; |
| 199 | aecpc->resample = kAecFalse; |
| 200 | aecpc->highSkewCtr = 0; |
| 201 | aecpc->sampFactor = (aecpc->scSampFreq * 1.0f) / aecpc->splitSampFreq; |
| 202 | |
bjornv@webrtc.org | 6f6acd9 | 2013-02-14 21:17:12 +0000 | [diff] [blame] | 203 | // Sampling frequency multiplier (SWB is processed as 160 frame size). |
| 204 | aecpc->rate_factor = aecpc->splitSampFreq / 8000; |
| 205 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 206 | // Default settings. |
| 207 | aecConfig.nlpMode = kAecNlpModerate; |
| 208 | aecConfig.skewMode = kAecFalse; |
| 209 | aecConfig.metricsMode = kAecFalse; |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 210 | aecConfig.delay_logging = kAecFalse; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 211 | |
| 212 | if (WebRtcAec_set_config(aecpc, aecConfig) == -1) { |
| 213 | aecpc->lastError = AEC_UNSPECIFIED_ERROR; |
| 214 | return -1; |
| 215 | } |
| 216 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 217 | #ifdef WEBRTC_AEC_DEBUG_DUMP |
| 218 | if (WebRtc_InitBuffer(aecpc->far_pre_buf_s16) == -1) { |
| 219 | aecpc->lastError = AEC_UNSPECIFIED_ERROR; |
| 220 | return -1; |
| 221 | } |
| 222 | WebRtc_MoveReadPtr(aecpc->far_pre_buf_s16, -PART_LEN); // Start overlap. |
| 223 | #endif |
| 224 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | // only buffer L band for farend |
| 229 | WebRtc_Word32 WebRtcAec_BufferFarend(void *aecInst, const WebRtc_Word16 *farend, |
| 230 | WebRtc_Word16 nrOfSamples) |
| 231 | { |
| 232 | aecpc_t *aecpc = aecInst; |
| 233 | WebRtc_Word32 retVal = 0; |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 234 | int newNrOfSamples = (int) nrOfSamples; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 235 | short newFarend[MAX_RESAMP_LEN]; |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 236 | const int16_t* farend_ptr = farend; |
| 237 | float tmp_farend[MAX_RESAMP_LEN]; |
| 238 | const float* farend_float = tmp_farend; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 239 | float skew; |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 240 | int i = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 241 | |
| 242 | if (aecpc == NULL) { |
| 243 | return -1; |
| 244 | } |
| 245 | |
| 246 | if (farend == NULL) { |
| 247 | aecpc->lastError = AEC_NULL_POINTER_ERROR; |
| 248 | return -1; |
| 249 | } |
| 250 | |
| 251 | if (aecpc->initFlag != initCheck) { |
| 252 | aecpc->lastError = AEC_UNINITIALIZED_ERROR; |
| 253 | return -1; |
| 254 | } |
| 255 | |
| 256 | // number of samples == 160 for SWB input |
| 257 | if (nrOfSamples != 80 && nrOfSamples != 160) { |
| 258 | aecpc->lastError = AEC_BAD_PARAMETER_ERROR; |
| 259 | return -1; |
| 260 | } |
| 261 | |
| 262 | skew = aecpc->skew; |
| 263 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 264 | if (aecpc->skewMode == kAecTrue && aecpc->resample == kAecTrue) { |
| 265 | // Resample and get a new number of samples |
bjornv@webrtc.org | 281b798 | 2012-05-30 07:41:57 +0000 | [diff] [blame] | 266 | WebRtcAec_ResampleLinear(aecpc->resampler, farend, nrOfSamples, skew, |
| 267 | newFarend, &newNrOfSamples); |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 268 | farend_ptr = (const int16_t*) newFarend; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 269 | } |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 270 | |
| 271 | aecpc->aec->system_delay += newNrOfSamples; |
| 272 | |
| 273 | #ifdef WEBRTC_AEC_DEBUG_DUMP |
| 274 | WebRtc_WriteBuffer(aecpc->far_pre_buf_s16, farend_ptr, |
| 275 | (size_t) newNrOfSamples); |
| 276 | #endif |
| 277 | // Cast to float and write the time-domain data to |far_pre_buf|. |
| 278 | for (i = 0; i < newNrOfSamples; i++) { |
| 279 | tmp_farend[i] = (float) farend_ptr[i]; |
| 280 | } |
| 281 | WebRtc_WriteBuffer(aecpc->far_pre_buf, farend_float, |
| 282 | (size_t) newNrOfSamples); |
| 283 | |
| 284 | // Transform to frequency domain if we have enough data. |
| 285 | while (WebRtc_available_read(aecpc->far_pre_buf) >= PART_LEN2) { |
| 286 | // We have enough data to pass to the FFT, hence read PART_LEN2 samples. |
| 287 | WebRtc_ReadBuffer(aecpc->far_pre_buf, (void**) &farend_float, tmp_farend, |
| 288 | PART_LEN2); |
| 289 | |
| 290 | WebRtcAec_BufferFarendPartition(aecpc->aec, farend_float); |
| 291 | |
| 292 | // Rewind |far_pre_buf| PART_LEN samples for overlap before continuing. |
| 293 | WebRtc_MoveReadPtr(aecpc->far_pre_buf, -PART_LEN); |
| 294 | #ifdef WEBRTC_AEC_DEBUG_DUMP |
| 295 | WebRtc_ReadBuffer(aecpc->far_pre_buf_s16, (void**) &farend_ptr, newFarend, |
| 296 | PART_LEN2); |
bjornv@webrtc.org | 0a480cb | 2013-02-19 21:41:27 +0000 | [diff] [blame] | 297 | WebRtc_WriteBuffer(WebRtcAec_far_time_buf(aecpc->aec), |
| 298 | &farend_ptr[PART_LEN], 1); |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 299 | WebRtc_MoveReadPtr(aecpc->far_pre_buf_s16, -PART_LEN); |
| 300 | #endif |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | return retVal; |
| 304 | } |
| 305 | |
| 306 | WebRtc_Word32 WebRtcAec_Process(void *aecInst, const WebRtc_Word16 *nearend, |
| 307 | const WebRtc_Word16 *nearendH, WebRtc_Word16 *out, WebRtc_Word16 *outH, |
| 308 | WebRtc_Word16 nrOfSamples, WebRtc_Word16 msInSndCardBuf, WebRtc_Word32 skew) |
| 309 | { |
| 310 | aecpc_t *aecpc = aecInst; |
| 311 | WebRtc_Word32 retVal = 0; |
| 312 | short i; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 313 | short nBlocks10ms; |
| 314 | short nFrames; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 315 | // Limit resampling to doubling/halving of signal |
| 316 | const float minSkewEst = -0.5f; |
| 317 | const float maxSkewEst = 1.0f; |
| 318 | |
| 319 | if (aecpc == NULL) { |
| 320 | return -1; |
| 321 | } |
| 322 | |
| 323 | if (nearend == NULL) { |
| 324 | aecpc->lastError = AEC_NULL_POINTER_ERROR; |
| 325 | return -1; |
| 326 | } |
| 327 | |
| 328 | if (out == NULL) { |
| 329 | aecpc->lastError = AEC_NULL_POINTER_ERROR; |
| 330 | return -1; |
| 331 | } |
| 332 | |
| 333 | if (aecpc->initFlag != initCheck) { |
| 334 | aecpc->lastError = AEC_UNINITIALIZED_ERROR; |
| 335 | return -1; |
| 336 | } |
| 337 | |
| 338 | // number of samples == 160 for SWB input |
| 339 | if (nrOfSamples != 80 && nrOfSamples != 160) { |
| 340 | aecpc->lastError = AEC_BAD_PARAMETER_ERROR; |
| 341 | return -1; |
| 342 | } |
| 343 | |
| 344 | // Check for valid pointers based on sampling rate |
| 345 | if (aecpc->sampFreq == 32000 && nearendH == NULL) { |
| 346 | aecpc->lastError = AEC_NULL_POINTER_ERROR; |
| 347 | return -1; |
| 348 | } |
| 349 | |
| 350 | if (msInSndCardBuf < 0) { |
| 351 | msInSndCardBuf = 0; |
| 352 | aecpc->lastError = AEC_BAD_PARAMETER_WARNING; |
| 353 | retVal = -1; |
| 354 | } |
| 355 | else if (msInSndCardBuf > 500) { |
| 356 | msInSndCardBuf = 500; |
| 357 | aecpc->lastError = AEC_BAD_PARAMETER_WARNING; |
| 358 | retVal = -1; |
| 359 | } |
andrew@webrtc.org | a919d3a | 2011-11-27 23:40:58 +0000 | [diff] [blame] | 360 | // TODO(andrew): we need to investigate if this +10 is really wanted. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 361 | msInSndCardBuf += 10; |
| 362 | aecpc->msInSndCardBuf = msInSndCardBuf; |
| 363 | |
| 364 | if (aecpc->skewMode == kAecTrue) { |
| 365 | if (aecpc->skewFrCtr < 25) { |
| 366 | aecpc->skewFrCtr++; |
| 367 | } |
| 368 | else { |
| 369 | retVal = WebRtcAec_GetSkew(aecpc->resampler, skew, &aecpc->skew); |
| 370 | if (retVal == -1) { |
| 371 | aecpc->skew = 0; |
| 372 | aecpc->lastError = AEC_BAD_PARAMETER_WARNING; |
| 373 | } |
| 374 | |
| 375 | aecpc->skew /= aecpc->sampFactor*nrOfSamples; |
| 376 | |
| 377 | if (aecpc->skew < 1.0e-3 && aecpc->skew > -1.0e-3) { |
| 378 | aecpc->resample = kAecFalse; |
| 379 | } |
| 380 | else { |
| 381 | aecpc->resample = kAecTrue; |
| 382 | } |
| 383 | |
| 384 | if (aecpc->skew < minSkewEst) { |
| 385 | aecpc->skew = minSkewEst; |
| 386 | } |
| 387 | else if (aecpc->skew > maxSkewEst) { |
| 388 | aecpc->skew = maxSkewEst; |
| 389 | } |
| 390 | |
andrew@webrtc.org | 8594f76 | 2011-11-22 00:51:41 +0000 | [diff] [blame] | 391 | #ifdef WEBRTC_AEC_DEBUG_DUMP |
andrew@webrtc.org | 7d8c567 | 2012-06-01 02:41:14 +0000 | [diff] [blame] | 392 | (void)fwrite(&aecpc->skew, sizeof(aecpc->skew), 1, aecpc->skewFile); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 393 | #endif |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | nFrames = nrOfSamples / FRAME_LEN; |
bjornv@webrtc.org | 6f6acd9 | 2013-02-14 21:17:12 +0000 | [diff] [blame] | 398 | nBlocks10ms = nFrames / aecpc->rate_factor; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 399 | |
| 400 | if (aecpc->ECstartup) { |
andrew@webrtc.org | 6423509 | 2011-08-19 21:22:08 +0000 | [diff] [blame] | 401 | if (nearend != out) { |
| 402 | // Only needed if they don't already point to the same place. |
| 403 | memcpy(out, nearend, sizeof(short) * nrOfSamples); |
| 404 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 405 | |
| 406 | // The AEC is in the start up mode |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 407 | // AEC is disabled until the system delay is OK |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 408 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 409 | // Mechanism to ensure that the system delay is reasonably stable. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 410 | if (aecpc->checkBuffSize) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 411 | aecpc->checkBufSizeCtr++; |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 412 | // Before we fill up the far-end buffer we require the system delay |
| 413 | // to be stable (+/-8 ms) compared to the first value. This |
| 414 | // comparison is made during the following 6 consecutive 10 ms |
| 415 | // blocks. If it seems to be stable then we start to fill up the |
| 416 | // far-end buffer. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 417 | if (aecpc->counter == 0) { |
| 418 | aecpc->firstVal = aecpc->msInSndCardBuf; |
| 419 | aecpc->sum = 0; |
| 420 | } |
| 421 | |
| 422 | if (abs(aecpc->firstVal - aecpc->msInSndCardBuf) < |
| 423 | WEBRTC_SPL_MAX(0.2 * aecpc->msInSndCardBuf, sampMsNb)) { |
| 424 | aecpc->sum += aecpc->msInSndCardBuf; |
| 425 | aecpc->counter++; |
| 426 | } |
| 427 | else { |
| 428 | aecpc->counter = 0; |
| 429 | } |
| 430 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 431 | if (aecpc->counter * nBlocks10ms >= 6) { |
| 432 | // The far-end buffer size is determined in partitions of |
| 433 | // PART_LEN samples. Use 75% of the average value of the system |
| 434 | // delay as buffer size to start with. |
| 435 | aecpc->bufSizeStart = WEBRTC_SPL_MIN((3 * aecpc->sum * |
bjornv@webrtc.org | 6f6acd9 | 2013-02-14 21:17:12 +0000 | [diff] [blame] | 436 | aecpc->rate_factor * 8) / (4 * aecpc->counter * PART_LEN), |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 437 | kMaxBufSizeStart); |
| 438 | // Buffer size has now been determined. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 439 | aecpc->checkBuffSize = 0; |
| 440 | } |
| 441 | |
| 442 | if (aecpc->checkBufSizeCtr * nBlocks10ms > 50) { |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 443 | // For really bad systems, don't disable the echo canceller for |
| 444 | // more than 0.5 sec. |
| 445 | aecpc->bufSizeStart = WEBRTC_SPL_MIN((aecpc->msInSndCardBuf * |
bjornv@webrtc.org | 6f6acd9 | 2013-02-14 21:17:12 +0000 | [diff] [blame] | 446 | aecpc->rate_factor * 3) / 40, kMaxBufSizeStart); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 447 | aecpc->checkBuffSize = 0; |
| 448 | } |
| 449 | } |
| 450 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 451 | // If |checkBuffSize| changed in the if-statement above. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 452 | if (!aecpc->checkBuffSize) { |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 453 | // The system delay is now reasonably stable (or has been unstable |
| 454 | // for too long). When the far-end buffer is filled with |
| 455 | // approximately the same amount of data as reported by the system |
| 456 | // we end the startup phase. |
| 457 | int overhead_elements = aecpc->aec->system_delay / PART_LEN - |
| 458 | aecpc->bufSizeStart; |
| 459 | if (overhead_elements == 0) { |
| 460 | // Enable the AEC |
| 461 | aecpc->ECstartup = 0; |
| 462 | } else if (overhead_elements > 0) { |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 463 | // TODO(bjornv): Do we need a check on how much we actually |
| 464 | // moved the read pointer? It should always be possible to move |
| 465 | // the pointer |overhead_elements| since we have only added data |
| 466 | // to the buffer and no delay compensation nor AEC processing |
| 467 | // has been done. |
andrew@webrtc.org | 61bf8e3 | 2012-03-15 19:04:55 +0000 | [diff] [blame] | 468 | WebRtcAec_MoveFarReadPtr(aecpc->aec, overhead_elements); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 469 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 470 | // Enable the AEC |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 471 | aecpc->ECstartup = 0; |
| 472 | } |
| 473 | } |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 474 | } else { |
| 475 | // AEC is enabled. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 476 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 477 | EstBufDelay(aecpc); |
| 478 | |
| 479 | // Note that 1 frame is supported for NB and 2 frames for WB. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 480 | for (i = 0; i < nFrames; i++) { |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 481 | // Call the AEC. |
| 482 | WebRtcAec_ProcessFrame(aecpc->aec, |
| 483 | &nearend[FRAME_LEN * i], |
| 484 | &nearendH[FRAME_LEN * i], |
bjornv@webrtc.org | 716fd90 | 2013-02-20 16:59:41 +0000 | [diff] [blame] | 485 | aecpc->knownDelay, |
| 486 | &out[FRAME_LEN * i], |
| 487 | &outH[FRAME_LEN * i]); |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 488 | // TODO(bjornv): Re-structure such that we don't have to pass |
| 489 | // |aecpc->knownDelay| as input. Change name to something like |
| 490 | // |system_buffer_diff|. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | |
andrew@webrtc.org | 8594f76 | 2011-11-22 00:51:41 +0000 | [diff] [blame] | 494 | #ifdef WEBRTC_AEC_DEBUG_DUMP |
| 495 | { |
andrew@webrtc.org | 7d8c567 | 2012-06-01 02:41:14 +0000 | [diff] [blame] | 496 | int16_t far_buf_size_ms = (int16_t)(aecpc->aec->system_delay / |
bjornv@webrtc.org | 6f6acd9 | 2013-02-14 21:17:12 +0000 | [diff] [blame] | 497 | (sampMsNb * aecpc->rate_factor)); |
andrew@webrtc.org | 7d8c567 | 2012-06-01 02:41:14 +0000 | [diff] [blame] | 498 | (void)fwrite(&far_buf_size_ms, 2, 1, aecpc->bufFile); |
| 499 | (void)fwrite(&aecpc->knownDelay, sizeof(aecpc->knownDelay), 1, |
| 500 | aecpc->delayFile); |
andrew@webrtc.org | 8594f76 | 2011-11-22 00:51:41 +0000 | [diff] [blame] | 501 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 502 | #endif |
| 503 | |
| 504 | return retVal; |
| 505 | } |
| 506 | |
bjornv@webrtc.org | 47b274d | 2013-02-20 17:09:47 +0000 | [diff] [blame^] | 507 | int WebRtcAec_set_config(void* handle, AecConfig config) { |
| 508 | aecpc_t* self = (aecpc_t*)handle; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 509 | |
bjornv@webrtc.org | 47b274d | 2013-02-20 17:09:47 +0000 | [diff] [blame^] | 510 | if (handle == NULL ) { |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 511 | return -1; |
| 512 | } |
bjornv@webrtc.org | 47b274d | 2013-02-20 17:09:47 +0000 | [diff] [blame^] | 513 | |
| 514 | if (self->initFlag != initCheck) { |
| 515 | self->lastError = AEC_UNINITIALIZED_ERROR; |
| 516 | return -1; |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 517 | } |
| 518 | |
bjornv@webrtc.org | 47b274d | 2013-02-20 17:09:47 +0000 | [diff] [blame^] | 519 | if (config.skewMode != kAecFalse && config.skewMode != kAecTrue) { |
| 520 | self->lastError = AEC_BAD_PARAMETER_ERROR; |
| 521 | return -1; |
| 522 | } |
| 523 | self->skewMode = config.skewMode; |
| 524 | |
| 525 | if (config.nlpMode != kAecNlpConservative && config.nlpMode != kAecNlpModerate |
| 526 | && config.nlpMode != kAecNlpAggressive) { |
| 527 | self->lastError = AEC_BAD_PARAMETER_ERROR; |
| 528 | return -1; |
| 529 | } |
| 530 | |
| 531 | if (config.metricsMode != kAecFalse && config.metricsMode != kAecTrue) { |
| 532 | self->lastError = AEC_BAD_PARAMETER_ERROR; |
| 533 | return -1; |
| 534 | } |
| 535 | |
| 536 | if (config.delay_logging != kAecFalse && config.delay_logging != kAecTrue) { |
| 537 | self->lastError = AEC_BAD_PARAMETER_ERROR; |
| 538 | return -1; |
| 539 | } |
| 540 | |
| 541 | WebRtcAec_SetConfigCore(self->aec, config.nlpMode, config.metricsMode, |
| 542 | config.delay_logging); |
| 543 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 544 | } |
| 545 | |
bjornv@webrtc.org | 21a2fc9 | 2013-02-15 17:01:03 +0000 | [diff] [blame] | 546 | int WebRtcAec_get_echo_status(void* handle, int* status) { |
| 547 | aecpc_t* self = (aecpc_t*)handle; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 548 | |
bjornv@webrtc.org | 21a2fc9 | 2013-02-15 17:01:03 +0000 | [diff] [blame] | 549 | if (handle == NULL ) { |
| 550 | return -1; |
| 551 | } |
| 552 | if (status == NULL ) { |
| 553 | self->lastError = AEC_NULL_POINTER_ERROR; |
| 554 | return -1; |
| 555 | } |
| 556 | if (self->initFlag != initCheck) { |
| 557 | self->lastError = AEC_UNINITIALIZED_ERROR; |
| 558 | return -1; |
| 559 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 560 | |
bjornv@webrtc.org | 21a2fc9 | 2013-02-15 17:01:03 +0000 | [diff] [blame] | 561 | *status = WebRtcAec_echo_state(self->aec); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 562 | |
bjornv@webrtc.org | 21a2fc9 | 2013-02-15 17:01:03 +0000 | [diff] [blame] | 563 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 564 | } |
| 565 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 566 | int WebRtcAec_GetMetrics(void* handle, AecMetrics* metrics) { |
| 567 | const float kUpWeight = 0.7f; |
| 568 | float dtmp; |
| 569 | int stmp; |
| 570 | aecpc_t* self = (aecpc_t*)handle; |
bjornv@webrtc.org | cea70f4 | 2013-02-19 21:03:10 +0000 | [diff] [blame] | 571 | Stats erl; |
| 572 | Stats erle; |
| 573 | Stats a_nlp; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 574 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 575 | if (handle == NULL ) { |
| 576 | return -1; |
| 577 | } |
| 578 | if (metrics == NULL ) { |
| 579 | self->lastError = AEC_NULL_POINTER_ERROR; |
| 580 | return -1; |
| 581 | } |
| 582 | if (self->initFlag != initCheck) { |
| 583 | self->lastError = AEC_UNINITIALIZED_ERROR; |
| 584 | return -1; |
| 585 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 586 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 587 | WebRtcAec_GetEchoStats(self->aec, &erl, &erle, &a_nlp); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 588 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 589 | // ERL |
| 590 | metrics->erl.instant = (int) erl.instant; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 591 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 592 | if ((erl.himean > offsetLevel) && (erl.average > offsetLevel)) { |
| 593 | // Use a mix between regular average and upper part average. |
| 594 | dtmp = kUpWeight * erl.himean + (1 - kUpWeight) * erl.average; |
| 595 | metrics->erl.average = (int) dtmp; |
| 596 | } else { |
| 597 | metrics->erl.average = offsetLevel; |
| 598 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 599 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 600 | metrics->erl.max = (int) erl.max; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 601 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 602 | if (erl.min < (offsetLevel * (-1))) { |
| 603 | metrics->erl.min = (int) erl.min; |
| 604 | } else { |
| 605 | metrics->erl.min = offsetLevel; |
| 606 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 607 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 608 | // ERLE |
| 609 | metrics->erle.instant = (int) erle.instant; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 610 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 611 | if ((erle.himean > offsetLevel) && (erle.average > offsetLevel)) { |
| 612 | // Use a mix between regular average and upper part average. |
| 613 | dtmp = kUpWeight * erle.himean + (1 - kUpWeight) * erle.average; |
| 614 | metrics->erle.average = (int) dtmp; |
| 615 | } else { |
| 616 | metrics->erle.average = offsetLevel; |
| 617 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 618 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 619 | metrics->erle.max = (int) erle.max; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 620 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 621 | if (erle.min < (offsetLevel * (-1))) { |
| 622 | metrics->erle.min = (int) erle.min; |
| 623 | } else { |
| 624 | metrics->erle.min = offsetLevel; |
| 625 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 626 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 627 | // RERL |
| 628 | if ((metrics->erl.average > offsetLevel) |
| 629 | && (metrics->erle.average > offsetLevel)) { |
| 630 | stmp = metrics->erl.average + metrics->erle.average; |
| 631 | } else { |
| 632 | stmp = offsetLevel; |
| 633 | } |
| 634 | metrics->rerl.average = stmp; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 635 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 636 | // No other statistics needed, but returned for completeness. |
| 637 | metrics->rerl.instant = stmp; |
| 638 | metrics->rerl.max = stmp; |
| 639 | metrics->rerl.min = stmp; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 640 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 641 | // A_NLP |
| 642 | metrics->aNlp.instant = (int) a_nlp.instant; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 643 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 644 | if ((a_nlp.himean > offsetLevel) && (a_nlp.average > offsetLevel)) { |
| 645 | // Use a mix between regular average and upper part average. |
| 646 | dtmp = kUpWeight * a_nlp.himean + (1 - kUpWeight) * a_nlp.average; |
| 647 | metrics->aNlp.average = (int) dtmp; |
| 648 | } else { |
| 649 | metrics->aNlp.average = offsetLevel; |
| 650 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 651 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 652 | metrics->aNlp.max = (int) a_nlp.max; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 653 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 654 | if (a_nlp.min < (offsetLevel * (-1))) { |
| 655 | metrics->aNlp.min = (int) a_nlp.min; |
| 656 | } else { |
| 657 | metrics->aNlp.min = offsetLevel; |
| 658 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 659 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 660 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 661 | } |
| 662 | |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 663 | int WebRtcAec_GetDelayMetrics(void* handle, int* median, int* std) { |
| 664 | aecpc_t* self = handle; |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 665 | |
bjornv@webrtc.org | 61d0745 | 2012-05-11 07:51:44 +0000 | [diff] [blame] | 666 | if (handle == NULL) { |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 667 | return -1; |
| 668 | } |
| 669 | if (median == NULL) { |
| 670 | self->lastError = AEC_NULL_POINTER_ERROR; |
| 671 | return -1; |
| 672 | } |
| 673 | if (std == NULL) { |
| 674 | self->lastError = AEC_NULL_POINTER_ERROR; |
| 675 | return -1; |
| 676 | } |
| 677 | if (self->initFlag != initCheck) { |
| 678 | self->lastError = AEC_UNINITIALIZED_ERROR; |
| 679 | return -1; |
| 680 | } |
bjornv@webrtc.org | 325f625 | 2013-02-15 15:21:02 +0000 | [diff] [blame] | 681 | if (WebRtcAec_GetDelayMetricsCore(self->aec, median, std) == -1) { |
| 682 | // Logging disabled. |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 683 | self->lastError = AEC_UNSUPPORTED_FUNCTION_ERROR; |
| 684 | return -1; |
| 685 | } |
| 686 | |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 687 | return 0; |
| 688 | } |
| 689 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 690 | WebRtc_Word32 WebRtcAec_get_error_code(void *aecInst) |
| 691 | { |
| 692 | aecpc_t *aecpc = aecInst; |
| 693 | |
| 694 | if (aecpc == NULL) { |
| 695 | return -1; |
| 696 | } |
| 697 | |
| 698 | return aecpc->lastError; |
| 699 | } |
| 700 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 701 | static int EstBufDelay(aecpc_t* aecpc) { |
bjornv@webrtc.org | 6f6acd9 | 2013-02-14 21:17:12 +0000 | [diff] [blame] | 702 | int nSampSndCard = aecpc->msInSndCardBuf * sampMsNb * aecpc->rate_factor; |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 703 | int current_delay = nSampSndCard - aecpc->aec->system_delay; |
| 704 | int delay_difference = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 705 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 706 | // Before we proceed with the delay estimate filtering we: |
| 707 | // 1) Compensate for the frame that will be read. |
| 708 | // 2) Compensate for drift resampling. |
bjornv@webrtc.org | 7056908 | 2012-04-12 12:13:50 +0000 | [diff] [blame] | 709 | // 3) Compensate for non-causality if needed, since the estimated delay can't |
| 710 | // be negative. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 711 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 712 | // 1) Compensating for the frame(s) that will be read/processed. |
bjornv@webrtc.org | 6f6acd9 | 2013-02-14 21:17:12 +0000 | [diff] [blame] | 713 | current_delay += FRAME_LEN * aecpc->rate_factor; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 714 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 715 | // 2) Account for resampling frame delay. |
| 716 | if (aecpc->skewMode == kAecTrue && aecpc->resample == kAecTrue) { |
| 717 | current_delay -= kResamplingDelay; |
| 718 | } |
| 719 | |
bjornv@webrtc.org | 7056908 | 2012-04-12 12:13:50 +0000 | [diff] [blame] | 720 | // 3) Compensate for non-causality, if needed, by flushing one block. |
andrew@webrtc.org | 61bf8e3 | 2012-03-15 19:04:55 +0000 | [diff] [blame] | 721 | if (current_delay < PART_LEN) { |
| 722 | current_delay += WebRtcAec_MoveFarReadPtr(aecpc->aec, 1) * PART_LEN; |
| 723 | } |
| 724 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 725 | aecpc->filtDelay = WEBRTC_SPL_MAX(0, (short) (0.8 * aecpc->filtDelay + |
| 726 | 0.2 * current_delay)); |
| 727 | |
| 728 | delay_difference = aecpc->filtDelay - aecpc->knownDelay; |
| 729 | if (delay_difference > 224) { |
| 730 | if (aecpc->lastDelayDiff < 96) { |
| 731 | aecpc->timeForDelayChange = 0; |
| 732 | } else { |
| 733 | aecpc->timeForDelayChange++; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 734 | } |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 735 | } else if (delay_difference < 96 && aecpc->knownDelay > 0) { |
| 736 | if (aecpc->lastDelayDiff > 224) { |
| 737 | aecpc->timeForDelayChange = 0; |
| 738 | } else { |
| 739 | aecpc->timeForDelayChange++; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 740 | } |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 741 | } else { |
| 742 | aecpc->timeForDelayChange = 0; |
| 743 | } |
| 744 | aecpc->lastDelayDiff = delay_difference; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 745 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 746 | if (aecpc->timeForDelayChange > 25) { |
| 747 | aecpc->knownDelay = WEBRTC_SPL_MAX((int) aecpc->filtDelay - 160, 0); |
| 748 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 749 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 750 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 751 | } |