blob: 8e71d4020e435fbdf9608c36114180b764e9b47a [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"
Jonas Olsson366a50c2018-09-06 13:41:30 +020019#include "rtc_base/strings/string_builder.h"
alessiob3ec96df2017-05-22 06:57:06 -070020
21namespace webrtc {
22
alessiob3ec96df2017-05-22 06:57:06 -070023int GainController2::instance_count_ = 0;
24
Alessio Bazzica270f7b52017-10-13 11:05:17 +020025GainController2::GainController2()
26 : data_dumper_(
27 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
Alessio Bazzica3e4c77f2018-11-01 21:31:38 +010028 gain_applier_(/*hard_clip_samples=*/false,
29 /*initial_gain_factor=*/0.f),
Per Åhgren2bd85ab2020-01-03 10:36:34 +010030 limiter_(static_cast<size_t>(48000), data_dumper_.get(), "Agc2") {
31 if (config_.adaptive_digital.enabled) {
32 adaptive_agc_.reset(new AdaptiveAgc(data_dumper_.get()));
33 }
34}
alessiob3ec96df2017-05-22 06:57:06 -070035
36GainController2::~GainController2() = default;
37
Alessio Bazzica270f7b52017-10-13 11:05:17 +020038void GainController2::Initialize(int sample_rate_hz) {
39 RTC_DCHECK(sample_rate_hz == AudioProcessing::kSampleRate8kHz ||
40 sample_rate_hz == AudioProcessing::kSampleRate16kHz ||
41 sample_rate_hz == AudioProcessing::kSampleRate32kHz ||
42 sample_rate_hz == AudioProcessing::kSampleRate48kHz);
Alessio Bazzica3e4c77f2018-11-01 21:31:38 +010043 limiter_.SetSampleRate(sample_rate_hz);
Alessio Bazzica270f7b52017-10-13 11:05:17 +020044 data_dumper_->InitiateNewSetOfRecordings();
Alex Loikoe36e8bb2018-02-16 11:54:07 +010045 data_dumper_->DumpRaw("sample_rate_hz", sample_rate_hz);
Alessio Bazzica270f7b52017-10-13 11:05:17 +020046}
47
alessiob3ec96df2017-05-22 06:57:06 -070048void GainController2::Process(AudioBuffer* audio) {
Per Åhgrend47941e2019-08-22 11:51:13 +020049 AudioFrameView<float> float_frame(audio->channels(), audio->num_channels(),
Alex Loikoe36e8bb2018-02-16 11:54:07 +010050 audio->num_frames());
Alessio Bazzica3e4c77f2018-11-01 21:31:38 +010051 // Apply fixed gain first, then the adaptive one.
52 gain_applier_.ApplyGain(float_frame);
Per Åhgren2bd85ab2020-01-03 10:36:34 +010053 if (adaptive_agc_) {
Alessio Bazzica3e4c77f2018-11-01 21:31:38 +010054 adaptive_agc_->Process(float_frame, limiter_.LastAudioLevel());
Alex Loikoe5831742018-08-24 11:28:36 +020055 }
Alessio Bazzica3e4c77f2018-11-01 21:31:38 +010056 limiter_.Process(float_frame);
alessiob3ec96df2017-05-22 06:57:06 -070057}
58
Alex Loikoa837dd72018-08-06 16:32:12 +020059void GainController2::NotifyAnalogLevel(int level) {
Per Åhgren2bd85ab2020-01-03 10:36:34 +010060 if (analog_level_ != level && adaptive_agc_) {
Alex Loiko5e784612018-11-01 14:51:56 +010061 adaptive_agc_->Reset();
Alex Loikoa837dd72018-08-06 16:32:12 +020062 }
63 analog_level_ = level;
64}
65
Alessio Bazzica270f7b52017-10-13 11:05:17 +020066void GainController2::ApplyConfig(
67 const AudioProcessing::Config::GainController2& config) {
Alex Loiko20f60f02018-11-12 12:09:57 +010068 RTC_DCHECK(Validate(config))
69 << " the invalid config was " << ToString(config);
70
Alessio Bazzica1e2542f2018-11-13 14:44:15 +010071 config_ = config;
72 if (config.fixed_digital.gain_db != config_.fixed_digital.gain_db) {
Alessio Bazzica3e4c77f2018-11-01 21:31:38 +010073 // Reset the limiter to quickly react on abrupt level changes caused by
74 // large changes of the fixed gain.
75 limiter_.Reset();
76 }
Alessio Bazzica1e2542f2018-11-13 14:44:15 +010077 gain_applier_.SetGainFactor(DbToRatio(config_.fixed_digital.gain_db));
Per Åhgren2bd85ab2020-01-03 10:36:34 +010078 if (config_.adaptive_digital.enabled) {
79 adaptive_agc_.reset(new AdaptiveAgc(data_dumper_.get(), config_));
80 } else {
81 adaptive_agc_.reset();
82 }
Alessio Bazzica270f7b52017-10-13 11:05:17 +020083}
84
alessiob3ec96df2017-05-22 06:57:06 -070085bool GainController2::Validate(
86 const AudioProcessing::Config::GainController2& config) {
Alessio Bazzica1e2542f2018-11-13 14:44:15 +010087 return config.fixed_digital.gain_db >= 0.f &&
88 config.fixed_digital.gain_db < 50.f &&
89 config.adaptive_digital.extra_saturation_margin_db >= 0.f &&
90 config.adaptive_digital.extra_saturation_margin_db <= 100.f;
alessiob3ec96df2017-05-22 06:57:06 -070091}
92
93std::string GainController2::ToString(
94 const AudioProcessing::Config::GainController2& config) {
Jonas Olsson366a50c2018-09-06 13:41:30 +020095 rtc::StringBuilder ss;
Alessio Bazzica1e2542f2018-11-13 14:44:15 +010096 std::string adaptive_digital_level_estimator;
97 using LevelEstimatorType =
98 AudioProcessing::Config::GainController2::LevelEstimator;
99 switch (config.adaptive_digital.level_estimator) {
100 case LevelEstimatorType::kRms:
101 adaptive_digital_level_estimator = "RMS";
102 break;
103 case LevelEstimatorType::kPeak:
104 adaptive_digital_level_estimator = "peak";
105 break;
106 }
107 // clang-format off
108 // clang formatting doesn't respect custom nested style.
109 ss << "{"
Jonas Olssonb2b20312020-01-14 12:11:31 +0100110 "enabled: " << (config.enabled ? "true" : "false") << ", "
111 "fixed_digital: {gain_db: " << config.fixed_digital.gain_db << "}, "
112 "adaptive_digital: {"
113 "enabled: "
114 << (config.adaptive_digital.enabled ? "true" : "false") << ", "
Alessio Bazzicaab4a4922020-10-01 17:18:37 +0200115 "level_estimator: {"
116 "type: " << adaptive_digital_level_estimator << ", "
117 "adjacent_speech_frames_threshold: "
118 << config.adaptive_digital
119 .level_estimator_adjacent_speech_frames_threshold << ", "
120 "initial_saturation_margin_db: "
121 << config.adaptive_digital.initial_saturation_margin_db << ", "
122 "extra_saturation_margin_db: "
123 << config.adaptive_digital.extra_saturation_margin_db << "}, "
124 "gain_applier: {"
125 "adjacent_speech_frames_threshold: "
126 << config.adaptive_digital
127 .gain_applier_adjacent_speech_frames_threshold << ", "
128 "max_gain_change_db_per_second: "
129 << config.adaptive_digital.max_gain_change_db_per_second << ", "
130 "max_output_noise_level_dbfs: "
131 << config.adaptive_digital.max_output_noise_level_dbfs << "}"
132 "}"
133 "}";
Alessio Bazzica1e2542f2018-11-13 14:44:15 +0100134 // clang-format on
Jonas Olsson84df1c72018-09-14 16:59:32 +0200135 return ss.Release();
alessiob3ec96df2017-05-22 06:57:06 -0700136}
137
138} // namespace webrtc