limiter.h 2.1 KB

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