blob: 304fa404897a5febf96f53d75f9feb3a4f55f2f4 [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"
21#include "modules/audio_processing/agc2/limiter.h"
Alessio Bazzicab4d4ae22021-10-15 13:57:56 +020022#include "modules/audio_processing/agc2/vad_wrapper.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "modules/audio_processing/include/audio_processing.h"
Alessio Bazzica8aaa6042021-03-31 15:16:05 +020024#include "modules/audio_processing/logging/apm_data_dumper.h"
alessiob3ec96df2017-05-22 06:57:06 -070025
26namespace webrtc {
27
alessiob3ec96df2017-05-22 06:57:06 -070028class AudioBuffer;
29
30// Gain Controller 2 aims to automatically adjust levels by acting on the
31// microphone gain and/or applying digital gain.
alessiob3ec96df2017-05-22 06:57:06 -070032class GainController2 {
33 public:
Hanna Silen0c1ad292022-06-16 16:35:45 +020034 // Ctor. If `use_internal_vad` is true, an internal voice activity
35 // detector is used for digital adaptive gain.
Alessio Bazzica38901042021-10-14 12:14:21 +020036 GainController2(const AudioProcessing::Config::GainController2& config,
37 int sample_rate_hz,
Hanna Silen0c1ad292022-06-16 16:35:45 +020038 int num_channels,
39 bool use_internal_vad);
Alessio Bazzica8aaa6042021-03-31 15:16:05 +020040 GainController2(const GainController2&) = delete;
41 GainController2& operator=(const GainController2&) = delete;
alessiob3ec96df2017-05-22 06:57:06 -070042 ~GainController2();
43
Alessio Bazzica38901042021-10-14 12:14:21 +020044 // Detects and handles changes of sample rate and/or number of channels.
Alessio Bazzicad66a6052021-04-29 16:13:25 +020045 void Initialize(int sample_rate_hz, int num_channels);
Alessio Bazzica38901042021-10-14 12:14:21 +020046
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 Silen0c1ad292022-06-16 16:35:45 +020051 // 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 Bazzica38901042021-10-14 12:14:21 +020055
56 // Handles analog level changes.
Alex Loikoa837dd72018-08-06 16:32:12 +020057 void NotifyAnalogLevel(int level);
alessiob3ec96df2017-05-22 06:57:06 -070058
59 static bool Validate(const AudioProcessing::Config::GainController2& config);
alessiob3ec96df2017-05-22 06:57:06 -070060
Hanna Silen0c1ad292022-06-16 16:35:45 +020061 AvailableCpuFeatures GetCpuFeatures() const { return cpu_features_; }
62
alessiob3ec96df2017-05-22 06:57:06 -070063 private:
Niels Möller7a669002022-06-27 09:47:02 +020064 static std::atomic<int> instance_count_;
Alessio Bazzicab4d4ae22021-10-15 13:57:56 +020065 const AvailableCpuFeatures cpu_features_;
Alessio Bazzica8aaa6042021-03-31 15:16:05 +020066 ApmDataDumper data_dumper_;
Alessio Bazzica82ea4ee2021-10-07 09:21:02 +020067 GainApplier fixed_gain_applier_;
Alessio Bazzicab4d4ae22021-10-15 13:57:56 +020068 std::unique_ptr<VoiceActivityDetectorWrapper> vad_;
Alessio Bazzica2fa46182021-10-26 14:08:23 +020069 std::unique_ptr<AdaptiveDigitalGainController> adaptive_digital_controller_;
Alessio Bazzica3e4c77f2018-11-01 21:31:38 +010070 Limiter limiter_;
Alessio Bazzica08d2a702020-11-20 16:26:24 +010071 int calls_since_last_limiter_log_;
Alessio Bazzica38901042021-10-14 12:14:21 +020072 int analog_level_;
alessiob3ec96df2017-05-22 06:57:06 -070073};
74
75} // namespace webrtc
76
Alex Loikoe36e8bb2018-02-16 11:54:07 +010077#endif // MODULES_AUDIO_PROCESSING_GAIN_CONTROLLER2_H_