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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_AUDIO_PROCESSING_AGC_LOUDNESS_HISTOGRAM_H_ |
| 12 | #define MODULES_AUDIO_PROCESSING_AGC_LOUDNESS_HISTOGRAM_H_ |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 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 | 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. |
peah | bbe4233 | 2016-06-08 06:42:02 -0700 | [diff] [blame] | 22 | class LoudnessHistogram { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 23 | public: |
peah | bbe4233 | 2016-06-08 06:42:02 -0700 | [diff] [blame] | 24 | // Create a non-sliding LoudnessHistogram. |
| 25 | static LoudnessHistogram* Create(); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 26 | |
peah | bbe4233 | 2016-06-08 06:42:02 -0700 | [diff] [blame] | 27 | // Create a sliding LoudnessHistogram, i.e. the histogram represents the last |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 28 | // |window_size| samples. |
peah | bbe4233 | 2016-06-08 06:42:02 -0700 | [diff] [blame] | 29 | static LoudnessHistogram* Create(int window_size); |
| 30 | ~LoudnessHistogram(); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 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. |
| 46 | int num_updates() const { return num_updates_; } |
| 47 | |
| 48 | private: |
peah | bbe4233 | 2016-06-08 06:42:02 -0700 | [diff] [blame] | 49 | LoudnessHistogram(); |
| 50 | explicit LoudnessHistogram(int window); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 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 | |
peah | bbe4233 | 2016-06-08 06:42:02 -0700 | [diff] [blame] | 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. |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 72 | int64_t bin_count_q10_[kHistSize]; |
| 73 | |
| 74 | // Circular buffer for probabilities |
kwiberg | 88788ad | 2016-02-19 07:04:49 -0800 | [diff] [blame] | 75 | std::unique_ptr<int[]> activity_probability_; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 76 | // Circular buffer for histogram-indices of probabilities. |
kwiberg | 88788ad | 2016-02-19 07:04:49 -0800 | [diff] [blame] | 77 | std::unique_ptr<int[]> hist_bin_index_; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 90 | #endif // MODULES_AUDIO_PROCESSING_AGC_LOUDNESS_HISTOGRAM_H_ |