saturation_protector.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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_SATURATION_PROTECTOR_H_
  11. #define MODULES_AUDIO_PROCESSING_AGC2_SATURATION_PROTECTOR_H_
  12. #include <array>
  13. #include "absl/types/optional.h"
  14. #include "modules/audio_processing/agc2/agc2_common.h"
  15. #include "rtc_base/numerics/safe_compare.h"
  16. namespace webrtc {
  17. namespace saturation_protector_impl {
  18. // Ring buffer which only supports (i) push back and (ii) read oldest item.
  19. class RingBuffer {
  20. public:
  21. bool operator==(const RingBuffer& b) const;
  22. inline bool operator!=(const RingBuffer& b) const { return !(*this == b); }
  23. // Maximum number of values that the buffer can contain.
  24. int Capacity() const { return buffer_.size(); }
  25. // Number of values in the buffer.
  26. int Size() const { return size_; }
  27. void Reset();
  28. // Pushes back `v`. If the buffer is full, the oldest value is replaced.
  29. void PushBack(float v);
  30. // Returns the oldest item in the buffer. Returns an empty value if the
  31. // buffer is empty.
  32. absl::optional<float> Front() const;
  33. private:
  34. inline int FrontIndex() const {
  35. return rtc::SafeEq(size_, buffer_.size()) ? next_ : 0;
  36. }
  37. // `buffer_` has `size_` elements (up to the size of `buffer_`) and `next_` is
  38. // the position where the next new value is written in `buffer_`.
  39. std::array<float, kPeakEnveloperBufferSize> buffer_;
  40. int next_ = 0;
  41. int size_ = 0;
  42. };
  43. } // namespace saturation_protector_impl
  44. // Saturation protector state. Exposed publicly for check-pointing and restore
  45. // ops.
  46. struct SaturationProtectorState {
  47. bool operator==(const SaturationProtectorState& s) const;
  48. inline bool operator!=(const SaturationProtectorState& s) const {
  49. return !(*this == s);
  50. }
  51. float margin_db; // Recommended margin.
  52. saturation_protector_impl::RingBuffer peak_delay_buffer;
  53. float max_peaks_dbfs;
  54. int time_since_push_ms; // Time since the last ring buffer push operation.
  55. };
  56. // Resets the saturation protector state.
  57. void ResetSaturationProtectorState(float initial_margin_db,
  58. SaturationProtectorState& state);
  59. // Updates `state` by analyzing the estimated speech level `speech_level_dbfs`
  60. // and the peak power `speech_peak_dbfs` for an observed frame which is
  61. // reliably classified as "speech". `state` must not be modified without calling
  62. // this function.
  63. void UpdateSaturationProtectorState(float speech_peak_dbfs,
  64. float speech_level_dbfs,
  65. SaturationProtectorState& state);
  66. } // namespace webrtc
  67. #endif // MODULES_AUDIO_PROCESSING_AGC2_SATURATION_PROTECTOR_H_