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