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" |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 19 | #include "rtc_base/strings/string_builder.h" |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 23 | int GainController2::instance_count_ = 0; |
| 24 | |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 25 | GainController2::GainController2() |
| 26 | : data_dumper_( |
| 27 | new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))), |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 28 | gain_applier_(/*hard_clip_samples=*/false, |
| 29 | /*initial_gain_factor=*/0.f), |
| 30 | adaptive_agc_(new AdaptiveAgc(data_dumper_.get())), |
| 31 | limiter_(static_cast<size_t>(48000), data_dumper_.get(), "Agc2") {} |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 32 | |
| 33 | GainController2::~GainController2() = default; |
| 34 | |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 35 | void GainController2::Initialize(int sample_rate_hz) { |
| 36 | RTC_DCHECK(sample_rate_hz == AudioProcessing::kSampleRate8kHz || |
| 37 | sample_rate_hz == AudioProcessing::kSampleRate16kHz || |
| 38 | sample_rate_hz == AudioProcessing::kSampleRate32kHz || |
| 39 | sample_rate_hz == AudioProcessing::kSampleRate48kHz); |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 40 | limiter_.SetSampleRate(sample_rate_hz); |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 41 | data_dumper_->InitiateNewSetOfRecordings(); |
Alex Loiko | e36e8bb | 2018-02-16 11:54:07 +0100 | [diff] [blame] | 42 | data_dumper_->DumpRaw("sample_rate_hz", sample_rate_hz); |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 43 | } |
| 44 | |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 45 | void GainController2::Process(AudioBuffer* audio) { |
Alex Loiko | e36e8bb | 2018-02-16 11:54:07 +0100 | [diff] [blame] | 46 | AudioFrameView<float> float_frame(audio->channels_f(), audio->num_channels(), |
| 47 | audio->num_frames()); |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 48 | // Apply fixed gain first, then the adaptive one. |
| 49 | gain_applier_.ApplyGain(float_frame); |
Alessio Bazzica | ecf6315 | 2018-11-21 11:47:37 +0100 | [diff] [blame] | 50 | if (config_.adaptive_digital.enabled) { |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 51 | adaptive_agc_->Process(float_frame, limiter_.LastAudioLevel()); |
Alex Loiko | e583174 | 2018-08-24 11:28:36 +0200 | [diff] [blame] | 52 | } |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 53 | limiter_.Process(float_frame); |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Alex Loiko | a837dd7 | 2018-08-06 16:32:12 +0200 | [diff] [blame] | 56 | void GainController2::NotifyAnalogLevel(int level) { |
Alessio Bazzica | ecf6315 | 2018-11-21 11:47:37 +0100 | [diff] [blame] | 57 | if (analog_level_ != level && config_.adaptive_digital.enabled) { |
Alex Loiko | 5e78461 | 2018-11-01 14:51:56 +0100 | [diff] [blame] | 58 | adaptive_agc_->Reset(); |
Alex Loiko | a837dd7 | 2018-08-06 16:32:12 +0200 | [diff] [blame] | 59 | } |
| 60 | analog_level_ = level; |
| 61 | } |
| 62 | |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 63 | void GainController2::ApplyConfig( |
| 64 | const AudioProcessing::Config::GainController2& config) { |
Alex Loiko | 20f60f0 | 2018-11-12 12:09:57 +0100 | [diff] [blame] | 65 | RTC_DCHECK(Validate(config)) |
| 66 | << " the invalid config was " << ToString(config); |
| 67 | |
Alessio Bazzica | 1e2542f | 2018-11-13 14:44:15 +0100 | [diff] [blame] | 68 | config_ = config; |
| 69 | if (config.fixed_digital.gain_db != config_.fixed_digital.gain_db) { |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 70 | // Reset the limiter to quickly react on abrupt level changes caused by |
| 71 | // large changes of the fixed gain. |
| 72 | limiter_.Reset(); |
| 73 | } |
Alessio Bazzica | 1e2542f | 2018-11-13 14:44:15 +0100 | [diff] [blame] | 74 | gain_applier_.SetGainFactor(DbToRatio(config_.fixed_digital.gain_db)); |
Alessio Bazzica | 1e2542f | 2018-11-13 14:44:15 +0100 | [diff] [blame] | 75 | adaptive_agc_.reset(new AdaptiveAgc(data_dumper_.get(), config_)); |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 76 | } |
| 77 | |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 78 | bool GainController2::Validate( |
| 79 | const AudioProcessing::Config::GainController2& config) { |
Alessio Bazzica | 1e2542f | 2018-11-13 14:44:15 +0100 | [diff] [blame] | 80 | return config.fixed_digital.gain_db >= 0.f && |
| 81 | config.fixed_digital.gain_db < 50.f && |
| 82 | config.adaptive_digital.extra_saturation_margin_db >= 0.f && |
| 83 | config.adaptive_digital.extra_saturation_margin_db <= 100.f; |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | std::string GainController2::ToString( |
| 87 | const AudioProcessing::Config::GainController2& config) { |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 88 | rtc::StringBuilder ss; |
Alessio Bazzica | 1e2542f | 2018-11-13 14:44:15 +0100 | [diff] [blame] | 89 | std::string adaptive_digital_level_estimator; |
| 90 | using LevelEstimatorType = |
| 91 | AudioProcessing::Config::GainController2::LevelEstimator; |
| 92 | switch (config.adaptive_digital.level_estimator) { |
| 93 | case LevelEstimatorType::kRms: |
| 94 | adaptive_digital_level_estimator = "RMS"; |
| 95 | break; |
| 96 | case LevelEstimatorType::kPeak: |
| 97 | adaptive_digital_level_estimator = "peak"; |
| 98 | break; |
| 99 | } |
| 100 | // clang-format off |
| 101 | // clang formatting doesn't respect custom nested style. |
| 102 | ss << "{" |
| 103 | << "enabled: " << (config.enabled ? "true" : "false") << ", " |
| 104 | << "fixed_digital: {gain_db: " << config.fixed_digital.gain_db << "}, " |
| 105 | << "adaptive_digital: {" |
| 106 | << "enabled: " |
| 107 | << (config.adaptive_digital.enabled ? "true" : "false") << ", " |
| 108 | << "level_estimator: " << adaptive_digital_level_estimator << ", " |
| 109 | << "extra_saturation_margin_db:" |
| 110 | << config.adaptive_digital.extra_saturation_margin_db << "}" |
| 111 | << "}"; |
| 112 | // clang-format on |
Jonas Olsson | 84df1c7 | 2018-09-14 16:59:32 +0200 | [diff] [blame] | 113 | return ss.Release(); |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | } // namespace webrtc |