blob: c3157500cf7744fde5e9270b764d71952b547199 [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
bjornv@webrtc.orgb395a5e2014-12-16 10:38:10 +000011#ifndef WEBRTC_MODULES_AUDIO_PROCESSING_AGC_LEGACY_GAIN_CONTROL_H_
12#define WEBRTC_MODULES_AUDIO_PROCESSING_AGC_LEGACY_GAIN_CONTROL_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
pbos@webrtc.org7fad4b82013-05-28 08:11:59 +000014#include "webrtc/typedefs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000015
16// Errors
minyuecac94aa2016-05-20 08:42:22 -070017#define AGC_UNSPECIFIED_ERROR 18000
18#define AGC_UNSUPPORTED_FUNCTION_ERROR 18001
19#define AGC_UNINITIALIZED_ERROR 18002
20#define AGC_NULL_POINTER_ERROR 18003
21#define AGC_BAD_PARAMETER_ERROR 18004
niklase@google.com470e71d2011-07-07 08:21:25 +000022
23// Warnings
minyuecac94aa2016-05-20 08:42:22 -070024#define AGC_BAD_PARAMETER_WARNING 18050
niklase@google.com470e71d2011-07-07 08:21:25 +000025
minyuecac94aa2016-05-20 08:42:22 -070026enum {
27 kAgcModeUnchanged,
28 kAgcModeAdaptiveAnalog,
29 kAgcModeAdaptiveDigital,
30 kAgcModeFixedDigital
niklase@google.com470e71d2011-07-07 08:21:25 +000031};
32
minyuecac94aa2016-05-20 08:42:22 -070033enum { kAgcFalse = 0, kAgcTrue };
niklase@google.com470e71d2011-07-07 08:21:25 +000034
minyuecac94aa2016-05-20 08:42:22 -070035typedef struct {
36 int16_t targetLevelDbfs; // default 3 (-3 dBOv)
37 int16_t compressionGaindB; // default 9 dB
38 uint8_t limiterEnable; // default kAgcTrue (on)
pbos@webrtc.orge468bc92014-12-18 09:11:33 +000039} WebRtcAgcConfig;
niklase@google.com470e71d2011-07-07 08:21:25 +000040
41#if defined(__cplusplus)
minyuecac94aa2016-05-20 08:42:22 -070042extern "C" {
niklase@google.com470e71d2011-07-07 08:21:25 +000043#endif
44
45/*
peah4d291f72015-11-16 23:52:25 -080046 * This function analyses the number of samples passed to
47 * farend and produces any error code that could arise.
48 *
49 * Input:
50 * - agcInst : AGC instance.
51 * - samples : Number of samples in input vector.
52 *
53 * Return value:
54 * : 0 - Normal operation.
55 * : -1 - Error.
56 */
57int WebRtcAgc_GetAddFarendError(void* state, size_t samples);
58
59/*
aluebs@webrtc.org96a62622014-12-15 21:54:50 +000060 * This function processes a 10 ms frame of far-end speech to determine
61 * if there is active speech. The length of the input speech vector must be
aluebs@webrtc.orgcf6d0b62014-12-16 20:56:09 +000062 * given in samples (80 when FS=8000, and 160 when FS=16000, FS=32000 or
63 * FS=48000).
niklase@google.com470e71d2011-07-07 08:21:25 +000064 *
65 * Input:
66 * - agcInst : AGC instance.
aluebs@webrtc.org96a62622014-12-15 21:54:50 +000067 * - inFar : Far-end input speech vector
niklase@google.com470e71d2011-07-07 08:21:25 +000068 * - samples : Number of samples in input vector
69 *
70 * Return value:
71 * : 0 - Normal operation.
72 * : -1 - Error
73 */
minyuecac94aa2016-05-20 08:42:22 -070074int WebRtcAgc_AddFarend(void* agcInst, const int16_t* inFar, size_t samples);
niklase@google.com470e71d2011-07-07 08:21:25 +000075
76/*
aluebs@webrtc.org96a62622014-12-15 21:54:50 +000077 * This function processes a 10 ms frame of microphone speech to determine
78 * if there is active speech. The length of the input speech vector must be
aluebs@webrtc.orgcf6d0b62014-12-16 20:56:09 +000079 * given in samples (80 when FS=8000, and 160 when FS=16000, FS=32000 or
80 * FS=48000). For very low input levels, the input signal is increased in level
81 * by multiplying and overwriting the samples in inMic[].
niklase@google.com470e71d2011-07-07 08:21:25 +000082 *
83 * This function should be called before any further processing of the
84 * near-end microphone signal.
85 *
86 * Input:
87 * - agcInst : AGC instance.
aluebs@webrtc.orgcf6d0b62014-12-16 20:56:09 +000088 * - inMic : Microphone input speech vector for each band
89 * - num_bands : Number of bands in input vector
niklase@google.com470e71d2011-07-07 08:21:25 +000090 * - samples : Number of samples in input vector
91 *
92 * Return value:
93 * : 0 - Normal operation.
94 * : -1 - Error
95 */
96int WebRtcAgc_AddMic(void* agcInst,
aluebs@webrtc.orgcf6d0b62014-12-16 20:56:09 +000097 int16_t* const* inMic,
Peter Kastingdce40cf2015-08-24 14:52:23 -070098 size_t num_bands,
99 size_t samples);
niklase@google.com470e71d2011-07-07 08:21:25 +0000100
101/*
102 * This function replaces the analog microphone with a virtual one.
103 * It is a digital gain applied to the input signal and is used in the
aluebs@webrtc.org96a62622014-12-15 21:54:50 +0000104 * agcAdaptiveDigital mode where no microphone level is adjustable. The length
105 * of the input speech vector must be given in samples (80 when FS=8000, and 160
aluebs@webrtc.orgcf6d0b62014-12-16 20:56:09 +0000106 * when FS=16000, FS=32000 or FS=48000).
niklase@google.com470e71d2011-07-07 08:21:25 +0000107 *
108 * Input:
109 * - agcInst : AGC instance.
aluebs@webrtc.orgcf6d0b62014-12-16 20:56:09 +0000110 * - inMic : Microphone input speech vector for each band
111 * - num_bands : Number of bands in input vector
niklase@google.com470e71d2011-07-07 08:21:25 +0000112 * - samples : Number of samples in input vector
113 * - micLevelIn : Input level of microphone (static)
114 *
115 * Output:
116 * - inMic : Microphone output after processing (L band)
117 * - inMic_H : Microphone output after processing (H band)
118 * - micLevelOut : Adjusted microphone level after processing
119 *
120 * Return value:
121 * : 0 - Normal operation.
122 * : -1 - Error
123 */
124int WebRtcAgc_VirtualMic(void* agcInst,
aluebs@webrtc.orgcf6d0b62014-12-16 20:56:09 +0000125 int16_t* const* inMic,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700126 size_t num_bands,
127 size_t samples,
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +0000128 int32_t micLevelIn,
129 int32_t* micLevelOut);
niklase@google.com470e71d2011-07-07 08:21:25 +0000130
131/*
aluebs@webrtc.org96a62622014-12-15 21:54:50 +0000132 * This function processes a 10 ms frame and adjusts (normalizes) the gain both
133 * analog and digitally. The gain adjustments are done only during active
134 * periods of speech. The length of the speech vectors must be given in samples
aluebs@webrtc.orgcf6d0b62014-12-16 20:56:09 +0000135 * (80 when FS=8000, and 160 when FS=16000, FS=32000 or FS=48000). The echo
136 * parameter can be used to ensure the AGC will not adjust upward in the
137 * presence of echo.
niklase@google.com470e71d2011-07-07 08:21:25 +0000138 *
139 * This function should be called after processing the near-end microphone
140 * signal, in any case after any echo cancellation.
141 *
142 * Input:
143 * - agcInst : AGC instance
aluebs@webrtc.orgcf6d0b62014-12-16 20:56:09 +0000144 * - inNear : Near-end input speech vector for each band
145 * - num_bands : Number of bands in input/output vector
niklase@google.com470e71d2011-07-07 08:21:25 +0000146 * - samples : Number of samples in input/output vector
147 * - inMicLevel : Current microphone volume level
148 * - echo : Set to 0 if the signal passed to add_mic is
149 * almost certainly free of echo; otherwise set
150 * to 1. If you have no information regarding echo
151 * set to 0.
152 *
153 * Output:
154 * - outMicLevel : Adjusted microphone volume level
aluebs@webrtc.orgcf6d0b62014-12-16 20:56:09 +0000155 * - out : Gain-adjusted near-end speech vector
niklase@google.com470e71d2011-07-07 08:21:25 +0000156 * : May be the same vector as the input.
niklase@google.com470e71d2011-07-07 08:21:25 +0000157 * - saturationWarning : A returned value of 1 indicates a saturation event
158 * has occurred and the volume cannot be further
159 * reduced. Otherwise will be set to 0.
160 *
161 * Return value:
162 * : 0 - Normal operation.
163 * : -1 - Error
164 */
165int WebRtcAgc_Process(void* agcInst,
aluebs@webrtc.orgcf6d0b62014-12-16 20:56:09 +0000166 const int16_t* const* inNear,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700167 size_t num_bands,
168 size_t samples,
aluebs@webrtc.orgcf6d0b62014-12-16 20:56:09 +0000169 int16_t* const* out,
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +0000170 int32_t inMicLevel,
171 int32_t* outMicLevel,
172 int16_t echo,
173 uint8_t* saturationWarning);
niklase@google.com470e71d2011-07-07 08:21:25 +0000174
175/*
176 * This function sets the config parameters (targetLevelDbfs,
177 * compressionGaindB and limiterEnable).
178 *
179 * Input:
180 * - agcInst : AGC instance
181 * - config : config struct
182 *
183 * Output:
184 *
185 * Return value:
186 * : 0 - Normal operation.
187 * : -1 - Error
188 */
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000189int WebRtcAgc_set_config(void* agcInst, WebRtcAgcConfig config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000190
191/*
192 * This function returns the config parameters (targetLevelDbfs,
193 * compressionGaindB and limiterEnable).
194 *
195 * Input:
196 * - agcInst : AGC instance
197 *
198 * Output:
199 * - config : config struct
200 *
201 * Return value:
202 * : 0 - Normal operation.
203 * : -1 - Error
204 */
pbos@webrtc.orge468bc92014-12-18 09:11:33 +0000205int WebRtcAgc_get_config(void* agcInst, WebRtcAgcConfig* config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000206
207/*
Bjorn Volcker9345e862015-06-10 21:43:36 +0200208 * This function creates and returns an AGC instance, which will contain the
209 * state information for one (duplex) channel.
niklase@google.com470e71d2011-07-07 08:21:25 +0000210 */
Bjorn Volcker9345e862015-06-10 21:43:36 +0200211void* WebRtcAgc_Create();
niklase@google.com470e71d2011-07-07 08:21:25 +0000212
213/*
214 * This function frees the AGC instance created at the beginning.
215 *
216 * Input:
217 * - agcInst : AGC instance.
niklase@google.com470e71d2011-07-07 08:21:25 +0000218 */
Bjorn Volckerf6a99e62015-04-10 07:56:57 +0200219void WebRtcAgc_Free(void* agcInst);
niklase@google.com470e71d2011-07-07 08:21:25 +0000220
221/*
222 * This function initializes an AGC instance.
223 *
224 * Input:
225 * - agcInst : AGC instance.
226 * - minLevel : Minimum possible mic level
227 * - maxLevel : Maximum possible mic level
228 * - agcMode : 0 - Unchanged
229 * : 1 - Adaptive Analog Automatic Gain Control -3dBOv
230 * : 2 - Adaptive Digital Automatic Gain Control -3dBOv
231 * : 3 - Fixed Digital Gain 0dB
232 * - fs : Sampling frequency
233 *
234 * Return value : 0 - Ok
235 * -1 - Error
236 */
minyuecac94aa2016-05-20 08:42:22 -0700237int WebRtcAgc_Init(void* agcInst,
pbos@webrtc.orgb7192b82013-04-10 07:50:54 +0000238 int32_t minLevel,
239 int32_t maxLevel,
240 int16_t agcMode,
241 uint32_t fs);
niklase@google.com470e71d2011-07-07 08:21:25 +0000242
niklase@google.com470e71d2011-07-07 08:21:25 +0000243#if defined(__cplusplus)
244}
245#endif
246
bjornv@webrtc.orgb395a5e2014-12-16 10:38:10 +0000247#endif // WEBRTC_MODULES_AUDIO_PROCESSING_AGC_LEGACY_GAIN_CONTROL_H_