blob: a0ac96dfa82897785680ece8bc194010d697dcc2 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
andrew@webrtc.orgb9d7d932012-01-25 19:21:13 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_PROCESSING_AGC_LEGACY_GAIN_CONTROL_H_
12#define MODULES_AUDIO_PROCESSING_AGC_LEGACY_GAIN_CONTROL_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
niklase@google.com470e71d2011-07-07 08:21:25 +000014// Errors
minyuecac94aa2016-05-20 08:42:22 -070015#define AGC_UNSPECIFIED_ERROR 18000
16#define AGC_UNSUPPORTED_FUNCTION_ERROR 18001
17#define AGC_UNINITIALIZED_ERROR 18002
18#define AGC_NULL_POINTER_ERROR 18003
19#define AGC_BAD_PARAMETER_ERROR 18004
niklase@google.com470e71d2011-07-07 08:21:25 +000020
21// Warnings
minyuecac94aa2016-05-20 08:42:22 -070022#define AGC_BAD_PARAMETER_WARNING 18050
niklase@google.com470e71d2011-07-07 08:21:25 +000023
minyuecac94aa2016-05-20 08:42:22 -070024enum {
25 kAgcModeUnchanged,
26 kAgcModeAdaptiveAnalog,
27 kAgcModeAdaptiveDigital,
28 kAgcModeFixedDigital
niklase@google.com470e71d2011-07-07 08:21:25 +000029};
30
minyuecac94aa2016-05-20 08:42:22 -070031enum { kAgcFalse = 0, kAgcTrue };
niklase@google.com470e71d2011-07-07 08:21:25 +000032
minyuecac94aa2016-05-20 08:42:22 -070033typedef struct {
34 int16_t targetLevelDbfs; // default 3 (-3 dBOv)
35 int16_t compressionGaindB; // default 9 dB
36 uint8_t limiterEnable; // default kAgcTrue (on)
pbos@webrtc.orge468bc92014-12-18 09:11:33 +000037} WebRtcAgcConfig;
niklase@google.com470e71d2011-07-07 08:21:25 +000038
39#if defined(__cplusplus)
minyuecac94aa2016-05-20 08:42:22 -070040extern "C" {
niklase@google.com470e71d2011-07-07 08:21:25 +000041#endif
42
43/*
peah4d291f72015-11-16 23:52:25 -080044 * This function analyses the number of samples passed to
45 * farend and produces any error code that could arise.
46 *
47 * Input:
48 * - agcInst : AGC instance.
49 * - samples : Number of samples in input vector.
50 *
51 * Return value:
52 * : 0 - Normal operation.
53 * : -1 - Error.
54 */
55int WebRtcAgc_GetAddFarendError(void* state, size_t samples);
56
57/*
aluebs@webrtc.org96a62622014-12-15 21:54:50 +000058 * This function processes a 10 ms frame of far-end speech to determine
59 * if there is active speech. The length of the input speech vector must be
aluebs@webrtc.orgcf6d0b62014-12-16 20:56:09 +000060 * given in samples (80 when FS=8000, and 160 when FS=16000, FS=32000 or
61 * FS=48000).
niklase@google.com470e71d2011-07-07 08:21:25 +000062 *
63 * Input:
64 * - agcInst : AGC instance.
aluebs@webrtc.org96a62622014-12-15 21:54:50 +000065 * - inFar : Far-end input speech vector
niklase@google.com470e71d2011-07-07 08:21:25 +000066 * - samples : Number of samples in input vector
67 *
68 * Return value:
69 * : 0 - Normal operation.
70 * : -1 - Error
71 */
minyuecac94aa2016-05-20 08:42:22 -070072int WebRtcAgc_AddFarend(void* agcInst, const int16_t* inFar, size_t samples);
niklase@google.com470e71d2011-07-07 08:21:25 +000073
74/*
aluebs@webrtc.org96a62622014-12-15 21:54:50 +000075 * This function processes a 10 ms frame of microphone speech to determine
76 * if there is active speech. The length of the input speech vector must be
aluebs@webrtc.orgcf6d0b62014-12-16 20:56:09 +000077 * given in samples (80 when FS=8000, and 160 when FS=16000, FS=32000 or
78 * FS=48000). For very low input levels, the input signal is increased in level
79 * by multiplying and overwriting the samples in inMic[].
niklase@google.com470e71d2011-07-07 08:21:25 +000080 *
81 * This function should be called before any further processing of the
82 * near-end microphone signal.
83 *
84 * Input:
85 * - agcInst : AGC instance.
aluebs@webrtc.orgcf6d0b62014-12-16 20:56:09 +000086 * - inMic : Microphone input speech vector for each band
87 * - num_bands : Number of bands in input vector
niklase@google.com470e71d2011-07-07 08:21:25 +000088 * - samples : Number of samples in input vector
89 *
90 * Return value:
91 * : 0 - Normal operation.
92 * : -1 - Error
93 */
94int WebRtcAgc_AddMic(void* agcInst,
aluebs@webrtc.orgcf6d0b62014-12-16 20:56:09 +000095 int16_t* const* inMic,
Peter Kastingdce40cf2015-08-24 14:52:23 -070096 size_t num_bands,
97 size_t samples);
niklase@google.com470e71d2011-07-07 08:21:25 +000098
99/*
100 * This function replaces the analog microphone with a virtual one.
101 * It is a digital gain applied to the input signal and is used in the
aluebs@webrtc.org96a62622014-12-15 21:54:50 +0000102 * agcAdaptiveDigital mode where no microphone level is adjustable. The length
103 * of the input speech vector must be given in samples (80 when FS=8000, and 160
aluebs@webrtc.orgcf6d0b62014-12-16 20:56:09 +0000104 * when FS=16000, FS=32000 or FS=48000).
niklase@google.com470e71d2011-07-07 08:21:25 +0000105 *
106 * Input:
107 * - agcInst : AGC instance.
aluebs@webrtc.orgcf6d0b62014-12-16 20:56:09 +0000108 * - inMic : Microphone input speech vector for each band
109 * - num_bands : Number of bands in input vector
niklase@google.com470e71d2011-07-07 08:21:25 +0000110 * - samples : Number of samples in input vector
111 * - micLevelIn : Input level of microphone (static)
112 *
113 * Output:
114 * - inMic : Microphone output after processing (L band)
115 * - inMic_H : Microphone output after processing (H band)
116 * - micLevelOut : Adjusted microphone level after processing
117 *
118 * Return value:
119 * : 0 - Normal operation.
120 * : -1 - Error
121 */
122int WebRtcAgc_VirtualMic(void* agcInst,
aluebs@webrtc.orgcf6d0b62014-12-16 20:56:09 +0000123 int16_t* const* inMic,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700124 size_t num_bands,
125 size_t samples,
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +0000126 int32_t micLevelIn,
127 int32_t* micLevelOut);
niklase@google.com470e71d2011-07-07 08:21:25 +0000128
129/*
aluebs@webrtc.org96a62622014-12-15 21:54:50 +0000130 * This function processes a 10 ms frame and adjusts (normalizes) the gain both
131 * analog and digitally. The gain adjustments are done only during active
132 * periods of speech. The length of the speech vectors must be given in samples
aluebs@webrtc.orgcf6d0b62014-12-16 20:56:09 +0000133 * (80 when FS=8000, and 160 when FS=16000, FS=32000 or FS=48000). The echo
134 * parameter can be used to ensure the AGC will not adjust upward in the
135 * presence of echo.
niklase@google.com470e71d2011-07-07 08:21:25 +0000136 *
137 * This function should be called after processing the near-end microphone
138 * signal, in any case after any echo cancellation.
139 *
140 * Input:
141 * - agcInst : AGC instance
aluebs@webrtc.orgcf6d0b62014-12-16 20:56:09 +0000142 * - inNear : Near-end input speech vector for each band
143 * - num_bands : Number of bands in input/output vector
niklase@google.com470e71d2011-07-07 08:21:25 +0000144 * - samples : Number of samples in input/output vector
145 * - inMicLevel : Current microphone volume level
146 * - echo : Set to 0 if the signal passed to add_mic is
147 * almost certainly free of echo; otherwise set
148 * to 1. If you have no information regarding echo
149 * set to 0.
150 *
151 * Output:
152 * - outMicLevel : Adjusted microphone volume level
aluebs@webrtc.orgcf6d0b62014-12-16 20:56:09 +0000153 * - out : Gain-adjusted near-end speech vector
niklase@google.com470e71d2011-07-07 08:21:25 +0000154 * : May be the same vector as the input.
niklase@google.com470e71d2011-07-07 08:21:25 +0000155 * - saturationWarning : A returned value of 1 indicates a saturation event
156 * has occurred and the volume cannot be further
157 * reduced. Otherwise will be set to 0.
158 *
159 * Return value:
160 * : 0 - Normal operation.
161 * : -1 - Error
162 */
163int WebRtcAgc_Process(void* agcInst,
aluebs@webrtc.orgcf6d0b62014-12-16 20:56:09 +0000164 const int16_t* const* inNear,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700165 size_t num_bands,
166 size_t samples,
aluebs@webrtc.orgcf6d0b62014-12-16 20:56:09 +0000167 int16_t* const* out,
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +0000168 int32_t inMicLevel,
169 int32_t* outMicLevel,
170 int16_t echo,
171 uint8_t* saturationWarning);
niklase@google.com470e71d2011-07-07 08:21:25 +0000172
173/*
174 * This function sets the config parameters (targetLevelDbfs,
175 * compressionGaindB and limiterEnable).
176 *
177 * Input:
178 * - agcInst : AGC instance
179 * - config : config struct
180 *
181 * Output:
182 *
183 * Return value:
184 * : 0 - Normal operation.
185 * : -1 - Error
186 */
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000187int WebRtcAgc_set_config(void* agcInst, WebRtcAgcConfig config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000188
189/*
190 * This function returns the config parameters (targetLevelDbfs,
191 * compressionGaindB and limiterEnable).
192 *
193 * Input:
194 * - agcInst : AGC instance
195 *
196 * Output:
197 * - config : config struct
198 *
199 * Return value:
200 * : 0 - Normal operation.
201 * : -1 - Error
202 */
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000203int WebRtcAgc_get_config(void* agcInst, WebRtcAgcConfig* config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000204
205/*
Bjorn Volcker9345e862015-06-10 21:43:36 +0200206 * This function creates and returns an AGC instance, which will contain the
207 * state information for one (duplex) channel.
niklase@google.com470e71d2011-07-07 08:21:25 +0000208 */
Mirko Bonadeid7573562018-03-19 16:23:48 +0100209void* WebRtcAgc_Create(void);
niklase@google.com470e71d2011-07-07 08:21:25 +0000210
211/*
212 * This function frees the AGC instance created at the beginning.
213 *
214 * Input:
215 * - agcInst : AGC instance.
niklase@google.com470e71d2011-07-07 08:21:25 +0000216 */
Bjorn Volckerf6a99e62015-04-10 07:56:57 +0200217void WebRtcAgc_Free(void* agcInst);
niklase@google.com470e71d2011-07-07 08:21:25 +0000218
219/*
220 * This function initializes an AGC instance.
221 *
222 * Input:
223 * - agcInst : AGC instance.
224 * - minLevel : Minimum possible mic level
225 * - maxLevel : Maximum possible mic level
226 * - agcMode : 0 - Unchanged
227 * : 1 - Adaptive Analog Automatic Gain Control -3dBOv
228 * : 2 - Adaptive Digital Automatic Gain Control -3dBOv
229 * : 3 - Fixed Digital Gain 0dB
230 * - fs : Sampling frequency
231 *
232 * Return value : 0 - Ok
233 * -1 - Error
234 */
minyuecac94aa2016-05-20 08:42:22 -0700235int WebRtcAgc_Init(void* agcInst,
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +0000236 int32_t minLevel,
237 int32_t maxLevel,
238 int16_t agcMode,
239 uint32_t fs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000240
niklase@google.com470e71d2011-07-07 08:21:25 +0000241#if defined(__cplusplus)
242}
243#endif
244
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200245#endif // MODULES_AUDIO_PROCESSING_AGC_LEGACY_GAIN_CONTROL_H_