loudness_histogram.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 2012 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_AGC_LOUDNESS_HISTOGRAM_H_
  11. #define MODULES_AUDIO_PROCESSING_AGC_LOUDNESS_HISTOGRAM_H_
  12. #include <stdint.h>
  13. #include <memory>
  14. namespace webrtc {
  15. // This class implements the histogram of loudness with circular buffers so that
  16. // the histogram tracks the last T seconds of the loudness.
  17. class LoudnessHistogram {
  18. public:
  19. // Create a non-sliding LoudnessHistogram.
  20. static LoudnessHistogram* Create();
  21. // Create a sliding LoudnessHistogram, i.e. the histogram represents the last
  22. // |window_size| samples.
  23. static LoudnessHistogram* Create(int window_size);
  24. ~LoudnessHistogram();
  25. // Insert RMS and the corresponding activity probability.
  26. void Update(double rms, double activity_probability);
  27. // Reset the histogram, forget the past.
  28. void Reset();
  29. // Current loudness, which is actually the mean of histogram in loudness
  30. // domain.
  31. double CurrentRms() const;
  32. // Sum of the histogram content.
  33. double AudioContent() const;
  34. // Number of times the histogram has been updated.
  35. int num_updates() const { return num_updates_; }
  36. private:
  37. LoudnessHistogram();
  38. explicit LoudnessHistogram(int window);
  39. // Find the histogram bin associated with the given |rms|.
  40. int GetBinIndex(double rms);
  41. void RemoveOldestEntryAndUpdate();
  42. void InsertNewestEntryAndUpdate(int activity_prob_q10, int hist_index);
  43. void UpdateHist(int activity_prob_q10, int hist_index);
  44. void RemoveTransient();
  45. // Number of histogram bins.
  46. static const int kHistSize = 77;
  47. // Number of times the histogram is updated
  48. int num_updates_;
  49. // Audio content, this should be equal to the sum of the components of
  50. // |bin_count_q10_|.
  51. int64_t audio_content_q10_;
  52. // LoudnessHistogram of input RMS in Q10 with |kHistSize_| bins. In each
  53. // 'Update(),' we increment the associated histogram-bin with the given
  54. // probability. The increment is implemented in Q10 to avoid rounding errors.
  55. int64_t bin_count_q10_[kHistSize];
  56. // Circular buffer for probabilities
  57. std::unique_ptr<int[]> activity_probability_;
  58. // Circular buffer for histogram-indices of probabilities.
  59. std::unique_ptr<int[]> hist_bin_index_;
  60. // Current index of circular buffer, where the newest data will be written to,
  61. // therefore, pointing to the oldest data if buffer is full.
  62. int buffer_index_;
  63. // Indicating if buffer is full and we had a wrap around.
  64. int buffer_is_full_;
  65. // Size of circular buffer.
  66. int len_circular_buffer_;
  67. int len_high_activity_;
  68. };
  69. } // namespace webrtc
  70. #endif // MODULES_AUDIO_PROCESSING_AGC_LOUDNESS_HISTOGRAM_H_