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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_processing/aecm/echo_control_mobile.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 12 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | #ifdef AEC_DEBUG |
| 14 | #include <stdio.h> |
| 15 | #endif |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 16 | #include <stdlib.h> |
| 17 | |
peah | 2704512 | 2016-04-10 22:38:14 -0700 | [diff] [blame] | 18 | extern "C" { |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "common_audio/ring_buffer.h" |
| 20 | #include "common_audio/signal_processing/include/signal_processing_library.h" |
peah | 2704512 | 2016-04-10 22:38:14 -0700 | [diff] [blame] | 21 | } |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "modules/audio_processing/aecm/aecm_core.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 23 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 24 | #define BUF_SIZE_FRAMES 50 // buffer size (frames) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 25 | // Maximum length of resampled signal. Must be an integer multiple of frames |
| 26 | // (ceil(1/(1 + MIN_SKEW)*2) + 1)*FRAME_LEN |
| 27 | // The factor of 2 handles wb, and the + 1 is as a safety margin |
| 28 | #define MAX_RESAMP_LEN (5 * FRAME_LEN) |
| 29 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 30 | static const size_t kBufSizeSamp = |
| 31 | BUF_SIZE_FRAMES * FRAME_LEN; // buffer size (samples) |
| 32 | static const int kSampMsNb = 8; // samples per ms in nb |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 33 | // Target suppression levels for nlp modes |
| 34 | // log{0.001, 0.00001, 0.00000001} |
| 35 | static const int kInitCheck = 42; |
| 36 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 37 | typedef struct { |
| 38 | int sampFreq; |
| 39 | int scSampFreq; |
| 40 | short bufSizeStart; |
| 41 | int knownDelay; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 42 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 43 | // Stores the last frame added to the farend buffer |
| 44 | short farendOld[2][FRAME_LEN]; |
| 45 | short initFlag; // indicates if AEC has been initialized |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 46 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 47 | // Variables used for averaging far end buffer size |
| 48 | short counter; |
| 49 | short sum; |
| 50 | short firstVal; |
| 51 | short checkBufSizeCtr; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 52 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 53 | // Variables used for delay shifts |
| 54 | short msInSndCardBuf; |
| 55 | short filtDelay; |
| 56 | int timeForDelayChange; |
| 57 | int ECstartup; |
| 58 | int checkBuffSize; |
| 59 | int delayChange; |
| 60 | short lastDelayDiff; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 61 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 62 | int16_t echoMode; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 63 | |
| 64 | #ifdef AEC_DEBUG |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 65 | FILE* bufFile; |
| 66 | FILE* delayFile; |
| 67 | FILE* preCompFile; |
| 68 | FILE* postCompFile; |
| 69 | #endif // AEC_DEBUG |
| 70 | // Structures |
| 71 | RingBuffer* farendBuf; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 72 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 73 | AecmCore* aecmCore; |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 +0000 | [diff] [blame] | 74 | } AecMobile; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 75 | |
| 76 | // Estimates delay to set the position of the farend buffer read pointer |
| 77 | // (controlled by knownDelay) |
Alex Loiko | 890988c | 2017-08-31 10:25:48 +0200 | [diff] [blame] | 78 | static int WebRtcAecm_EstBufDelay(AecMobile* aecm, short msInSndCardBuf); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 79 | |
| 80 | // Stuffs the farend buffer if the estimated delay is too large |
Alex Loiko | 890988c | 2017-08-31 10:25:48 +0200 | [diff] [blame] | 81 | static int WebRtcAecm_DelayComp(AecMobile* aecm); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 82 | |
Bjorn Volcker | a743794 | 2015-05-28 15:58:42 +0200 | [diff] [blame] | 83 | void* WebRtcAecm_Create() { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 84 | AecMobile* aecm = static_cast<AecMobile*>(malloc(sizeof(AecMobile))); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 85 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 86 | WebRtcSpl_Init(); |
kma@webrtc.org | 31829a7 | 2013-03-19 00:25:01 +0000 | [diff] [blame] | 87 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 88 | aecm->aecmCore = WebRtcAecm_CreateCore(); |
| 89 | if (!aecm->aecmCore) { |
| 90 | WebRtcAecm_Free(aecm); |
| 91 | return NULL; |
| 92 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 93 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 94 | aecm->farendBuf = WebRtc_CreateBuffer(kBufSizeSamp, sizeof(int16_t)); |
| 95 | if (!aecm->farendBuf) { |
| 96 | WebRtcAecm_Free(aecm); |
| 97 | return NULL; |
| 98 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 99 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 100 | aecm->initFlag = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 101 | |
| 102 | #ifdef AEC_DEBUG |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 103 | aecm->aecmCore->farFile = fopen("aecFar.pcm", "wb"); |
| 104 | aecm->aecmCore->nearFile = fopen("aecNear.pcm", "wb"); |
| 105 | aecm->aecmCore->outFile = fopen("aecOut.pcm", "wb"); |
| 106 | // aecm->aecmCore->outLpFile = fopen("aecOutLp.pcm","wb"); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 107 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 108 | aecm->bufFile = fopen("aecBuf.dat", "wb"); |
| 109 | aecm->delayFile = fopen("aecDelay.dat", "wb"); |
| 110 | aecm->preCompFile = fopen("preComp.pcm", "wb"); |
| 111 | aecm->postCompFile = fopen("postComp.pcm", "wb"); |
| 112 | #endif // AEC_DEBUG |
| 113 | return aecm; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Bjorn Volcker | f6a99e6 | 2015-04-10 07:56:57 +0200 | [diff] [blame] | 116 | void WebRtcAecm_Free(void* aecmInst) { |
peah | 2704512 | 2016-04-10 22:38:14 -0700 | [diff] [blame] | 117 | AecMobile* aecm = static_cast<AecMobile*>(aecmInst); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 118 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 119 | if (aecm == NULL) { |
| 120 | return; |
| 121 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 122 | |
| 123 | #ifdef AEC_DEBUG |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 124 | fclose(aecm->aecmCore->farFile); |
| 125 | fclose(aecm->aecmCore->nearFile); |
| 126 | fclose(aecm->aecmCore->outFile); |
| 127 | // fclose(aecm->aecmCore->outLpFile); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 128 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 129 | fclose(aecm->bufFile); |
| 130 | fclose(aecm->delayFile); |
| 131 | fclose(aecm->preCompFile); |
| 132 | fclose(aecm->postCompFile); |
| 133 | #endif // AEC_DEBUG |
| 134 | WebRtcAecm_FreeCore(aecm->aecmCore); |
| 135 | WebRtc_FreeBuffer(aecm->farendBuf); |
| 136 | free(aecm); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 139 | int32_t WebRtcAecm_Init(void* aecmInst, int32_t sampFreq) { |
| 140 | AecMobile* aecm = static_cast<AecMobile*>(aecmInst); |
| 141 | AecmConfig aecConfig; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 142 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 143 | if (aecm == NULL) { |
| 144 | return -1; |
| 145 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 146 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 147 | if (sampFreq != 8000 && sampFreq != 16000) { |
| 148 | return AECM_BAD_PARAMETER_ERROR; |
| 149 | } |
| 150 | aecm->sampFreq = sampFreq; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 151 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 152 | // Initialize AECM core |
| 153 | if (WebRtcAecm_InitCore(aecm->aecmCore, aecm->sampFreq) == -1) { |
| 154 | return AECM_UNSPECIFIED_ERROR; |
| 155 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 156 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 157 | // Initialize farend buffer |
| 158 | WebRtc_InitBuffer(aecm->farendBuf); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 159 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 160 | aecm->initFlag = kInitCheck; // indicates that initialization has been done |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 161 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 162 | aecm->delayChange = 1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 163 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 164 | aecm->sum = 0; |
| 165 | aecm->counter = 0; |
| 166 | aecm->checkBuffSize = 1; |
| 167 | aecm->firstVal = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 168 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 169 | aecm->ECstartup = 1; |
| 170 | aecm->bufSizeStart = 0; |
| 171 | aecm->checkBufSizeCtr = 0; |
| 172 | aecm->filtDelay = 0; |
| 173 | aecm->timeForDelayChange = 0; |
| 174 | aecm->knownDelay = 0; |
| 175 | aecm->lastDelayDiff = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 176 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 177 | memset(&aecm->farendOld, 0, sizeof(aecm->farendOld)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 178 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 179 | // Default settings. |
| 180 | aecConfig.cngMode = AecmTrue; |
| 181 | aecConfig.echoMode = 3; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 182 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 183 | if (WebRtcAecm_set_config(aecm, aecConfig) == -1) { |
| 184 | return AECM_UNSPECIFIED_ERROR; |
| 185 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 186 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 187 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 188 | } |
| 189 | |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame] | 190 | // Returns any error that is caused when buffering the |
| 191 | // farend signal. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 192 | int32_t WebRtcAecm_GetBufferFarendError(void* aecmInst, |
| 193 | const int16_t* farend, |
| 194 | size_t nrOfSamples) { |
peah | 2704512 | 2016-04-10 22:38:14 -0700 | [diff] [blame] | 195 | AecMobile* aecm = static_cast<AecMobile*>(aecmInst); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 196 | |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame] | 197 | if (aecm == NULL) |
| 198 | return -1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 199 | |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame] | 200 | if (farend == NULL) |
| 201 | return AECM_NULL_POINTER_ERROR; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 202 | |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame] | 203 | if (aecm->initFlag != kInitCheck) |
| 204 | return AECM_UNINITIALIZED_ERROR; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 205 | |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame] | 206 | if (nrOfSamples != 80 && nrOfSamples != 160) |
| 207 | return AECM_BAD_PARAMETER_ERROR; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 208 | |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame] | 209 | return 0; |
| 210 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 211 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 212 | int32_t WebRtcAecm_BufferFarend(void* aecmInst, |
| 213 | const int16_t* farend, |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame] | 214 | size_t nrOfSamples) { |
peah | 2704512 | 2016-04-10 22:38:14 -0700 | [diff] [blame] | 215 | AecMobile* aecm = static_cast<AecMobile*>(aecmInst); |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame] | 216 | |
| 217 | const int32_t err = |
| 218 | WebRtcAecm_GetBufferFarendError(aecmInst, farend, nrOfSamples); |
| 219 | |
| 220 | if (err != 0) |
| 221 | return err; |
| 222 | |
| 223 | // TODO(unknown): Is this really a good idea? |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 224 | if (!aecm->ECstartup) { |
peah | c12be39 | 2015-11-09 23:53:50 -0800 | [diff] [blame] | 225 | WebRtcAecm_DelayComp(aecm); |
| 226 | } |
| 227 | |
| 228 | WebRtc_WriteBuffer(aecm->farendBuf, farend, nrOfSamples); |
| 229 | |
| 230 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 233 | int32_t WebRtcAecm_Process(void* aecmInst, |
| 234 | const int16_t* nearendNoisy, |
| 235 | const int16_t* nearendClean, |
| 236 | int16_t* out, |
| 237 | size_t nrOfSamples, |
| 238 | int16_t msInSndCardBuf) { |
| 239 | AecMobile* aecm = static_cast<AecMobile*>(aecmInst); |
| 240 | int32_t retVal = 0; |
| 241 | size_t i; |
| 242 | short nmbrOfFilledBuffers; |
| 243 | size_t nBlocks10ms; |
| 244 | size_t nFrames; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 245 | #ifdef AEC_DEBUG |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 246 | short msInAECBuf; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 247 | #endif |
| 248 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 249 | if (aecm == NULL) { |
| 250 | return -1; |
| 251 | } |
| 252 | |
| 253 | if (nearendNoisy == NULL) { |
| 254 | return AECM_NULL_POINTER_ERROR; |
| 255 | } |
| 256 | |
| 257 | if (out == NULL) { |
| 258 | return AECM_NULL_POINTER_ERROR; |
| 259 | } |
| 260 | |
| 261 | if (aecm->initFlag != kInitCheck) { |
| 262 | return AECM_UNINITIALIZED_ERROR; |
| 263 | } |
| 264 | |
| 265 | if (nrOfSamples != 80 && nrOfSamples != 160) { |
| 266 | return AECM_BAD_PARAMETER_ERROR; |
| 267 | } |
| 268 | |
| 269 | if (msInSndCardBuf < 0) { |
| 270 | msInSndCardBuf = 0; |
| 271 | retVal = AECM_BAD_PARAMETER_WARNING; |
| 272 | } else if (msInSndCardBuf > 500) { |
| 273 | msInSndCardBuf = 500; |
| 274 | retVal = AECM_BAD_PARAMETER_WARNING; |
| 275 | } |
| 276 | msInSndCardBuf += 10; |
| 277 | aecm->msInSndCardBuf = msInSndCardBuf; |
| 278 | |
| 279 | nFrames = nrOfSamples / FRAME_LEN; |
| 280 | nBlocks10ms = nFrames / aecm->aecmCore->mult; |
| 281 | |
| 282 | if (aecm->ECstartup) { |
| 283 | if (nearendClean == NULL) { |
| 284 | if (out != nearendNoisy) { |
| 285 | memcpy(out, nearendNoisy, sizeof(short) * nrOfSamples); |
| 286 | } |
| 287 | } else if (out != nearendClean) { |
| 288 | memcpy(out, nearendClean, sizeof(short) * nrOfSamples); |
| 289 | } |
| 290 | |
| 291 | nmbrOfFilledBuffers = |
| 292 | (short)WebRtc_available_read(aecm->farendBuf) / FRAME_LEN; |
| 293 | // The AECM is in the start up mode |
| 294 | // AECM is disabled until the soundcard buffer and farend buffers are OK |
| 295 | |
| 296 | // Mechanism to ensure that the soundcard buffer is reasonably stable. |
| 297 | if (aecm->checkBuffSize) { |
| 298 | aecm->checkBufSizeCtr++; |
| 299 | // Before we fill up the far end buffer we require the amount of data on |
| 300 | // the sound card to be stable (+/-8 ms) compared to the first value. This |
| 301 | // comparison is made during the following 4 consecutive frames. If it |
| 302 | // seems to be stable then we start to fill up the far end buffer. |
| 303 | |
| 304 | if (aecm->counter == 0) { |
| 305 | aecm->firstVal = aecm->msInSndCardBuf; |
| 306 | aecm->sum = 0; |
| 307 | } |
| 308 | |
| 309 | if (abs(aecm->firstVal - aecm->msInSndCardBuf) < |
| 310 | WEBRTC_SPL_MAX(0.2 * aecm->msInSndCardBuf, kSampMsNb)) { |
| 311 | aecm->sum += aecm->msInSndCardBuf; |
| 312 | aecm->counter++; |
| 313 | } else { |
| 314 | aecm->counter = 0; |
| 315 | } |
| 316 | |
| 317 | if (aecm->counter * nBlocks10ms >= 6) { |
| 318 | // The farend buffer size is determined in blocks of 80 samples |
| 319 | // Use 75% of the average value of the soundcard buffer |
| 320 | aecm->bufSizeStart = WEBRTC_SPL_MIN( |
| 321 | (3 * aecm->sum * aecm->aecmCore->mult) / (aecm->counter * 40), |
| 322 | BUF_SIZE_FRAMES); |
| 323 | // buffersize has now been determined |
| 324 | aecm->checkBuffSize = 0; |
| 325 | } |
| 326 | |
| 327 | if (aecm->checkBufSizeCtr * nBlocks10ms > 50) { |
| 328 | // for really bad sound cards, don't disable echocanceller for more than |
| 329 | // 0.5 sec |
| 330 | aecm->bufSizeStart = WEBRTC_SPL_MIN( |
| 331 | (3 * aecm->msInSndCardBuf * aecm->aecmCore->mult) / 40, |
| 332 | BUF_SIZE_FRAMES); |
| 333 | aecm->checkBuffSize = 0; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | // if checkBuffSize changed in the if-statement above |
| 338 | if (!aecm->checkBuffSize) { |
| 339 | // soundcard buffer is now reasonably stable |
| 340 | // When the far end buffer is filled with approximately the same amount of |
| 341 | // data as the amount on the sound card we end the start up phase and |
| 342 | // start to cancel echoes. |
| 343 | |
| 344 | if (nmbrOfFilledBuffers == aecm->bufSizeStart) { |
| 345 | aecm->ECstartup = 0; // Enable the AECM |
| 346 | } else if (nmbrOfFilledBuffers > aecm->bufSizeStart) { |
| 347 | WebRtc_MoveReadPtr(aecm->farendBuf, |
| 348 | (int)WebRtc_available_read(aecm->farendBuf) - |
| 349 | (int)aecm->bufSizeStart * FRAME_LEN); |
| 350 | aecm->ECstartup = 0; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | } else { |
| 355 | // AECM is enabled |
| 356 | |
| 357 | // Note only 1 block supported for nb and 2 blocks for wb |
| 358 | for (i = 0; i < nFrames; i++) { |
| 359 | int16_t farend[FRAME_LEN]; |
| 360 | const int16_t* farend_ptr = NULL; |
| 361 | |
| 362 | nmbrOfFilledBuffers = |
| 363 | (short)WebRtc_available_read(aecm->farendBuf) / FRAME_LEN; |
| 364 | |
| 365 | // Check that there is data in the far end buffer |
| 366 | if (nmbrOfFilledBuffers > 0) { |
| 367 | // Get the next 80 samples from the farend buffer |
| 368 | WebRtc_ReadBuffer(aecm->farendBuf, (void**)&farend_ptr, farend, |
| 369 | FRAME_LEN); |
| 370 | |
| 371 | // Always store the last frame for use when we run out of data |
| 372 | memcpy(&(aecm->farendOld[i][0]), farend_ptr, FRAME_LEN * sizeof(short)); |
| 373 | } else { |
| 374 | // We have no data so we use the last played frame |
| 375 | memcpy(farend, &(aecm->farendOld[i][0]), FRAME_LEN * sizeof(short)); |
| 376 | farend_ptr = farend; |
| 377 | } |
| 378 | |
| 379 | // Call buffer delay estimator when all data is extracted, |
| 380 | // i,e. i = 0 for NB and i = 1 for WB |
| 381 | if ((i == 0 && aecm->sampFreq == 8000) || |
| 382 | (i == 1 && aecm->sampFreq == 16000)) { |
| 383 | WebRtcAecm_EstBufDelay(aecm, aecm->msInSndCardBuf); |
| 384 | } |
| 385 | |
| 386 | // Call the AECM |
| 387 | /*WebRtcAecm_ProcessFrame(aecm->aecmCore, farend, &nearend[FRAME_LEN * i], |
| 388 | &out[FRAME_LEN * i], aecm->knownDelay);*/ |
| 389 | if (WebRtcAecm_ProcessFrame( |
| 390 | aecm->aecmCore, farend_ptr, &nearendNoisy[FRAME_LEN * i], |
| 391 | (nearendClean ? &nearendClean[FRAME_LEN * i] : NULL), |
| 392 | &out[FRAME_LEN * i]) == -1) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 393 | return -1; |
| 394 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 395 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 396 | |
| 397 | #ifdef AEC_DEBUG |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 398 | msInAECBuf = (short)WebRtc_available_read(aecm->farendBuf) / |
| 399 | (kSampMsNb * aecm->aecmCore->mult); |
| 400 | fwrite(&msInAECBuf, 2, 1, aecm->bufFile); |
| 401 | fwrite(&(aecm->knownDelay), sizeof(aecm->knownDelay), 1, aecm->delayFile); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 402 | #endif |
| 403 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 404 | return retVal; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 407 | int32_t WebRtcAecm_set_config(void* aecmInst, AecmConfig config) { |
| 408 | AecMobile* aecm = static_cast<AecMobile*>(aecmInst); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 409 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 410 | if (aecm == NULL) { |
| 411 | return -1; |
| 412 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 413 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 414 | if (aecm->initFlag != kInitCheck) { |
| 415 | return AECM_UNINITIALIZED_ERROR; |
| 416 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 417 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 418 | if (config.cngMode != AecmFalse && config.cngMode != AecmTrue) { |
| 419 | return AECM_BAD_PARAMETER_ERROR; |
| 420 | } |
| 421 | aecm->aecmCore->cngMode = config.cngMode; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 422 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 423 | if (config.echoMode < 0 || config.echoMode > 4) { |
| 424 | return AECM_BAD_PARAMETER_ERROR; |
| 425 | } |
| 426 | aecm->echoMode = config.echoMode; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 427 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 428 | if (aecm->echoMode == 0) { |
| 429 | aecm->aecmCore->supGain = SUPGAIN_DEFAULT >> 3; |
| 430 | aecm->aecmCore->supGainOld = SUPGAIN_DEFAULT >> 3; |
| 431 | aecm->aecmCore->supGainErrParamA = SUPGAIN_ERROR_PARAM_A >> 3; |
| 432 | aecm->aecmCore->supGainErrParamD = SUPGAIN_ERROR_PARAM_D >> 3; |
| 433 | aecm->aecmCore->supGainErrParamDiffAB = |
| 434 | (SUPGAIN_ERROR_PARAM_A >> 3) - (SUPGAIN_ERROR_PARAM_B >> 3); |
| 435 | aecm->aecmCore->supGainErrParamDiffBD = |
| 436 | (SUPGAIN_ERROR_PARAM_B >> 3) - (SUPGAIN_ERROR_PARAM_D >> 3); |
| 437 | } else if (aecm->echoMode == 1) { |
| 438 | aecm->aecmCore->supGain = SUPGAIN_DEFAULT >> 2; |
| 439 | aecm->aecmCore->supGainOld = SUPGAIN_DEFAULT >> 2; |
| 440 | aecm->aecmCore->supGainErrParamA = SUPGAIN_ERROR_PARAM_A >> 2; |
| 441 | aecm->aecmCore->supGainErrParamD = SUPGAIN_ERROR_PARAM_D >> 2; |
| 442 | aecm->aecmCore->supGainErrParamDiffAB = |
| 443 | (SUPGAIN_ERROR_PARAM_A >> 2) - (SUPGAIN_ERROR_PARAM_B >> 2); |
| 444 | aecm->aecmCore->supGainErrParamDiffBD = |
| 445 | (SUPGAIN_ERROR_PARAM_B >> 2) - (SUPGAIN_ERROR_PARAM_D >> 2); |
| 446 | } else if (aecm->echoMode == 2) { |
| 447 | aecm->aecmCore->supGain = SUPGAIN_DEFAULT >> 1; |
| 448 | aecm->aecmCore->supGainOld = SUPGAIN_DEFAULT >> 1; |
| 449 | aecm->aecmCore->supGainErrParamA = SUPGAIN_ERROR_PARAM_A >> 1; |
| 450 | aecm->aecmCore->supGainErrParamD = SUPGAIN_ERROR_PARAM_D >> 1; |
| 451 | aecm->aecmCore->supGainErrParamDiffAB = |
| 452 | (SUPGAIN_ERROR_PARAM_A >> 1) - (SUPGAIN_ERROR_PARAM_B >> 1); |
| 453 | aecm->aecmCore->supGainErrParamDiffBD = |
| 454 | (SUPGAIN_ERROR_PARAM_B >> 1) - (SUPGAIN_ERROR_PARAM_D >> 1); |
| 455 | } else if (aecm->echoMode == 3) { |
| 456 | aecm->aecmCore->supGain = SUPGAIN_DEFAULT; |
| 457 | aecm->aecmCore->supGainOld = SUPGAIN_DEFAULT; |
| 458 | aecm->aecmCore->supGainErrParamA = SUPGAIN_ERROR_PARAM_A; |
| 459 | aecm->aecmCore->supGainErrParamD = SUPGAIN_ERROR_PARAM_D; |
| 460 | aecm->aecmCore->supGainErrParamDiffAB = |
| 461 | SUPGAIN_ERROR_PARAM_A - SUPGAIN_ERROR_PARAM_B; |
| 462 | aecm->aecmCore->supGainErrParamDiffBD = |
| 463 | SUPGAIN_ERROR_PARAM_B - SUPGAIN_ERROR_PARAM_D; |
| 464 | } else if (aecm->echoMode == 4) { |
| 465 | aecm->aecmCore->supGain = SUPGAIN_DEFAULT << 1; |
| 466 | aecm->aecmCore->supGainOld = SUPGAIN_DEFAULT << 1; |
| 467 | aecm->aecmCore->supGainErrParamA = SUPGAIN_ERROR_PARAM_A << 1; |
| 468 | aecm->aecmCore->supGainErrParamD = SUPGAIN_ERROR_PARAM_D << 1; |
| 469 | aecm->aecmCore->supGainErrParamDiffAB = |
| 470 | (SUPGAIN_ERROR_PARAM_A << 1) - (SUPGAIN_ERROR_PARAM_B << 1); |
| 471 | aecm->aecmCore->supGainErrParamDiffBD = |
| 472 | (SUPGAIN_ERROR_PARAM_B << 1) - (SUPGAIN_ERROR_PARAM_D << 1); |
| 473 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 474 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 475 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 476 | } |
| 477 | |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 478 | int32_t WebRtcAecm_InitEchoPath(void* aecmInst, |
| 479 | const void* echo_path, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 480 | size_t size_bytes) { |
| 481 | AecMobile* aecm = static_cast<AecMobile*>(aecmInst); |
| 482 | const int16_t* echo_path_ptr = static_cast<const int16_t*>(echo_path); |
bjornv@google.com | c4b939c | 2011-07-13 08:09:56 +0000 | [diff] [blame] | 483 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 484 | if (aecmInst == NULL) { |
| 485 | return -1; |
| 486 | } |
| 487 | if (echo_path == NULL) { |
| 488 | return AECM_NULL_POINTER_ERROR; |
| 489 | } |
| 490 | if (size_bytes != WebRtcAecm_echo_path_size_bytes()) { |
| 491 | // Input channel size does not match the size of AECM |
| 492 | return AECM_BAD_PARAMETER_ERROR; |
| 493 | } |
| 494 | if (aecm->initFlag != kInitCheck) { |
| 495 | return AECM_UNINITIALIZED_ERROR; |
| 496 | } |
bjornv@google.com | c4b939c | 2011-07-13 08:09:56 +0000 | [diff] [blame] | 497 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 498 | WebRtcAecm_InitEchoPathCore(aecm->aecmCore, echo_path_ptr); |
bjornv@google.com | c4b939c | 2011-07-13 08:09:56 +0000 | [diff] [blame] | 499 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 500 | return 0; |
bjornv@google.com | c4b939c | 2011-07-13 08:09:56 +0000 | [diff] [blame] | 501 | } |
| 502 | |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 503 | int32_t WebRtcAecm_GetEchoPath(void* aecmInst, |
| 504 | void* echo_path, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 505 | size_t size_bytes) { |
| 506 | AecMobile* aecm = static_cast<AecMobile*>(aecmInst); |
| 507 | int16_t* echo_path_ptr = static_cast<int16_t*>(echo_path); |
bjornv@google.com | c4b939c | 2011-07-13 08:09:56 +0000 | [diff] [blame] | 508 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 509 | if (aecmInst == NULL) { |
| 510 | return -1; |
| 511 | } |
| 512 | if (echo_path == NULL) { |
| 513 | return AECM_NULL_POINTER_ERROR; |
| 514 | } |
| 515 | if (size_bytes != WebRtcAecm_echo_path_size_bytes()) { |
| 516 | // Input channel size does not match the size of AECM |
| 517 | return AECM_BAD_PARAMETER_ERROR; |
| 518 | } |
| 519 | if (aecm->initFlag != kInitCheck) { |
| 520 | return AECM_UNINITIALIZED_ERROR; |
| 521 | } |
bjornv@google.com | c4b939c | 2011-07-13 08:09:56 +0000 | [diff] [blame] | 522 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 523 | memcpy(echo_path_ptr, aecm->aecmCore->channelStored, size_bytes); |
| 524 | return 0; |
bjornv@google.com | c4b939c | 2011-07-13 08:09:56 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 527 | size_t WebRtcAecm_echo_path_size_bytes() { |
| 528 | return (PART_LEN1 * sizeof(int16_t)); |
bjornv@google.com | c4b939c | 2011-07-13 08:09:56 +0000 | [diff] [blame] | 529 | } |
| 530 | |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 +0000 | [diff] [blame] | 531 | static int WebRtcAecm_EstBufDelay(AecMobile* aecm, short msInSndCardBuf) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 532 | short delayNew, nSampSndCard; |
| 533 | short nSampFar = (short)WebRtc_available_read(aecm->farendBuf); |
| 534 | short diff; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 535 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 536 | nSampSndCard = msInSndCardBuf * kSampMsNb * aecm->aecmCore->mult; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 537 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 538 | delayNew = nSampSndCard - nSampFar; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 539 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 540 | if (delayNew < FRAME_LEN) { |
| 541 | WebRtc_MoveReadPtr(aecm->farendBuf, FRAME_LEN); |
| 542 | delayNew += FRAME_LEN; |
| 543 | } |
| 544 | |
| 545 | aecm->filtDelay = |
| 546 | WEBRTC_SPL_MAX(0, (8 * aecm->filtDelay + 2 * delayNew) / 10); |
| 547 | |
| 548 | diff = aecm->filtDelay - aecm->knownDelay; |
| 549 | if (diff > 224) { |
| 550 | if (aecm->lastDelayDiff < 96) { |
| 551 | aecm->timeForDelayChange = 0; |
| 552 | } else { |
| 553 | aecm->timeForDelayChange++; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 554 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 555 | } else if (diff < 96 && aecm->knownDelay > 0) { |
| 556 | if (aecm->lastDelayDiff > 224) { |
| 557 | aecm->timeForDelayChange = 0; |
| 558 | } else { |
| 559 | aecm->timeForDelayChange++; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 560 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 561 | } else { |
| 562 | aecm->timeForDelayChange = 0; |
| 563 | } |
| 564 | aecm->lastDelayDiff = diff; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 565 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 566 | if (aecm->timeForDelayChange > 25) { |
| 567 | aecm->knownDelay = WEBRTC_SPL_MAX((int)aecm->filtDelay - 160, 0); |
| 568 | } |
| 569 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 570 | } |
| 571 | |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 +0000 | [diff] [blame] | 572 | static int WebRtcAecm_DelayComp(AecMobile* aecm) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 573 | int nSampFar = (int)WebRtc_available_read(aecm->farendBuf); |
| 574 | int nSampSndCard, delayNew, nSampAdd; |
| 575 | const int maxStuffSamp = 10 * FRAME_LEN; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 576 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 577 | nSampSndCard = aecm->msInSndCardBuf * kSampMsNb * aecm->aecmCore->mult; |
| 578 | delayNew = nSampSndCard - nSampFar; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 579 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 580 | if (delayNew > FAR_BUF_LEN - FRAME_LEN * aecm->aecmCore->mult) { |
| 581 | // The difference of the buffer sizes is larger than the maximum |
| 582 | // allowed known delay. Compensate by stuffing the buffer. |
| 583 | nSampAdd = |
| 584 | (int)(WEBRTC_SPL_MAX(((nSampSndCard >> 1) - nSampFar), FRAME_LEN)); |
| 585 | nSampAdd = WEBRTC_SPL_MIN(nSampAdd, maxStuffSamp); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 586 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 587 | WebRtc_MoveReadPtr(aecm->farendBuf, -nSampAdd); |
| 588 | aecm->delayChange = 1; // the delay needs to be updated |
| 589 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 590 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 591 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 592 | } |