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