blob: 43b5828d35386847bea5902049d6ad600ef6a32e [file] [log] [blame]
alessiob3ec96df2017-05-22 06:57:06 -07001/*
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 Loikoe36e8bb2018-02-16 11:54:07 +010011#ifndef MODULES_AUDIO_PROCESSING_GAIN_CONTROLLER2_H_
12#define MODULES_AUDIO_PROCESSING_GAIN_CONTROLLER2_H_
alessiob3ec96df2017-05-22 06:57:06 -070013
Niels Möller7a669002022-06-27 09:47:02 +020014#include <atomic>
alessiob3ec96df2017-05-22 06:57:06 -070015#include <memory>
16#include <string>
17
Alessio Bazzicaf72bc5f2022-12-09 08:46:06 +010018#include "modules/audio_processing/agc2/adaptive_digital_gain_controller.h"
Alessio Bazzicab4d4ae22021-10-15 13:57:56 +020019#include "modules/audio_processing/agc2/cpu_features.h"
Alessio Bazzica3e4c77f2018-11-01 21:31:38 +010020#include "modules/audio_processing/agc2/gain_applier.h"
Hanna Silend7cfbe32022-11-02 19:12:20 +010021#include "modules/audio_processing/agc2/input_volume_controller.h"
Alessio Bazzica3e4c77f2018-11-01 21:31:38 +010022#include "modules/audio_processing/agc2/limiter.h"
Alessio Bazzica17e14fd2022-12-07 17:08:45 +010023#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 Bazzicab4d4ae22021-10-15 13:57:56 +020026#include "modules/audio_processing/agc2/vad_wrapper.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "modules/audio_processing/include/audio_processing.h"
Alessio Bazzica8aaa6042021-03-31 15:16:05 +020028#include "modules/audio_processing/logging/apm_data_dumper.h"
alessiob3ec96df2017-05-22 06:57:06 -070029
30namespace webrtc {
31
alessiob3ec96df2017-05-22 06:57:06 -070032class AudioBuffer;
33
34// Gain Controller 2 aims to automatically adjust levels by acting on the
35// microphone gain and/or applying digital gain.
alessiob3ec96df2017-05-22 06:57:06 -070036class GainController2 {
37 public:
Hanna Silen0c1ad292022-06-16 16:35:45 +020038 // Ctor. If `use_internal_vad` is true, an internal voice activity
39 // detector is used for digital adaptive gain.
Hanna Silena6574902022-11-30 16:59:05 +010040 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 Bazzica8aaa6042021-03-31 15:16:05 +020046 GainController2(const GainController2&) = delete;
47 GainController2& operator=(const GainController2&) = delete;
alessiob3ec96df2017-05-22 06:57:06 -070048 ~GainController2();
49
Alessio Bazzica38901042021-10-14 12:14:21 +020050 // Sets the fixed digital gain.
51 void SetFixedGainDb(float gain_db);
52
Hanna Silend7cfbe32022-11-02 19:12:20 +010053 // 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 Bazzica17e14fd2022-12-07 17:08:45 +010064 // 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 Bazzicafcf1af32022-09-07 17:14:26 +020071 void Process(absl::optional<float> speech_probability,
72 bool input_volume_changed,
73 AudioBuffer* audio);
alessiob3ec96df2017-05-22 06:57:06 -070074
75 static bool Validate(const AudioProcessing::Config::GainController2& config);
alessiob3ec96df2017-05-22 06:57:06 -070076
Hanna Silen0c1ad292022-06-16 16:35:45 +020077 AvailableCpuFeatures GetCpuFeatures() const { return cpu_features_; }
78
Hanna Silen597a2ba2022-12-14 12:48:37 +010079 absl::optional<int> recommended_input_volume() const {
80 return recommended_input_volume_;
81 }
Hanna Silend7cfbe32022-11-02 19:12:20 +010082
alessiob3ec96df2017-05-22 06:57:06 -070083 private:
Niels Möller7a669002022-06-27 09:47:02 +020084 static std::atomic<int> instance_count_;
Alessio Bazzicab4d4ae22021-10-15 13:57:56 +020085 const AvailableCpuFeatures cpu_features_;
Alessio Bazzica8aaa6042021-03-31 15:16:05 +020086 ApmDataDumper data_dumper_;
Alessio Bazzica17e14fd2022-12-07 17:08:45 +010087
Alessio Bazzica82ea4ee2021-10-07 09:21:02 +020088 GainApplier fixed_gain_applier_;
Alessio Bazzica17e14fd2022-12-07 17:08:45 +010089 std::unique_ptr<NoiseLevelEstimator> noise_level_estimator_;
Alessio Bazzicab4d4ae22021-10-15 13:57:56 +020090 std::unique_ptr<VoiceActivityDetectorWrapper> vad_;
Alessio Bazzica17e14fd2022-12-07 17:08:45 +010091 std::unique_ptr<SpeechLevelEstimator> speech_level_estimator_;
Hanna Silend7cfbe32022-11-02 19:12:20 +010092 std::unique_ptr<InputVolumeController> input_volume_controller_;
Alessio Bazzica17e14fd2022-12-07 17:08:45 +010093 // TODO(bugs.webrtc.org/7494): Rename to `CrestFactorEstimator`.
94 std::unique_ptr<SaturationProtector> saturation_protector_;
Alessio Bazzicaf72bc5f2022-12-09 08:46:06 +010095 std::unique_ptr<AdaptiveDigitalGainController> adaptive_digital_controller_;
Alessio Bazzica3e4c77f2018-11-01 21:31:38 +010096 Limiter limiter_;
Alessio Bazzica17e14fd2022-12-07 17:08:45 +010097
Alessio Bazzica08d2a702020-11-20 16:26:24 +010098 int calls_since_last_limiter_log_;
Hanna Silen597a2ba2022-12-14 12:48:37 +010099
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_;
alessiob3ec96df2017-05-22 06:57:06 -0700106};
107
108} // namespace webrtc
109
Alex Loikoe36e8bb2018-02-16 11:54:07 +0100110#endif // MODULES_AUDIO_PROCESSING_GAIN_CONTROLLER2_H_