Alex Loiko | a05ee82 | 2018-02-20 15:58:36 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
Alessio Bazzica | 746d46b | 2018-10-30 10:48:38 +0100 | [diff] [blame] | 11 | #include "modules/audio_processing/agc2/limiter.h" |
Alex Loiko | a05ee82 | 2018-02-20 15:58:36 +0100 | [diff] [blame] | 12 | |
| 13 | #include <algorithm> |
| 14 | #include <array> |
| 15 | #include <cmath> |
| 16 | |
| 17 | #include "api/array_view.h" |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 18 | #include "modules/audio_processing/agc2/agc2_common.h" |
Alex Loiko | 507e8d1 | 2018-02-27 13:51:47 +0100 | [diff] [blame] | 19 | #include "modules/audio_processing/logging/apm_data_dumper.h" |
Alex Loiko | a05ee82 | 2018-02-20 15:58:36 +0100 | [diff] [blame] | 20 | #include "rtc_base/checks.h" |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 21 | #include "rtc_base/numerics/safe_minmax.h" |
Alex Loiko | a05ee82 | 2018-02-20 15:58:36 +0100 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | namespace { |
| 25 | |
| 26 | // This constant affects the way scaling factors are interpolated for the first |
| 27 | // sub-frame of a frame. Only in the case in which the first sub-frame has an |
| 28 | // estimated level which is greater than the that of the previous analyzed |
| 29 | // sub-frame, linear interpolation is replaced with a power function which |
| 30 | // reduces the chances of over-shooting (and hence saturation), however reducing |
| 31 | // the fixed gain effectiveness. |
| 32 | constexpr float kAttackFirstSubframeInterpolationPower = 8.f; |
| 33 | |
| 34 | void InterpolateFirstSubframe(float last_factor, |
| 35 | float current_factor, |
| 36 | rtc::ArrayView<float> subframe) { |
| 37 | const auto n = subframe.size(); |
| 38 | constexpr auto p = kAttackFirstSubframeInterpolationPower; |
| 39 | for (size_t i = 0; i < n; ++i) { |
| 40 | subframe[i] = std::pow(1.f - i / n, p) * (last_factor - current_factor) + |
| 41 | current_factor; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | void ComputePerSampleSubframeFactors( |
| 46 | const std::array<float, kSubFramesInFrame + 1>& scaling_factors, |
| 47 | size_t samples_per_channel, |
| 48 | rtc::ArrayView<float> per_sample_scaling_factors) { |
| 49 | const size_t num_subframes = scaling_factors.size() - 1; |
| 50 | const size_t subframe_size = |
| 51 | rtc::CheckedDivExact(samples_per_channel, num_subframes); |
| 52 | |
| 53 | // Handle first sub-frame differently in case of attack. |
| 54 | const bool is_attack = scaling_factors[0] > scaling_factors[1]; |
| 55 | if (is_attack) { |
| 56 | InterpolateFirstSubframe( |
| 57 | scaling_factors[0], scaling_factors[1], |
| 58 | rtc::ArrayView<float>( |
| 59 | per_sample_scaling_factors.subview(0, subframe_size))); |
| 60 | } |
| 61 | |
| 62 | for (size_t i = is_attack ? 1 : 0; i < num_subframes; ++i) { |
| 63 | const size_t subframe_start = i * subframe_size; |
| 64 | const float scaling_start = scaling_factors[i]; |
| 65 | const float scaling_end = scaling_factors[i + 1]; |
| 66 | const float scaling_diff = (scaling_end - scaling_start) / subframe_size; |
| 67 | for (size_t j = 0; j < subframe_size; ++j) { |
| 68 | per_sample_scaling_factors[subframe_start + j] = |
| 69 | scaling_start + scaling_diff * j; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void ScaleSamples(rtc::ArrayView<const float> per_sample_scaling_factors, |
| 75 | AudioFrameView<float> signal) { |
| 76 | const size_t samples_per_channel = signal.samples_per_channel(); |
| 77 | RTC_DCHECK_EQ(samples_per_channel, per_sample_scaling_factors.size()); |
| 78 | for (size_t i = 0; i < signal.num_channels(); ++i) { |
| 79 | auto channel = signal.channel(i); |
| 80 | for (size_t j = 0; j < samples_per_channel; ++j) { |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 81 | channel[j] = rtc::SafeClamp(channel[j] * per_sample_scaling_factors[j], |
| 82 | kMinFloatS16Value, kMaxFloatS16Value); |
Alex Loiko | a05ee82 | 2018-02-20 15:58:36 +0100 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 87 | void CheckLimiterSampleRate(size_t sample_rate_hz) { |
| 88 | // Check that per_sample_scaling_factors_ is large enough. |
| 89 | RTC_DCHECK_LE(sample_rate_hz, |
| 90 | kMaximalNumberOfSamplesPerChannel * 1000 / kFrameDurationMs); |
| 91 | } |
| 92 | |
Alex Loiko | a05ee82 | 2018-02-20 15:58:36 +0100 | [diff] [blame] | 93 | } // namespace |
| 94 | |
Alessio Bazzica | 746d46b | 2018-10-30 10:48:38 +0100 | [diff] [blame] | 95 | Limiter::Limiter(size_t sample_rate_hz, |
| 96 | ApmDataDumper* apm_data_dumper, |
Alessio Bazzica | 76714a6 | 2021-01-13 18:08:40 +0100 | [diff] [blame] | 97 | const std::string& histogram_name) |
Alex Loiko | 03ad9b8 | 2018-08-13 17:40:43 +0200 | [diff] [blame] | 98 | : interp_gain_curve_(apm_data_dumper, histogram_name), |
Alex Loiko | a05ee82 | 2018-02-20 15:58:36 +0100 | [diff] [blame] | 99 | level_estimator_(sample_rate_hz, apm_data_dumper), |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 100 | apm_data_dumper_(apm_data_dumper) { |
| 101 | CheckLimiterSampleRate(sample_rate_hz); |
| 102 | } |
Alex Loiko | a05ee82 | 2018-02-20 15:58:36 +0100 | [diff] [blame] | 103 | |
Alessio Bazzica | 746d46b | 2018-10-30 10:48:38 +0100 | [diff] [blame] | 104 | Limiter::~Limiter() = default; |
Alex Loiko | a05ee82 | 2018-02-20 15:58:36 +0100 | [diff] [blame] | 105 | |
Alessio Bazzica | 746d46b | 2018-10-30 10:48:38 +0100 | [diff] [blame] | 106 | void Limiter::Process(AudioFrameView<float> signal) { |
Alex Loiko | a05ee82 | 2018-02-20 15:58:36 +0100 | [diff] [blame] | 107 | const auto level_estimate = level_estimator_.ComputeLevel(signal); |
| 108 | |
| 109 | RTC_DCHECK_EQ(level_estimate.size() + 1, scaling_factors_.size()); |
| 110 | scaling_factors_[0] = last_scaling_factor_; |
| 111 | std::transform(level_estimate.begin(), level_estimate.end(), |
| 112 | scaling_factors_.begin() + 1, [this](float x) { |
| 113 | return interp_gain_curve_.LookUpGainToApply(x); |
| 114 | }); |
| 115 | |
| 116 | const size_t samples_per_channel = signal.samples_per_channel(); |
| 117 | RTC_DCHECK_LE(samples_per_channel, kMaximalNumberOfSamplesPerChannel); |
| 118 | |
| 119 | auto per_sample_scaling_factors = rtc::ArrayView<float>( |
| 120 | &per_sample_scaling_factors_[0], samples_per_channel); |
| 121 | ComputePerSampleSubframeFactors(scaling_factors_, samples_per_channel, |
| 122 | per_sample_scaling_factors); |
| 123 | ScaleSamples(per_sample_scaling_factors, signal); |
| 124 | |
| 125 | last_scaling_factor_ = scaling_factors_.back(); |
| 126 | |
| 127 | // Dump data for debug. |
Alessio Bazzica | 8aaa604 | 2021-03-31 15:16:05 +0200 | [diff] [blame] | 128 | apm_data_dumper_->DumpRaw("agc2_limiter_last_scaling_factor", |
| 129 | last_scaling_factor_); |
| 130 | apm_data_dumper_->DumpRaw( |
| 131 | "agc2_limiter_region", |
| 132 | static_cast<int>(interp_gain_curve_.get_stats().region)); |
Alex Loiko | a05ee82 | 2018-02-20 15:58:36 +0100 | [diff] [blame] | 133 | } |
| 134 | |
Alessio Bazzica | 746d46b | 2018-10-30 10:48:38 +0100 | [diff] [blame] | 135 | InterpolatedGainCurve::Stats Limiter::GetGainCurveStats() const { |
Alex Loiko | a05ee82 | 2018-02-20 15:58:36 +0100 | [diff] [blame] | 136 | return interp_gain_curve_.get_stats(); |
| 137 | } |
| 138 | |
Alessio Bazzica | 746d46b | 2018-10-30 10:48:38 +0100 | [diff] [blame] | 139 | void Limiter::SetSampleRate(size_t sample_rate_hz) { |
Alessio Bazzica | 3e4c77f | 2018-11-01 21:31:38 +0100 | [diff] [blame] | 140 | CheckLimiterSampleRate(sample_rate_hz); |
Alex Loiko | a05ee82 | 2018-02-20 15:58:36 +0100 | [diff] [blame] | 141 | level_estimator_.SetSampleRate(sample_rate_hz); |
Alex Loiko | a05ee82 | 2018-02-20 15:58:36 +0100 | [diff] [blame] | 142 | } |
| 143 | |
Alessio Bazzica | 746d46b | 2018-10-30 10:48:38 +0100 | [diff] [blame] | 144 | void Limiter::Reset() { |
Alessio Bazzica | 82ec0fa | 2018-08-27 14:24:16 +0200 | [diff] [blame] | 145 | level_estimator_.Reset(); |
| 146 | } |
| 147 | |
Alessio Bazzica | 746d46b | 2018-10-30 10:48:38 +0100 | [diff] [blame] | 148 | float Limiter::LastAudioLevel() const { |
Alex Loiko | 93e5750 | 2018-10-01 16:28:47 +0200 | [diff] [blame] | 149 | return level_estimator_.LastAudioLevel(); |
| 150 | } |
| 151 | |
Alex Loiko | a05ee82 | 2018-02-20 15:58:36 +0100 | [diff] [blame] | 152 | } // namespace webrtc |