1/*
2 *  Copyright (c) 2011 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_NS_NS_CORE_H_
12#define WEBRTC_MODULES_AUDIO_PROCESSING_NS_NS_CORE_H_
13
14#include "webrtc/modules/audio_processing/ns/defines.h"
15
16typedef struct NSParaExtract_ {
17  // Bin size of histogram.
18  float binSizeLrt;
19  float binSizeSpecFlat;
20  float binSizeSpecDiff;
21  // Range of histogram over which LRT threshold is computed.
22  float rangeAvgHistLrt;
23  // Scale parameters: multiply dominant peaks of the histograms by scale factor
24  // to obtain thresholds for prior model.
25  float factor1ModelPars;  // For LRT and spectral difference.
26  float factor2ModelPars;  // For spectral_flatness: used when noise is flatter
27                           // than speech.
28  // Peak limit for spectral flatness (varies between 0 and 1).
29  float thresPosSpecFlat;
30  // Limit on spacing of two highest peaks in histogram: spacing determined by
31  // bin size.
32  float limitPeakSpacingSpecFlat;
33  float limitPeakSpacingSpecDiff;
34  // Limit on relevance of second peak.
35  float limitPeakWeightsSpecFlat;
36  float limitPeakWeightsSpecDiff;
37  // Limit on fluctuation of LRT feature.
38  float thresFluctLrt;
39  // Limit on the max and min values for the feature thresholds.
40  float maxLrt;
41  float minLrt;
42  float maxSpecFlat;
43  float minSpecFlat;
44  float maxSpecDiff;
45  float minSpecDiff;
46  // Criteria of weight of histogram peak to accept/reject feature.
47  int thresWeightSpecFlat;
48  int thresWeightSpecDiff;
49
50} NSParaExtract;
51
52typedef struct NoiseSuppressionC_ {
53  uint32_t fs;
54  size_t blockLen;
55  size_t windShift;
56  size_t anaLen;
57  size_t magnLen;
58  int aggrMode;
59  const float* window;
60  float analyzeBuf[ANAL_BLOCKL_MAX];
61  float dataBuf[ANAL_BLOCKL_MAX];
62  float syntBuf[ANAL_BLOCKL_MAX];
63
64  int initFlag;
65  // Parameters for quantile noise estimation.
66  float density[SIMULT * HALF_ANAL_BLOCKL];
67  float lquantile[SIMULT * HALF_ANAL_BLOCKL];
68  float quantile[HALF_ANAL_BLOCKL];
69  int counter[SIMULT];
70  int updates;
71  // Parameters for Wiener filter.
72  float smooth[HALF_ANAL_BLOCKL];
73  float overdrive;
74  float denoiseBound;
75  int gainmap;
76  // FFT work arrays.
77  size_t ip[IP_LENGTH];
78  float wfft[W_LENGTH];
79
80  // Parameters for new method: some not needed, will reduce/cleanup later.
81  int32_t blockInd;  // Frame index counter.
82  int modelUpdatePars[4];  // Parameters for updating or estimating.
83  // Thresholds/weights for prior model.
84  float priorModelPars[7];  // Parameters for prior model.
85  float noise[HALF_ANAL_BLOCKL];  // Noise spectrum from current frame.
86  float noisePrev[HALF_ANAL_BLOCKL];  // Noise spectrum from previous frame.
87  // Magnitude spectrum of previous analyze frame.
88  float magnPrevAnalyze[HALF_ANAL_BLOCKL];
89  // Magnitude spectrum of previous process frame.
90  float magnPrevProcess[HALF_ANAL_BLOCKL];
91  float logLrtTimeAvg[HALF_ANAL_BLOCKL];  // Log LRT factor with time-smoothing.
92  float priorSpeechProb;  // Prior speech/noise probability.
93  float featureData[7];
94  // Conservative noise spectrum estimate.
95  float magnAvgPause[HALF_ANAL_BLOCKL];
96  float signalEnergy;  // Energy of |magn|.
97  float sumMagn;
98  float whiteNoiseLevel;  // Initial noise estimate.
99  float initMagnEst[HALF_ANAL_BLOCKL];  // Initial magnitude spectrum estimate.
100  float pinkNoiseNumerator;  // Pink noise parameter: numerator.
101  float pinkNoiseExp;  // Pink noise parameter: power of frequencies.
102  float parametricNoise[HALF_ANAL_BLOCKL];
103  // Parameters for feature extraction.
104  NSParaExtract featureExtractionParams;
105  // Histograms for parameter estimation.
106  int histLrt[HIST_PAR_EST];
107  int histSpecFlat[HIST_PAR_EST];
108  int histSpecDiff[HIST_PAR_EST];
109  // Quantities for high band estimate.
110  float speechProb[HALF_ANAL_BLOCKL];  // Final speech/noise prob: prior + LRT.
111  // Buffering data for HB.
112  float dataBufHB[NUM_HIGH_BANDS_MAX][ANAL_BLOCKL_MAX];
113
114} NoiseSuppressionC;
115
116#ifdef __cplusplus
117extern "C" {
118#endif
119
120/****************************************************************************
121 * WebRtcNs_InitCore(...)
122 *
123 * This function initializes a noise suppression instance
124 *
125 * Input:
126 *      - self          : Instance that should be initialized
127 *      - fs            : Sampling frequency
128 *
129 * Output:
130 *      - self          : Initialized instance
131 *
132 * Return value         :  0 - Ok
133 *                        -1 - Error
134 */
135int WebRtcNs_InitCore(NoiseSuppressionC* self, uint32_t fs);
136
137/****************************************************************************
138 * WebRtcNs_set_policy_core(...)
139 *
140 * This changes the aggressiveness of the noise suppression method.
141 *
142 * Input:
143 *      - self          : Instance that should be initialized
144 *      - mode          : 0: Mild (6dB), 1: Medium (10dB), 2: Aggressive (15dB)
145 *
146 * Output:
147 *      - self          : Initialized instance
148 *
149 * Return value         :  0 - Ok
150 *                        -1 - Error
151 */
152int WebRtcNs_set_policy_core(NoiseSuppressionC* self, int mode);
153
154/****************************************************************************
155 * WebRtcNs_AnalyzeCore
156 *
157 * Estimate the background noise.
158 *
159 * Input:
160 *      - self          : Instance that should be initialized
161 *      - speechFrame   : Input speech frame for lower band
162 *
163 * Output:
164 *      - self          : Updated instance
165 */
166void WebRtcNs_AnalyzeCore(NoiseSuppressionC* self, const float* speechFrame);
167
168/****************************************************************************
169 * WebRtcNs_ProcessCore
170 *
171 * Do noise suppression.
172 *
173 * Input:
174 *      - self          : Instance that should be initialized
175 *      - inFrame       : Input speech frame for each band
176 *      - num_bands     : Number of bands
177 *
178 * Output:
179 *      - self          : Updated instance
180 *      - outFrame      : Output speech frame for each band
181 */
182void WebRtcNs_ProcessCore(NoiseSuppressionC* self,
183                          const float* const* inFrame,
184                          size_t num_bands,
185                          float* const* outFrame);
186
187#ifdef __cplusplus
188}
189#endif
190#endif  // WEBRTC_MODULES_AUDIO_PROCESSING_NS_NS_CORE_H_
191