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 | 2fa4618 | 2021-10-26 14:08:23 +0200 | [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" |
| 21 | #include "modules/audio_processing/agc2/limiter.h" |
Alessio Bazzica | b4d4ae2 | 2021-10-15 13:57:56 +0200 | [diff] [blame] | 22 | #include "modules/audio_processing/agc2/vad_wrapper.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 23 | #include "modules/audio_processing/include/audio_processing.h" |
Alessio Bazzica | 8aaa604 | 2021-03-31 15:16:05 +0200 | [diff] [blame] | 24 | #include "modules/audio_processing/logging/apm_data_dumper.h" |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 25 | |
| 26 | namespace webrtc { |
| 27 | |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 28 | class AudioBuffer; |
| 29 | |
| 30 | // Gain Controller 2 aims to automatically adjust levels by acting on the |
| 31 | // microphone gain and/or applying digital gain. |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 32 | class GainController2 { |
| 33 | public: |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame] | 34 | // Ctor. If `use_internal_vad` is true, an internal voice activity |
| 35 | // detector is used for digital adaptive gain. |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 36 | GainController2(const AudioProcessing::Config::GainController2& config, |
| 37 | int sample_rate_hz, |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame] | 38 | int num_channels, |
| 39 | bool use_internal_vad); |
Alessio Bazzica | 8aaa604 | 2021-03-31 15:16:05 +0200 | [diff] [blame] | 40 | GainController2(const GainController2&) = delete; |
| 41 | GainController2& operator=(const GainController2&) = delete; |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 42 | ~GainController2(); |
| 43 | |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 44 | // Detects and handles changes of sample rate and/or number of channels. |
Alessio Bazzica | d66a605 | 2021-04-29 16:13:25 +0200 | [diff] [blame] | 45 | void Initialize(int sample_rate_hz, int num_channels); |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 46 | |
| 47 | // Sets the fixed digital gain. |
| 48 | void SetFixedGainDb(float gain_db); |
| 49 | |
| 50 | // Applies fixed and adaptive digital gains to `audio` and runs a limiter. |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame] | 51 | // If the internal VAD is used, `speech_probability` is ignored. Otherwise |
| 52 | // `speech_probability` is used for digital adaptive gain if it's available |
| 53 | // (limited to values [0.0, 1.0]). |
| 54 | void Process(absl::optional<float> speech_probability, AudioBuffer* audio); |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 55 | |
| 56 | // Handles analog level changes. |
Alex Loiko | a837dd7 | 2018-08-06 16:32:12 +0200 | [diff] [blame] | 57 | void NotifyAnalogLevel(int level); |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 58 | |
| 59 | static bool Validate(const AudioProcessing::Config::GainController2& config); |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 60 | |
Hanna Silen | 0c1ad29 | 2022-06-16 16:35:45 +0200 | [diff] [blame] | 61 | AvailableCpuFeatures GetCpuFeatures() const { return cpu_features_; } |
| 62 | |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 63 | private: |
Niels Möller | 7a66900 | 2022-06-27 09:47:02 +0200 | [diff] [blame^] | 64 | static std::atomic<int> instance_count_; |
Alessio Bazzica | b4d4ae2 | 2021-10-15 13:57:56 +0200 | [diff] [blame] | 65 | const AvailableCpuFeatures cpu_features_; |
Alessio Bazzica | 8aaa604 | 2021-03-31 15:16:05 +0200 | [diff] [blame] | 66 | ApmDataDumper data_dumper_; |
Alessio Bazzica | 82ea4ee | 2021-10-07 09:21:02 +0200 | [diff] [blame] | 67 | GainApplier fixed_gain_applier_; |
Alessio Bazzica | b4d4ae2 | 2021-10-15 13:57:56 +0200 | [diff] [blame] | 68 | std::unique_ptr<VoiceActivityDetectorWrapper> vad_; |
Alessio Bazzica | 2fa4618 | 2021-10-26 14:08:23 +0200 | [diff] [blame] | 69 | std::unique_ptr<AdaptiveDigitalGainController> adaptive_digital_controller_; |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 70 | Limiter limiter_; |
Alessio Bazzica | 08d2a70 | 2020-11-20 16:26:24 +0100 | [diff] [blame] | 71 | int calls_since_last_limiter_log_; |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 72 | int analog_level_; |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | } // namespace webrtc |
| 76 | |
Alex Loiko | e36e8bb | 2018-02-16 11:54:07 +0100 | [diff] [blame] | 77 | #endif // MODULES_AUDIO_PROCESSING_GAIN_CONTROLLER2_H_ |