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 | e36e8bb | 2018-02-16 11:54:07 +0100 | [diff] [blame] | 26 | gain_controller_(data_dumper_.get()) {} |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 27 | |
| 28 | GainController2::~GainController2() = default; |
| 29 | |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 30 | void GainController2::Initialize(int sample_rate_hz) { |
| 31 | RTC_DCHECK(sample_rate_hz == AudioProcessing::kSampleRate8kHz || |
| 32 | sample_rate_hz == AudioProcessing::kSampleRate16kHz || |
| 33 | sample_rate_hz == AudioProcessing::kSampleRate32kHz || |
| 34 | sample_rate_hz == AudioProcessing::kSampleRate48kHz); |
Alex Loiko | e36e8bb | 2018-02-16 11:54:07 +0100 | [diff] [blame] | 35 | gain_controller_.SetSampleRate(sample_rate_hz); |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 36 | data_dumper_->InitiateNewSetOfRecordings(); |
Alex Loiko | e36e8bb | 2018-02-16 11:54:07 +0100 | [diff] [blame] | 37 | data_dumper_->DumpRaw("sample_rate_hz", sample_rate_hz); |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 38 | } |
| 39 | |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 40 | void GainController2::Process(AudioBuffer* audio) { |
Alex Loiko | e36e8bb | 2018-02-16 11:54:07 +0100 | [diff] [blame] | 41 | AudioFrameView<float> float_frame(audio->channels_f(), audio->num_channels(), |
| 42 | audio->num_frames()); |
| 43 | gain_controller_.Process(float_frame); |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 46 | void GainController2::ApplyConfig( |
| 47 | const AudioProcessing::Config::GainController2& config) { |
| 48 | RTC_DCHECK(Validate(config)); |
Alex Loiko | e36e8bb | 2018-02-16 11:54:07 +0100 | [diff] [blame] | 49 | config_ = config; |
| 50 | gain_controller_.SetGain(config_.fixed_gain_db); |
| 51 | gain_controller_.EnableLimiter(config_.enable_limiter); |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 52 | } |
| 53 | |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 54 | bool GainController2::Validate( |
| 55 | const AudioProcessing::Config::GainController2& config) { |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 56 | return config.fixed_gain_db >= 0.f; |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | std::string GainController2::ToString( |
| 60 | const AudioProcessing::Config::GainController2& config) { |
| 61 | std::stringstream ss; |
Alessio Bazzica | 270f7b5 | 2017-10-13 11:05:17 +0200 | [diff] [blame] | 62 | ss << "{enabled: " << (config.enabled ? "true" : "false") << ", " |
Alex Loiko | e36e8bb | 2018-02-16 11:54:07 +0100 | [diff] [blame] | 63 | << "fixed_gain_dB: " << config.fixed_gain_db << ", " |
| 64 | << "enable_limiter: " << (config.enable_limiter ? "true" : "false") << "}"; |
alessiob | 3ec96df | 2017-05-22 06:57:06 -0700 | [diff] [blame] | 65 | return ss.str(); |
| 66 | } |
| 67 | |
| 68 | } // namespace webrtc |