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 <string> 17 18 #include "modules/audio_processing/agc2/adaptive_digital_gain_controller.h" 19 #include "modules/audio_processing/agc2/cpu_features.h" 20 #include "modules/audio_processing/agc2/gain_applier.h" 21 #include "modules/audio_processing/agc2/input_volume_controller.h" 22 #include "modules/audio_processing/agc2/limiter.h" 23 #include "modules/audio_processing/agc2/vad_wrapper.h" 24 #include "modules/audio_processing/include/audio_processing.h" 25 #include "modules/audio_processing/logging/apm_data_dumper.h" 26 27 namespace webrtc { 28 29 class AudioBuffer; 30 31 // Gain Controller 2 aims to automatically adjust levels by acting on the 32 // microphone gain and/or applying digital gain. 33 class GainController2 { 34 public: 35 // Ctor. If `use_internal_vad` is true, an internal voice activity 36 // detector is used for digital adaptive gain. 37 GainController2( 38 const AudioProcessing::Config::GainController2& config, 39 const InputVolumeController::Config& input_volume_controller_config, 40 int sample_rate_hz, 41 int num_channels, 42 bool use_internal_vad); 43 GainController2(const GainController2&) = delete; 44 GainController2& operator=(const GainController2&) = delete; 45 ~GainController2(); 46 47 // Sets the fixed digital gain. 48 void SetFixedGainDb(float gain_db); 49 50 // Updates the input volume controller about whether the capture output is 51 // used or not. 52 void SetCaptureOutputUsed(bool capture_output_used); 53 54 // Analyzes `audio_buffer` before `Process()` is called so that the analysis 55 // can be performed before digital processing operations take place (e.g., 56 // echo cancellation). The analysis consists of input clipping detection and 57 // prediction (if enabled). The value of `applied_input_volume` is limited to 58 // [0, 255]. 59 void Analyze(int applied_input_volume, const AudioBuffer& audio_buffer); 60 61 // Applies fixed and adaptive digital gains to `audio` and runs a limiter. 62 // If the internal VAD is used, `speech_probability` is ignored. Otherwise 63 // `speech_probability` is used for digital adaptive gain if it's available 64 // (limited to values [0.0, 1.0]). Handles input volume changes; if the caller 65 // cannot determine whether an input volume change occurred, set 66 // `input_volume_changed` to false. 67 void Process(absl::optional<float> speech_probability, 68 bool input_volume_changed, 69 AudioBuffer* audio); 70 71 static bool Validate(const AudioProcessing::Config::GainController2& config); 72 GetCpuFeatures()73 AvailableCpuFeatures GetCpuFeatures() const { return cpu_features_; } 74 75 // Returns the recommended input volume if input volume controller is enabled 76 // and if a volume recommendation is available. 77 absl::optional<int> GetRecommendedInputVolume() const; 78 79 private: 80 static std::atomic<int> instance_count_; 81 const AvailableCpuFeatures cpu_features_; 82 ApmDataDumper data_dumper_; 83 GainApplier fixed_gain_applier_; 84 std::unique_ptr<VoiceActivityDetectorWrapper> vad_; 85 std::unique_ptr<AdaptiveDigitalGainController> adaptive_digital_controller_; 86 std::unique_ptr<InputVolumeController> input_volume_controller_; 87 Limiter limiter_; 88 int calls_since_last_limiter_log_; 89 }; 90 91 } // namespace webrtc 92 93 #endif // MODULES_AUDIO_PROCESSING_GAIN_CONTROLLER2_H_ 94