Home | History | Annotate | Download | only in source
      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_AGC_MAIN_SOURCE_ANALOG_AGC_H_
     12 #define WEBRTC_MODULES_AUDIO_PROCESSING_AGC_MAIN_SOURCE_ANALOG_AGC_H_
     13 
     14 #include "typedefs.h"
     15 #include "gain_control.h"
     16 #include "digital_agc.h"
     17 
     18 //#define AGC_DEBUG
     19 //#define MIC_LEVEL_FEEDBACK
     20 #ifdef AGC_DEBUG
     21 #include <stdio.h>
     22 #endif
     23 
     24 /* Analog Automatic Gain Control variables:
     25  * Constant declarations (inner limits inside which no changes are done)
     26  * In the beginning the range is narrower to widen as soon as the measure
     27  * 'Rxx160_LP' is inside it. Currently the starting limits are -22.2+/-1dBm0
     28  * and the final limits -22.2+/-2.5dBm0. These levels makes the speech signal
     29  * go towards -25.4dBm0 (-31.4dBov). Tuned with wbfile-31.4dBov.pcm
     30  * The limits are created by running the AGC with a file having the desired
     31  * signal level and thereafter plotting Rxx160_LP in the dBm0-domain defined
     32  * by out=10*log10(in/260537279.7); Set the target level to the average level
     33  * of our measure Rxx160_LP. Remember that the levels are in blocks of 16 in
     34  * Q(-7). (Example matlab code: round(db2pow(-21.2)*16/2^7) )
     35  */
     36 #define RXX_BUFFER_LEN  10
     37 
     38 static const WebRtc_Word16 kMsecSpeechInner = 520;
     39 static const WebRtc_Word16 kMsecSpeechOuter = 340;
     40 
     41 static const WebRtc_Word16 kNormalVadThreshold = 400;
     42 
     43 static const WebRtc_Word16 kAlphaShortTerm = 6; // 1 >> 6 = 0.0156
     44 static const WebRtc_Word16 kAlphaLongTerm = 10; // 1 >> 10 = 0.000977
     45 
     46 typedef struct
     47 {
     48     // Configurable parameters/variables
     49     WebRtc_UWord32      fs;                 // Sampling frequency
     50     WebRtc_Word16       compressionGaindB;  // Fixed gain level in dB
     51     WebRtc_Word16       targetLevelDbfs;    // Target level in -dBfs of envelope (default -3)
     52     WebRtc_Word16       agcMode;            // Hard coded mode (adaptAna/adaptDig/fixedDig)
     53     WebRtc_UWord8       limiterEnable;      // Enabling limiter (on/off (default off))
     54     WebRtcAgc_config_t  defaultConfig;
     55     WebRtcAgc_config_t  usedConfig;
     56 
     57     // General variables
     58     WebRtc_Word16       initFlag;
     59     WebRtc_Word16       lastError;
     60 
     61     // Target level parameters
     62     // Based on the above: analogTargetLevel = round((32767*10^(-22/20))^2*16/2^7)
     63     WebRtc_Word32       analogTargetLevel;  // = RXX_BUFFER_LEN * 846805;       -22 dBfs
     64     WebRtc_Word32       startUpperLimit;    // = RXX_BUFFER_LEN * 1066064;      -21 dBfs
     65     WebRtc_Word32       startLowerLimit;    // = RXX_BUFFER_LEN * 672641;       -23 dBfs
     66     WebRtc_Word32       upperPrimaryLimit;  // = RXX_BUFFER_LEN * 1342095;      -20 dBfs
     67     WebRtc_Word32       lowerPrimaryLimit;  // = RXX_BUFFER_LEN * 534298;       -24 dBfs
     68     WebRtc_Word32       upperSecondaryLimit;// = RXX_BUFFER_LEN * 2677832;      -17 dBfs
     69     WebRtc_Word32       lowerSecondaryLimit;// = RXX_BUFFER_LEN * 267783;       -27 dBfs
     70     WebRtc_UWord16      targetIdx;          // Table index for corresponding target level
     71 #ifdef MIC_LEVEL_FEEDBACK
     72     WebRtc_UWord16      targetIdxOffset;    // Table index offset for level compensation
     73 #endif
     74     WebRtc_Word16       analogTarget;       // Digital reference level in ENV scale
     75 
     76     // Analog AGC specific variables
     77     WebRtc_Word32       filterState[8];     // For downsampling wb to nb
     78     WebRtc_Word32       upperLimit;         // Upper limit for mic energy
     79     WebRtc_Word32       lowerLimit;         // Lower limit for mic energy
     80     WebRtc_Word32       Rxx160w32;          // Average energy for one frame
     81     WebRtc_Word32       Rxx16_LPw32;        // Low pass filtered subframe energies
     82     WebRtc_Word32       Rxx160_LPw32;       // Low pass filtered frame energies
     83     WebRtc_Word32       Rxx16_LPw32Max;     // Keeps track of largest energy subframe
     84     WebRtc_Word32       Rxx16_vectorw32[RXX_BUFFER_LEN];// Array with subframe energies
     85     WebRtc_Word32       Rxx16w32_array[2][5];// Energy values of microphone signal
     86     WebRtc_Word32       env[2][10];         // Envelope values of subframes
     87 
     88     WebRtc_Word16       Rxx16pos;           // Current position in the Rxx16_vectorw32
     89     WebRtc_Word16       envSum;             // Filtered scaled envelope in subframes
     90     WebRtc_Word16       vadThreshold;       // Threshold for VAD decision
     91     WebRtc_Word16       inActive;           // Inactive time in milliseconds
     92     WebRtc_Word16       msTooLow;           // Milliseconds of speech at a too low level
     93     WebRtc_Word16       msTooHigh;          // Milliseconds of speech at a too high level
     94     WebRtc_Word16       changeToSlowMode;   // Change to slow mode after some time at target
     95     WebRtc_Word16       firstCall;          // First call to the process-function
     96     WebRtc_Word16       msZero;             // Milliseconds of zero input
     97     WebRtc_Word16       msecSpeechOuterChange;// Min ms of speech between volume changes
     98     WebRtc_Word16       msecSpeechInnerChange;// Min ms of speech between volume changes
     99     WebRtc_Word16       activeSpeech;       // Milliseconds of active speech
    100     WebRtc_Word16       muteGuardMs;        // Counter to prevent mute action
    101     WebRtc_Word16       inQueue;            // 10 ms batch indicator
    102 
    103     // Microphone level variables
    104     WebRtc_Word32       micRef;             // Remember ref. mic level for virtual mic
    105     WebRtc_UWord16      gainTableIdx;       // Current position in virtual gain table
    106     WebRtc_Word32       micGainIdx;         // Gain index of mic level to increase slowly
    107     WebRtc_Word32       micVol;             // Remember volume between frames
    108     WebRtc_Word32       maxLevel;           // Max possible vol level, incl dig gain
    109     WebRtc_Word32       maxAnalog;          // Maximum possible analog volume level
    110     WebRtc_Word32       maxInit;            // Initial value of "max"
    111     WebRtc_Word32       minLevel;           // Minimum possible volume level
    112     WebRtc_Word32       minOutput;          // Minimum output volume level
    113     WebRtc_Word32       zeroCtrlMax;        // Remember max gain => don't amp low input
    114 
    115     WebRtc_Word16       scale;              // Scale factor for internal volume levels
    116 #ifdef MIC_LEVEL_FEEDBACK
    117     WebRtc_Word16       numBlocksMicLvlSat;
    118     WebRtc_UWord8 micLvlSat;
    119 #endif
    120     // Structs for VAD and digital_agc
    121     AgcVad_t            vadMic;
    122     DigitalAgc_t        digitalAgc;
    123 
    124 #ifdef AGC_DEBUG
    125     FILE*               fpt;
    126     FILE*               agcLog;
    127     WebRtc_Word32       fcount;
    128 #endif
    129 
    130     WebRtc_Word16       lowLevelSignal;
    131 } Agc_t;
    132 
    133 #endif // WEBRTC_MODULES_AUDIO_PROCESSING_AGC_MAIN_SOURCE_ANALOG_AGC_H_
    134