blob: 599fd0f4bb5397b4222da2d5ea8f6437d5ca3ecf [file] [log] [blame]
Alex Loikoa05ee822018-02-20 15:58:36 +01001/*
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 Bazzica746d46b2018-10-30 10:48:38 +010011#ifndef MODULES_AUDIO_PROCESSING_AGC2_LIMITER_H_
12#define MODULES_AUDIO_PROCESSING_AGC2_LIMITER_H_
Alex Loikoa05ee822018-02-20 15:58:36 +010013
Alessio Bazzica746d46b2018-10-30 10:48:38 +010014#include <string>
Alex Loikoa05ee822018-02-20 15:58:36 +010015#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"
Steve Anton10542f22019-01-11 09:11:00 -080020#include "rtc_base/constructor_magic.h"
Alex Loikoa05ee822018-02-20 15:58:36 +010021
22namespace webrtc {
23class ApmDataDumper;
24
Alessio Bazzica746d46b2018-10-30 10:48:38 +010025class Limiter {
Alex Loikoa05ee822018-02-20 15:58:36 +010026 public:
Alessio Bazzica746d46b2018-10-30 10:48:38 +010027 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();
Alex Loikoa05ee822018-02-20 15:58:36 +010033
Alessio Bazzica3e4c77f2018-11-01 21:31:38 +010034 // Applies limiter and hard-clipping to |signal|.
Alex Loikoa05ee822018-02-20 15:58:36 +010035 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
Alessio Bazzica82ec0fa2018-08-27 14:24:16 +020045 // Resets the internal state.
46 void Reset();
47
Alex Loiko93e57502018-10-01 16:28:47 +020048 float LastAudioLevel() const;
49
Alex Loikoa05ee822018-02-20 15:58:36 +010050 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;
Alex Loikoa05ee822018-02-20 15:58:36 +010060};
61
62} // namespace webrtc
63
Alessio Bazzica746d46b2018-10-30 10:48:38 +010064#endif // MODULES_AUDIO_PROCESSING_AGC2_LIMITER_H_