gain_control.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) 2018 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_GAIN_CONTROL_H_
  11. #define MODULES_AUDIO_PROCESSING_AGC_GAIN_CONTROL_H_
  12. namespace webrtc {
  13. // The automatic gain control (AGC) component brings the signal to an
  14. // appropriate range. This is done by applying a digital gain directly and, in
  15. // the analog mode, prescribing an analog gain to be applied at the audio HAL.
  16. //
  17. // Recommended to be enabled on the client-side.
  18. class GainControl {
  19. public:
  20. // When an analog mode is set, this must be called prior to |ProcessStream()|
  21. // to pass the current analog level from the audio HAL. Must be within the
  22. // range provided to |set_analog_level_limits()|.
  23. virtual int set_stream_analog_level(int level) = 0;
  24. // When an analog mode is set, this should be called after |ProcessStream()|
  25. // to obtain the recommended new analog level for the audio HAL. It is the
  26. // users responsibility to apply this level.
  27. virtual int stream_analog_level() const = 0;
  28. enum Mode {
  29. // Adaptive mode intended for use if an analog volume control is available
  30. // on the capture device. It will require the user to provide coupling
  31. // between the OS mixer controls and AGC through the |stream_analog_level()|
  32. // functions.
  33. //
  34. // It consists of an analog gain prescription for the audio device and a
  35. // digital compression stage.
  36. kAdaptiveAnalog,
  37. // Adaptive mode intended for situations in which an analog volume control
  38. // is unavailable. It operates in a similar fashion to the adaptive analog
  39. // mode, but with scaling instead applied in the digital domain. As with
  40. // the analog mode, it additionally uses a digital compression stage.
  41. kAdaptiveDigital,
  42. // Fixed mode which enables only the digital compression stage also used by
  43. // the two adaptive modes.
  44. //
  45. // It is distinguished from the adaptive modes by considering only a
  46. // short time-window of the input signal. It applies a fixed gain through
  47. // most of the input level range, and compresses (gradually reduces gain
  48. // with increasing level) the input signal at higher levels. This mode is
  49. // preferred on embedded devices where the capture signal level is
  50. // predictable, so that a known gain can be applied.
  51. kFixedDigital
  52. };
  53. virtual int set_mode(Mode mode) = 0;
  54. virtual Mode mode() const = 0;
  55. // Sets the target peak |level| (or envelope) of the AGC in dBFs (decibels
  56. // from digital full-scale). The convention is to use positive values. For
  57. // instance, passing in a value of 3 corresponds to -3 dBFs, or a target
  58. // level 3 dB below full-scale. Limited to [0, 31].
  59. //
  60. // TODO(ajm): use a negative value here instead, if/when VoE will similarly
  61. // update its interface.
  62. virtual int set_target_level_dbfs(int level) = 0;
  63. virtual int target_level_dbfs() const = 0;
  64. // Sets the maximum |gain| the digital compression stage may apply, in dB. A
  65. // higher number corresponds to greater compression, while a value of 0 will
  66. // leave the signal uncompressed. Limited to [0, 90].
  67. virtual int set_compression_gain_db(int gain) = 0;
  68. virtual int compression_gain_db() const = 0;
  69. // When enabled, the compression stage will hard limit the signal to the
  70. // target level. Otherwise, the signal will be compressed but not limited
  71. // above the target level.
  72. virtual int enable_limiter(bool enable) = 0;
  73. virtual bool is_limiter_enabled() const = 0;
  74. // Sets the |minimum| and |maximum| analog levels of the audio capture device.
  75. // Must be set if and only if an analog mode is used. Limited to [0, 65535].
  76. virtual int set_analog_level_limits(int minimum, int maximum) = 0;
  77. virtual int analog_level_minimum() const = 0;
  78. virtual int analog_level_maximum() const = 0;
  79. // Returns true if the AGC has detected a saturation event (period where the
  80. // signal reaches digital full-scale) in the current frame and the analog
  81. // level cannot be reduced.
  82. //
  83. // This could be used as an indicator to reduce or disable analog mic gain at
  84. // the audio HAL.
  85. virtual bool stream_is_saturated() const = 0;
  86. protected:
  87. virtual ~GainControl() {}
  88. };
  89. } // namespace webrtc
  90. #endif // MODULES_AUDIO_PROCESSING_AGC_GAIN_CONTROL_H_