blob: b210be96e3b91eaa282d1df539377cfabd39dcf2 [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>
kwiberg88788ad2016-02-19 07:04:49 -080015#include <memory>
16
pbos@webrtc.org788acd12014-12-15 09:41:24 +000017namespace webrtc {
18
19// This class implements the histogram of loudness with circular buffers so that
20// the histogram tracks the last T seconds of the loudness.
peahbbe42332016-06-08 06:42:02 -070021class LoudnessHistogram {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000022 public:
peahbbe42332016-06-08 06:42:02 -070023 // Create a non-sliding LoudnessHistogram.
24 static LoudnessHistogram* Create();
pbos@webrtc.org788acd12014-12-15 09:41:24 +000025
peahbbe42332016-06-08 06:42:02 -070026 // Create a sliding LoudnessHistogram, i.e. the histogram represents the last
pbos@webrtc.org788acd12014-12-15 09:41:24 +000027 // |window_size| samples.
peahbbe42332016-06-08 06:42:02 -070028 static LoudnessHistogram* Create(int window_size);
29 ~LoudnessHistogram();
pbos@webrtc.org788acd12014-12-15 09:41:24 +000030
31 // Insert RMS and the corresponding activity probability.
32 void Update(double rms, double activity_probability);
33
34 // Reset the histogram, forget the past.
35 void Reset();
36
37 // Current loudness, which is actually the mean of histogram in loudness
38 // domain.
39 double CurrentRms() const;
40
41 // Sum of the histogram content.
42 double AudioContent() const;
43
44 // Number of times the histogram has been updated.
45 int num_updates() const { return num_updates_; }
46
47 private:
peahbbe42332016-06-08 06:42:02 -070048 LoudnessHistogram();
49 explicit LoudnessHistogram(int window);
pbos@webrtc.org788acd12014-12-15 09:41:24 +000050
51 // Find the histogram bin associated with the given |rms|.
52 int GetBinIndex(double rms);
53
54 void RemoveOldestEntryAndUpdate();
55 void InsertNewestEntryAndUpdate(int activity_prob_q10, int hist_index);
56 void UpdateHist(int activity_prob_q10, int hist_index);
57 void RemoveTransient();
58
59 // Number of histogram bins.
60 static const int kHistSize = 77;
61
62 // Number of times the histogram is updated
63 int num_updates_;
64 // Audio content, this should be equal to the sum of the components of
65 // |bin_count_q10_|.
66 int64_t audio_content_q10_;
67
peahbbe42332016-06-08 06:42:02 -070068 // LoudnessHistogram of input RMS in Q10 with |kHistSize_| bins. In each
69 // 'Update(),' we increment the associated histogram-bin with the given
70 // probability. The increment is implemented in Q10 to avoid rounding errors.
pbos@webrtc.org788acd12014-12-15 09:41:24 +000071 int64_t bin_count_q10_[kHistSize];
72
73 // Circular buffer for probabilities
kwiberg88788ad2016-02-19 07:04:49 -080074 std::unique_ptr<int[]> activity_probability_;
pbos@webrtc.org788acd12014-12-15 09:41:24 +000075 // Circular buffer for histogram-indices of probabilities.
kwiberg88788ad2016-02-19 07:04:49 -080076 std::unique_ptr<int[]> hist_bin_index_;
pbos@webrtc.org788acd12014-12-15 09:41:24 +000077 // Current index of circular buffer, where the newest data will be written to,
78 // therefore, pointing to the oldest data if buffer is full.
79 int buffer_index_;
80 // Indicating if buffer is full and we had a wrap around.
81 int buffer_is_full_;
82 // Size of circular buffer.
83 int len_circular_buffer_;
84 int len_high_activity_;
85};
86
87} // namespace webrtc
88
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020089#endif // MODULES_AUDIO_PROCESSING_AGC_LOUDNESS_HISTOGRAM_H_