blob: 8a22fed97e63d2d3e9bfdcfd8895a3441d49b872 [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#include "modules/audio_processing/gain_controller2.h"
alessiob3ec96df2017-05-22 06:57:06 -070012
Alessio Bazzica3e4c77f2018-11-01 21:31:38 +010013#include "common_audio/include/audio_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "modules/audio_processing/audio_buffer.h"
Alex Loikoe36e8bb2018-02-16 11:54:07 +010015#include "modules/audio_processing/include/audio_frame_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "modules/audio_processing/logging/apm_data_dumper.h"
Steve Anton10542f22019-01-11 09:11:00 -080017#include "rtc_base/atomic_ops.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/checks.h"
Alessio Bazzica08d2a702020-11-20 16:26:24 +010019#include "rtc_base/logging.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020020#include "rtc_base/strings/string_builder.h"
alessiob3ec96df2017-05-22 06:57:06 -070021
22namespace webrtc {
23
alessiob3ec96df2017-05-22 06:57:06 -070024int GainController2::instance_count_ = 0;
25
Alessio Bazzica270f7b52017-10-13 11:05:17 +020026GainController2::GainController2()
Alessio Bazzica8aaa6042021-03-31 15:16:05 +020027 : data_dumper_(rtc::AtomicOps::Increment(&instance_count_)),
Alessio Bazzica3e4c77f2018-11-01 21:31:38 +010028 gain_applier_(/*hard_clip_samples=*/false,
Alessio Bazzicad66a6052021-04-29 16:13:25 +020029 /*initial_gain_factor=*/0.0f),
Alessio Bazzica8aaa6042021-03-31 15:16:05 +020030 limiter_(static_cast<size_t>(48000), &data_dumper_, "Agc2"),
Alessio Bazzica08d2a702020-11-20 16:26:24 +010031 calls_since_last_limiter_log_(0) {
Per Åhgren2bd85ab2020-01-03 10:36:34 +010032 if (config_.adaptive_digital.enabled) {
Alessio Bazzicad66a6052021-04-29 16:13:25 +020033 adaptive_agc_ =
34 std::make_unique<AdaptiveAgc>(&data_dumper_, config_.adaptive_digital);
Per Åhgren2bd85ab2020-01-03 10:36:34 +010035 }
36}
alessiob3ec96df2017-05-22 06:57:06 -070037
38GainController2::~GainController2() = default;
39
Alessio Bazzicad66a6052021-04-29 16:13:25 +020040void GainController2::Initialize(int sample_rate_hz, int num_channels) {
Alessio Bazzica270f7b52017-10-13 11:05:17 +020041 RTC_DCHECK(sample_rate_hz == AudioProcessing::kSampleRate8kHz ||
42 sample_rate_hz == AudioProcessing::kSampleRate16kHz ||
43 sample_rate_hz == AudioProcessing::kSampleRate32kHz ||
44 sample_rate_hz == AudioProcessing::kSampleRate48kHz);
Alessio Bazzica3e4c77f2018-11-01 21:31:38 +010045 limiter_.SetSampleRate(sample_rate_hz);
Alessio Bazzicad66a6052021-04-29 16:13:25 +020046 if (adaptive_agc_) {
47 adaptive_agc_->Initialize(sample_rate_hz, num_channels);
48 }
Alessio Bazzica8aaa6042021-03-31 15:16:05 +020049 data_dumper_.InitiateNewSetOfRecordings();
50 data_dumper_.DumpRaw("sample_rate_hz", sample_rate_hz);
Alessio Bazzica08d2a702020-11-20 16:26:24 +010051 calls_since_last_limiter_log_ = 0;
Alessio Bazzica270f7b52017-10-13 11:05:17 +020052}
53
alessiob3ec96df2017-05-22 06:57:06 -070054void GainController2::Process(AudioBuffer* audio) {
Alessio Bazzica8aaa6042021-03-31 15:16:05 +020055 data_dumper_.DumpRaw("agc2_notified_analog_level", analog_level_);
Per Åhgrend47941e2019-08-22 11:51:13 +020056 AudioFrameView<float> float_frame(audio->channels(), audio->num_channels(),
Alex Loikoe36e8bb2018-02-16 11:54:07 +010057 audio->num_frames());
Alessio Bazzica3e4c77f2018-11-01 21:31:38 +010058 // Apply fixed gain first, then the adaptive one.
59 gain_applier_.ApplyGain(float_frame);
Per Åhgren2bd85ab2020-01-03 10:36:34 +010060 if (adaptive_agc_) {
Alessio Bazzica3e4c77f2018-11-01 21:31:38 +010061 adaptive_agc_->Process(float_frame, limiter_.LastAudioLevel());
Alex Loikoe5831742018-08-24 11:28:36 +020062 }
Alessio Bazzica3e4c77f2018-11-01 21:31:38 +010063 limiter_.Process(float_frame);
Alessio Bazzica08d2a702020-11-20 16:26:24 +010064
65 // Log limiter stats every 30 seconds.
66 ++calls_since_last_limiter_log_;
67 if (calls_since_last_limiter_log_ == 3000) {
68 calls_since_last_limiter_log_ = 0;
69 InterpolatedGainCurve::Stats stats = limiter_.GetGainCurveStats();
70 RTC_LOG(LS_INFO) << "AGC2 limiter stats"
71 << " | identity: " << stats.look_ups_identity_region
72 << " | knee: " << stats.look_ups_knee_region
73 << " | limiter: " << stats.look_ups_limiter_region
74 << " | saturation: " << stats.look_ups_saturation_region;
75 }
alessiob3ec96df2017-05-22 06:57:06 -070076}
77
Alex Loikoa837dd72018-08-06 16:32:12 +020078void GainController2::NotifyAnalogLevel(int level) {
Per Åhgren2bd85ab2020-01-03 10:36:34 +010079 if (analog_level_ != level && adaptive_agc_) {
Alessio Bazzica980c4602021-04-14 19:09:17 +020080 adaptive_agc_->HandleInputGainChange();
Alex Loikoa837dd72018-08-06 16:32:12 +020081 }
82 analog_level_ = level;
83}
84
Alessio Bazzica270f7b52017-10-13 11:05:17 +020085void GainController2::ApplyConfig(
86 const AudioProcessing::Config::GainController2& config) {
Alessio Bazzica0c83e152020-10-14 12:49:54 +020087 RTC_DCHECK(Validate(config));
Alex Loiko20f60f02018-11-12 12:09:57 +010088
Alessio Bazzica1e2542f2018-11-13 14:44:15 +010089 config_ = config;
90 if (config.fixed_digital.gain_db != config_.fixed_digital.gain_db) {
Alessio Bazzica3e4c77f2018-11-01 21:31:38 +010091 // Reset the limiter to quickly react on abrupt level changes caused by
92 // large changes of the fixed gain.
93 limiter_.Reset();
94 }
Alessio Bazzica1e2542f2018-11-13 14:44:15 +010095 gain_applier_.SetGainFactor(DbToRatio(config_.fixed_digital.gain_db));
Per Åhgren2bd85ab2020-01-03 10:36:34 +010096 if (config_.adaptive_digital.enabled) {
Alessio Bazzica61982a72021-04-14 16:17:09 +020097 adaptive_agc_ =
98 std::make_unique<AdaptiveAgc>(&data_dumper_, config_.adaptive_digital);
Per Åhgren2bd85ab2020-01-03 10:36:34 +010099 } else {
100 adaptive_agc_.reset();
101 }
Alessio Bazzica270f7b52017-10-13 11:05:17 +0200102}
103
alessiob3ec96df2017-05-22 06:57:06 -0700104bool GainController2::Validate(
105 const AudioProcessing::Config::GainController2& config) {
Alessio Bazzica0c83e152020-10-14 12:49:54 +0200106 const auto& fixed = config.fixed_digital;
107 const auto& adaptive = config.adaptive_digital;
Alessio Bazzicaa850e6c2021-10-04 13:35:55 +0200108 return fixed.gain_db >= 0.0f && fixed.gain_db < 50.f &&
109 adaptive.headroom_db >= 0.0f && adaptive.max_gain_db > 0.0f &&
110 adaptive.initial_gain_db >= 0.0f &&
Alessio Bazzica1ac4f2a2021-09-24 14:59:30 +0200111 adaptive.max_gain_change_db_per_second > 0.0f &&
112 adaptive.max_output_noise_level_dbfs <= 0.0f;
alessiob3ec96df2017-05-22 06:57:06 -0700113}
114
115} // namespace webrtc