Home | History | Annotate | Download | only in ns
      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 
     16 typedef struct NSParaExtract_t_ {
     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_t;
     51 
     52 typedef struct NSinst_t_ {
     53   uint32_t fs;
     54   int blockLen;
     55   int blockLen10ms;
     56   int windShift;
     57   int outLen;
     58   int anaLen;
     59   int magnLen;
     60   int aggrMode;
     61   const float* window;
     62   float analyzeBuf[ANAL_BLOCKL_MAX];
     63   float dataBuf[ANAL_BLOCKL_MAX];
     64   float syntBuf[ANAL_BLOCKL_MAX];
     65   float outBuf[3 * BLOCKL_MAX];
     66 
     67   int initFlag;
     68   // parameters for quantile noise estimation
     69   float density[SIMULT * HALF_ANAL_BLOCKL];
     70   float lquantile[SIMULT * HALF_ANAL_BLOCKL];
     71   float quantile[HALF_ANAL_BLOCKL];
     72   int counter[SIMULT];
     73   int updates;
     74   // parameters for Wiener filter
     75   float previousEstimateStsa[HALF_ANAL_BLOCKL];
     76   float smooth[HALF_ANAL_BLOCKL];
     77   float overdrive;
     78   float denoiseBound;
     79   int gainmap;
     80   // fft work arrays.
     81   int ip[IP_LENGTH];
     82   float wfft[W_LENGTH];
     83 
     84   // parameters for new method: some not needed, will reduce/cleanup later
     85   int32_t blockInd;  // frame index counter
     86   int modelUpdatePars[4];  // parameters for updating or estimating
     87   // thresholds/weights for prior model
     88   float priorModelPars[7];  // parameters for prior model
     89   float noisePrev[HALF_ANAL_BLOCKL];  // noise spectrum from previous frame
     90   float magnPrev[HALF_ANAL_BLOCKL];  // magnitude spectrum of previous frame
     91   float logLrtTimeAvg[HALF_ANAL_BLOCKL];  // log lrt factor with time-smoothing
     92   float priorSpeechProb;  // prior speech/noise probability
     93   float featureData[7];  // data for features
     94   float magnAvgPause[HALF_ANAL_BLOCKL];  // conservative noise spectrum estimate
     95   float signalEnergy;  // energy of magn
     96   float sumMagn;  // sum of magn
     97   float whiteNoiseLevel;  // initial noise estimate
     98   float initMagnEst[HALF_ANAL_BLOCKL];  // initial magnitude spectrum estimate
     99   float pinkNoiseNumerator;  // pink noise parameter: numerator
    100   float pinkNoiseExp;  // pink noise parameter: power of freq
    101   float parametricNoise[HALF_ANAL_BLOCKL];
    102   NSParaExtract_t featureExtractionParams;  // parameters for feature extraction
    103   // histograms for parameter estimation
    104   int histLrt[HIST_PAR_EST];
    105   int histSpecFlat[HIST_PAR_EST];
    106   int histSpecDiff[HIST_PAR_EST];
    107   // quantities for high band estimate
    108   float speechProb[HALF_ANAL_BLOCKL];  // final speech/noise prob: prior + LRT
    109   float dataBufHB[ANAL_BLOCKL_MAX];  // buffering data for HB
    110 
    111 } NSinst_t;
    112 
    113 #ifdef __cplusplus
    114 extern "C" {
    115 #endif
    116 
    117 /****************************************************************************
    118  * WebRtcNs_InitCore(...)
    119  *
    120  * This function initializes a noise suppression instance
    121  *
    122  * Input:
    123  *      - inst          : Instance that should be initialized
    124  *      - fs            : Sampling frequency
    125  *
    126  * Output:
    127  *      - inst          : Initialized instance
    128  *
    129  * Return value         :  0 - Ok
    130  *                        -1 - Error
    131  */
    132 int WebRtcNs_InitCore(NSinst_t* inst, uint32_t fs);
    133 
    134 /****************************************************************************
    135  * WebRtcNs_set_policy_core(...)
    136  *
    137  * This changes the aggressiveness of the noise suppression method.
    138  *
    139  * Input:
    140  *      - inst          : Instance that should be initialized
    141  *      - mode          : 0: Mild (6dB), 1: Medium (10dB), 2: Aggressive (15dB)
    142  *
    143  * Output:
    144  *      - NS_inst      : Initialized instance
    145  *
    146  * Return value         :  0 - Ok
    147  *                        -1 - Error
    148  */
    149 int WebRtcNs_set_policy_core(NSinst_t* inst, int mode);
    150 
    151 /****************************************************************************
    152  * WebRtcNs_AnalyzeCore
    153  *
    154  * Estimate the background noise.
    155  *
    156  * Input:
    157  *      - inst          : Instance that should be initialized
    158  *      - speechFrame   : Input speech frame for lower band
    159  *
    160  * Output:
    161  *      - inst          : Updated instance
    162  *
    163  * Return value         :  0 - OK
    164  *                        -1 - Error
    165  */
    166 int WebRtcNs_AnalyzeCore(NSinst_t* inst, float* speechFrame);
    167 
    168 /****************************************************************************
    169  * WebRtcNs_ProcessCore
    170  *
    171  * Do noise suppression.
    172  *
    173  * Input:
    174  *      - inst          : Instance that should be initialized
    175  *      - inFrameLow    : Input speech frame for lower band
    176  *      - inFrameHigh   : Input speech frame for higher band
    177  *
    178  * Output:
    179  *      - inst          : Updated instance
    180  *      - outFrameLow   : Output speech frame for lower band
    181  *      - outFrameHigh  : Output speech frame for higher band
    182  *
    183  * Return value         :  0 - OK
    184  *                        -1 - Error
    185  */
    186 int WebRtcNs_ProcessCore(NSinst_t* inst,
    187                          float* inFrameLow,
    188                          float* inFrameHigh,
    189                          float* outFrameLow,
    190                          float* outFrameHigh);
    191 
    192 #ifdef __cplusplus
    193 }
    194 #endif
    195 #endif  // WEBRTC_MODULES_AUDIO_PROCESSING_NS_NS_CORE_H_
    196