alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 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 | |
Alex Loiko | e36e8bb | 2018-02-16 11:54:07 +0100 | [diff] [blame] | 11 | #include "modules/audio_processing/gain_controller2.h" |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 12 | |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 13 | #include <memory> |
| 14 | #include <utility> |
| 15 | |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 16 | #include "common_audio/include/audio_util.h" |
Alessio Bazzica | b4d4ae2 | 2021-10-15 13:57:56 +0200 | [diff] [blame^] | 17 | #include "modules/audio_processing/agc2/cpu_features.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "modules/audio_processing/audio_buffer.h" |
Alex Loiko | e36e8bb | 2018-02-16 11:54:07 +0100 | [diff] [blame] | 19 | #include "modules/audio_processing/include/audio_frame_view.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "modules/audio_processing/logging/apm_data_dumper.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 21 | #include "rtc_base/atomic_ops.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "rtc_base/checks.h" |
Alessio Bazzica | 08d2a70 | 2020-11-20 16:26:24 +0100 | [diff] [blame] | 23 | #include "rtc_base/logging.h" |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 24 | #include "rtc_base/strings/string_builder.h" |
Alessio Bazzica | b4d4ae2 | 2021-10-15 13:57:56 +0200 | [diff] [blame^] | 25 | #include "system_wrappers/include/field_trial.h" |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 26 | |
| 27 | namespace webrtc { |
Alessio Bazzica | 82ea4ee | 2021-10-07 09:21:02 +0200 | [diff] [blame] | 28 | namespace { |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 29 | |
| 30 | using Agc2Config = AudioProcessing::Config::GainController2; |
| 31 | |
| 32 | constexpr int kUnspecifiedAnalogLevel = -1; |
Alessio Bazzica | 82ea4ee | 2021-10-07 09:21:02 +0200 | [diff] [blame] | 33 | constexpr int kLogLimiterStatsPeriodMs = 30'000; |
| 34 | constexpr int kFrameLengthMs = 10; |
| 35 | constexpr int kLogLimiterStatsPeriodNumFrames = |
| 36 | kLogLimiterStatsPeriodMs / kFrameLengthMs; |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 37 | |
Alessio Bazzica | b4d4ae2 | 2021-10-15 13:57:56 +0200 | [diff] [blame^] | 38 | // Detects the available CPU features and applies any kill-switches. |
| 39 | AvailableCpuFeatures GetAllowedCpuFeatures() { |
| 40 | AvailableCpuFeatures features = GetAvailableCpuFeatures(); |
| 41 | if (field_trial::IsEnabled("WebRTC-Agc2SimdSse2KillSwitch")) { |
| 42 | features.sse2 = false; |
| 43 | } |
| 44 | if (field_trial::IsEnabled("WebRTC-Agc2SimdAvx2KillSwitch")) { |
| 45 | features.avx2 = false; |
| 46 | } |
| 47 | if (field_trial::IsEnabled("WebRTC-Agc2SimdNeonKillSwitch")) { |
| 48 | features.neon = false; |
| 49 | } |
| 50 | return features; |
| 51 | } |
| 52 | |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 53 | // Creates an adaptive digital gain controller if enabled. |
| 54 | std::unique_ptr<AdaptiveAgc> CreateAdaptiveDigitalController( |
| 55 | const Agc2Config::AdaptiveDigital& config, |
| 56 | int sample_rate_hz, |
| 57 | int num_channels, |
| 58 | ApmDataDumper* data_dumper) { |
| 59 | if (config.enabled) { |
Alessio Bazzica | b4d4ae2 | 2021-10-15 13:57:56 +0200 | [diff] [blame^] | 60 | // TODO(bugs.webrtc.org/7494): Also init with sample rate and num |
| 61 | // channels. |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 62 | auto controller = std::make_unique<AdaptiveAgc>(data_dumper, config); |
| 63 | // TODO(bugs.webrtc.org/7494): Remove once passed to the ctor. |
| 64 | controller->Initialize(sample_rate_hz, num_channels); |
| 65 | return controller; |
| 66 | } |
| 67 | return nullptr; |
| 68 | } |
| 69 | |
Alessio Bazzica | 82ea4ee | 2021-10-07 09:21:02 +0200 | [diff] [blame] | 70 | } // namespace |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 71 | |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 72 | int GainController2::instance_count_ = 0; |
| 73 | |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 74 | GainController2::GainController2(const Agc2Config& config, |
| 75 | int sample_rate_hz, |
| 76 | int num_channels) |
Alessio Bazzica | b4d4ae2 | 2021-10-15 13:57:56 +0200 | [diff] [blame^] | 77 | : cpu_features_(GetAllowedCpuFeatures()), |
| 78 | data_dumper_(rtc::AtomicOps::Increment(&instance_count_)), |
Alessio Bazzica | 82ea4ee | 2021-10-07 09:21:02 +0200 | [diff] [blame] | 79 | fixed_gain_applier_(/*hard_clip_samples=*/false, |
| 80 | /*initial_gain_factor=*/0.0f), |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 81 | adaptive_digital_controller_( |
| 82 | CreateAdaptiveDigitalController(config.adaptive_digital, |
| 83 | sample_rate_hz, |
| 84 | num_channels, |
| 85 | &data_dumper_)), |
| 86 | limiter_(sample_rate_hz, &data_dumper_, /*histogram_name_prefix=*/"Agc2"), |
| 87 | calls_since_last_limiter_log_(0), |
| 88 | analog_level_(kUnspecifiedAnalogLevel) { |
| 89 | RTC_DCHECK(Validate(config)); |
| 90 | data_dumper_.InitiateNewSetOfRecordings(); |
| 91 | // TODO(bugs.webrtc.org/7494): Set gain when `fixed_gain_applier_` is init'd. |
| 92 | fixed_gain_applier_.SetGainFactor(DbToRatio(config.fixed_digital.gain_db)); |
Alessio Bazzica | b4d4ae2 | 2021-10-15 13:57:56 +0200 | [diff] [blame^] | 93 | const bool use_vad = config.adaptive_digital.enabled; |
| 94 | if (use_vad) { |
| 95 | // TODO(bugs.webrtc.org/7494): Move `vad_reset_period_ms` from adaptive |
| 96 | // digital to gain controller 2 config. |
| 97 | vad_ = std::make_unique<VoiceActivityDetectorWrapper>( |
| 98 | config.adaptive_digital.vad_reset_period_ms, cpu_features_, |
| 99 | sample_rate_hz); |
| 100 | } |
Per Åhgren | 2bd85ab | 2020-01-03 10:36:34 +0100 | [diff] [blame] | 101 | } |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 102 | |
| 103 | GainController2::~GainController2() = default; |
| 104 | |
Alessio Bazzica | d66a605 | 2021-04-29 16:13:25 +0200 | [diff] [blame] | 105 | void GainController2::Initialize(int sample_rate_hz, int num_channels) { |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 106 | RTC_DCHECK(sample_rate_hz == AudioProcessing::kSampleRate8kHz || |
| 107 | sample_rate_hz == AudioProcessing::kSampleRate16kHz || |
| 108 | sample_rate_hz == AudioProcessing::kSampleRate32kHz || |
| 109 | sample_rate_hz == AudioProcessing::kSampleRate48kHz); |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 110 | // TODO(bugs.webrtc.org/7494): Initialize `fixed_gain_applier_`. |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 111 | limiter_.SetSampleRate(sample_rate_hz); |
Alessio Bazzica | b4d4ae2 | 2021-10-15 13:57:56 +0200 | [diff] [blame^] | 112 | if (vad_) { |
| 113 | vad_->Initialize(sample_rate_hz); |
| 114 | } |
Alessio Bazzica | 82ea4ee | 2021-10-07 09:21:02 +0200 | [diff] [blame] | 115 | if (adaptive_digital_controller_) { |
| 116 | adaptive_digital_controller_->Initialize(sample_rate_hz, num_channels); |
Alessio Bazzica | d66a605 | 2021-04-29 16:13:25 +0200 | [diff] [blame] | 117 | } |
Alessio Bazzica | 8aaa604 | 2021-03-31 15:16:05 +0200 | [diff] [blame] | 118 | data_dumper_.InitiateNewSetOfRecordings(); |
Alessio Bazzica | 08d2a70 | 2020-11-20 16:26:24 +0100 | [diff] [blame] | 119 | calls_since_last_limiter_log_ = 0; |
Alessio Bazzica | 3890104 | 2021-10-14 12:14:21 +0200 | [diff] [blame] | 120 | analog_level_ = kUnspecifiedAnalogLevel; |
| 121 | } |
| 122 | |
| 123 | void GainController2::SetFixedGainDb(float gain_db) { |
| 124 | const float gain_factor = DbToRatio(gain_db); |
| 125 | if (fixed_gain_applier_.GetGainFactor() != gain_factor) { |
| 126 | // Reset the limiter to quickly react on abrupt level changes caused by |
| 127 | // large changes of the fixed gain. |
| 128 | limiter_.Reset(); |
| 129 | } |
| 130 | fixed_gain_applier_.SetGainFactor(gain_factor); |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 131 | } |
| 132 | |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 133 | void GainController2::Process(AudioBuffer* audio) { |
Alessio Bazzica | 8aaa604 | 2021-03-31 15:16:05 +0200 | [diff] [blame] | 134 | data_dumper_.DumpRaw("agc2_notified_analog_level", analog_level_); |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 135 | AudioFrameView<float> float_frame(audio->channels(), audio->num_channels(), |
Alex Loiko | e36e8bb | 2018-02-16 11:54:07 +0100 | [diff] [blame] | 136 | audio->num_frames()); |
Alessio Bazzica | b4d4ae2 | 2021-10-15 13:57:56 +0200 | [diff] [blame^] | 137 | absl::optional<float> speech_probability; |
| 138 | // TODO(bugs.webrtc.org/7494): Apply fixed digital gain after VAD. |
Alessio Bazzica | 82ea4ee | 2021-10-07 09:21:02 +0200 | [diff] [blame] | 139 | fixed_gain_applier_.ApplyGain(float_frame); |
Alessio Bazzica | b4d4ae2 | 2021-10-15 13:57:56 +0200 | [diff] [blame^] | 140 | if (vad_) { |
| 141 | speech_probability = vad_->Analyze(float_frame); |
| 142 | data_dumper_.DumpRaw("agc2_speech_probability", speech_probability.value()); |
| 143 | } |
Alessio Bazzica | 82ea4ee | 2021-10-07 09:21:02 +0200 | [diff] [blame] | 144 | if (adaptive_digital_controller_) { |
Alessio Bazzica | b4d4ae2 | 2021-10-15 13:57:56 +0200 | [diff] [blame^] | 145 | RTC_DCHECK(speech_probability.has_value()); |
| 146 | adaptive_digital_controller_->Process( |
| 147 | float_frame, speech_probability.value(), limiter_.LastAudioLevel()); |
Alex Loiko | e583174 | 2018-08-24 11:28:36 +0200 | [diff] [blame] | 148 | } |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 149 | limiter_.Process(float_frame); |
Alessio Bazzica | 08d2a70 | 2020-11-20 16:26:24 +0100 | [diff] [blame] | 150 | |
Alessio Bazzica | 82ea4ee | 2021-10-07 09:21:02 +0200 | [diff] [blame] | 151 | // Periodically log limiter stats. |
| 152 | if (++calls_since_last_limiter_log_ == kLogLimiterStatsPeriodNumFrames) { |
Alessio Bazzica | 08d2a70 | 2020-11-20 16:26:24 +0100 | [diff] [blame] | 153 | calls_since_last_limiter_log_ = 0; |
| 154 | InterpolatedGainCurve::Stats stats = limiter_.GetGainCurveStats(); |
| 155 | RTC_LOG(LS_INFO) << "AGC2 limiter stats" |
| 156 | << " | identity: " << stats.look_ups_identity_region |
| 157 | << " | knee: " << stats.look_ups_knee_region |
| 158 | << " | limiter: " << stats.look_ups_limiter_region |
| 159 | << " | saturation: " << stats.look_ups_saturation_region; |
| 160 | } |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Alex Loiko | a837dd7 | 2018-08-06 16:32:12 +0200 | [diff] [blame] | 163 | void GainController2::NotifyAnalogLevel(int level) { |
Alessio Bazzica | 82ea4ee | 2021-10-07 09:21:02 +0200 | [diff] [blame] | 164 | if (analog_level_ != level && adaptive_digital_controller_) { |
| 165 | adaptive_digital_controller_->HandleInputGainChange(); |
Alex Loiko | a837dd7 | 2018-08-06 16:32:12 +0200 | [diff] [blame] | 166 | } |
| 167 | analog_level_ = level; |
| 168 | } |
| 169 | |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 170 | bool GainController2::Validate( |
| 171 | const AudioProcessing::Config::GainController2& config) { |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 172 | const auto& fixed = config.fixed_digital; |
| 173 | const auto& adaptive = config.adaptive_digital; |
Alessio Bazzica | a850e6c | 2021-10-04 13:35:55 +0200 | [diff] [blame] | 174 | return fixed.gain_db >= 0.0f && fixed.gain_db < 50.f && |
| 175 | adaptive.headroom_db >= 0.0f && adaptive.max_gain_db > 0.0f && |
| 176 | adaptive.initial_gain_db >= 0.0f && |
Alessio Bazzica | 1ac4f2a | 2021-09-24 14:59:30 +0200 | [diff] [blame] | 177 | adaptive.max_gain_change_db_per_second > 0.0f && |
| 178 | adaptive.max_output_noise_level_dbfs <= 0.0f; |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | } // namespace webrtc |