xref: /aosp_15_r20/external/webrtc/modules/audio_processing/agc2/clipping_predictor.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_H_
12 #define MODULES_AUDIO_PROCESSING_AGC2_CLIPPING_PREDICTOR_H_
13 
14 #include <memory>
15 #include <vector>
16 
17 #include "absl/types/optional.h"
18 #include "modules/audio_processing/include/audio_frame_view.h"
19 #include "modules/audio_processing/include/audio_processing.h"
20 
21 namespace webrtc {
22 
23 // Frame-wise clipping prediction and clipped level step estimation. Analyzes
24 // 10 ms multi-channel frames and estimates an analog mic level decrease step
25 // to possibly avoid clipping when predicted. `Analyze()` and
26 // `EstimateClippedLevelStep()` can be called in any order.
27 class ClippingPredictor {
28  public:
29   virtual ~ClippingPredictor() = default;
30 
31   virtual void Reset() = 0;
32 
33   // Analyzes a 10 ms multi-channel audio frame.
34   virtual void Analyze(const AudioFrameView<const float>& frame) = 0;
35 
36   // Predicts if clipping is going to occur for the specified `channel` in the
37   // near-future and, if so, it returns a recommended analog mic level decrease
38   // step. Returns absl::nullopt if clipping is not predicted.
39   // `level` is the current analog mic level, `default_step` is the amount the
40   // mic level is lowered by the analog controller with every clipping event and
41   // `min_mic_level` and `max_mic_level` is the range of allowed analog mic
42   // levels.
43   virtual absl::optional<int> EstimateClippedLevelStep(
44       int channel,
45       int level,
46       int default_step,
47       int min_mic_level,
48       int max_mic_level) const = 0;
49 };
50 
51 // Creates a ClippingPredictor based on the provided `config`. When enabled,
52 // the following must hold for `config`:
53 // `window_length < reference_window_length + reference_window_delay`.
54 // Returns `nullptr` if `config.enabled` is false.
55 std::unique_ptr<ClippingPredictor> CreateClippingPredictor(
56     int num_channels,
57     const AudioProcessing::Config::GainController1::AnalogGainController::
58         ClippingPredictor& config);
59 
60 }  // namespace webrtc
61 
62 #endif  // MODULES_AUDIO_PROCESSING_AGC2_CLIPPING_PREDICTOR_H_
63