xref: /aosp_15_r20/external/webrtc/modules/audio_processing/agc2/speech_probability_buffer.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2022 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_AGC2_SPEECH_PROBABILITY_BUFFER_H_
12 #define MODULES_AUDIO_PROCESSING_AGC2_SPEECH_PROBABILITY_BUFFER_H_
13 
14 #include <vector>
15 
16 #include "rtc_base/gtest_prod_util.h"
17 
18 namespace webrtc {
19 
20 // This class implements a circular buffer that stores speech probabilities
21 // for a speech segment and estimates speech activity for that segment.
22 class SpeechProbabilityBuffer {
23  public:
24   // Ctor. The value of `low_probability_threshold` is required to be on the
25   // range [0.0f, 1.0f].
26   explicit SpeechProbabilityBuffer(float low_probability_threshold);
~SpeechProbabilityBuffer()27   ~SpeechProbabilityBuffer() {}
28   SpeechProbabilityBuffer(const SpeechProbabilityBuffer&) = delete;
29   SpeechProbabilityBuffer& operator=(const SpeechProbabilityBuffer&) = delete;
30 
31   // Adds `probability` in the buffer and computes an updatds sum of the buffer
32   // probabilities. Value of `probability` is required to be on the range
33   // [0.0f, 1.0f].
34   void Update(float probability);
35 
36   // Resets the histogram, forgets the past.
37   void Reset();
38 
39   // Returns true if the segment is active (a long enough segment with an
40   // average speech probability above `low_probability_threshold`).
41   bool IsActiveSegment() const;
42 
43  private:
44   void RemoveTransient();
45 
46   // Use only for testing.
GetSumProbabilities()47   float GetSumProbabilities() const { return sum_probabilities_; }
48 
49   FRIEND_TEST_ALL_PREFIXES(SpeechProbabilityBufferTest,
50                            CheckSumAfterInitialization);
51   FRIEND_TEST_ALL_PREFIXES(SpeechProbabilityBufferTest, CheckSumAfterUpdate);
52   FRIEND_TEST_ALL_PREFIXES(SpeechProbabilityBufferTest, CheckSumAfterReset);
53   FRIEND_TEST_ALL_PREFIXES(SpeechProbabilityBufferTest,
54                            CheckSumAfterTransientNotRemoved);
55   FRIEND_TEST_ALL_PREFIXES(SpeechProbabilityBufferTest,
56                            CheckSumAfterTransientRemoved);
57 
58   const float low_probability_threshold_;
59 
60   // Sum of probabilities stored in `probabilities_`. Must be updated if
61   // `probabilities_` is updated.
62   float sum_probabilities_ = 0.0f;
63 
64   // Circular buffer for probabilities.
65   std::vector<float> probabilities_;
66 
67   // Current index of the circular buffer, where the newest data will be written
68   // to, therefore, pointing to the oldest data if buffer is full.
69   int buffer_index_ = 0;
70 
71   // Indicates if the buffer is full and adding a new value removes the oldest
72   // value.
73   int buffer_is_full_ = false;
74 
75   int num_high_probability_observations_ = 0;
76 };
77 
78 }  // namespace webrtc
79 
80 #endif  // MODULES_AUDIO_PROCESSING_AGC2_SPEECH_PROBABILITY_BUFFER_H_
81