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