1 /* 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. 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 #ifndef WEBRTC_MODULES_AUDIO_PROCESSING_AEC_AEC_CORE_INTERNAL_H_ 12 #define WEBRTC_MODULES_AUDIO_PROCESSING_AEC_AEC_CORE_INTERNAL_H_ 13 14 #include "webrtc/common_audio/ring_buffer.h" 15 #include "webrtc/common_audio/wav_file.h" 16 #include "webrtc/modules/audio_processing/aec/aec_common.h" 17 #include "webrtc/modules/audio_processing/aec/aec_core.h" 18 #include "webrtc/typedefs.h" 19 20 // Number of partitions for the extended filter mode. The first one is an enum 21 // to be used in array declarations, as it represents the maximum filter length. 22 enum { 23 kExtendedNumPartitions = 32 24 }; 25 static const int kNormalNumPartitions = 12; 26 27 // Delay estimator constants, used for logging and delay compensation if 28 // if reported delays are disabled. 29 enum { 30 kLookaheadBlocks = 15 31 }; 32 enum { 33 // 500 ms for 16 kHz which is equivalent with the limit of reported delays. 34 kHistorySizeBlocks = 125 35 }; 36 37 // Extended filter adaptation parameters. 38 // TODO(ajm): No narrowband tuning yet. 39 static const float kExtendedMu = 0.4f; 40 static const float kExtendedErrorThreshold = 1.0e-6f; 41 42 typedef struct PowerLevel { 43 float sfrsum; 44 int sfrcounter; 45 float framelevel; 46 float frsum; 47 int frcounter; 48 float minlevel; 49 float averagelevel; 50 } PowerLevel; 51 52 struct AecCore { 53 int farBufWritePos, farBufReadPos; 54 55 int knownDelay; 56 int inSamples, outSamples; 57 int delayEstCtr; 58 59 RingBuffer* nearFrBuf; 60 RingBuffer* outFrBuf; 61 62 RingBuffer* nearFrBufH[NUM_HIGH_BANDS_MAX]; 63 RingBuffer* outFrBufH[NUM_HIGH_BANDS_MAX]; 64 65 float dBuf[PART_LEN2]; // nearend 66 float eBuf[PART_LEN2]; // error 67 68 float dBufH[NUM_HIGH_BANDS_MAX][PART_LEN2]; // nearend 69 70 float xPow[PART_LEN1]; 71 float dPow[PART_LEN1]; 72 float dMinPow[PART_LEN1]; 73 float dInitMinPow[PART_LEN1]; 74 float* noisePow; 75 76 float xfBuf[2][kExtendedNumPartitions * PART_LEN1]; // farend fft buffer 77 float wfBuf[2][kExtendedNumPartitions * PART_LEN1]; // filter fft 78 complex_t sde[PART_LEN1]; // cross-psd of nearend and error 79 complex_t sxd[PART_LEN1]; // cross-psd of farend and nearend 80 // Farend windowed fft buffer. 81 complex_t xfwBuf[kExtendedNumPartitions * PART_LEN1]; 82 83 float sx[PART_LEN1], sd[PART_LEN1], se[PART_LEN1]; // far, near, error psd 84 float hNs[PART_LEN1]; 85 float hNlFbMin, hNlFbLocalMin; 86 float hNlXdAvgMin; 87 int hNlNewMin, hNlMinCtr; 88 float overDrive, overDriveSm; 89 int nlp_mode; 90 float outBuf[PART_LEN]; 91 int delayIdx; 92 93 short stNearState, echoState; 94 short divergeState; 95 96 int xfBufBlockPos; 97 98 RingBuffer* far_time_buf; 99 100 int system_delay; // Current system delay buffered in AEC. 101 102 int mult; // sampling frequency multiple 103 int sampFreq; 104 size_t num_bands; 105 uint32_t seed; 106 107 float normal_mu; // stepsize 108 float normal_error_threshold; // error threshold 109 110 int noiseEstCtr; 111 112 PowerLevel farlevel; 113 PowerLevel nearlevel; 114 PowerLevel linoutlevel; 115 PowerLevel nlpoutlevel; 116 117 int metricsMode; 118 int stateCounter; 119 Stats erl; 120 Stats erle; 121 Stats aNlp; 122 Stats rerl; 123 124 // Quantities to control H band scaling for SWB input 125 int freq_avg_ic; // initial bin for averaging nlp gain 126 int flag_Hband_cn; // for comfort noise 127 float cn_scale_Hband; // scale for comfort noise in H band 128 129 int delay_metrics_delivered; 130 int delay_histogram[kHistorySizeBlocks]; 131 int num_delay_values; 132 int delay_median; 133 int delay_std; 134 float fraction_poor_delays; 135 int delay_logging_enabled; 136 void* delay_estimator_farend; 137 void* delay_estimator; 138 // Variables associated with delay correction through signal based delay 139 // estimation feedback. 140 int signal_delay_correction; 141 int previous_delay; 142 int delay_correction_count; 143 int shift_offset; 144 float delay_quality_threshold; 145 int frame_count; 146 147 // 0 = delay agnostic mode (signal based delay correction) disabled. 148 // Otherwise enabled. 149 int delay_agnostic_enabled; 150 // 1 = extended filter mode enabled, 0 = disabled. 151 int extended_filter_enabled; 152 // Runtime selection of number of filter partitions. 153 int num_partitions; 154 155 // Flag that extreme filter divergence has been detected by the Echo 156 // Suppressor. 157 int extreme_filter_divergence; 158 159 #ifdef WEBRTC_AEC_DEBUG_DUMP 160 // Sequence number of this AEC instance, so that different instances can 161 // choose different dump file names. 162 int instance_index; 163 164 // Number of times we've restarted dumping; used to pick new dump file names 165 // each time. 166 int debug_dump_count; 167 168 rtc_WavWriter* farFile; 169 rtc_WavWriter* nearFile; 170 rtc_WavWriter* outFile; 171 rtc_WavWriter* outLinearFile; 172 FILE* e_fft_file; 173 #endif 174 }; 175 176 typedef void (*WebRtcAecFilterFar)( 177 int num_partitions, 178 int x_fft_buf_block_pos, 179 float x_fft_buf[2][kExtendedNumPartitions * PART_LEN1], 180 float h_fft_buf[2][kExtendedNumPartitions * PART_LEN1], 181 float y_fft[2][PART_LEN1]); 182 extern WebRtcAecFilterFar WebRtcAec_FilterFar; 183 typedef void (*WebRtcAecScaleErrorSignal)(int extended_filter_enabled, 184 float normal_mu, 185 float normal_error_threshold, 186 float x_pow[PART_LEN1], 187 float ef[2][PART_LEN1]); 188 extern WebRtcAecScaleErrorSignal WebRtcAec_ScaleErrorSignal; 189 typedef void (*WebRtcAecFilterAdaptation)( 190 int num_partitions, 191 int x_fft_buf_block_pos, 192 float x_fft_buf[2][kExtendedNumPartitions * PART_LEN1], 193 float e_fft[2][PART_LEN1], 194 float h_fft_buf[2][kExtendedNumPartitions * PART_LEN1]); 195 extern WebRtcAecFilterAdaptation WebRtcAec_FilterAdaptation; 196 typedef void (*WebRtcAecOverdriveAndSuppress)(AecCore* aec, 197 float hNl[PART_LEN1], 198 const float hNlFb, 199 float efw[2][PART_LEN1]); 200 extern WebRtcAecOverdriveAndSuppress WebRtcAec_OverdriveAndSuppress; 201 202 typedef void (*WebRtcAecComfortNoise)(AecCore* aec, 203 float efw[2][PART_LEN1], 204 float comfortNoiseHband[2][PART_LEN1], 205 const float* noisePow, 206 const float* lambda); 207 extern WebRtcAecComfortNoise WebRtcAec_ComfortNoise; 208 209 typedef void (*WebRtcAecSubBandCoherence)(AecCore* aec, 210 float efw[2][PART_LEN1], 211 float dfw[2][PART_LEN1], 212 float xfw[2][PART_LEN1], 213 float* fft, 214 float* cohde, 215 float* cohxd, 216 int* extreme_filter_divergence); 217 extern WebRtcAecSubBandCoherence WebRtcAec_SubbandCoherence; 218 219 typedef int (*WebRtcAecPartitionDelay)(const AecCore* aec); 220 extern WebRtcAecPartitionDelay WebRtcAec_PartitionDelay; 221 222 typedef void (*WebRtcAecStoreAsComplex)(const float* data, 223 float data_complex[2][PART_LEN1]); 224 extern WebRtcAecStoreAsComplex WebRtcAec_StoreAsComplex; 225 226 typedef void (*WebRtcAecWindowData)(float* x_windowed, const float* x); 227 extern WebRtcAecWindowData WebRtcAec_WindowData; 228 229 #endif // WEBRTC_MODULES_AUDIO_PROCESSING_AEC_AEC_CORE_INTERNAL_H_ 230