blob: abb8e632280c92623b4a408d1c61a8c7b08393c0 [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
Per Åhgren5b139d62020-03-20 15:50:14 +010014namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000015
minyuecac94aa2016-05-20 08:42:22 -070016enum {
17 kAgcModeUnchanged,
18 kAgcModeAdaptiveAnalog,
19 kAgcModeAdaptiveDigital,
20 kAgcModeFixedDigital
niklase@google.com470e71d2011-07-07 08:21:25 +000021};
22
minyuecac94aa2016-05-20 08:42:22 -070023enum { kAgcFalse = 0, kAgcTrue };
niklase@google.com470e71d2011-07-07 08:21:25 +000024
minyuecac94aa2016-05-20 08:42:22 -070025typedef struct {
26 int16_t targetLevelDbfs; // default 3 (-3 dBOv)
27 int16_t compressionGaindB; // default 9 dB
28 uint8_t limiterEnable; // default kAgcTrue (on)
pbos@webrtc.orge468bc92014-12-18 09:11:33 +000029} WebRtcAgcConfig;
niklase@google.com470e71d2011-07-07 08:21:25 +000030
niklase@google.com470e71d2011-07-07 08:21:25 +000031/*
peah4d291f72015-11-16 23:52:25 -080032 * This function analyses the number of samples passed to
33 * farend and produces any error code that could arise.
34 *
35 * Input:
36 * - agcInst : AGC instance.
37 * - samples : Number of samples in input vector.
38 *
39 * Return value:
40 * : 0 - Normal operation.
41 * : -1 - Error.
42 */
43int WebRtcAgc_GetAddFarendError(void* state, size_t samples);
44
45/*
aluebs@webrtc.org96a62622014-12-15 21:54:50 +000046 * This function processes a 10 ms frame of far-end speech to determine
47 * if there is active speech. The length of the input speech vector must be
aluebs@webrtc.orgcf6d0b62014-12-16 20:56:09 +000048 * given in samples (80 when FS=8000, and 160 when FS=16000, FS=32000 or
49 * FS=48000).
niklase@google.com470e71d2011-07-07 08:21:25 +000050 *
51 * Input:
52 * - agcInst : AGC instance.
aluebs@webrtc.org96a62622014-12-15 21:54:50 +000053 * - inFar : Far-end input speech vector
niklase@google.com470e71d2011-07-07 08:21:25 +000054 * - samples : Number of samples in input vector
55 *
56 * Return value:
57 * : 0 - Normal operation.
58 * : -1 - Error
59 */
minyuecac94aa2016-05-20 08:42:22 -070060int WebRtcAgc_AddFarend(void* agcInst, const int16_t* inFar, size_t samples);
niklase@google.com470e71d2011-07-07 08:21:25 +000061
62/*
aluebs@webrtc.org96a62622014-12-15 21:54:50 +000063 * This function processes a 10 ms frame of microphone speech to determine
64 * if there is active speech. The length of the input speech vector must be
aluebs@webrtc.orgcf6d0b62014-12-16 20:56:09 +000065 * given in samples (80 when FS=8000, and 160 when FS=16000, FS=32000 or
66 * FS=48000). For very low input levels, the input signal is increased in level
67 * by multiplying and overwriting the samples in inMic[].
niklase@google.com470e71d2011-07-07 08:21:25 +000068 *
69 * This function should be called before any further processing of the
70 * near-end microphone signal.
71 *
72 * Input:
73 * - agcInst : AGC instance.
aluebs@webrtc.orgcf6d0b62014-12-16 20:56:09 +000074 * - inMic : Microphone input speech vector for each band
75 * - num_bands : Number of bands in input vector
niklase@google.com470e71d2011-07-07 08:21:25 +000076 * - samples : Number of samples in input vector
77 *
78 * Return value:
79 * : 0 - Normal operation.
80 * : -1 - Error
81 */
82int WebRtcAgc_AddMic(void* agcInst,
aluebs@webrtc.orgcf6d0b62014-12-16 20:56:09 +000083 int16_t* const* inMic,
Peter Kastingdce40cf2015-08-24 14:52:23 -070084 size_t num_bands,
85 size_t samples);
niklase@google.com470e71d2011-07-07 08:21:25 +000086
87/*
88 * This function replaces the analog microphone with a virtual one.
89 * It is a digital gain applied to the input signal and is used in the
aluebs@webrtc.org96a62622014-12-15 21:54:50 +000090 * agcAdaptiveDigital mode where no microphone level is adjustable. The length
91 * of the input speech vector must be given in samples (80 when FS=8000, and 160
aluebs@webrtc.orgcf6d0b62014-12-16 20:56:09 +000092 * when FS=16000, FS=32000 or FS=48000).
niklase@google.com470e71d2011-07-07 08:21:25 +000093 *
94 * Input:
95 * - agcInst : AGC instance.
aluebs@webrtc.orgcf6d0b62014-12-16 20:56:09 +000096 * - inMic : Microphone input speech vector for each band
97 * - num_bands : Number of bands in input vector
niklase@google.com470e71d2011-07-07 08:21:25 +000098 * - samples : Number of samples in input vector
99 * - micLevelIn : Input level of microphone (static)
100 *
101 * Output:
102 * - inMic : Microphone output after processing (L band)
103 * - inMic_H : Microphone output after processing (H band)
104 * - micLevelOut : Adjusted microphone level after processing
105 *
106 * Return value:
107 * : 0 - Normal operation.
108 * : -1 - Error
109 */
110int WebRtcAgc_VirtualMic(void* agcInst,
aluebs@webrtc.orgcf6d0b62014-12-16 20:56:09 +0000111 int16_t* const* inMic,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700112 size_t num_bands,
113 size_t samples,
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +0000114 int32_t micLevelIn,
115 int32_t* micLevelOut);
niklase@google.com470e71d2011-07-07 08:21:25 +0000116
117/*
Per Åhgren77dc1992019-11-23 00:14:31 +0100118 * This function analyses a 10 ms frame and produces the analog and digital
119 * gains required to normalize the signal. The gain adjustments are done only
120 * during active periods of speech. The length of the speech vectors must be
121 * given in samples (80 when FS=8000, and 160 when FS=16000, FS=32000 or
122 * FS=48000). The echo parameter can be used to ensure the AGC will not adjust
123 * upward in the presence of echo.
niklase@google.com470e71d2011-07-07 08:21:25 +0000124 *
125 * This function should be called after processing the near-end microphone
126 * signal, in any case after any echo cancellation.
127 *
128 * Input:
129 * - agcInst : AGC instance
aluebs@webrtc.orgcf6d0b62014-12-16 20:56:09 +0000130 * - inNear : Near-end input speech vector for each band
131 * - num_bands : Number of bands in input/output vector
niklase@google.com470e71d2011-07-07 08:21:25 +0000132 * - samples : Number of samples in input/output vector
133 * - inMicLevel : Current microphone volume level
134 * - echo : Set to 0 if the signal passed to add_mic is
135 * almost certainly free of echo; otherwise set
136 * to 1. If you have no information regarding echo
137 * set to 0.
138 *
139 * Output:
140 * - outMicLevel : Adjusted microphone volume level
niklase@google.com470e71d2011-07-07 08:21:25 +0000141 * - saturationWarning : A returned value of 1 indicates a saturation event
142 * has occurred and the volume cannot be further
143 * reduced. Otherwise will be set to 0.
Per Åhgren77dc1992019-11-23 00:14:31 +0100144 * - gains : Vector of gains to apply for digital normalization
niklase@google.com470e71d2011-07-07 08:21:25 +0000145 *
146 * Return value:
147 * : 0 - Normal operation.
148 * : -1 - Error
149 */
Per Åhgren77dc1992019-11-23 00:14:31 +0100150int WebRtcAgc_Analyze(void* agcInst,
aluebs@webrtc.orgcf6d0b62014-12-16 20:56:09 +0000151 const int16_t* const* inNear,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700152 size_t num_bands,
153 size_t samples,
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +0000154 int32_t inMicLevel,
155 int32_t* outMicLevel,
156 int16_t echo,
Per Åhgren77dc1992019-11-23 00:14:31 +0100157 uint8_t* saturationWarning,
158 int32_t gains[11]);
159
160/*
161 * This function processes a 10 ms frame by applying precomputed digital gains.
162 *
163 * Input:
164 * - agcInst : AGC instance
165 * - gains : Vector of gains to apply for digital normalization
166 * - in_near : Near-end input speech vector for each band
167 * - num_bands : Number of bands in input/output vector
168 *
169 * Output:
170 * - out : Gain-adjusted near-end speech vector
171 * : May be the same vector as the input.
172 *
173 * Return value:
174 * : 0 - Normal operation.
175 * : -1 - Error
176 */
177int WebRtcAgc_Process(const void* agcInst,
178 const int32_t gains[11],
179 const int16_t* const* in_near,
180 size_t num_bands,
181 int16_t* const* out);
niklase@google.com470e71d2011-07-07 08:21:25 +0000182
183/*
184 * This function sets the config parameters (targetLevelDbfs,
185 * compressionGaindB and limiterEnable).
186 *
187 * Input:
188 * - agcInst : AGC instance
189 * - config : config struct
190 *
191 * Output:
192 *
193 * Return value:
194 * : 0 - Normal operation.
195 * : -1 - Error
196 */
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000197int WebRtcAgc_set_config(void* agcInst, WebRtcAgcConfig config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000198
199/*
200 * This function returns the config parameters (targetLevelDbfs,
201 * compressionGaindB and limiterEnable).
202 *
203 * Input:
204 * - agcInst : AGC instance
205 *
206 * Output:
207 * - config : config struct
208 *
209 * Return value:
210 * : 0 - Normal operation.
211 * : -1 - Error
212 */
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000213int WebRtcAgc_get_config(void* agcInst, WebRtcAgcConfig* config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000214
215/*
Bjorn Volcker9345e862015-06-10 21:43:36 +0200216 * This function creates and returns an AGC instance, which will contain the
217 * state information for one (duplex) channel.
niklase@google.com470e71d2011-07-07 08:21:25 +0000218 */
Mirko Bonadeid7573562018-03-19 16:23:48 +0100219void* WebRtcAgc_Create(void);
niklase@google.com470e71d2011-07-07 08:21:25 +0000220
221/*
222 * This function frees the AGC instance created at the beginning.
223 *
224 * Input:
225 * - agcInst : AGC instance.
niklase@google.com470e71d2011-07-07 08:21:25 +0000226 */
Bjorn Volckerf6a99e62015-04-10 07:56:57 +0200227void WebRtcAgc_Free(void* agcInst);
niklase@google.com470e71d2011-07-07 08:21:25 +0000228
229/*
230 * This function initializes an AGC instance.
231 *
232 * Input:
233 * - agcInst : AGC instance.
234 * - minLevel : Minimum possible mic level
235 * - maxLevel : Maximum possible mic level
236 * - agcMode : 0 - Unchanged
237 * : 1 - Adaptive Analog Automatic Gain Control -3dBOv
238 * : 2 - Adaptive Digital Automatic Gain Control -3dBOv
239 * : 3 - Fixed Digital Gain 0dB
240 * - fs : Sampling frequency
241 *
242 * Return value : 0 - Ok
243 * -1 - Error
244 */
minyuecac94aa2016-05-20 08:42:22 -0700245int WebRtcAgc_Init(void* agcInst,
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +0000246 int32_t minLevel,
247 int32_t maxLevel,
248 int16_t agcMode,
249 uint32_t fs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000250
Per Åhgren5b139d62020-03-20 15:50:14 +0100251} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000252
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200253#endif // MODULES_AUDIO_PROCESSING_AGC_LEGACY_GAIN_CONTROL_H_