blob: 619d95ad0adad99d48b27d92b944266c2a36cb3a [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_PROCESSING_AGC_LEGACY_ANALOG_AGC_H_
12#define MODULES_AUDIO_PROCESSING_AGC_LEGACY_ANALOG_AGC_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
niklase@google.com470e71d2011-07-07 08:21:25 +000014//#define MIC_LEVEL_FEEDBACK
bjornv@webrtc.orgea297872014-09-23 11:21:39 +000015#ifdef WEBRTC_AGC_DEBUG_DUMP
niklase@google.com470e71d2011-07-07 08:21:25 +000016#include <stdio.h>
17#endif
18
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/audio_processing/agc/legacy/digital_agc.h"
20#include "modules/audio_processing/agc/legacy/gain_control.h"
bjornv@webrtc.orgb395a5e2014-12-16 10:38:10 +000021
niklase@google.com470e71d2011-07-07 08:21:25 +000022/* Analog Automatic Gain Control variables:
23 * Constant declarations (inner limits inside which no changes are done)
24 * In the beginning the range is narrower to widen as soon as the measure
25 * 'Rxx160_LP' is inside it. Currently the starting limits are -22.2+/-1dBm0
26 * and the final limits -22.2+/-2.5dBm0. These levels makes the speech signal
27 * go towards -25.4dBm0 (-31.4dBov). Tuned with wbfile-31.4dBov.pcm
28 * The limits are created by running the AGC with a file having the desired
29 * signal level and thereafter plotting Rxx160_LP in the dBm0-domain defined
30 * by out=10*log10(in/260537279.7); Set the target level to the average level
31 * of our measure Rxx160_LP. Remember that the levels are in blocks of 16 in
32 * Q(-7). (Example matlab code: round(db2pow(-21.2)*16/2^7) )
33 */
minyuecac94aa2016-05-20 08:42:22 -070034#define RXX_BUFFER_LEN 10
niklase@google.com470e71d2011-07-07 08:21:25 +000035
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +000036static const int16_t kMsecSpeechInner = 520;
37static const int16_t kMsecSpeechOuter = 340;
niklase@google.com470e71d2011-07-07 08:21:25 +000038
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +000039static const int16_t kNormalVadThreshold = 400;
niklase@google.com470e71d2011-07-07 08:21:25 +000040
minyuecac94aa2016-05-20 08:42:22 -070041static const int16_t kAlphaShortTerm = 6; // 1 >> 6 = 0.0156
42static const int16_t kAlphaLongTerm = 10; // 1 >> 10 = 0.000977
niklase@google.com470e71d2011-07-07 08:21:25 +000043
minyuecac94aa2016-05-20 08:42:22 -070044typedef struct {
45 // Configurable parameters/variables
46 uint32_t fs; // Sampling frequency
47 int16_t compressionGaindB; // Fixed gain level in dB
48 int16_t targetLevelDbfs; // Target level in -dBfs of envelope (default -3)
49 int16_t agcMode; // Hard coded mode (adaptAna/adaptDig/fixedDig)
50 uint8_t limiterEnable; // Enabling limiter (on/off (default off))
51 WebRtcAgcConfig defaultConfig;
52 WebRtcAgcConfig usedConfig;
niklase@google.com470e71d2011-07-07 08:21:25 +000053
minyuecac94aa2016-05-20 08:42:22 -070054 // General variables
55 int16_t initFlag;
56 int16_t lastError;
niklase@google.com470e71d2011-07-07 08:21:25 +000057
minyuecac94aa2016-05-20 08:42:22 -070058 // Target level parameters
59 // Based on the above: analogTargetLevel = round((32767*10^(-22/20))^2*16/2^7)
60 int32_t analogTargetLevel; // = RXX_BUFFER_LEN * 846805; -22 dBfs
61 int32_t startUpperLimit; // = RXX_BUFFER_LEN * 1066064; -21 dBfs
62 int32_t startLowerLimit; // = RXX_BUFFER_LEN * 672641; -23 dBfs
63 int32_t upperPrimaryLimit; // = RXX_BUFFER_LEN * 1342095; -20 dBfs
64 int32_t lowerPrimaryLimit; // = RXX_BUFFER_LEN * 534298; -24 dBfs
65 int32_t upperSecondaryLimit; // = RXX_BUFFER_LEN * 2677832; -17 dBfs
66 int32_t lowerSecondaryLimit; // = RXX_BUFFER_LEN * 267783; -27 dBfs
67 uint16_t targetIdx; // Table index for corresponding target level
niklase@google.com470e71d2011-07-07 08:21:25 +000068#ifdef MIC_LEVEL_FEEDBACK
minyuecac94aa2016-05-20 08:42:22 -070069 uint16_t targetIdxOffset; // Table index offset for level compensation
niklase@google.com470e71d2011-07-07 08:21:25 +000070#endif
minyuecac94aa2016-05-20 08:42:22 -070071 int16_t analogTarget; // Digital reference level in ENV scale
niklase@google.com470e71d2011-07-07 08:21:25 +000072
minyuecac94aa2016-05-20 08:42:22 -070073 // Analog AGC specific variables
74 int32_t filterState[8]; // For downsampling wb to nb
75 int32_t upperLimit; // Upper limit for mic energy
76 int32_t lowerLimit; // Lower limit for mic energy
77 int32_t Rxx160w32; // Average energy for one frame
78 int32_t Rxx16_LPw32; // Low pass filtered subframe energies
79 int32_t Rxx160_LPw32; // Low pass filtered frame energies
80 int32_t Rxx16_LPw32Max; // Keeps track of largest energy subframe
81 int32_t Rxx16_vectorw32[RXX_BUFFER_LEN]; // Array with subframe energies
82 int32_t Rxx16w32_array[2][5]; // Energy values of microphone signal
83 int32_t env[2][10]; // Envelope values of subframes
niklase@google.com470e71d2011-07-07 08:21:25 +000084
minyuecac94aa2016-05-20 08:42:22 -070085 int16_t Rxx16pos; // Current position in the Rxx16_vectorw32
86 int16_t envSum; // Filtered scaled envelope in subframes
87 int16_t vadThreshold; // Threshold for VAD decision
88 int16_t inActive; // Inactive time in milliseconds
89 int16_t msTooLow; // Milliseconds of speech at a too low level
90 int16_t msTooHigh; // Milliseconds of speech at a too high level
91 int16_t changeToSlowMode; // Change to slow mode after some time at target
92 int16_t firstCall; // First call to the process-function
93 int16_t msZero; // Milliseconds of zero input
94 int16_t msecSpeechOuterChange; // Min ms of speech between volume changes
95 int16_t msecSpeechInnerChange; // Min ms of speech between volume changes
96 int16_t activeSpeech; // Milliseconds of active speech
97 int16_t muteGuardMs; // Counter to prevent mute action
98 int16_t inQueue; // 10 ms batch indicator
niklase@google.com470e71d2011-07-07 08:21:25 +000099
minyuecac94aa2016-05-20 08:42:22 -0700100 // Microphone level variables
101 int32_t micRef; // Remember ref. mic level for virtual mic
102 uint16_t gainTableIdx; // Current position in virtual gain table
103 int32_t micGainIdx; // Gain index of mic level to increase slowly
104 int32_t micVol; // Remember volume between frames
105 int32_t maxLevel; // Max possible vol level, incl dig gain
106 int32_t maxAnalog; // Maximum possible analog volume level
107 int32_t maxInit; // Initial value of "max"
108 int32_t minLevel; // Minimum possible volume level
109 int32_t minOutput; // Minimum output volume level
110 int32_t zeroCtrlMax; // Remember max gain => don't amp low input
111 int32_t lastInMicLevel;
niklase@google.com470e71d2011-07-07 08:21:25 +0000112
minyuecac94aa2016-05-20 08:42:22 -0700113 int16_t scale; // Scale factor for internal volume levels
niklase@google.com470e71d2011-07-07 08:21:25 +0000114#ifdef MIC_LEVEL_FEEDBACK
minyuecac94aa2016-05-20 08:42:22 -0700115 int16_t numBlocksMicLvlSat;
116 uint8_t micLvlSat;
niklase@google.com470e71d2011-07-07 08:21:25 +0000117#endif
minyuecac94aa2016-05-20 08:42:22 -0700118 // Structs for VAD and digital_agc
119 AgcVad vadMic;
120 DigitalAgc digitalAgc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000121
bjornv@webrtc.orgea297872014-09-23 11:21:39 +0000122#ifdef WEBRTC_AGC_DEBUG_DUMP
minyuecac94aa2016-05-20 08:42:22 -0700123 FILE* fpt;
124 FILE* agcLog;
125 int32_t fcount;
niklase@google.com470e71d2011-07-07 08:21:25 +0000126#endif
127
minyuecac94aa2016-05-20 08:42:22 -0700128 int16_t lowLevelSignal;
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000129} LegacyAgc;
niklase@google.com470e71d2011-07-07 08:21:25 +0000130
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200131#endif // MODULES_AUDIO_PROCESSING_AGC_LEGACY_ANALOG_AGC_H_