blob: ee2b6ef1e7ae2157775a395430c9994f7245a563 [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
Alessio Bazzicab237a872021-06-11 12:37:54 +020023// 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 Silen4b3a0612021-06-02 23:03:24 +020027class ClippingPredictor {
28 public:
29 virtual ~ClippingPredictor() = default;
30
31 virtual void Reset() = 0;
32
Alessio Bazzicab237a872021-06-11 12:37:54 +020033 // 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 Silen4b3a0612021-06-02 23:03:24 +020043 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 Silen4b3a0612021-06-02 23:03:24 +020050};
51
Alessio Bazzicab237a872021-06-11 12:37:54 +020052// 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.
56std::unique_ptr<ClippingPredictor> CreateClippingPredictor(
Hanna Silen4b3a0612021-06-02 23:03:24 +020057 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} // namespace webrtc
62
63#endif // MODULES_AUDIO_PROCESSING_AGC_CLIPPING_PREDICTOR_H_