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_buf;
99   RingBuffer* far_buf_windowed;
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 #ifdef WEBRTC_AEC_DEBUG_DUMP
156   // Sequence number of this AEC instance, so that different instances can
157   // choose different dump file names.
158   int instance_index;
159 
160   // Number of times we've restarted dumping; used to pick new dump file names
161   // each time.
162   int debug_dump_count;
163 
164   RingBuffer* far_time_buf;
165   rtc_WavWriter* farFile;
166   rtc_WavWriter* nearFile;
167   rtc_WavWriter* outFile;
168   rtc_WavWriter* outLinearFile;
169   FILE* e_fft_file;
170 #endif
171 };
172 
173 typedef void (*WebRtcAecFilterFar)(AecCore* aec, float yf[2][PART_LEN1]);
174 extern WebRtcAecFilterFar WebRtcAec_FilterFar;
175 typedef void (*WebRtcAecScaleErrorSignal)(AecCore* aec, float ef[2][PART_LEN1]);
176 extern WebRtcAecScaleErrorSignal WebRtcAec_ScaleErrorSignal;
177 typedef void (*WebRtcAecFilterAdaptation)(AecCore* aec,
178                                           float* fft,
179                                           float ef[2][PART_LEN1]);
180 extern WebRtcAecFilterAdaptation WebRtcAec_FilterAdaptation;
181 typedef void (*WebRtcAecOverdriveAndSuppress)(AecCore* aec,
182                                               float hNl[PART_LEN1],
183                                               const float hNlFb,
184                                               float efw[2][PART_LEN1]);
185 extern WebRtcAecOverdriveAndSuppress WebRtcAec_OverdriveAndSuppress;
186 
187 typedef void (*WebRtcAecComfortNoise)(AecCore* aec,
188                                       float efw[2][PART_LEN1],
189                                       complex_t* comfortNoiseHband,
190                                       const float* noisePow,
191                                       const float* lambda);
192 extern WebRtcAecComfortNoise WebRtcAec_ComfortNoise;
193 
194 typedef void (*WebRtcAecSubBandCoherence)(AecCore* aec,
195                                           float efw[2][PART_LEN1],
196                                           float xfw[2][PART_LEN1],
197                                           float* fft,
198                                           float* cohde,
199                                           float* cohxd);
200 extern WebRtcAecSubBandCoherence WebRtcAec_SubbandCoherence;
201 
202 #endif  // WEBRTC_MODULES_AUDIO_PROCESSING_AEC_AEC_CORE_INTERNAL_H_
203