niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
andrew@webrtc.org | b9d7d93 | 2012-01-25 19:21:13 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_AUDIO_PROCESSING_AGC_LEGACY_GAIN_CONTROL_H_ |
| 12 | #define MODULES_AUDIO_PROCESSING_AGC_LEGACY_GAIN_CONTROL_H_ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | |
Per Åhgren | 5b139d6 | 2020-03-20 15:50:14 +0100 | [diff] [blame] | 14 | namespace webrtc { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 15 | |
minyue | cac94aa | 2016-05-20 08:42:22 -0700 | [diff] [blame] | 16 | enum { |
| 17 | kAgcModeUnchanged, |
| 18 | kAgcModeAdaptiveAnalog, |
| 19 | kAgcModeAdaptiveDigital, |
| 20 | kAgcModeFixedDigital |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 21 | }; |
| 22 | |
minyue | cac94aa | 2016-05-20 08:42:22 -0700 | [diff] [blame] | 23 | enum { kAgcFalse = 0, kAgcTrue }; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 24 | |
minyue | cac94aa | 2016-05-20 08:42:22 -0700 | [diff] [blame] | 25 | typedef 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.org | e468bc9 | 2014-12-18 09:11:33 +0000 | [diff] [blame] | 29 | } WebRtcAgcConfig; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 30 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 31 | /* |
peah | 4d291f7 | 2015-11-16 23:52:25 -0800 | [diff] [blame] | 32 | * 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 | */ |
| 43 | int WebRtcAgc_GetAddFarendError(void* state, size_t samples); |
| 44 | |
| 45 | /* |
aluebs@webrtc.org | 96a6262 | 2014-12-15 21:54:50 +0000 | [diff] [blame] | 46 | * 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.org | cf6d0b6 | 2014-12-16 20:56:09 +0000 | [diff] [blame] | 48 | * given in samples (80 when FS=8000, and 160 when FS=16000, FS=32000 or |
| 49 | * FS=48000). |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 50 | * |
| 51 | * Input: |
| 52 | * - agcInst : AGC instance. |
aluebs@webrtc.org | 96a6262 | 2014-12-15 21:54:50 +0000 | [diff] [blame] | 53 | * - inFar : Far-end input speech vector |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 54 | * - samples : Number of samples in input vector |
| 55 | * |
| 56 | * Return value: |
| 57 | * : 0 - Normal operation. |
| 58 | * : -1 - Error |
| 59 | */ |
minyue | cac94aa | 2016-05-20 08:42:22 -0700 | [diff] [blame] | 60 | int WebRtcAgc_AddFarend(void* agcInst, const int16_t* inFar, size_t samples); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 61 | |
| 62 | /* |
aluebs@webrtc.org | 96a6262 | 2014-12-15 21:54:50 +0000 | [diff] [blame] | 63 | * 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.org | cf6d0b6 | 2014-12-16 20:56:09 +0000 | [diff] [blame] | 65 | * 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 68 | * |
| 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.org | cf6d0b6 | 2014-12-16 20:56:09 +0000 | [diff] [blame] | 74 | * - inMic : Microphone input speech vector for each band |
| 75 | * - num_bands : Number of bands in input vector |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 76 | * - samples : Number of samples in input vector |
| 77 | * |
| 78 | * Return value: |
| 79 | * : 0 - Normal operation. |
| 80 | * : -1 - Error |
| 81 | */ |
| 82 | int WebRtcAgc_AddMic(void* agcInst, |
aluebs@webrtc.org | cf6d0b6 | 2014-12-16 20:56:09 +0000 | [diff] [blame] | 83 | int16_t* const* inMic, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 84 | size_t num_bands, |
| 85 | size_t samples); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 86 | |
| 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.org | 96a6262 | 2014-12-15 21:54:50 +0000 | [diff] [blame] | 90 | * 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.org | cf6d0b6 | 2014-12-16 20:56:09 +0000 | [diff] [blame] | 92 | * when FS=16000, FS=32000 or FS=48000). |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 93 | * |
| 94 | * Input: |
| 95 | * - agcInst : AGC instance. |
aluebs@webrtc.org | cf6d0b6 | 2014-12-16 20:56:09 +0000 | [diff] [blame] | 96 | * - inMic : Microphone input speech vector for each band |
| 97 | * - num_bands : Number of bands in input vector |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 98 | * - 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 | */ |
| 110 | int WebRtcAgc_VirtualMic(void* agcInst, |
aluebs@webrtc.org | cf6d0b6 | 2014-12-16 20:56:09 +0000 | [diff] [blame] | 111 | int16_t* const* inMic, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 112 | size_t num_bands, |
| 113 | size_t samples, |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 114 | int32_t micLevelIn, |
| 115 | int32_t* micLevelOut); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 116 | |
| 117 | /* |
Per Åhgren | 77dc199 | 2019-11-23 00:14:31 +0100 | [diff] [blame] | 118 | * 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 124 | * |
| 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.org | cf6d0b6 | 2014-12-16 20:56:09 +0000 | [diff] [blame] | 130 | * - inNear : Near-end input speech vector for each band |
| 131 | * - num_bands : Number of bands in input/output vector |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 132 | * - 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 141 | * - 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 Åhgren | 77dc199 | 2019-11-23 00:14:31 +0100 | [diff] [blame] | 144 | * - gains : Vector of gains to apply for digital normalization |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 145 | * |
| 146 | * Return value: |
| 147 | * : 0 - Normal operation. |
| 148 | * : -1 - Error |
| 149 | */ |
Per Åhgren | 77dc199 | 2019-11-23 00:14:31 +0100 | [diff] [blame] | 150 | int WebRtcAgc_Analyze(void* agcInst, |
aluebs@webrtc.org | cf6d0b6 | 2014-12-16 20:56:09 +0000 | [diff] [blame] | 151 | const int16_t* const* inNear, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 152 | size_t num_bands, |
| 153 | size_t samples, |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 154 | int32_t inMicLevel, |
| 155 | int32_t* outMicLevel, |
| 156 | int16_t echo, |
Per Åhgren | 77dc199 | 2019-11-23 00:14:31 +0100 | [diff] [blame] | 157 | 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 | */ |
| 177 | int 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 182 | |
| 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.org | e468bc9 | 2014-12-18 09:11:33 +0000 | [diff] [blame] | 197 | int WebRtcAgc_set_config(void* agcInst, WebRtcAgcConfig config); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 198 | |
| 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.org | e468bc9 | 2014-12-18 09:11:33 +0000 | [diff] [blame] | 213 | int WebRtcAgc_get_config(void* agcInst, WebRtcAgcConfig* config); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 214 | |
| 215 | /* |
Bjorn Volcker | 9345e86 | 2015-06-10 21:43:36 +0200 | [diff] [blame] | 216 | * This function creates and returns an AGC instance, which will contain the |
| 217 | * state information for one (duplex) channel. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 218 | */ |
Mirko Bonadei | d757356 | 2018-03-19 16:23:48 +0100 | [diff] [blame] | 219 | void* WebRtcAgc_Create(void); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 220 | |
| 221 | /* |
| 222 | * This function frees the AGC instance created at the beginning. |
| 223 | * |
| 224 | * Input: |
| 225 | * - agcInst : AGC instance. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 226 | */ |
Bjorn Volcker | f6a99e6 | 2015-04-10 07:56:57 +0200 | [diff] [blame] | 227 | void WebRtcAgc_Free(void* agcInst); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 228 | |
| 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 | */ |
minyue | cac94aa | 2016-05-20 08:42:22 -0700 | [diff] [blame] | 245 | int WebRtcAgc_Init(void* agcInst, |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 246 | int32_t minLevel, |
| 247 | int32_t maxLevel, |
| 248 | int16_t agcMode, |
| 249 | uint32_t fs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 250 | |
Per Åhgren | 5b139d6 | 2020-03-20 15:50:14 +0100 | [diff] [blame] | 251 | } // namespace webrtc |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 252 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 253 | #endif // MODULES_AUDIO_PROCESSING_AGC_LEGACY_GAIN_CONTROL_H_ |