blob: 0fe98273fb928c532474def1903e44336171821f [file] [log] [blame]
Hanna Silen4b3a0612021-06-02 23:03:24 +02001/*
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
21namespace 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.
27class 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.
49std::unique_ptr<ClippingPredictor> CreateClippingEventPredictor(
50 int num_channels,
Hanna Silena0047152021-06-03 03:29:38 +020051 const AudioProcessing::Config::GainController1::AnalogGainController::
Hanna Silen4b3a0612021-06-02 23:03:24 +020052 ClippingPredictor& config);
53
54// Creates a ClippingPredictor based on crest factor-based peak estimation and
55// fixed-step clipped level step estimation.
56std::unique_ptr<ClippingPredictor> CreateFixedStepClippingPeakPredictor(
57 int num_channels,
Hanna Silena0047152021-06-03 03:29:38 +020058 const AudioProcessing::Config::GainController1::AnalogGainController::
Hanna Silen4b3a0612021-06-02 23:03:24 +020059 ClippingPredictor& config);
60
61// Creates a ClippingPredictor based on crest factor-based peak estimation and
62// adaptive-step clipped level step estimation.
63std::unique_ptr<ClippingPredictor> CreateAdaptiveStepClippingPeakPredictor(
64 int num_channels,
Hanna Silena0047152021-06-03 03:29:38 +020065 const AudioProcessing::Config::GainController1::AnalogGainController::
Hanna Silen4b3a0612021-06-02 23:03:24 +020066 ClippingPredictor& config);
67
68} // namespace webrtc
69
70#endif // MODULES_AUDIO_PROCESSING_AGC_CLIPPING_PREDICTOR_H_