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_MAIN_SOURCE_NSX_CORE_H_
12#define WEBRTC_MODULES_AUDIO_PROCESSING_NS_MAIN_SOURCE_NSX_CORE_H_
13
14#include "typedefs.h"
15#include "signal_processing_library.h"
16
17#include "nsx_defines.h"
18
19#ifdef NS_FILEDEBUG
20#include <stdio.h>
21#endif
22
23typedef struct NsxInst_t_
24{
25    WebRtc_UWord32          fs;
26
27    const WebRtc_Word16*    window;
28    WebRtc_Word16           analysisBuffer[ANAL_BLOCKL_MAX];
29    WebRtc_Word16           synthesisBuffer[ANAL_BLOCKL_MAX];
30    WebRtc_UWord16          noiseSupFilter[HALF_ANAL_BLOCKL];
31    WebRtc_UWord16          overdrive; /* Q8 */
32    WebRtc_UWord16          denoiseBound; /* Q14 */
33    const WebRtc_Word16*    factor2Table;
34    WebRtc_Word16           noiseEstLogQuantile[SIMULT * HALF_ANAL_BLOCKL];
35    WebRtc_Word16           noiseEstDensity[SIMULT * HALF_ANAL_BLOCKL];
36    WebRtc_Word16           noiseEstCounter[SIMULT];
37    WebRtc_Word16           noiseEstQuantile[HALF_ANAL_BLOCKL];
38
39    WebRtc_Word16           anaLen;
40    int                     anaLen2;
41    int                     magnLen;
42    int                     aggrMode;
43    int                     stages;
44    int                     initFlag;
45    int                     gainMap;
46
47    WebRtc_Word32           maxLrt;
48    WebRtc_Word32           minLrt;
49    WebRtc_Word32           logLrtTimeAvgW32[HALF_ANAL_BLOCKL]; //log lrt factor with time-smoothing in Q8
50    WebRtc_Word32           featureLogLrt;
51    WebRtc_Word32           thresholdLogLrt;
52    WebRtc_Word16           weightLogLrt;
53
54    WebRtc_UWord32          featureSpecDiff;
55    WebRtc_UWord32          thresholdSpecDiff;
56    WebRtc_Word16           weightSpecDiff;
57
58    WebRtc_UWord32          featureSpecFlat;
59    WebRtc_UWord32          thresholdSpecFlat;
60    WebRtc_Word16           weightSpecFlat;
61
62    WebRtc_Word32           avgMagnPause[HALF_ANAL_BLOCKL]; //conservative estimate of noise spectrum
63    WebRtc_UWord32          magnEnergy;
64    WebRtc_UWord32          sumMagn;
65    WebRtc_UWord32          curAvgMagnEnergy;
66    WebRtc_UWord32          timeAvgMagnEnergy;
67    WebRtc_UWord32          timeAvgMagnEnergyTmp;
68
69    WebRtc_UWord32          whiteNoiseLevel;              //initial noise estimate
70    WebRtc_UWord32          initMagnEst[HALF_ANAL_BLOCKL];//initial magnitude spectrum estimate
71    WebRtc_Word32           pinkNoiseNumerator;           //pink noise parameter: numerator
72    WebRtc_Word32           pinkNoiseExp;                 //pink noise parameter: power of freq
73    int                     minNorm;                      //smallest normalization factor
74    int                     zeroInputSignal;              //zero input signal flag
75
76    WebRtc_UWord32          prevNoiseU32[HALF_ANAL_BLOCKL]; //noise spectrum from previous frame
77    WebRtc_UWord16          prevMagnU16[HALF_ANAL_BLOCKL]; //magnitude spectrum from previous frame
78    WebRtc_Word16           priorNonSpeechProb; //prior speech/noise probability // Q14
79
80    int                     blockIndex; //frame index counter
81    int                     modelUpdate; //parameter for updating or estimating thresholds/weights for prior model
82    int                     cntThresUpdate;
83
84    //histograms for parameter estimation
85    WebRtc_Word16           histLrt[HIST_PAR_EST];
86    WebRtc_Word16           histSpecFlat[HIST_PAR_EST];
87    WebRtc_Word16           histSpecDiff[HIST_PAR_EST];
88
89    //quantities for high band estimate
90    WebRtc_Word16           dataBufHBFX[ANAL_BLOCKL_MAX]; /* Q0 */
91
92    int                     qNoise;
93    int                     prevQNoise;
94    int                     prevQMagn;
95    int                     blockLen10ms;
96
97    WebRtc_Word16           real[ANAL_BLOCKL_MAX];
98    WebRtc_Word16           imag[ANAL_BLOCKL_MAX];
99    WebRtc_Word32           energyIn;
100    int                     scaleEnergyIn;
101    int                     normData;
102
103} NsxInst_t;
104
105#ifdef __cplusplus
106extern "C"
107{
108#endif
109
110/****************************************************************************
111 * WebRtcNsx_InitCore(...)
112 *
113 * This function initializes a noise suppression instance
114 *
115 * Input:
116 *      - inst          : Instance that should be initialized
117 *      - fs            : Sampling frequency
118 *
119 * Output:
120 *      - inst          : Initialized instance
121 *
122 * Return value         :  0 - Ok
123 *                        -1 - Error
124 */
125WebRtc_Word32 WebRtcNsx_InitCore(NsxInst_t *inst, WebRtc_UWord32 fs);
126
127/****************************************************************************
128 * WebRtcNsx_set_policy_core(...)
129 *
130 * This changes the aggressiveness of the noise suppression method.
131 *
132 * Input:
133 *      - inst          : Instance that should be initialized
134 *      - mode          : 0: Mild (6 dB), 1: Medium (10 dB), 2: Aggressive (15 dB)
135 *
136 * Output:
137 *      - NS_inst      : Initialized instance
138 *
139 * Return value         :  0 - Ok
140 *                        -1 - Error
141 */
142int WebRtcNsx_set_policy_core(NsxInst_t *inst, int mode);
143
144/****************************************************************************
145 * WebRtcNsx_ProcessCore
146 *
147 * Do noise suppression.
148 *
149 * Input:
150 *      - inst          : Instance that should be initialized
151 *      - inFrameLow    : Input speech frame for lower band
152 *      - inFrameHigh   : Input speech frame for higher band
153 *
154 * Output:
155 *      - inst          : Updated instance
156 *      - outFrameLow   : Output speech frame for lower band
157 *      - outFrameHigh  : Output speech frame for higher band
158 *
159 * Return value         :  0 - OK
160 *                        -1 - Error
161 */
162int WebRtcNsx_ProcessCore(NsxInst_t *inst, short *inFrameLow, short *inFrameHigh,
163                          short *outFrameLow, short *outFrameHigh);
164
165#ifdef __cplusplus
166}
167#endif
168
169#endif // WEBRTC_MODULES_AUDIO_PROCESSING_NS_MAIN_SOURCE_NSX_CORE_H_
170