blob: 69208a760fab75177d2fa8b501c4ee28f85f750c [file] [log] [blame]
Alex Loikoed8ff642018-07-06 14:54:30 +02001/*
2 * Copyright (c) 2018 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_INCLUDE_GAIN_CONTROL_H_
12#define MODULES_AUDIO_PROCESSING_INCLUDE_GAIN_CONTROL_H_
13
14namespace webrtc {
15
16// The automatic gain control (AGC) component brings the signal to an
17// appropriate range. This is done by applying a digital gain directly and, in
18// the analog mode, prescribing an analog gain to be applied at the audio HAL.
19//
20// Recommended to be enabled on the client-side.
21class GainControl {
22 public:
23 virtual int Enable(bool enable) = 0;
24 virtual bool is_enabled() const = 0;
25
26 // When an analog mode is set, this must be called prior to |ProcessStream()|
27 // to pass the current analog level from the audio HAL. Must be within the
28 // range provided to |set_analog_level_limits()|.
29 virtual int set_stream_analog_level(int level) = 0;
30
31 // When an analog mode is set, this should be called after |ProcessStream()|
32 // to obtain the recommended new analog level for the audio HAL. It is the
33 // users responsibility to apply this level.
Sam Zackrissonf0d1c032019-03-27 13:28:08 +010034 virtual int stream_analog_level() const = 0;
Alex Loikoed8ff642018-07-06 14:54:30 +020035
36 enum Mode {
37 // Adaptive mode intended for use if an analog volume control is available
38 // on the capture device. It will require the user to provide coupling
39 // between the OS mixer controls and AGC through the |stream_analog_level()|
40 // functions.
41 //
42 // It consists of an analog gain prescription for the audio device and a
43 // digital compression stage.
44 kAdaptiveAnalog,
45
46 // Adaptive mode intended for situations in which an analog volume control
47 // is unavailable. It operates in a similar fashion to the adaptive analog
48 // mode, but with scaling instead applied in the digital domain. As with
49 // the analog mode, it additionally uses a digital compression stage.
50 kAdaptiveDigital,
51
52 // Fixed mode which enables only the digital compression stage also used by
53 // the two adaptive modes.
54 //
55 // It is distinguished from the adaptive modes by considering only a
56 // short time-window of the input signal. It applies a fixed gain through
57 // most of the input level range, and compresses (gradually reduces gain
58 // with increasing level) the input signal at higher levels. This mode is
59 // preferred on embedded devices where the capture signal level is
60 // predictable, so that a known gain can be applied.
61 kFixedDigital
62 };
63
64 virtual int set_mode(Mode mode) = 0;
65 virtual Mode mode() const = 0;
66
67 // Sets the target peak |level| (or envelope) of the AGC in dBFs (decibels
68 // from digital full-scale). The convention is to use positive values. For
69 // instance, passing in a value of 3 corresponds to -3 dBFs, or a target
70 // level 3 dB below full-scale. Limited to [0, 31].
71 //
72 // TODO(ajm): use a negative value here instead, if/when VoE will similarly
73 // update its interface.
74 virtual int set_target_level_dbfs(int level) = 0;
75 virtual int target_level_dbfs() const = 0;
76
77 // Sets the maximum |gain| the digital compression stage may apply, in dB. A
78 // higher number corresponds to greater compression, while a value of 0 will
79 // leave the signal uncompressed. Limited to [0, 90].
80 virtual int set_compression_gain_db(int gain) = 0;
81 virtual int compression_gain_db() const = 0;
82
83 // When enabled, the compression stage will hard limit the signal to the
84 // target level. Otherwise, the signal will be compressed but not limited
85 // above the target level.
86 virtual int enable_limiter(bool enable) = 0;
87 virtual bool is_limiter_enabled() const = 0;
88
89 // Sets the |minimum| and |maximum| analog levels of the audio capture device.
90 // Must be set if and only if an analog mode is used. Limited to [0, 65535].
91 virtual int set_analog_level_limits(int minimum, int maximum) = 0;
92 virtual int analog_level_minimum() const = 0;
93 virtual int analog_level_maximum() const = 0;
94
95 // Returns true if the AGC has detected a saturation event (period where the
96 // signal reaches digital full-scale) in the current frame and the analog
97 // level cannot be reduced.
98 //
99 // This could be used as an indicator to reduce or disable analog mic gain at
100 // the audio HAL.
101 virtual bool stream_is_saturated() const = 0;
102
103 protected:
104 virtual ~GainControl() {}
105};
106} // namespace webrtc
107
108#endif // MODULES_AUDIO_PROCESSING_INCLUDE_GAIN_CONTROL_H_