analog_agc.h 5.6 KB

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