xref: /aosp_15_r20/external/webrtc/modules/audio_processing/agc2/limiter.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 
11 #ifndef MODULES_AUDIO_PROCESSING_AGC2_LIMITER_H_
12 #define MODULES_AUDIO_PROCESSING_AGC2_LIMITER_H_
13 
14 #include <vector>
15 
16 #include "absl/strings/string_view.h"
17 #include "modules/audio_processing/agc2/fixed_digital_level_estimator.h"
18 #include "modules/audio_processing/agc2/interpolated_gain_curve.h"
19 #include "modules/audio_processing/include/audio_frame_view.h"
20 
21 namespace webrtc {
22 class ApmDataDumper;
23 
24 class Limiter {
25  public:
26   Limiter(int sample_rate_hz,
27           ApmDataDumper* apm_data_dumper,
28           absl::string_view histogram_name_prefix);
29   Limiter(const Limiter& limiter) = delete;
30   Limiter& operator=(const Limiter& limiter) = delete;
31   ~Limiter();
32 
33   // Applies limiter and hard-clipping to `signal`.
34   void Process(AudioFrameView<float> signal);
35   InterpolatedGainCurve::Stats GetGainCurveStats() const;
36 
37   // Supported rates must be
38   // * supported by FixedDigitalLevelEstimator
39   // * below kMaximalNumberOfSamplesPerChannel*1000/kFrameDurationMs
40   //   so that samples_per_channel fit in the
41   //   per_sample_scaling_factors_ array.
42   void SetSampleRate(int sample_rate_hz);
43 
44   // Resets the internal state.
45   void Reset();
46 
47   float LastAudioLevel() const;
48 
49  private:
50   const InterpolatedGainCurve interp_gain_curve_;
51   FixedDigitalLevelEstimator level_estimator_;
52   ApmDataDumper* const apm_data_dumper_ = nullptr;
53 
54   // Work array containing the sub-frame scaling factors to be interpolated.
55   std::array<float, kSubFramesInFrame + 1> scaling_factors_ = {};
56   std::array<float, kMaximalNumberOfSamplesPerChannel>
57       per_sample_scaling_factors_ = {};
58   float last_scaling_factor_ = 1.f;
59 };
60 
61 }  // namespace webrtc
62 
63 #endif  // MODULES_AUDIO_PROCESSING_AGC2_LIMITER_H_
64