xref: /aosp_15_r20/external/webrtc/modules/audio_processing/agc2/clipping_predictor_level_buffer.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2021 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_CLIPPING_PREDICTOR_LEVEL_BUFFER_H_
12 #define MODULES_AUDIO_PROCESSING_AGC2_CLIPPING_PREDICTOR_LEVEL_BUFFER_H_
13 
14 #include <memory>
15 #include <vector>
16 
17 #include "absl/types/optional.h"
18 
19 namespace webrtc {
20 
21 // A circular buffer to store frame-wise `Level` items for clipping prediction.
22 // The current implementation is not optimized for large buffer lengths.
23 class ClippingPredictorLevelBuffer {
24  public:
25   struct Level {
26     float average;
27     float max;
28     bool operator==(const Level& level) const;
29   };
30 
31   // Recommended maximum capacity. It is possible to create a buffer with a
32   // larger capacity, but the implementation is not optimized for large values.
33   static constexpr int kMaxCapacity = 100;
34 
35   // Ctor. Sets the buffer capacity to max(1, `capacity`) and logs a warning
36   // message if the capacity is greater than `kMaxCapacity`.
37   explicit ClippingPredictorLevelBuffer(int capacity);
~ClippingPredictorLevelBuffer()38   ~ClippingPredictorLevelBuffer() {}
39   ClippingPredictorLevelBuffer(const ClippingPredictorLevelBuffer&) = delete;
40   ClippingPredictorLevelBuffer& operator=(const ClippingPredictorLevelBuffer&) =
41       delete;
42 
43   void Reset();
44 
45   // Returns the current number of items stored in the buffer.
Size()46   int Size() const { return size_; }
47 
48   // Returns the capacity of the buffer.
Capacity()49   int Capacity() const { return data_.size(); }
50 
51   // Adds a `level` item into the circular buffer `data_`. Stores at most
52   // `Capacity()` items. If more items are pushed, the new item replaces the
53   // least recently pushed item.
54   void Push(Level level);
55 
56   // If at least `num_items` + `delay` items have been pushed, returns the
57   // average and maximum value for the `num_items` most recently pushed items
58   // from `delay` to `delay` - `num_items` (a delay equal to zero corresponds
59   // to the most recently pushed item). The value of `delay` is limited to
60   // [0, N] and `num_items` to [1, M] where N + M is the capacity of the buffer.
61   absl::optional<Level> ComputePartialMetrics(int delay, int num_items) const;
62 
63  private:
64   int tail_;
65   int size_;
66   std::vector<Level> data_;
67 };
68 
69 }  // namespace webrtc
70 
71 #endif  // MODULES_AUDIO_PROCESSING_AGC2_CLIPPING_PREDICTOR_LEVEL_BUFFER_H_
72