agc_manager_direct.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (c) 2013 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_AGC_MANAGER_DIRECT_H_
  11. #define MODULES_AUDIO_PROCESSING_AGC_AGC_MANAGER_DIRECT_H_
  12. #include <memory>
  13. #include "absl/types/optional.h"
  14. #include "modules/audio_processing/agc/agc.h"
  15. #include "modules/audio_processing/audio_buffer.h"
  16. #include "modules/audio_processing/logging/apm_data_dumper.h"
  17. #include "rtc_base/gtest_prod_util.h"
  18. namespace webrtc {
  19. class MonoAgc;
  20. class GainControl;
  21. // Direct interface to use AGC to set volume and compression values.
  22. // AudioProcessing uses this interface directly to integrate the callback-less
  23. // AGC.
  24. //
  25. // This class is not thread-safe.
  26. class AgcManagerDirect final {
  27. public:
  28. // AgcManagerDirect will configure GainControl internally. The user is
  29. // responsible for processing the audio using it after the call to Process.
  30. // The operating range of startup_min_level is [12, 255] and any input value
  31. // outside that range will be clamped.
  32. AgcManagerDirect(int num_capture_channels,
  33. int startup_min_level,
  34. int clipped_level_min,
  35. bool use_agc2_level_estimation,
  36. bool disable_digital_adaptive,
  37. int sample_rate_hz);
  38. ~AgcManagerDirect();
  39. AgcManagerDirect(const AgcManagerDirect&) = delete;
  40. AgcManagerDirect& operator=(const AgcManagerDirect&) = delete;
  41. void Initialize();
  42. void SetupDigitalGainControl(GainControl* gain_control) const;
  43. void AnalyzePreProcess(const AudioBuffer* audio);
  44. void Process(const AudioBuffer* audio);
  45. // Call when the capture stream has been muted/unmuted. This causes the
  46. // manager to disregard all incoming audio; chances are good it's background
  47. // noise to which we'd like to avoid adapting.
  48. void SetCaptureMuted(bool muted);
  49. float voice_probability() const;
  50. int stream_analog_level() const { return stream_analog_level_; }
  51. void set_stream_analog_level(int level);
  52. int num_channels() const { return num_capture_channels_; }
  53. int sample_rate_hz() const { return sample_rate_hz_; }
  54. // If available, returns a new compression gain for the digital gain control.
  55. absl::optional<int> GetDigitalComressionGain();
  56. private:
  57. friend class AgcManagerDirectTest;
  58. FRIEND_TEST_ALL_PREFIXES(AgcManagerDirectStandaloneTest,
  59. DisableDigitalDisablesDigital);
  60. FRIEND_TEST_ALL_PREFIXES(AgcManagerDirectStandaloneTest,
  61. AgcMinMicLevelExperiment);
  62. // Dependency injection for testing. Don't delete |agc| as the memory is owned
  63. // by the manager.
  64. AgcManagerDirect(Agc* agc,
  65. int startup_min_level,
  66. int clipped_level_min,
  67. int sample_rate_hz);
  68. void AnalyzePreProcess(const float* const* audio, size_t samples_per_channel);
  69. void AggregateChannelLevels();
  70. std::unique_ptr<ApmDataDumper> data_dumper_;
  71. static int instance_counter_;
  72. const bool use_min_channel_level_;
  73. const int sample_rate_hz_;
  74. const int num_capture_channels_;
  75. const bool disable_digital_adaptive_;
  76. int frames_since_clipped_;
  77. int stream_analog_level_ = 0;
  78. bool capture_muted_;
  79. int channel_controlling_gain_ = 0;
  80. std::vector<std::unique_ptr<MonoAgc>> channel_agcs_;
  81. std::vector<absl::optional<int>> new_compressions_to_set_;
  82. };
  83. class MonoAgc {
  84. public:
  85. MonoAgc(ApmDataDumper* data_dumper,
  86. int startup_min_level,
  87. int clipped_level_min,
  88. bool use_agc2_level_estimation,
  89. bool disable_digital_adaptive,
  90. int min_mic_level);
  91. ~MonoAgc();
  92. MonoAgc(const MonoAgc&) = delete;
  93. MonoAgc& operator=(const MonoAgc&) = delete;
  94. void Initialize();
  95. void SetCaptureMuted(bool muted);
  96. void HandleClipping();
  97. void Process(const int16_t* audio,
  98. size_t samples_per_channel,
  99. int sample_rate_hz);
  100. void set_stream_analog_level(int level) { stream_analog_level_ = level; }
  101. int stream_analog_level() const { return stream_analog_level_; }
  102. float voice_probability() const { return agc_->voice_probability(); }
  103. void ActivateLogging() { log_to_histograms_ = true; }
  104. absl::optional<int> new_compression() const {
  105. return new_compression_to_set_;
  106. }
  107. // Only used for testing.
  108. void set_agc(Agc* agc) { agc_.reset(agc); }
  109. int min_mic_level() const { return min_mic_level_; }
  110. int startup_min_level() const { return startup_min_level_; }
  111. private:
  112. // Sets a new microphone level, after first checking that it hasn't been
  113. // updated by the user, in which case no action is taken.
  114. void SetLevel(int new_level);
  115. // Set the maximum level the AGC is allowed to apply. Also updates the
  116. // maximum compression gain to compensate. The level must be at least
  117. // |kClippedLevelMin|.
  118. void SetMaxLevel(int level);
  119. int CheckVolumeAndReset();
  120. void UpdateGain();
  121. void UpdateCompressor();
  122. const int min_mic_level_;
  123. const bool disable_digital_adaptive_;
  124. std::unique_ptr<Agc> agc_;
  125. int level_ = 0;
  126. int max_level_;
  127. int max_compression_gain_;
  128. int target_compression_;
  129. int compression_;
  130. float compression_accumulator_;
  131. bool capture_muted_ = false;
  132. bool check_volume_on_next_process_ = true;
  133. bool startup_ = true;
  134. int startup_min_level_;
  135. int calls_since_last_gain_log_ = 0;
  136. int stream_analog_level_ = 0;
  137. absl::optional<int> new_compression_to_set_;
  138. bool log_to_histograms_ = false;
  139. const int clipped_level_min_;
  140. };
  141. } // namespace webrtc
  142. #endif // MODULES_AUDIO_PROCESSING_AGC_AGC_MANAGER_DIRECT_H_