blob: badd443a5de4934475e902ca221507ad1cbfc796 [file] [log] [blame]
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_PROCESSING_AGC_LOUDNESS_HISTOGRAM_H_
12#define MODULES_AUDIO_PROCESSING_AGC_LOUDNESS_HISTOGRAM_H_
pbos@webrtc.org788acd12014-12-15 09:41:24 +000013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
kwiberg88788ad2016-02-19 07:04:49 -080016#include <memory>
17
pbos@webrtc.org788acd12014-12-15 09:41:24 +000018namespace 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.
peahbbe42332016-06-08 06:42:02 -070022class LoudnessHistogram {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000023 public:
peahbbe42332016-06-08 06:42:02 -070024 // Create a non-sliding LoudnessHistogram.
25 static LoudnessHistogram* Create();
pbos@webrtc.org788acd12014-12-15 09:41:24 +000026
peahbbe42332016-06-08 06:42:02 -070027 // Create a sliding LoudnessHistogram, i.e. the histogram represents the last
pbos@webrtc.org788acd12014-12-15 09:41:24 +000028 // |window_size| samples.
peahbbe42332016-06-08 06:42:02 -070029 static LoudnessHistogram* Create(int window_size);
30 ~LoudnessHistogram();
pbos@webrtc.org788acd12014-12-15 09:41:24 +000031
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:
peahbbe42332016-06-08 06:42:02 -070049 LoudnessHistogram();
50 explicit LoudnessHistogram(int window);
pbos@webrtc.org788acd12014-12-15 09:41:24 +000051
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
peahbbe42332016-06-08 06:42:02 -070069 // 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.org788acd12014-12-15 09:41:24 +000072 int64_t bin_count_q10_[kHistSize];
73
74 // Circular buffer for probabilities
kwiberg88788ad2016-02-19 07:04:49 -080075 std::unique_ptr<int[]> activity_probability_;
pbos@webrtc.org788acd12014-12-15 09:41:24 +000076 // Circular buffer for histogram-indices of probabilities.
kwiberg88788ad2016-02-19 07:04:49 -080077 std::unique_ptr<int[]> hist_bin_index_;
pbos@webrtc.org788acd12014-12-15 09:41:24 +000078 // 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 Bonadei92ea95e2017-09-15 06:47:31 +020090#endif // MODULES_AUDIO_PROCESSING_AGC_LOUDNESS_HISTOGRAM_H_