gain_controller2.h (4697B)
1 /* 2 * Copyright (c) 2017 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 11 #ifndef MODULES_AUDIO_PROCESSING_GAIN_CONTROLLER2_H_ 12 #define MODULES_AUDIO_PROCESSING_GAIN_CONTROLLER2_H_ 13 14 #include <atomic> 15 #include <memory> 16 #include <optional> 17 18 #include "api/audio/audio_processing.h" 19 #include "api/environment/environment.h" 20 #include "modules/audio_processing/agc2/adaptive_digital_gain_controller.h" 21 #include "modules/audio_processing/agc2/cpu_features.h" 22 #include "modules/audio_processing/agc2/gain_applier.h" 23 #include "modules/audio_processing/agc2/input_volume_controller.h" 24 #include "modules/audio_processing/agc2/limiter.h" 25 #include "modules/audio_processing/agc2/noise_level_estimator.h" 26 #include "modules/audio_processing/agc2/saturation_protector.h" 27 #include "modules/audio_processing/agc2/speech_level_estimator.h" 28 #include "modules/audio_processing/agc2/vad_wrapper.h" 29 #include "modules/audio_processing/logging/apm_data_dumper.h" 30 31 namespace webrtc { 32 33 class AudioBuffer; 34 35 // Gain Controller 2 aims to automatically adjust levels by acting on the 36 // microphone gain and/or applying digital gain. 37 class GainController2 { 38 public: 39 // Ctor. If `use_internal_vad` is true, an internal voice activity 40 // detector is used for digital adaptive gain. 41 GainController2( 42 const Environment& env, 43 const AudioProcessing::Config::GainController2& config, 44 const InputVolumeController::Config& input_volume_controller_config, 45 int sample_rate_hz, 46 int num_channels, 47 bool use_internal_vad); 48 GainController2(const GainController2&) = delete; 49 GainController2& operator=(const GainController2&) = delete; 50 ~GainController2(); 51 52 // Sets the fixed digital gain. 53 void SetFixedGainDb(float gain_db); 54 55 // Updates the input volume controller about whether the capture output is 56 // used or not. 57 void SetCaptureOutputUsed(bool capture_output_used); 58 59 // Analyzes `audio_buffer` before `Process()` is called so that the analysis 60 // can be performed before digital processing operations take place (e.g., 61 // echo cancellation). The analysis consists of input clipping detection and 62 // prediction (if enabled). The value of `applied_input_volume` is limited to 63 // [0, 255]. 64 void Analyze(int applied_input_volume, const AudioBuffer& audio_buffer); 65 66 // Updates the recommended input volume, applies the adaptive digital and the 67 // fixed digital gains and runs a limiter on `audio`. 68 // When the internal VAD is not used, `speech_probability` should be specified 69 // and in the [0, 1] range. Otherwise ignores `speech_probability` and 70 // computes the speech probability via `vad_`. 71 // Handles input volume changes; if the caller cannot determine whether an 72 // input volume change occurred, set `input_volume_changed` to false. 73 // TODO(bugs.webrtc.org/7494): Remove `speech_probability`. 74 void Process(std::optional<float> speech_probability, 75 bool input_volume_changed, 76 AudioBuffer* audio); 77 78 static bool Validate(const AudioProcessing::Config::GainController2& config); 79 80 AvailableCpuFeatures GetCpuFeatures() const { return cpu_features_; } 81 82 std::optional<int> recommended_input_volume() const { 83 return recommended_input_volume_; 84 } 85 86 private: 87 static std::atomic<int> instance_count_; 88 const AvailableCpuFeatures cpu_features_; 89 ApmDataDumper data_dumper_; 90 91 GainApplier fixed_gain_applier_; 92 std::unique_ptr<NoiseLevelEstimator> noise_level_estimator_; 93 std::unique_ptr<VoiceActivityDetectorWrapper> vad_; 94 std::unique_ptr<SpeechLevelEstimator> speech_level_estimator_; 95 std::unique_ptr<InputVolumeController> input_volume_controller_; 96 // TODO(bugs.webrtc.org/7494): Rename to `CrestFactorEstimator`. 97 std::unique_ptr<SaturationProtector> saturation_protector_; 98 std::unique_ptr<AdaptiveDigitalGainController> adaptive_digital_controller_; 99 Limiter limiter_; 100 101 int calls_since_last_limiter_log_; 102 103 // TODO(bugs.webrtc.org/7494): Remove intermediate storing at this level once 104 // APM refactoring is completed. 105 // Recommended input volume from `InputVolumecontroller`. Non-empty after 106 // `Process()` if input volume controller is enabled and 107 // `InputVolumeController::Process()` has returned a non-empty value. 108 std::optional<int> recommended_input_volume_; 109 }; 110 111 } // namespace webrtc 112 113 #endif // MODULES_AUDIO_PROCESSING_GAIN_CONTROLLER2_H_