blob: d49d18117b881ecbdd80496a3d8fd9ad5e290c87 [file] [log] [blame]
Alex Loikoe36e8bb2018-02-16 11:54:07 +01001/*
2 * Copyright (c) 2018 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
11#include "modules/audio_processing/agc2/fixed_gain_controller.h"
12
13#include <algorithm>
14#include <cmath>
15
16#include "api/array_view.h"
17#include "common_audio/include/audio_util.h"
18#include "modules/audio_processing/agc2/agc2_common.h"
Alex Loikoa05ee822018-02-20 15:58:36 +010019#include "modules/audio_processing/agc2/interpolated_gain_curve.h"
Alex Loikoe36e8bb2018-02-16 11:54:07 +010020#include "modules/audio_processing/logging/apm_data_dumper.h"
21#include "rtc_base/checks.h"
22#include "rtc_base/logging.h"
23#include "rtc_base/numerics/safe_minmax.h"
24
25namespace webrtc {
26namespace {
27
28// Returns true when the gain factor is so close to 1 that it would
29// not affect int16 samples.
30bool CloseToOne(float gain_factor) {
Alex Loikoa05ee822018-02-20 15:58:36 +010031 return 1.f - 1.f / kMaxFloatS16Value <= gain_factor &&
32 gain_factor <= 1.f + 1.f / kMaxFloatS16Value;
Alex Loikoe36e8bb2018-02-16 11:54:07 +010033}
34} // namespace
35
36FixedGainController::FixedGainController(ApmDataDumper* apm_data_dumper)
Alex Loiko03ad9b82018-08-13 17:40:43 +020037 : FixedGainController(apm_data_dumper, "Agc2") {}
38
39FixedGainController::FixedGainController(ApmDataDumper* apm_data_dumper,
40 std::string histogram_name_prefix)
Alex Loikoa05ee822018-02-20 15:58:36 +010041 : apm_data_dumper_(apm_data_dumper),
Alex Loiko03ad9b82018-08-13 17:40:43 +020042 gain_curve_applier_(48000, apm_data_dumper_, histogram_name_prefix) {
43 // Do update histograms.xml when adding name prefixes.
44 RTC_DCHECK(histogram_name_prefix == "" || histogram_name_prefix == "Test" ||
45 histogram_name_prefix == "AudioMixer" ||
46 histogram_name_prefix == "Agc2");
47}
Alex Loikoe36e8bb2018-02-16 11:54:07 +010048
49void FixedGainController::SetGain(float gain_to_apply_db) {
50 // Changes in gain_to_apply_ cause discontinuities. We assume
51 // gain_to_apply_ is set in the beginning of the call. If it is
52 // frequently changed, we should add interpolation between the
53 // values.
Alex Loikoa05ee822018-02-20 15:58:36 +010054 // The gain
55 RTC_DCHECK_LE(-50.f, gain_to_apply_db);
56 RTC_DCHECK_LE(gain_to_apply_db, 50.f);
Alessio Bazzica82ec0fa2018-08-27 14:24:16 +020057 const float previous_applied_gained = gain_to_apply_;
Alex Loikoe36e8bb2018-02-16 11:54:07 +010058 gain_to_apply_ = DbToRatio(gain_to_apply_db);
Alex Loikoa05ee822018-02-20 15:58:36 +010059 RTC_DCHECK_LT(0.f, gain_to_apply_);
60 RTC_DLOG(LS_INFO) << "Gain to apply: " << gain_to_apply_db << " db.";
Alessio Bazzica82ec0fa2018-08-27 14:24:16 +020061 // Reset the gain curve applier to quickly react on abrupt level changes
62 // caused by large changes of the applied gain.
63 if (previous_applied_gained != gain_to_apply_) {
64 gain_curve_applier_.Reset();
65 }
Alex Loikoe36e8bb2018-02-16 11:54:07 +010066}
67
68void FixedGainController::SetSampleRate(size_t sample_rate_hz) {
Alex Loikoa05ee822018-02-20 15:58:36 +010069 gain_curve_applier_.SetSampleRate(sample_rate_hz);
Alex Loikoe36e8bb2018-02-16 11:54:07 +010070}
71
Alex Loikoe36e8bb2018-02-16 11:54:07 +010072void FixedGainController::Process(AudioFrameView<float> signal) {
Alex Loiko8a3eadd2018-04-13 11:15:34 +020073 // Apply fixed digital gain. One of the
Alex Loikoe36e8bb2018-02-16 11:54:07 +010074 // planned usages of the FGC is to only use the limiter. In that
75 // case, the gain would be 1.0. Not doing the multiplications speeds
76 // it up considerably. Hence the check.
77 if (!CloseToOne(gain_to_apply_)) {
78 for (size_t k = 0; k < signal.num_channels(); ++k) {
79 rtc::ArrayView<float> channel_view = signal.channel(k);
80 for (auto& sample : channel_view) {
81 sample *= gain_to_apply_;
82 }
83 }
84 }
85
Alex Loiko9d2788f2018-03-29 11:02:43 +020086 // Use the limiter.
87 gain_curve_applier_.Process(signal);
Alex Loikoe36e8bb2018-02-16 11:54:07 +010088
Alex Loiko9d2788f2018-03-29 11:02:43 +020089 // Dump data for debug.
90 const auto channel_view = signal.channel(0);
91 apm_data_dumper_->DumpRaw("agc2_fixed_digital_gain_curve_applier",
92 channel_view.size(), channel_view.data());
Alex Loikoe36e8bb2018-02-16 11:54:07 +010093 // Hard-clipping.
94 for (size_t k = 0; k < signal.num_channels(); ++k) {
95 rtc::ArrayView<float> channel_view = signal.channel(k);
96 for (auto& sample : channel_view) {
Alex Loikoa05ee822018-02-20 15:58:36 +010097 sample = rtc::SafeClamp(sample, kMinFloatS16Value, kMaxFloatS16Value);
Alex Loikoe36e8bb2018-02-16 11:54:07 +010098 }
99 }
100}
101} // namespace webrtc