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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 13 | #include "modules/audio_processing/audio_buffer.h" |
Alex Loiko | e36e8bb | 2018-02-16 11:54:07 +0100 | [diff] [blame] | 14 | #include "modules/audio_processing/include/audio_frame_view.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "modules/audio_processing/logging/apm_data_dumper.h" |
| 16 | #include "rtc_base/atomicops.h" |
| 17 | #include "rtc_base/checks.h" |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 18 | #include "rtc_base/strings/string_builder.h" |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 19 | |
| 20 | namespace webrtc { |
| 21 | |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 22 | int GainController2::instance_count_ = 0; |
| 23 | |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 24 | GainController2::GainController2() |
| 25 | : data_dumper_( |
| 26 | new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))), |
Alex Loiko | 9d2788f | 2018-03-29 11:02:43 +0200 | [diff] [blame] | 27 | fixed_gain_controller_(data_dumper_.get()), |
Alex Loiko | 5e78461 | 2018-11-01 14:51:56 +0100 | [diff] [blame] | 28 | adaptive_agc_(new AdaptiveAgc(data_dumper_.get())) {} |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 29 | |
| 30 | GainController2::~GainController2() = default; |
| 31 | |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 32 | void GainController2::Initialize(int sample_rate_hz) { |
| 33 | RTC_DCHECK(sample_rate_hz == AudioProcessing::kSampleRate8kHz || |
| 34 | sample_rate_hz == AudioProcessing::kSampleRate16kHz || |
| 35 | sample_rate_hz == AudioProcessing::kSampleRate32kHz || |
| 36 | sample_rate_hz == AudioProcessing::kSampleRate48kHz); |
Alex Loiko | 9d2788f | 2018-03-29 11:02:43 +0200 | [diff] [blame] | 37 | fixed_gain_controller_.SetSampleRate(sample_rate_hz); |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 38 | data_dumper_->InitiateNewSetOfRecordings(); |
Alex Loiko | e36e8bb | 2018-02-16 11:54:07 +0100 | [diff] [blame] | 39 | data_dumper_->DumpRaw("sample_rate_hz", sample_rate_hz); |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 40 | } |
| 41 | |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 42 | void GainController2::Process(AudioBuffer* audio) { |
Alex Loiko | e36e8bb | 2018-02-16 11:54:07 +0100 | [diff] [blame] | 43 | AudioFrameView<float> float_frame(audio->channels_f(), audio->num_channels(), |
| 44 | audio->num_frames()); |
Alex Loiko | e583174 | 2018-08-24 11:28:36 +0200 | [diff] [blame] | 45 | if (adaptive_digital_mode_) { |
Alex Loiko | 5e78461 | 2018-11-01 14:51:56 +0100 | [diff] [blame] | 46 | adaptive_agc_->Process(float_frame, |
| 47 | fixed_gain_controller_.LastAudioLevel()); |
Alex Loiko | e583174 | 2018-08-24 11:28:36 +0200 | [diff] [blame] | 48 | } |
Alex Loiko | 9d2788f | 2018-03-29 11:02:43 +0200 | [diff] [blame] | 49 | fixed_gain_controller_.Process(float_frame); |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Alex Loiko | a837dd7 | 2018-08-06 16:32:12 +0200 | [diff] [blame] | 52 | void GainController2::NotifyAnalogLevel(int level) { |
Alex Loiko | e583174 | 2018-08-24 11:28:36 +0200 | [diff] [blame] | 53 | if (analog_level_ != level && adaptive_digital_mode_) { |
Alex Loiko | 5e78461 | 2018-11-01 14:51:56 +0100 | [diff] [blame] | 54 | adaptive_agc_->Reset(); |
Alex Loiko | a837dd7 | 2018-08-06 16:32:12 +0200 | [diff] [blame] | 55 | } |
| 56 | analog_level_ = level; |
| 57 | } |
| 58 | |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 59 | void GainController2::ApplyConfig( |
| 60 | const AudioProcessing::Config::GainController2& config) { |
| 61 | RTC_DCHECK(Validate(config)); |
Alex Loiko | e36e8bb | 2018-02-16 11:54:07 +0100 | [diff] [blame] | 62 | config_ = config; |
Alex Loiko | 9d2788f | 2018-03-29 11:02:43 +0200 | [diff] [blame] | 63 | fixed_gain_controller_.SetGain(config_.fixed_gain_db); |
Alex Loiko | e583174 | 2018-08-24 11:28:36 +0200 | [diff] [blame] | 64 | adaptive_digital_mode_ = config_.adaptive_digital_mode; |
Alex Loiko | 5e78461 | 2018-11-01 14:51:56 +0100 | [diff] [blame] | 65 | adaptive_agc_.reset( |
| 66 | new AdaptiveAgc(data_dumper_.get(), config_.extra_saturation_margin_db)); |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 67 | } |
| 68 | |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 69 | bool GainController2::Validate( |
| 70 | const AudioProcessing::Config::GainController2& config) { |
Alex Loiko | 5e78461 | 2018-11-01 14:51:56 +0100 | [diff] [blame] | 71 | return config.fixed_gain_db >= 0.f && |
| 72 | config.extra_saturation_margin_db >= 0.f && |
| 73 | config.extra_saturation_margin_db <= 100.f; |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | std::string GainController2::ToString( |
| 77 | const AudioProcessing::Config::GainController2& config) { |
Jonas Olsson | 366a50c | 2018-09-06 13:41:30 +0200 | [diff] [blame] | 78 | rtc::StringBuilder ss; |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 79 | ss << "{enabled: " << (config.enabled ? "true" : "false") << ", " |
Alex Loiko | 9d2788f | 2018-03-29 11:02:43 +0200 | [diff] [blame] | 80 | << "fixed_gain_dB: " << config.fixed_gain_db << "}"; |
Jonas Olsson | 84df1c7 | 2018-09-14 16:59:32 +0200 | [diff] [blame] | 81 | return ss.Release(); |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | } // namespace webrtc |