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 | |
| 23 | // Frame-wise clipping prediction and clipped level step estimation. Processing |
| 24 | // is done in two steps: Calling `Process` analyses a frame of audio and stores |
| 25 | // the frame metrics and `EstimateClippedLevelStep` produces an estimate for the |
| 26 | // required analog gain level decrease if clipping is predicted. |
| 27 | class ClippingPredictor { |
| 28 | public: |
| 29 | virtual ~ClippingPredictor() = default; |
| 30 | |
| 31 | virtual void Reset() = 0; |
| 32 | |
| 33 | // Estimates the analog gain clipped level step for channel `channel`. |
| 34 | // Returns absl::nullopt if clipping is not predicted, otherwise returns the |
| 35 | // suggested decrease in the analog gain level. |
| 36 | virtual absl::optional<int> EstimateClippedLevelStep( |
| 37 | int channel, |
| 38 | int level, |
| 39 | int default_step, |
| 40 | int min_mic_level, |
| 41 | int max_mic_level) const = 0; |
| 42 | |
| 43 | // Analyses a frame of audio and stores the resulting metrics in `data_`. |
| 44 | virtual void Process(const AudioFrameView<const float>& frame) = 0; |
| 45 | }; |
| 46 | |
| 47 | // Creates a ClippingPredictor based on crest factor-based clipping event |
| 48 | // prediction. |
| 49 | std::unique_ptr<ClippingPredictor> CreateClippingEventPredictor( |
| 50 | int num_channels, |
Hanna Silen | a004715 | 2021-06-03 03:29:38 +0200 | [diff] [blame^] | 51 | const AudioProcessing::Config::GainController1::AnalogGainController:: |
Hanna Silen | 4b3a061 | 2021-06-02 23:03:24 +0200 | [diff] [blame] | 52 | ClippingPredictor& config); |
| 53 | |
| 54 | // Creates a ClippingPredictor based on crest factor-based peak estimation and |
| 55 | // fixed-step clipped level step estimation. |
| 56 | std::unique_ptr<ClippingPredictor> CreateFixedStepClippingPeakPredictor( |
| 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 | // Creates a ClippingPredictor based on crest factor-based peak estimation and |
| 62 | // adaptive-step clipped level step estimation. |
| 63 | std::unique_ptr<ClippingPredictor> CreateAdaptiveStepClippingPeakPredictor( |
| 64 | int num_channels, |
Hanna Silen | a004715 | 2021-06-03 03:29:38 +0200 | [diff] [blame^] | 65 | const AudioProcessing::Config::GainController1::AnalogGainController:: |
Hanna Silen | 4b3a061 | 2021-06-02 23:03:24 +0200 | [diff] [blame] | 66 | ClippingPredictor& config); |
| 67 | |
| 68 | } // namespace webrtc |
| 69 | |
| 70 | #endif // MODULES_AUDIO_PROCESSING_AGC_CLIPPING_PREDICTOR_H_ |