blob: b036f449c701f554d4674d579524e653ba17c27c [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
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
pbos@webrtc.org7fad4b82013-05-28 08:11:59 +000014#include "webrtc/modules/audio_processing/agc/digital_agc.h"
15#include "webrtc/modules/audio_processing/agc/include/gain_control.h"
16#include "webrtc/typedefs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000017
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
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +000038static const int16_t kMsecSpeechInner = 520;
39static const int16_t kMsecSpeechOuter = 340;
niklase@google.com470e71d2011-07-07 08:21:25 +000040
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +000041static const int16_t kNormalVadThreshold = 400;
niklase@google.com470e71d2011-07-07 08:21:25 +000042
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +000043static const int16_t kAlphaShortTerm = 6; // 1 >> 6 = 0.0156
44static const int16_t kAlphaLongTerm = 10; // 1 >> 10 = 0.000977
niklase@google.com470e71d2011-07-07 08:21:25 +000045
46typedef struct
47{
48 // Configurable parameters/variables
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +000049 uint32_t fs; // Sampling frequency
50 int16_t compressionGaindB; // Fixed gain level in dB
51 int16_t targetLevelDbfs; // Target level in -dBfs of envelope (default -3)
52 int16_t agcMode; // Hard coded mode (adaptAna/adaptDig/fixedDig)
53 uint8_t limiterEnable; // Enabling limiter (on/off (default off))
niklase@google.com470e71d2011-07-07 08:21:25 +000054 WebRtcAgc_config_t defaultConfig;
55 WebRtcAgc_config_t usedConfig;
56
57 // General variables
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +000058 int16_t initFlag;
59 int16_t lastError;
niklase@google.com470e71d2011-07-07 08:21:25 +000060
61 // Target level parameters
62 // Based on the above: analogTargetLevel = round((32767*10^(-22/20))^2*16/2^7)
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +000063 int32_t analogTargetLevel; // = RXX_BUFFER_LEN * 846805; -22 dBfs
64 int32_t startUpperLimit; // = RXX_BUFFER_LEN * 1066064; -21 dBfs
65 int32_t startLowerLimit; // = RXX_BUFFER_LEN * 672641; -23 dBfs
66 int32_t upperPrimaryLimit; // = RXX_BUFFER_LEN * 1342095; -20 dBfs
67 int32_t lowerPrimaryLimit; // = RXX_BUFFER_LEN * 534298; -24 dBfs
68 int32_t upperSecondaryLimit;// = RXX_BUFFER_LEN * 2677832; -17 dBfs
69 int32_t lowerSecondaryLimit;// = RXX_BUFFER_LEN * 267783; -27 dBfs
70 uint16_t targetIdx; // Table index for corresponding target level
niklase@google.com470e71d2011-07-07 08:21:25 +000071#ifdef MIC_LEVEL_FEEDBACK
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +000072 uint16_t targetIdxOffset; // Table index offset for level compensation
niklase@google.com470e71d2011-07-07 08:21:25 +000073#endif
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +000074 int16_t analogTarget; // Digital reference level in ENV scale
niklase@google.com470e71d2011-07-07 08:21:25 +000075
76 // Analog AGC specific variables
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +000077 int32_t filterState[8]; // For downsampling wb to nb
78 int32_t upperLimit; // Upper limit for mic energy
79 int32_t lowerLimit; // Lower limit for mic energy
80 int32_t Rxx160w32; // Average energy for one frame
81 int32_t Rxx16_LPw32; // Low pass filtered subframe energies
82 int32_t Rxx160_LPw32; // Low pass filtered frame energies
83 int32_t Rxx16_LPw32Max; // Keeps track of largest energy subframe
84 int32_t Rxx16_vectorw32[RXX_BUFFER_LEN];// Array with subframe energies
85 int32_t Rxx16w32_array[2][5];// Energy values of microphone signal
86 int32_t env[2][10]; // Envelope values of subframes
niklase@google.com470e71d2011-07-07 08:21:25 +000087
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +000088 int16_t Rxx16pos; // Current position in the Rxx16_vectorw32
89 int16_t envSum; // Filtered scaled envelope in subframes
90 int16_t vadThreshold; // Threshold for VAD decision
91 int16_t inActive; // Inactive time in milliseconds
92 int16_t msTooLow; // Milliseconds of speech at a too low level
93 int16_t msTooHigh; // Milliseconds of speech at a too high level
94 int16_t changeToSlowMode; // Change to slow mode after some time at target
95 int16_t firstCall; // First call to the process-function
96 int16_t msZero; // Milliseconds of zero input
97 int16_t msecSpeechOuterChange;// Min ms of speech between volume changes
98 int16_t msecSpeechInnerChange;// Min ms of speech between volume changes
99 int16_t activeSpeech; // Milliseconds of active speech
100 int16_t muteGuardMs; // Counter to prevent mute action
101 int16_t inQueue; // 10 ms batch indicator
niklase@google.com470e71d2011-07-07 08:21:25 +0000102
103 // Microphone level variables
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +0000104 int32_t micRef; // Remember ref. mic level for virtual mic
105 uint16_t gainTableIdx; // Current position in virtual gain table
106 int32_t micGainIdx; // Gain index of mic level to increase slowly
107 int32_t micVol; // Remember volume between frames
108 int32_t maxLevel; // Max possible vol level, incl dig gain
109 int32_t maxAnalog; // Maximum possible analog volume level
110 int32_t maxInit; // Initial value of "max"
111 int32_t minLevel; // Minimum possible volume level
112 int32_t minOutput; // Minimum output volume level
113 int32_t zeroCtrlMax; // Remember max gain => don't amp low input
andrew@webrtc.org27c69802014-02-18 20:24:56 +0000114 int32_t lastInMicLevel;
niklase@google.com470e71d2011-07-07 08:21:25 +0000115
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +0000116 int16_t scale; // Scale factor for internal volume levels
niklase@google.com470e71d2011-07-07 08:21:25 +0000117#ifdef MIC_LEVEL_FEEDBACK
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +0000118 int16_t numBlocksMicLvlSat;
119 uint8_t micLvlSat;
niklase@google.com470e71d2011-07-07 08:21:25 +0000120#endif
121 // Structs for VAD and digital_agc
122 AgcVad_t vadMic;
123 DigitalAgc_t digitalAgc;
124
125#ifdef AGC_DEBUG
126 FILE* fpt;
127 FILE* agcLog;
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +0000128 int32_t fcount;
niklase@google.com470e71d2011-07-07 08:21:25 +0000129#endif
130
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +0000131 int16_t lowLevelSignal;
niklase@google.com470e71d2011-07-07 08:21:25 +0000132} Agc_t;
133
134#endif // WEBRTC_MODULES_AUDIO_PROCESSING_AGC_MAIN_SOURCE_ANALOG_AGC_H_