niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_AUDIO_PROCESSING_AGC_LEGACY_ANALOG_AGC_H_ |
| 12 | #define MODULES_AUDIO_PROCESSING_AGC_LEGACY_ANALOG_AGC_H_ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "modules/audio_processing/agc/legacy/digital_agc.h" |
| 16 | #include "modules/audio_processing/agc/legacy/gain_control.h" |
bjornv@webrtc.org | b395a5e | 2014-12-16 10:38:10 +0000 | [diff] [blame] | 17 | |
Per Åhgren | 5b139d6 | 2020-03-20 15:50:14 +0100 | [diff] [blame] | 18 | namespace webrtc { |
| 19 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | /* Analog Automatic Gain Control variables: |
| 21 | * Constant declarations (inner limits inside which no changes are done) |
| 22 | * In the beginning the range is narrower to widen as soon as the measure |
| 23 | * 'Rxx160_LP' is inside it. Currently the starting limits are -22.2+/-1dBm0 |
| 24 | * and the final limits -22.2+/-2.5dBm0. These levels makes the speech signal |
| 25 | * go towards -25.4dBm0 (-31.4dBov). Tuned with wbfile-31.4dBov.pcm |
| 26 | * The limits are created by running the AGC with a file having the desired |
| 27 | * signal level and thereafter plotting Rxx160_LP in the dBm0-domain defined |
| 28 | * by out=10*log10(in/260537279.7); Set the target level to the average level |
| 29 | * of our measure Rxx160_LP. Remember that the levels are in blocks of 16 in |
| 30 | * Q(-7). (Example matlab code: round(db2pow(-21.2)*16/2^7) ) |
| 31 | */ |
Per Åhgren | 5b139d6 | 2020-03-20 15:50:14 +0100 | [diff] [blame] | 32 | constexpr int16_t kRxxBufferLen = 10; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 33 | |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 34 | static const int16_t kMsecSpeechInner = 520; |
| 35 | static const int16_t kMsecSpeechOuter = 340; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 36 | |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 37 | static const int16_t kNormalVadThreshold = 400; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 38 | |
minyue | cac94aa | 2016-05-20 08:42:22 -0700 | [diff] [blame] | 39 | static const int16_t kAlphaShortTerm = 6; // 1 >> 6 = 0.0156 |
| 40 | static const int16_t kAlphaLongTerm = 10; // 1 >> 10 = 0.000977 |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 41 | |
minyue | cac94aa | 2016-05-20 08:42:22 -0700 | [diff] [blame] | 42 | typedef struct { |
| 43 | // Configurable parameters/variables |
| 44 | uint32_t fs; // Sampling frequency |
| 45 | int16_t compressionGaindB; // Fixed gain level in dB |
| 46 | int16_t targetLevelDbfs; // Target level in -dBfs of envelope (default -3) |
| 47 | int16_t agcMode; // Hard coded mode (adaptAna/adaptDig/fixedDig) |
| 48 | uint8_t limiterEnable; // Enabling limiter (on/off (default off)) |
| 49 | WebRtcAgcConfig defaultConfig; |
| 50 | WebRtcAgcConfig usedConfig; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 51 | |
minyue | cac94aa | 2016-05-20 08:42:22 -0700 | [diff] [blame] | 52 | // General variables |
| 53 | int16_t initFlag; |
| 54 | int16_t lastError; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 55 | |
minyue | cac94aa | 2016-05-20 08:42:22 -0700 | [diff] [blame] | 56 | // Target level parameters |
| 57 | // Based on the above: analogTargetLevel = round((32767*10^(-22/20))^2*16/2^7) |
Per Åhgren | 5b139d6 | 2020-03-20 15:50:14 +0100 | [diff] [blame] | 58 | int32_t analogTargetLevel; // = kRxxBufferLen * 846805; -22 dBfs |
| 59 | int32_t startUpperLimit; // = kRxxBufferLen * 1066064; -21 dBfs |
| 60 | int32_t startLowerLimit; // = kRxxBufferLen * 672641; -23 dBfs |
| 61 | int32_t upperPrimaryLimit; // = kRxxBufferLen * 1342095; -20 dBfs |
| 62 | int32_t lowerPrimaryLimit; // = kRxxBufferLen * 534298; -24 dBfs |
| 63 | int32_t upperSecondaryLimit; // = kRxxBufferLen * 2677832; -17 dBfs |
| 64 | int32_t lowerSecondaryLimit; // = kRxxBufferLen * 267783; -27 dBfs |
minyue | cac94aa | 2016-05-20 08:42:22 -0700 | [diff] [blame] | 65 | uint16_t targetIdx; // Table index for corresponding target level |
minyue | cac94aa | 2016-05-20 08:42:22 -0700 | [diff] [blame] | 66 | int16_t analogTarget; // Digital reference level in ENV scale |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 67 | |
minyue | cac94aa | 2016-05-20 08:42:22 -0700 | [diff] [blame] | 68 | // Analog AGC specific variables |
| 69 | int32_t filterState[8]; // For downsampling wb to nb |
| 70 | int32_t upperLimit; // Upper limit for mic energy |
| 71 | int32_t lowerLimit; // Lower limit for mic energy |
| 72 | int32_t Rxx160w32; // Average energy for one frame |
| 73 | int32_t Rxx16_LPw32; // Low pass filtered subframe energies |
| 74 | int32_t Rxx160_LPw32; // Low pass filtered frame energies |
| 75 | int32_t Rxx16_LPw32Max; // Keeps track of largest energy subframe |
Per Åhgren | 5b139d6 | 2020-03-20 15:50:14 +0100 | [diff] [blame] | 76 | int32_t Rxx16_vectorw32[kRxxBufferLen]; // Array with subframe energies |
minyue | cac94aa | 2016-05-20 08:42:22 -0700 | [diff] [blame] | 77 | int32_t Rxx16w32_array[2][5]; // Energy values of microphone signal |
| 78 | int32_t env[2][10]; // Envelope values of subframes |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 79 | |
minyue | cac94aa | 2016-05-20 08:42:22 -0700 | [diff] [blame] | 80 | int16_t Rxx16pos; // Current position in the Rxx16_vectorw32 |
| 81 | int16_t envSum; // Filtered scaled envelope in subframes |
| 82 | int16_t vadThreshold; // Threshold for VAD decision |
| 83 | int16_t inActive; // Inactive time in milliseconds |
| 84 | int16_t msTooLow; // Milliseconds of speech at a too low level |
| 85 | int16_t msTooHigh; // Milliseconds of speech at a too high level |
| 86 | int16_t changeToSlowMode; // Change to slow mode after some time at target |
| 87 | int16_t firstCall; // First call to the process-function |
| 88 | int16_t msZero; // Milliseconds of zero input |
| 89 | int16_t msecSpeechOuterChange; // Min ms of speech between volume changes |
| 90 | int16_t msecSpeechInnerChange; // Min ms of speech between volume changes |
| 91 | int16_t activeSpeech; // Milliseconds of active speech |
| 92 | int16_t muteGuardMs; // Counter to prevent mute action |
| 93 | int16_t inQueue; // 10 ms batch indicator |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 94 | |
minyue | cac94aa | 2016-05-20 08:42:22 -0700 | [diff] [blame] | 95 | // Microphone level variables |
| 96 | int32_t micRef; // Remember ref. mic level for virtual mic |
| 97 | uint16_t gainTableIdx; // Current position in virtual gain table |
| 98 | int32_t micGainIdx; // Gain index of mic level to increase slowly |
| 99 | int32_t micVol; // Remember volume between frames |
| 100 | int32_t maxLevel; // Max possible vol level, incl dig gain |
| 101 | int32_t maxAnalog; // Maximum possible analog volume level |
| 102 | int32_t maxInit; // Initial value of "max" |
| 103 | int32_t minLevel; // Minimum possible volume level |
| 104 | int32_t minOutput; // Minimum output volume level |
| 105 | int32_t zeroCtrlMax; // Remember max gain => don't amp low input |
| 106 | int32_t lastInMicLevel; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 107 | |
minyue | cac94aa | 2016-05-20 08:42:22 -0700 | [diff] [blame] | 108 | int16_t scale; // Scale factor for internal volume levels |
minyue | cac94aa | 2016-05-20 08:42:22 -0700 | [diff] [blame] | 109 | // Structs for VAD and digital_agc |
| 110 | AgcVad vadMic; |
| 111 | DigitalAgc digitalAgc; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 112 | |
minyue | cac94aa | 2016-05-20 08:42:22 -0700 | [diff] [blame] | 113 | int16_t lowLevelSignal; |
pbos@webrtc.org | e468bc9 | 2014-12-18 09:11:33 +0000 | [diff] [blame] | 114 | } LegacyAgc; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 115 | |
Per Åhgren | 5b139d6 | 2020-03-20 15:50:14 +0100 | [diff] [blame] | 116 | } // namespace webrtc |
| 117 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 118 | #endif // MODULES_AUDIO_PROCESSING_AGC_LEGACY_ANALOG_AGC_H_ |