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