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 | int out_elements = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 478 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 479 | EstBufDelay(aecpc); |
| 480 | |
| 481 | // 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] | 482 | for (i = 0; i < nFrames; i++) { |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 483 | int16_t* out_ptr = NULL; |
| 484 | int16_t out_tmp[FRAME_LEN]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 485 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 486 | // Call the AEC. |
| 487 | WebRtcAec_ProcessFrame(aecpc->aec, |
| 488 | &nearend[FRAME_LEN * i], |
| 489 | &nearendH[FRAME_LEN * i], |
| 490 | aecpc->knownDelay); |
| 491 | // TODO(bjornv): Re-structure such that we don't have to pass |
| 492 | // |aecpc->knownDelay| as input. Change name to something like |
| 493 | // |system_buffer_diff|. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 494 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 495 | // Stuff the out buffer if we have less than a frame to output. |
| 496 | // This should only happen for the first frame. |
| 497 | out_elements = (int) WebRtc_available_read(aecpc->aec->outFrBuf); |
| 498 | if (out_elements < FRAME_LEN) { |
| 499 | WebRtc_MoveReadPtr(aecpc->aec->outFrBuf, |
| 500 | out_elements - FRAME_LEN); |
| 501 | if (aecpc->sampFreq == 32000) { |
| 502 | WebRtc_MoveReadPtr(aecpc->aec->outFrBufH, |
| 503 | out_elements - FRAME_LEN); |
| 504 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 505 | } |
| 506 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 507 | // Obtain an output frame. |
| 508 | WebRtc_ReadBuffer(aecpc->aec->outFrBuf, (void**) &out_ptr, |
| 509 | out_tmp, FRAME_LEN); |
| 510 | memcpy(&out[FRAME_LEN * i], out_ptr, sizeof(int16_t) * FRAME_LEN); |
| 511 | // For H band |
| 512 | if (aecpc->sampFreq == 32000) { |
| 513 | WebRtc_ReadBuffer(aecpc->aec->outFrBufH, (void**) &out_ptr, |
| 514 | out_tmp, FRAME_LEN); |
| 515 | memcpy(&outH[FRAME_LEN * i], out_ptr, |
| 516 | sizeof(int16_t) * FRAME_LEN); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 517 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | |
andrew@webrtc.org | 8594f76 | 2011-11-22 00:51:41 +0000 | [diff] [blame] | 521 | #ifdef WEBRTC_AEC_DEBUG_DUMP |
| 522 | { |
andrew@webrtc.org | 7d8c567 | 2012-06-01 02:41:14 +0000 | [diff] [blame] | 523 | 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] | 524 | (sampMsNb * aecpc->rate_factor)); |
andrew@webrtc.org | 7d8c567 | 2012-06-01 02:41:14 +0000 | [diff] [blame] | 525 | (void)fwrite(&far_buf_size_ms, 2, 1, aecpc->bufFile); |
| 526 | (void)fwrite(&aecpc->knownDelay, sizeof(aecpc->knownDelay), 1, |
| 527 | aecpc->delayFile); |
andrew@webrtc.org | 8594f76 | 2011-11-22 00:51:41 +0000 | [diff] [blame] | 528 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 529 | #endif |
| 530 | |
| 531 | return retVal; |
| 532 | } |
| 533 | |
| 534 | WebRtc_Word32 WebRtcAec_set_config(void *aecInst, AecConfig config) |
| 535 | { |
| 536 | aecpc_t *aecpc = aecInst; |
| 537 | |
| 538 | if (aecpc == NULL) { |
| 539 | return -1; |
| 540 | } |
| 541 | |
| 542 | if (aecpc->initFlag != initCheck) { |
| 543 | aecpc->lastError = AEC_UNINITIALIZED_ERROR; |
| 544 | return -1; |
| 545 | } |
| 546 | |
| 547 | if (config.skewMode != kAecFalse && config.skewMode != kAecTrue) { |
| 548 | aecpc->lastError = AEC_BAD_PARAMETER_ERROR; |
| 549 | return -1; |
| 550 | } |
| 551 | aecpc->skewMode = config.skewMode; |
| 552 | |
| 553 | if (config.nlpMode != kAecNlpConservative && config.nlpMode != |
| 554 | kAecNlpModerate && config.nlpMode != kAecNlpAggressive) { |
| 555 | aecpc->lastError = AEC_BAD_PARAMETER_ERROR; |
| 556 | return -1; |
| 557 | } |
bjornv@webrtc.org | 5fc8292 | 2013-02-19 21:06:52 +0000 | [diff] [blame] | 558 | aecpc->aec->nlp_mode = config.nlpMode; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 559 | |
| 560 | if (config.metricsMode != kAecFalse && config.metricsMode != kAecTrue) { |
| 561 | aecpc->lastError = AEC_BAD_PARAMETER_ERROR; |
| 562 | return -1; |
| 563 | } |
| 564 | aecpc->aec->metricsMode = config.metricsMode; |
| 565 | if (aecpc->aec->metricsMode == kAecTrue) { |
| 566 | WebRtcAec_InitMetrics(aecpc->aec); |
| 567 | } |
| 568 | |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 569 | if (config.delay_logging != kAecFalse && config.delay_logging != kAecTrue) { |
| 570 | aecpc->lastError = AEC_BAD_PARAMETER_ERROR; |
| 571 | return -1; |
| 572 | } |
| 573 | aecpc->aec->delay_logging_enabled = config.delay_logging; |
| 574 | if (aecpc->aec->delay_logging_enabled == kAecTrue) { |
| 575 | memset(aecpc->aec->delay_histogram, 0, sizeof(aecpc->aec->delay_histogram)); |
| 576 | } |
| 577 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 578 | return 0; |
| 579 | } |
| 580 | |
| 581 | WebRtc_Word32 WebRtcAec_get_config(void *aecInst, AecConfig *config) |
| 582 | { |
| 583 | aecpc_t *aecpc = aecInst; |
| 584 | |
| 585 | if (aecpc == NULL) { |
| 586 | return -1; |
| 587 | } |
| 588 | |
| 589 | if (config == NULL) { |
| 590 | aecpc->lastError = AEC_NULL_POINTER_ERROR; |
| 591 | return -1; |
| 592 | } |
| 593 | |
| 594 | if (aecpc->initFlag != initCheck) { |
| 595 | aecpc->lastError = AEC_UNINITIALIZED_ERROR; |
| 596 | return -1; |
| 597 | } |
| 598 | |
bjornv@webrtc.org | 5fc8292 | 2013-02-19 21:06:52 +0000 | [diff] [blame] | 599 | config->nlpMode = aecpc->aec->nlp_mode; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 600 | config->skewMode = aecpc->skewMode; |
| 601 | config->metricsMode = aecpc->aec->metricsMode; |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 602 | config->delay_logging = aecpc->aec->delay_logging_enabled; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 603 | |
| 604 | return 0; |
| 605 | } |
| 606 | |
bjornv@webrtc.org | 21a2fc9 | 2013-02-15 17:01:03 +0000 | [diff] [blame] | 607 | int WebRtcAec_get_echo_status(void* handle, int* status) { |
| 608 | aecpc_t* self = (aecpc_t*)handle; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 609 | |
bjornv@webrtc.org | 21a2fc9 | 2013-02-15 17:01:03 +0000 | [diff] [blame] | 610 | if (handle == NULL ) { |
| 611 | return -1; |
| 612 | } |
| 613 | if (status == NULL ) { |
| 614 | self->lastError = AEC_NULL_POINTER_ERROR; |
| 615 | return -1; |
| 616 | } |
| 617 | if (self->initFlag != initCheck) { |
| 618 | self->lastError = AEC_UNINITIALIZED_ERROR; |
| 619 | return -1; |
| 620 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 621 | |
bjornv@webrtc.org | 21a2fc9 | 2013-02-15 17:01:03 +0000 | [diff] [blame] | 622 | *status = WebRtcAec_echo_state(self->aec); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 623 | |
bjornv@webrtc.org | 21a2fc9 | 2013-02-15 17:01:03 +0000 | [diff] [blame] | 624 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 625 | } |
| 626 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 627 | int WebRtcAec_GetMetrics(void* handle, AecMetrics* metrics) { |
| 628 | const float kUpWeight = 0.7f; |
| 629 | float dtmp; |
| 630 | int stmp; |
| 631 | aecpc_t* self = (aecpc_t*)handle; |
bjornv@webrtc.org | cea70f4 | 2013-02-19 21:03:10 +0000 | [diff] [blame] | 632 | Stats erl; |
| 633 | Stats erle; |
| 634 | Stats a_nlp; |
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 | if (handle == NULL ) { |
| 637 | return -1; |
| 638 | } |
| 639 | if (metrics == NULL ) { |
| 640 | self->lastError = AEC_NULL_POINTER_ERROR; |
| 641 | return -1; |
| 642 | } |
| 643 | if (self->initFlag != initCheck) { |
| 644 | self->lastError = AEC_UNINITIALIZED_ERROR; |
| 645 | return -1; |
| 646 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 647 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 648 | WebRtcAec_GetEchoStats(self->aec, &erl, &erle, &a_nlp); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 649 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 650 | // ERL |
| 651 | metrics->erl.instant = (int) erl.instant; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 652 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 653 | if ((erl.himean > offsetLevel) && (erl.average > offsetLevel)) { |
| 654 | // Use a mix between regular average and upper part average. |
| 655 | dtmp = kUpWeight * erl.himean + (1 - kUpWeight) * erl.average; |
| 656 | metrics->erl.average = (int) dtmp; |
| 657 | } else { |
| 658 | metrics->erl.average = offsetLevel; |
| 659 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 660 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 661 | metrics->erl.max = (int) erl.max; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 662 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 663 | if (erl.min < (offsetLevel * (-1))) { |
| 664 | metrics->erl.min = (int) erl.min; |
| 665 | } else { |
| 666 | metrics->erl.min = offsetLevel; |
| 667 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 668 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 669 | // ERLE |
| 670 | metrics->erle.instant = (int) erle.instant; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 671 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 672 | if ((erle.himean > offsetLevel) && (erle.average > offsetLevel)) { |
| 673 | // Use a mix between regular average and upper part average. |
| 674 | dtmp = kUpWeight * erle.himean + (1 - kUpWeight) * erle.average; |
| 675 | metrics->erle.average = (int) dtmp; |
| 676 | } else { |
| 677 | metrics->erle.average = offsetLevel; |
| 678 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 679 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 680 | metrics->erle.max = (int) erle.max; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 681 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 682 | if (erle.min < (offsetLevel * (-1))) { |
| 683 | metrics->erle.min = (int) erle.min; |
| 684 | } else { |
| 685 | metrics->erle.min = offsetLevel; |
| 686 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 687 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 688 | // RERL |
| 689 | if ((metrics->erl.average > offsetLevel) |
| 690 | && (metrics->erle.average > offsetLevel)) { |
| 691 | stmp = metrics->erl.average + metrics->erle.average; |
| 692 | } else { |
| 693 | stmp = offsetLevel; |
| 694 | } |
| 695 | metrics->rerl.average = stmp; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 696 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 697 | // No other statistics needed, but returned for completeness. |
| 698 | metrics->rerl.instant = stmp; |
| 699 | metrics->rerl.max = stmp; |
| 700 | metrics->rerl.min = stmp; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 701 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 702 | // A_NLP |
| 703 | metrics->aNlp.instant = (int) a_nlp.instant; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 704 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 705 | if ((a_nlp.himean > offsetLevel) && (a_nlp.average > offsetLevel)) { |
| 706 | // Use a mix between regular average and upper part average. |
| 707 | dtmp = kUpWeight * a_nlp.himean + (1 - kUpWeight) * a_nlp.average; |
| 708 | metrics->aNlp.average = (int) dtmp; |
| 709 | } else { |
| 710 | metrics->aNlp.average = offsetLevel; |
| 711 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 712 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 713 | metrics->aNlp.max = (int) a_nlp.max; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 714 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 715 | if (a_nlp.min < (offsetLevel * (-1))) { |
| 716 | metrics->aNlp.min = (int) a_nlp.min; |
| 717 | } else { |
| 718 | metrics->aNlp.min = offsetLevel; |
| 719 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 720 | |
bjornv@webrtc.org | b4cd342 | 2013-02-15 18:40:34 +0000 | [diff] [blame] | 721 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 722 | } |
| 723 | |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 724 | int WebRtcAec_GetDelayMetrics(void* handle, int* median, int* std) { |
| 725 | aecpc_t* self = handle; |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 726 | |
bjornv@webrtc.org | 61d0745 | 2012-05-11 07:51:44 +0000 | [diff] [blame] | 727 | if (handle == NULL) { |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 728 | return -1; |
| 729 | } |
| 730 | if (median == NULL) { |
| 731 | self->lastError = AEC_NULL_POINTER_ERROR; |
| 732 | return -1; |
| 733 | } |
| 734 | if (std == NULL) { |
| 735 | self->lastError = AEC_NULL_POINTER_ERROR; |
| 736 | return -1; |
| 737 | } |
| 738 | if (self->initFlag != initCheck) { |
| 739 | self->lastError = AEC_UNINITIALIZED_ERROR; |
| 740 | return -1; |
| 741 | } |
bjornv@webrtc.org | 325f625 | 2013-02-15 15:21:02 +0000 | [diff] [blame] | 742 | if (WebRtcAec_GetDelayMetricsCore(self->aec, median, std) == -1) { |
| 743 | // Logging disabled. |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 744 | self->lastError = AEC_UNSUPPORTED_FUNCTION_ERROR; |
| 745 | return -1; |
| 746 | } |
| 747 | |
bjornv@google.com | 1ba3dbe | 2011-10-03 08:18:10 +0000 | [diff] [blame] | 748 | return 0; |
| 749 | } |
| 750 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 751 | WebRtc_Word32 WebRtcAec_get_error_code(void *aecInst) |
| 752 | { |
| 753 | aecpc_t *aecpc = aecInst; |
| 754 | |
| 755 | if (aecpc == NULL) { |
| 756 | return -1; |
| 757 | } |
| 758 | |
| 759 | return aecpc->lastError; |
| 760 | } |
| 761 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 762 | static int EstBufDelay(aecpc_t* aecpc) { |
bjornv@webrtc.org | 6f6acd9 | 2013-02-14 21:17:12 +0000 | [diff] [blame] | 763 | int nSampSndCard = aecpc->msInSndCardBuf * sampMsNb * aecpc->rate_factor; |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 764 | int current_delay = nSampSndCard - aecpc->aec->system_delay; |
| 765 | int delay_difference = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 766 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 767 | // Before we proceed with the delay estimate filtering we: |
| 768 | // 1) Compensate for the frame that will be read. |
| 769 | // 2) Compensate for drift resampling. |
bjornv@webrtc.org | 7056908 | 2012-04-12 12:13:50 +0000 | [diff] [blame] | 770 | // 3) Compensate for non-causality if needed, since the estimated delay can't |
| 771 | // be negative. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 772 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 773 | // 1) Compensating for the frame(s) that will be read/processed. |
bjornv@webrtc.org | 6f6acd9 | 2013-02-14 21:17:12 +0000 | [diff] [blame] | 774 | current_delay += FRAME_LEN * aecpc->rate_factor; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 775 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 776 | // 2) Account for resampling frame delay. |
| 777 | if (aecpc->skewMode == kAecTrue && aecpc->resample == kAecTrue) { |
| 778 | current_delay -= kResamplingDelay; |
| 779 | } |
| 780 | |
bjornv@webrtc.org | 7056908 | 2012-04-12 12:13:50 +0000 | [diff] [blame] | 781 | // 3) Compensate for non-causality, if needed, by flushing one block. |
andrew@webrtc.org | 61bf8e3 | 2012-03-15 19:04:55 +0000 | [diff] [blame] | 782 | if (current_delay < PART_LEN) { |
| 783 | current_delay += WebRtcAec_MoveFarReadPtr(aecpc->aec, 1) * PART_LEN; |
| 784 | } |
| 785 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 786 | aecpc->filtDelay = WEBRTC_SPL_MAX(0, (short) (0.8 * aecpc->filtDelay + |
| 787 | 0.2 * current_delay)); |
| 788 | |
| 789 | delay_difference = aecpc->filtDelay - aecpc->knownDelay; |
| 790 | if (delay_difference > 224) { |
| 791 | if (aecpc->lastDelayDiff < 96) { |
| 792 | aecpc->timeForDelayChange = 0; |
| 793 | } else { |
| 794 | aecpc->timeForDelayChange++; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 795 | } |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 796 | } else if (delay_difference < 96 && aecpc->knownDelay > 0) { |
| 797 | if (aecpc->lastDelayDiff > 224) { |
| 798 | aecpc->timeForDelayChange = 0; |
| 799 | } else { |
| 800 | aecpc->timeForDelayChange++; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 801 | } |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 802 | } else { |
| 803 | aecpc->timeForDelayChange = 0; |
| 804 | } |
| 805 | aecpc->lastDelayDiff = delay_difference; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 806 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 807 | if (aecpc->timeForDelayChange > 25) { |
| 808 | aecpc->knownDelay = WEBRTC_SPL_MAX((int) aecpc->filtDelay - 160, 0); |
| 809 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 810 | |
bjornv@webrtc.org | 7270a6b | 2011-12-28 08:44:17 +0000 | [diff] [blame] | 811 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 812 | } |