You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
5.5 KiB

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