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 | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 13 | #include "common_audio/include/audio_util.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "modules/audio_processing/audio_buffer.h" |
Alex Loiko | e36e8bb | 2018-02-16 11:54:07 +0100 | [diff] [blame] | 15 | #include "modules/audio_processing/include/audio_frame_view.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "modules/audio_processing/logging/apm_data_dumper.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 17 | #include "rtc_base/atomic_ops.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "rtc_base/checks.h" |
Alessio Bazzica | 08d2a70 | 2020-11-20 16:26:24 +0100 | [diff] [blame] | 19 | #include "rtc_base/logging.h" |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 20 | #include "rtc_base/strings/string_builder.h" |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 21 | |
| 22 | namespace webrtc { |
| 23 | |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 24 | int GainController2::instance_count_ = 0; |
| 25 | |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 26 | GainController2::GainController2() |
| 27 | : data_dumper_( |
| 28 | new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))), |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 29 | gain_applier_(/*hard_clip_samples=*/false, |
| 30 | /*initial_gain_factor=*/0.f), |
Alessio Bazzica | 08d2a70 | 2020-11-20 16:26:24 +0100 | [diff] [blame] | 31 | limiter_(static_cast<size_t>(48000), data_dumper_.get(), "Agc2"), |
| 32 | calls_since_last_limiter_log_(0) { |
Per Åhgren | 2bd85ab | 2020-01-03 10:36:34 +0100 | [diff] [blame] | 33 | if (config_.adaptive_digital.enabled) { |
| 34 | adaptive_agc_.reset(new AdaptiveAgc(data_dumper_.get())); |
| 35 | } |
| 36 | } |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 37 | |
| 38 | GainController2::~GainController2() = default; |
| 39 | |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 40 | void GainController2::Initialize(int sample_rate_hz) { |
| 41 | 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 Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 45 | limiter_.SetSampleRate(sample_rate_hz); |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 46 | data_dumper_->InitiateNewSetOfRecordings(); |
Alex Loiko | e36e8bb | 2018-02-16 11:54:07 +0100 | [diff] [blame] | 47 | data_dumper_->DumpRaw("sample_rate_hz", sample_rate_hz); |
Alessio Bazzica | 08d2a70 | 2020-11-20 16:26:24 +0100 | [diff] [blame] | 48 | calls_since_last_limiter_log_ = 0; |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 49 | } |
| 50 | |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 51 | void GainController2::Process(AudioBuffer* audio) { |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 52 | AudioFrameView<float> float_frame(audio->channels(), audio->num_channels(), |
Alex Loiko | e36e8bb | 2018-02-16 11:54:07 +0100 | [diff] [blame] | 53 | audio->num_frames()); |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 54 | // Apply fixed gain first, then the adaptive one. |
| 55 | gain_applier_.ApplyGain(float_frame); |
Per Åhgren | 2bd85ab | 2020-01-03 10:36:34 +0100 | [diff] [blame] | 56 | if (adaptive_agc_) { |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 57 | adaptive_agc_->Process(float_frame, limiter_.LastAudioLevel()); |
Alex Loiko | e583174 | 2018-08-24 11:28:36 +0200 | [diff] [blame] | 58 | } |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 59 | limiter_.Process(float_frame); |
Alessio Bazzica | 08d2a70 | 2020-11-20 16:26:24 +0100 | [diff] [blame] | 60 | |
| 61 | // Log limiter stats every 30 seconds. |
| 62 | ++calls_since_last_limiter_log_; |
| 63 | if (calls_since_last_limiter_log_ == 3000) { |
| 64 | calls_since_last_limiter_log_ = 0; |
| 65 | InterpolatedGainCurve::Stats stats = limiter_.GetGainCurveStats(); |
| 66 | RTC_LOG(LS_INFO) << "AGC2 limiter stats" |
| 67 | << " | identity: " << stats.look_ups_identity_region |
| 68 | << " | knee: " << stats.look_ups_knee_region |
| 69 | << " | limiter: " << stats.look_ups_limiter_region |
| 70 | << " | saturation: " << stats.look_ups_saturation_region; |
| 71 | } |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Alex Loiko | a837dd7 | 2018-08-06 16:32:12 +0200 | [diff] [blame] | 74 | void GainController2::NotifyAnalogLevel(int level) { |
Per Åhgren | 2bd85ab | 2020-01-03 10:36:34 +0100 | [diff] [blame] | 75 | if (analog_level_ != level && adaptive_agc_) { |
Alex Loiko | 5e78461 | 2018-11-01 14:51:56 +0100 | [diff] [blame] | 76 | adaptive_agc_->Reset(); |
Alex Loiko | a837dd7 | 2018-08-06 16:32:12 +0200 | [diff] [blame] | 77 | } |
| 78 | analog_level_ = level; |
| 79 | } |
| 80 | |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 81 | void GainController2::ApplyConfig( |
| 82 | const AudioProcessing::Config::GainController2& config) { |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 83 | RTC_DCHECK(Validate(config)); |
Alex Loiko | 20f60f0 | 2018-11-12 12:09:57 +0100 | [diff] [blame] | 84 | |
Alessio Bazzica | 1e2542f | 2018-11-13 14:44:15 +0100 | [diff] [blame] | 85 | config_ = config; |
| 86 | if (config.fixed_digital.gain_db != config_.fixed_digital.gain_db) { |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 87 | // Reset the limiter to quickly react on abrupt level changes caused by |
| 88 | // large changes of the fixed gain. |
| 89 | limiter_.Reset(); |
| 90 | } |
Alessio Bazzica | 1e2542f | 2018-11-13 14:44:15 +0100 | [diff] [blame] | 91 | gain_applier_.SetGainFactor(DbToRatio(config_.fixed_digital.gain_db)); |
Per Åhgren | 2bd85ab | 2020-01-03 10:36:34 +0100 | [diff] [blame] | 92 | if (config_.adaptive_digital.enabled) { |
| 93 | adaptive_agc_.reset(new AdaptiveAgc(data_dumper_.get(), config_)); |
| 94 | } else { |
| 95 | adaptive_agc_.reset(); |
| 96 | } |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 97 | } |
| 98 | |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 99 | bool GainController2::Validate( |
| 100 | const AudioProcessing::Config::GainController2& config) { |
Alessio Bazzica | 0c83e15 | 2020-10-14 12:49:54 +0200 | [diff] [blame] | 101 | const auto& fixed = config.fixed_digital; |
| 102 | const auto& adaptive = config.adaptive_digital; |
| 103 | return fixed.gain_db >= 0.f && fixed.gain_db < 50.f && |
| 104 | adaptive.vad_probability_attack > 0.f && |
| 105 | adaptive.vad_probability_attack <= 1.f && |
| 106 | adaptive.level_estimator_adjacent_speech_frames_threshold >= 1 && |
| 107 | adaptive.initial_saturation_margin_db >= 0.f && |
| 108 | adaptive.initial_saturation_margin_db <= 100.f && |
| 109 | adaptive.extra_saturation_margin_db >= 0.f && |
| 110 | adaptive.extra_saturation_margin_db <= 100.f && |
| 111 | adaptive.gain_applier_adjacent_speech_frames_threshold >= 1 && |
| 112 | adaptive.max_gain_change_db_per_second > 0.f && |
| 113 | adaptive.max_output_noise_level_dbfs <= 0.f; |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | } // namespace webrtc |