blob: 38eb1de03f9589bbd7fed044b830e88fc28c1470 [file] [log] [blame]
Alex Loiko8a3eadd2018-04-13 11:15:34 +02001/*
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/gain_applier.h"
12
13#include "modules/audio_processing/agc2/agc2_common.h"
14#include "rtc_base/numerics/safe_minmax.h"
15
16namespace webrtc {
17namespace {
18
19// Returns true when the gain factor is so close to 1 that it would
20// not affect int16 samples.
21bool GainCloseToOne(float gain_factor) {
22 return 1.f - 1.f / kMaxFloatS16Value <= gain_factor &&
23 gain_factor <= 1.f + 1.f / kMaxFloatS16Value;
24}
25
26void ClipSignal(AudioFrameView<float> signal) {
27 for (size_t k = 0; k < signal.num_channels(); ++k) {
28 rtc::ArrayView<float> channel_view = signal.channel(k);
29 for (auto& sample : channel_view) {
30 sample = rtc::SafeClamp(sample, kMinFloatS16Value, kMaxFloatS16Value);
31 }
32 }
33}
34
35void ApplyGainWithRamping(float last_gain_linear,
36 float gain_at_end_of_frame_linear,
37 float inverse_samples_per_channel,
38 AudioFrameView<float> float_frame) {
39 // Do not modify the signal.
40 if (last_gain_linear == gain_at_end_of_frame_linear &&
41 GainCloseToOne(gain_at_end_of_frame_linear)) {
42 return;
43 }
44
45 // Gain is constant and different from 1.
46 if (last_gain_linear == gain_at_end_of_frame_linear) {
47 for (size_t k = 0; k < float_frame.num_channels(); ++k) {
48 rtc::ArrayView<float> channel_view = float_frame.channel(k);
49 for (auto& sample : channel_view) {
50 sample *= gain_at_end_of_frame_linear;
51 }
52 }
53 return;
54 }
55
56 // The gain changes. We have to change slowly to avoid discontinuities.
57 const float increment = (gain_at_end_of_frame_linear - last_gain_linear) *
58 inverse_samples_per_channel;
59 float gain = last_gain_linear;
60 for (size_t i = 0; i < float_frame.samples_per_channel(); ++i) {
61 for (size_t ch = 0; ch < float_frame.num_channels(); ++ch) {
62 float_frame.channel(ch)[i] *= gain;
63 }
64 gain += increment;
65 }
66}
67
68} // namespace
69
70GainApplier::GainApplier(bool hard_clip_samples, float initial_gain_factor)
71 : hard_clip_samples_(hard_clip_samples),
72 last_gain_factor_(initial_gain_factor),
73 current_gain_factor_(initial_gain_factor) {}
74
75void GainApplier::ApplyGain(AudioFrameView<float> signal) {
76 if (static_cast<int>(signal.samples_per_channel()) != samples_per_channel_) {
77 Initialize(signal.samples_per_channel());
78 }
79
80 ApplyGainWithRamping(last_gain_factor_, current_gain_factor_,
81 inverse_samples_per_channel_, signal);
82
83 last_gain_factor_ = current_gain_factor_;
84
85 if (hard_clip_samples_) {
86 ClipSignal(signal);
87 }
88}
89
90void GainApplier::SetGainFactor(float gain_factor) {
91 RTC_DCHECK_GT(gain_factor, 0.f);
92 current_gain_factor_ = gain_factor;
93}
94
95void GainApplier::Initialize(size_t samples_per_channel) {
96 RTC_DCHECK_GT(samples_per_channel, 0);
97 samples_per_channel_ = static_cast<int>(samples_per_channel);
98 inverse_samples_per_channel_ = 1.f / samples_per_channel_;
99}
100
101} // namespace webrtc