blob: f256be04f4fb3817a13660e2bf1f6c7423e68122 [file] [log] [blame]
alessiob3ec96df2017-05-22 06:57:06 -07001/*
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 Loikoe36e8bb2018-02-16 11:54:07 +010011#include "modules/audio_processing/gain_controller2.h"
alessiob3ec96df2017-05-22 06:57:06 -070012
Alessio Bazzica3e4c77f2018-11-01 21:31:38 +010013#include "common_audio/include/audio_util.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "modules/audio_processing/audio_buffer.h"
Alex Loikoe36e8bb2018-02-16 11:54:07 +010015#include "modules/audio_processing/include/audio_frame_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "modules/audio_processing/logging/apm_data_dumper.h"
17#include "rtc_base/atomicops.h"
18#include "rtc_base/checks.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020019#include "rtc_base/strings/string_builder.h"
alessiob3ec96df2017-05-22 06:57:06 -070020
21namespace webrtc {
22
alessiob3ec96df2017-05-22 06:57:06 -070023int GainController2::instance_count_ = 0;
24
Alessio Bazzica270f7b52017-10-13 11:05:17 +020025GainController2::GainController2()
26 : data_dumper_(
27 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
Alessio Bazzica3e4c77f2018-11-01 21:31:38 +010028 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") {}
alessiob3ec96df2017-05-22 06:57:06 -070032
33GainController2::~GainController2() = default;
34
Alessio Bazzica270f7b52017-10-13 11:05:17 +020035void 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 Bazzica3e4c77f2018-11-01 21:31:38 +010040 limiter_.SetSampleRate(sample_rate_hz);
Alessio Bazzica270f7b52017-10-13 11:05:17 +020041 data_dumper_->InitiateNewSetOfRecordings();
Alex Loikoe36e8bb2018-02-16 11:54:07 +010042 data_dumper_->DumpRaw("sample_rate_hz", sample_rate_hz);
Alessio Bazzica270f7b52017-10-13 11:05:17 +020043}
44
alessiob3ec96df2017-05-22 06:57:06 -070045void GainController2::Process(AudioBuffer* audio) {
Alex Loikoe36e8bb2018-02-16 11:54:07 +010046 AudioFrameView<float> float_frame(audio->channels_f(), audio->num_channels(),
47 audio->num_frames());
Alessio Bazzica3e4c77f2018-11-01 21:31:38 +010048 // Apply fixed gain first, then the adaptive one.
49 gain_applier_.ApplyGain(float_frame);
Alex Loikoe5831742018-08-24 11:28:36 +020050 if (adaptive_digital_mode_) {
Alessio Bazzica3e4c77f2018-11-01 21:31:38 +010051 adaptive_agc_->Process(float_frame, limiter_.LastAudioLevel());
Alex Loikoe5831742018-08-24 11:28:36 +020052 }
Alessio Bazzica3e4c77f2018-11-01 21:31:38 +010053 limiter_.Process(float_frame);
alessiob3ec96df2017-05-22 06:57:06 -070054}
55
Alex Loikoa837dd72018-08-06 16:32:12 +020056void GainController2::NotifyAnalogLevel(int level) {
Alex Loikoe5831742018-08-24 11:28:36 +020057 if (analog_level_ != level && adaptive_digital_mode_) {
Alex Loiko5e784612018-11-01 14:51:56 +010058 adaptive_agc_->Reset();
Alex Loikoa837dd72018-08-06 16:32:12 +020059 }
60 analog_level_ = level;
61}
62
Alessio Bazzica270f7b52017-10-13 11:05:17 +020063void GainController2::ApplyConfig(
64 const AudioProcessing::Config::GainController2& config) {
65 RTC_DCHECK(Validate(config));
Alex Loikoe36e8bb2018-02-16 11:54:07 +010066 config_ = config;
Alessio Bazzica3e4c77f2018-11-01 21:31:38 +010067 if (gain_applier_.GetGainFactor() != config_.fixed_gain_db) {
68 // Reset the limiter to quickly react on abrupt level changes caused by
69 // large changes of the fixed gain.
70 limiter_.Reset();
71 }
72 gain_applier_.SetGainFactor(DbToRatio(config_.fixed_gain_db));
Alex Loikoe5831742018-08-24 11:28:36 +020073 adaptive_digital_mode_ = config_.adaptive_digital_mode;
Alex Loiko5e784612018-11-01 14:51:56 +010074 adaptive_agc_.reset(
75 new AdaptiveAgc(data_dumper_.get(), config_.extra_saturation_margin_db));
Alessio Bazzica270f7b52017-10-13 11:05:17 +020076}
77
alessiob3ec96df2017-05-22 06:57:06 -070078bool GainController2::Validate(
79 const AudioProcessing::Config::GainController2& config) {
Alex Loiko5e784612018-11-01 14:51:56 +010080 return config.fixed_gain_db >= 0.f &&
81 config.extra_saturation_margin_db >= 0.f &&
82 config.extra_saturation_margin_db <= 100.f;
alessiob3ec96df2017-05-22 06:57:06 -070083}
84
85std::string GainController2::ToString(
86 const AudioProcessing::Config::GainController2& config) {
Jonas Olsson366a50c2018-09-06 13:41:30 +020087 rtc::StringBuilder ss;
Alessio Bazzica270f7b52017-10-13 11:05:17 +020088 ss << "{enabled: " << (config.enabled ? "true" : "false") << ", "
Alex Loiko9d2788f2018-03-29 11:02:43 +020089 << "fixed_gain_dB: " << config.fixed_gain_db << "}";
Jonas Olsson84df1c72018-09-14 16:59:32 +020090 return ss.Release();
alessiob3ec96df2017-05-22 06:57:06 -070091}
92
93} // namespace webrtc