blob: 0d41eaa148fd8796abc2f734dbb8267737e08a8b [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 Bazzica2fa46182021-10-26 14:08:23 +020018#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 Bazzicab4d4ae22021-10-15 13:57:56 +020023#include "modules/audio_processing/agc2/vad_wrapper.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "modules/audio_processing/include/audio_processing.h"
Alessio Bazzica8aaa6042021-03-31 15:16:05 +020025#include "modules/audio_processing/logging/apm_data_dumper.h"
alessiob3ec96df2017-05-22 06:57:06 -070026
27namespace webrtc {
28
alessiob3ec96df2017-05-22 06:57:06 -070029class AudioBuffer;
30
31// Gain Controller 2 aims to automatically adjust levels by acting on the
32// microphone gain and/or applying digital gain.
alessiob3ec96df2017-05-22 06:57:06 -070033class GainController2 {
34 public:
Hanna Silen0c1ad292022-06-16 16:35:45 +020035 // Ctor. If `use_internal_vad` is true, an internal voice activity
36 // detector is used for digital adaptive gain.
Hanna Silena6574902022-11-30 16:59:05 +010037 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);
Alessio Bazzica8aaa6042021-03-31 15:16:05 +020043 GainController2(const GainController2&) = delete;
44 GainController2& operator=(const GainController2&) = delete;
alessiob3ec96df2017-05-22 06:57:06 -070045 ~GainController2();
46
Alessio Bazzica38901042021-10-14 12:14:21 +020047 // Sets the fixed digital gain.
48 void SetFixedGainDb(float gain_db);
49
Hanna Silend7cfbe32022-11-02 19:12:20 +010050 // 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
Alessio Bazzica38901042021-10-14 12:14:21 +020061 // Applies fixed and adaptive digital gains to `audio` and runs a limiter.
Hanna Silen0c1ad292022-06-16 16:35:45 +020062 // If the internal VAD is used, `speech_probability` is ignored. Otherwise
63 // `speech_probability` is used for digital adaptive gain if it's available
Alessio Bazzicafcf1af32022-09-07 17:14:26 +020064 // (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);
alessiob3ec96df2017-05-22 06:57:06 -070070
71 static bool Validate(const AudioProcessing::Config::GainController2& config);
alessiob3ec96df2017-05-22 06:57:06 -070072
Hanna Silen0c1ad292022-06-16 16:35:45 +020073 AvailableCpuFeatures GetCpuFeatures() const { return cpu_features_; }
74
Hanna Silend7cfbe32022-11-02 19:12:20 +010075 // 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
alessiob3ec96df2017-05-22 06:57:06 -070079 private:
Niels Möller7a669002022-06-27 09:47:02 +020080 static std::atomic<int> instance_count_;
Alessio Bazzicab4d4ae22021-10-15 13:57:56 +020081 const AvailableCpuFeatures cpu_features_;
Alessio Bazzica8aaa6042021-03-31 15:16:05 +020082 ApmDataDumper data_dumper_;
Alessio Bazzica82ea4ee2021-10-07 09:21:02 +020083 GainApplier fixed_gain_applier_;
Alessio Bazzicab4d4ae22021-10-15 13:57:56 +020084 std::unique_ptr<VoiceActivityDetectorWrapper> vad_;
Alessio Bazzica2fa46182021-10-26 14:08:23 +020085 std::unique_ptr<AdaptiveDigitalGainController> adaptive_digital_controller_;
Hanna Silend7cfbe32022-11-02 19:12:20 +010086 std::unique_ptr<InputVolumeController> input_volume_controller_;
Alessio Bazzica3e4c77f2018-11-01 21:31:38 +010087 Limiter limiter_;
Alessio Bazzica08d2a702020-11-20 16:26:24 +010088 int calls_since_last_limiter_log_;
alessiob3ec96df2017-05-22 06:57:06 -070089};
90
91} // namespace webrtc
92
Alex Loikoe36e8bb2018-02-16 11:54:07 +010093#endif // MODULES_AUDIO_PROCESSING_GAIN_CONTROLLER2_H_