blob: 3341cd22d002427eb3935aa85ec081afc670bed5 [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.
Alessio Bazzica38901042021-10-14 12:14:21 +020037 GainController2(const AudioProcessing::Config::GainController2& config,
38 int sample_rate_hz,
Hanna Silen0c1ad292022-06-16 16:35:45 +020039 int num_channels,
40 bool use_internal_vad);
Alessio Bazzica8aaa6042021-03-31 15:16:05 +020041 GainController2(const GainController2&) = delete;
42 GainController2& operator=(const GainController2&) = delete;
alessiob3ec96df2017-05-22 06:57:06 -070043 ~GainController2();
44
Alessio Bazzica38901042021-10-14 12:14:21 +020045 // Sets the fixed digital gain.
46 void SetFixedGainDb(float gain_db);
47
Hanna Silend7cfbe32022-11-02 19:12:20 +010048 // Updates the input volume controller about whether the capture output is
49 // used or not.
50 void SetCaptureOutputUsed(bool capture_output_used);
51
52 // Analyzes `audio_buffer` before `Process()` is called so that the analysis
53 // can be performed before digital processing operations take place (e.g.,
54 // echo cancellation). The analysis consists of input clipping detection and
55 // prediction (if enabled). The value of `applied_input_volume` is limited to
56 // [0, 255].
57 void Analyze(int applied_input_volume, const AudioBuffer& audio_buffer);
58
Alessio Bazzica38901042021-10-14 12:14:21 +020059 // Applies fixed and adaptive digital gains to `audio` and runs a limiter.
Hanna Silen0c1ad292022-06-16 16:35:45 +020060 // If the internal VAD is used, `speech_probability` is ignored. Otherwise
61 // `speech_probability` is used for digital adaptive gain if it's available
Alessio Bazzicafcf1af32022-09-07 17:14:26 +020062 // (limited to values [0.0, 1.0]). Handles input volume changes; if the caller
63 // cannot determine whether an input volume change occurred, set
64 // `input_volume_changed` to false.
65 void Process(absl::optional<float> speech_probability,
66 bool input_volume_changed,
67 AudioBuffer* audio);
alessiob3ec96df2017-05-22 06:57:06 -070068
69 static bool Validate(const AudioProcessing::Config::GainController2& config);
alessiob3ec96df2017-05-22 06:57:06 -070070
Hanna Silen0c1ad292022-06-16 16:35:45 +020071 AvailableCpuFeatures GetCpuFeatures() const { return cpu_features_; }
72
Hanna Silend7cfbe32022-11-02 19:12:20 +010073 // Returns the recommended input volume if input volume controller is enabled
74 // and if a volume recommendation is available.
75 absl::optional<int> GetRecommendedInputVolume() const;
76
alessiob3ec96df2017-05-22 06:57:06 -070077 private:
Niels Möller7a669002022-06-27 09:47:02 +020078 static std::atomic<int> instance_count_;
Alessio Bazzicab4d4ae22021-10-15 13:57:56 +020079 const AvailableCpuFeatures cpu_features_;
Alessio Bazzica8aaa6042021-03-31 15:16:05 +020080 ApmDataDumper data_dumper_;
Alessio Bazzica82ea4ee2021-10-07 09:21:02 +020081 GainApplier fixed_gain_applier_;
Alessio Bazzicab4d4ae22021-10-15 13:57:56 +020082 std::unique_ptr<VoiceActivityDetectorWrapper> vad_;
Alessio Bazzica2fa46182021-10-26 14:08:23 +020083 std::unique_ptr<AdaptiveDigitalGainController> adaptive_digital_controller_;
Hanna Silend7cfbe32022-11-02 19:12:20 +010084 std::unique_ptr<InputVolumeController> input_volume_controller_;
Alessio Bazzica3e4c77f2018-11-01 21:31:38 +010085 Limiter limiter_;
Alessio Bazzica08d2a702020-11-20 16:26:24 +010086 int calls_since_last_limiter_log_;
alessiob3ec96df2017-05-22 06:57:06 -070087};
88
89} // namespace webrtc
90
Alex Loikoe36e8bb2018-02-16 11:54:07 +010091#endif // MODULES_AUDIO_PROCESSING_GAIN_CONTROLLER2_H_