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" |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 21 | int GainController2::instance_count_ = 0; |
| 22 | |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 23 | GainController2::GainController2() |
| 24 | : data_dumper_( |
| 25 | new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))), |
Alex Loiko | 9d2788f | 2018-03-29 11:02:43 +0200 | [diff] [blame] | 26 | fixed_gain_controller_(data_dumper_.get()), |
| 27 | adaptive_agc_(data_dumper_.get()) {} |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 28 | |
| 29 | GainController2::~GainController2() = default; |
| 30 | |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 31 | void GainController2::Initialize(int sample_rate_hz) { |
| 32 | RTC_DCHECK(sample_rate_hz == AudioProcessing::kSampleRate8kHz || |
| 33 | sample_rate_hz == AudioProcessing::kSampleRate16kHz || |
| 34 | sample_rate_hz == AudioProcessing::kSampleRate32kHz || |
| 35 | sample_rate_hz == AudioProcessing::kSampleRate48kHz); |
Alex Loiko | 9d2788f | 2018-03-29 11:02:43 +0200 | [diff] [blame] | 36 | fixed_gain_controller_.SetSampleRate(sample_rate_hz); |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 37 | data_dumper_->InitiateNewSetOfRecordings(); |
Alex Loiko | e36e8bb | 2018-02-16 11:54:07 +0100 | [diff] [blame] | 38 | data_dumper_->DumpRaw("sample_rate_hz", sample_rate_hz); |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 39 | } |
| 40 | |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 41 | void GainController2::Process(AudioBuffer* audio) { |
Alex Loiko | e36e8bb | 2018-02-16 11:54:07 +0100 | [diff] [blame] | 42 | AudioFrameView<float> float_frame(audio->channels_f(), audio->num_channels(), |
| 43 | audio->num_frames()); |
Alex Loiko | 9d2788f | 2018-03-29 11:02:43 +0200 | [diff] [blame] | 44 | adaptive_agc_.Process(float_frame); |
| 45 | fixed_gain_controller_.Process(float_frame); |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Alex Loiko | a837dd7 | 2018-08-06 16:32:12 +0200 | [diff] [blame^] | 48 | void GainController2::NotifyAnalogLevel(int level) { |
| 49 | if (analog_level_ != level) { |
| 50 | adaptive_agc_.Reset(); |
| 51 | } |
| 52 | analog_level_ = level; |
| 53 | } |
| 54 | |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 55 | void GainController2::ApplyConfig( |
| 56 | const AudioProcessing::Config::GainController2& config) { |
| 57 | RTC_DCHECK(Validate(config)); |
Alex Loiko | e36e8bb | 2018-02-16 11:54:07 +0100 | [diff] [blame] | 58 | config_ = config; |
Alex Loiko | 9d2788f | 2018-03-29 11:02:43 +0200 | [diff] [blame] | 59 | fixed_gain_controller_.SetGain(config_.fixed_gain_db); |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 60 | } |
| 61 | |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 62 | bool GainController2::Validate( |
| 63 | const AudioProcessing::Config::GainController2& config) { |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 64 | return config.fixed_gain_db >= 0.f; |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | std::string GainController2::ToString( |
| 68 | const AudioProcessing::Config::GainController2& config) { |
| 69 | std::stringstream ss; |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 70 | ss << "{enabled: " << (config.enabled ? "true" : "false") << ", " |
Alex Loiko | 9d2788f | 2018-03-29 11:02:43 +0200 | [diff] [blame] | 71 | << "fixed_gain_dB: " << config.fixed_gain_db << "}"; |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 72 | return ss.str(); |
| 73 | } |
| 74 | |
| 75 | } // namespace webrtc |