Hanna Silen | 4b3a061 | 2021-06-02 23:03:24 +0200 | [diff] [blame] | 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_AGC_CLIPPING_PREDICTOR_H_ |
| 12 | #define MODULES_AUDIO_PROCESSING_AGC_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 | |
Alessio Bazzica | b237a87 | 2021-06-11 12:37:54 +0200 | [diff] [blame] | 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. |
Hanna Silen | 4b3a061 | 2021-06-02 23:03:24 +0200 | [diff] [blame] | 27 | class ClippingPredictor { |
| 28 | public: |
| 29 | virtual ~ClippingPredictor() = default; |
| 30 | |
| 31 | virtual void Reset() = 0; |
| 32 | |
Alessio Bazzica | b237a87 | 2021-06-11 12:37:54 +0200 | [diff] [blame] | 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. |
Hanna Silen | 4b3a061 | 2021-06-02 23:03:24 +0200 | [diff] [blame] | 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 | |
Hanna Silen | 4b3a061 | 2021-06-02 23:03:24 +0200 | [diff] [blame] | 50 | }; |
| 51 | |
Alessio Bazzica | b237a87 | 2021-06-11 12:37:54 +0200 | [diff] [blame] | 52 | // Creates a ClippingPredictor based on the provided `config`. When enabled, |
| 53 | // the following must hold for `config`: |
| 54 | // `window_length < reference_window_length + reference_window_delay`. |
| 55 | // Returns `nullptr` if `config.enabled` is false. |
| 56 | std::unique_ptr<ClippingPredictor> CreateClippingPredictor( |
Hanna Silen | 4b3a061 | 2021-06-02 23:03:24 +0200 | [diff] [blame] | 57 | int num_channels, |
Hanna Silen | a004715 | 2021-06-03 03:29:38 +0200 | [diff] [blame] | 58 | const AudioProcessing::Config::GainController1::AnalogGainController:: |
Hanna Silen | 4b3a061 | 2021-06-02 23:03:24 +0200 | [diff] [blame] | 59 | ClippingPredictor& config); |
| 60 | |
| 61 | } // namespace webrtc |
| 62 | |
| 63 | #endif // MODULES_AUDIO_PROCESSING_AGC_CLIPPING_PREDICTOR_H_ |