1 /*
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
11 #include "modules/audio_processing/gain_controller2.h"
12
13 #include "common_audio/include/audio_util.h"
14 #include "modules/audio_processing/audio_buffer.h"
15 #include "modules/audio_processing/include/audio_frame_view.h"
16 #include "modules/audio_processing/logging/apm_data_dumper.h"
17 #include "rtc_base/atomic_ops.h"
18 #include "rtc_base/checks.h"
19 #include "rtc_base/strings/string_builder.h"
20
21 namespace webrtc {
22
23 int GainController2::instance_count_ = 0;
24
GainController2()25 GainController2::GainController2()
26 : data_dumper_(
27 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
28 gain_applier_(/*hard_clip_samples=*/false,
29 /*initial_gain_factor=*/0.f),
30 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 }
35
36 GainController2::~GainController2() = default;
37
Initialize(int sample_rate_hz)38 void 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);
43 limiter_.SetSampleRate(sample_rate_hz);
44 data_dumper_->InitiateNewSetOfRecordings();
45 data_dumper_->DumpRaw("sample_rate_hz", sample_rate_hz);
46 }
47
Process(AudioBuffer * audio)48 void GainController2::Process(AudioBuffer* audio) {
49 AudioFrameView<float> float_frame(audio->channels(), audio->num_channels(),
50 audio->num_frames());
51 // Apply fixed gain first, then the adaptive one.
52 gain_applier_.ApplyGain(float_frame);
53 if (adaptive_agc_) {
54 adaptive_agc_->Process(float_frame, limiter_.LastAudioLevel());
55 }
56 limiter_.Process(float_frame);
57 }
58
NotifyAnalogLevel(int level)59 void GainController2::NotifyAnalogLevel(int level) {
60 if (analog_level_ != level && adaptive_agc_) {
61 adaptive_agc_->Reset();
62 }
63 analog_level_ = level;
64 }
65
ApplyConfig(const AudioProcessing::Config::GainController2 & config)66 void GainController2::ApplyConfig(
67 const AudioProcessing::Config::GainController2& config) {
68 RTC_DCHECK(Validate(config))
69 << " the invalid config was " << ToString(config);
70
71 config_ = config;
72 if (config.fixed_digital.gain_db != config_.fixed_digital.gain_db) {
73 // Reset the limiter to quickly react on abrupt level changes caused by
74 // large changes of the fixed gain.
75 limiter_.Reset();
76 }
77 gain_applier_.SetGainFactor(DbToRatio(config_.fixed_digital.gain_db));
78 if (config_.adaptive_digital.enabled) {
79 adaptive_agc_.reset(new AdaptiveAgc(data_dumper_.get(), config_));
80 } else {
81 adaptive_agc_.reset();
82 }
83 }
84
Validate(const AudioProcessing::Config::GainController2 & config)85 bool GainController2::Validate(
86 const AudioProcessing::Config::GainController2& config) {
87 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;
91 }
92
ToString(const AudioProcessing::Config::GainController2 & config)93 std::string GainController2::ToString(
94 const AudioProcessing::Config::GainController2& config) {
95 rtc::StringBuilder ss;
96 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 << "{"
110 "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") << ", "
115 "level_estimator: " << adaptive_digital_level_estimator << ", "
116 "extra_saturation_margin_db:"
117 << config.adaptive_digital.extra_saturation_margin_db << "}"
118 "}";
119 // clang-format on
120 return ss.Release();
121 }
122
123 } // namespace webrtc
124