blob: 0921b28855c8a125a82857411d97b1c2c3bf5470 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "modules/audio_processing/audio_buffer.h"
Alex Loikoe36e8bb2018-02-16 11:54:07 +010014#include "modules/audio_processing/include/audio_frame_view.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "modules/audio_processing/logging/apm_data_dumper.h"
16#include "rtc_base/atomicops.h"
17#include "rtc_base/checks.h"
Jonas Olsson366a50c2018-09-06 13:41:30 +020018#include "rtc_base/strings/string_builder.h"
alessiob3ec96df2017-05-22 06:57:06 -070019
20namespace webrtc {
21
alessiob3ec96df2017-05-22 06:57:06 -070022int GainController2::instance_count_ = 0;
23
Alessio Bazzica270f7b52017-10-13 11:05:17 +020024GainController2::GainController2()
25 : data_dumper_(
26 new ApmDataDumper(rtc::AtomicOps::Increment(&instance_count_))),
Alex Loiko9d2788f2018-03-29 11:02:43 +020027 fixed_gain_controller_(data_dumper_.get()),
Alex Loiko5e784612018-11-01 14:51:56 +010028 adaptive_agc_(new AdaptiveAgc(data_dumper_.get())) {}
alessiob3ec96df2017-05-22 06:57:06 -070029
30GainController2::~GainController2() = default;
31
Alessio Bazzica270f7b52017-10-13 11:05:17 +020032void 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 Loiko9d2788f2018-03-29 11:02:43 +020037 fixed_gain_controller_.SetSampleRate(sample_rate_hz);
Alessio Bazzica270f7b52017-10-13 11:05:17 +020038 data_dumper_->InitiateNewSetOfRecordings();
Alex Loikoe36e8bb2018-02-16 11:54:07 +010039 data_dumper_->DumpRaw("sample_rate_hz", sample_rate_hz);
Alessio Bazzica270f7b52017-10-13 11:05:17 +020040}
41
alessiob3ec96df2017-05-22 06:57:06 -070042void GainController2::Process(AudioBuffer* audio) {
Alex Loikoe36e8bb2018-02-16 11:54:07 +010043 AudioFrameView<float> float_frame(audio->channels_f(), audio->num_channels(),
44 audio->num_frames());
Alex Loikoe5831742018-08-24 11:28:36 +020045 if (adaptive_digital_mode_) {
Alex Loiko5e784612018-11-01 14:51:56 +010046 adaptive_agc_->Process(float_frame,
47 fixed_gain_controller_.LastAudioLevel());
Alex Loikoe5831742018-08-24 11:28:36 +020048 }
Alex Loiko9d2788f2018-03-29 11:02:43 +020049 fixed_gain_controller_.Process(float_frame);
alessiob3ec96df2017-05-22 06:57:06 -070050}
51
Alex Loikoa837dd72018-08-06 16:32:12 +020052void GainController2::NotifyAnalogLevel(int level) {
Alex Loikoe5831742018-08-24 11:28:36 +020053 if (analog_level_ != level && adaptive_digital_mode_) {
Alex Loiko5e784612018-11-01 14:51:56 +010054 adaptive_agc_->Reset();
Alex Loikoa837dd72018-08-06 16:32:12 +020055 }
56 analog_level_ = level;
57}
58
Alessio Bazzica270f7b52017-10-13 11:05:17 +020059void GainController2::ApplyConfig(
60 const AudioProcessing::Config::GainController2& config) {
61 RTC_DCHECK(Validate(config));
Alex Loikoe36e8bb2018-02-16 11:54:07 +010062 config_ = config;
Alex Loiko9d2788f2018-03-29 11:02:43 +020063 fixed_gain_controller_.SetGain(config_.fixed_gain_db);
Alex Loikoe5831742018-08-24 11:28:36 +020064 adaptive_digital_mode_ = config_.adaptive_digital_mode;
Alex Loiko5e784612018-11-01 14:51:56 +010065 adaptive_agc_.reset(
66 new AdaptiveAgc(data_dumper_.get(), config_.extra_saturation_margin_db));
Alessio Bazzica270f7b52017-10-13 11:05:17 +020067}
68
alessiob3ec96df2017-05-22 06:57:06 -070069bool GainController2::Validate(
70 const AudioProcessing::Config::GainController2& config) {
Alex Loiko5e784612018-11-01 14:51:56 +010071 return config.fixed_gain_db >= 0.f &&
72 config.extra_saturation_margin_db >= 0.f &&
73 config.extra_saturation_margin_db <= 100.f;
alessiob3ec96df2017-05-22 06:57:06 -070074}
75
76std::string GainController2::ToString(
77 const AudioProcessing::Config::GainController2& config) {
Jonas Olsson366a50c2018-09-06 13:41:30 +020078 rtc::StringBuilder ss;
Alessio Bazzica270f7b52017-10-13 11:05:17 +020079 ss << "{enabled: " << (config.enabled ? "true" : "false") << ", "
Alex Loiko9d2788f2018-03-29 11:02:43 +020080 << "fixed_gain_dB: " << config.fixed_gain_db << "}";
Jonas Olsson84df1c72018-09-14 16:59:32 +020081 return ss.Release();
alessiob3ec96df2017-05-22 06:57:06 -070082}
83
84} // namespace webrtc