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 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 14 | // Errors |
minyue | cac94aa | 2016-05-20 08:42:22 -0700 | [diff] [blame] | 15 | #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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 20 | |
| 21 | // Warnings |
minyue | cac94aa | 2016-05-20 08:42:22 -0700 | [diff] [blame] | 22 | #define AGC_BAD_PARAMETER_WARNING 18050 |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 23 | |
minyue | cac94aa | 2016-05-20 08:42:22 -0700 | [diff] [blame] | 24 | enum { |
| 25 | kAgcModeUnchanged, |
| 26 | kAgcModeAdaptiveAnalog, |
| 27 | kAgcModeAdaptiveDigital, |
| 28 | kAgcModeFixedDigital |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 29 | }; |
| 30 | |
minyue | cac94aa | 2016-05-20 08:42:22 -0700 | [diff] [blame] | 31 | enum { kAgcFalse = 0, kAgcTrue }; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 32 | |
minyue | cac94aa | 2016-05-20 08:42:22 -0700 | [diff] [blame] | 33 | typedef 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.org | e468bc9 | 2014-12-18 09:11:33 +0000 | [diff] [blame] | 37 | } WebRtcAgcConfig; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 38 | |
| 39 | #if defined(__cplusplus) |
minyue | cac94aa | 2016-05-20 08:42:22 -0700 | [diff] [blame] | 40 | extern "C" { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 41 | #endif |
| 42 | |
| 43 | /* |
peah | 4d291f7 | 2015-11-16 23:52:25 -0800 | [diff] [blame] | 44 | * 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 | */ |
| 55 | int WebRtcAgc_GetAddFarendError(void* state, size_t samples); |
| 56 | |
| 57 | /* |
aluebs@webrtc.org | 96a6262 | 2014-12-15 21:54:50 +0000 | [diff] [blame] | 58 | * 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.org | cf6d0b6 | 2014-12-16 20:56:09 +0000 | [diff] [blame] | 60 | * given in samples (80 when FS=8000, and 160 when FS=16000, FS=32000 or |
| 61 | * FS=48000). |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 62 | * |
| 63 | * Input: |
| 64 | * - agcInst : AGC instance. |
aluebs@webrtc.org | 96a6262 | 2014-12-15 21:54:50 +0000 | [diff] [blame] | 65 | * - inFar : Far-end input speech vector |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 66 | * - samples : Number of samples in input vector |
| 67 | * |
| 68 | * Return value: |
| 69 | * : 0 - Normal operation. |
| 70 | * : -1 - Error |
| 71 | */ |
minyue | cac94aa | 2016-05-20 08:42:22 -0700 | [diff] [blame] | 72 | 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] | 73 | |
| 74 | /* |
aluebs@webrtc.org | 96a6262 | 2014-12-15 21:54:50 +0000 | [diff] [blame] | 75 | * 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.org | cf6d0b6 | 2014-12-16 20:56:09 +0000 | [diff] [blame] | 77 | * 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 80 | * |
| 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.org | cf6d0b6 | 2014-12-16 20:56:09 +0000 | [diff] [blame] | 86 | * - inMic : Microphone input speech vector for each band |
| 87 | * - num_bands : Number of bands in input vector |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 88 | * - samples : Number of samples in input vector |
| 89 | * |
| 90 | * Return value: |
| 91 | * : 0 - Normal operation. |
| 92 | * : -1 - Error |
| 93 | */ |
| 94 | int WebRtcAgc_AddMic(void* agcInst, |
aluebs@webrtc.org | cf6d0b6 | 2014-12-16 20:56:09 +0000 | [diff] [blame] | 95 | int16_t* const* inMic, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 96 | size_t num_bands, |
| 97 | size_t samples); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 98 | |
| 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.org | 96a6262 | 2014-12-15 21:54:50 +0000 | [diff] [blame] | 102 | * 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.org | cf6d0b6 | 2014-12-16 20:56:09 +0000 | [diff] [blame] | 104 | * when FS=16000, FS=32000 or FS=48000). |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 105 | * |
| 106 | * Input: |
| 107 | * - agcInst : AGC instance. |
aluebs@webrtc.org | cf6d0b6 | 2014-12-16 20:56:09 +0000 | [diff] [blame] | 108 | * - inMic : Microphone input speech vector for each band |
| 109 | * - num_bands : Number of bands in input vector |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 110 | * - 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 | */ |
| 122 | int WebRtcAgc_VirtualMic(void* agcInst, |
aluebs@webrtc.org | cf6d0b6 | 2014-12-16 20:56:09 +0000 | [diff] [blame] | 123 | int16_t* const* inMic, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 124 | size_t num_bands, |
| 125 | size_t samples, |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 126 | int32_t micLevelIn, |
| 127 | int32_t* micLevelOut); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 128 | |
| 129 | /* |
aluebs@webrtc.org | 96a6262 | 2014-12-15 21:54:50 +0000 | [diff] [blame] | 130 | * 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.org | cf6d0b6 | 2014-12-16 20:56:09 +0000 | [diff] [blame] | 133 | * (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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 136 | * |
| 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.org | cf6d0b6 | 2014-12-16 20:56:09 +0000 | [diff] [blame] | 142 | * - inNear : Near-end input speech vector for each band |
| 143 | * - num_bands : Number of bands in input/output vector |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 144 | * - 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.org | cf6d0b6 | 2014-12-16 20:56:09 +0000 | [diff] [blame] | 153 | * - out : Gain-adjusted near-end speech vector |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 154 | * : May be the same vector as the input. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 155 | * - 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 | */ |
| 163 | int WebRtcAgc_Process(void* agcInst, |
aluebs@webrtc.org | cf6d0b6 | 2014-12-16 20:56:09 +0000 | [diff] [blame] | 164 | const int16_t* const* inNear, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 165 | size_t num_bands, |
| 166 | size_t samples, |
aluebs@webrtc.org | cf6d0b6 | 2014-12-16 20:56:09 +0000 | [diff] [blame] | 167 | int16_t* const* out, |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 168 | int32_t inMicLevel, |
| 169 | int32_t* outMicLevel, |
| 170 | int16_t echo, |
| 171 | uint8_t* saturationWarning); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 172 | |
| 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.org | e468bc9 | 2014-12-18 09:11:33 +0000 | [diff] [blame] | 187 | int WebRtcAgc_set_config(void* agcInst, WebRtcAgcConfig config); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 188 | |
| 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.org | e468bc9 | 2014-12-18 09:11:33 +0000 | [diff] [blame] | 203 | int WebRtcAgc_get_config(void* agcInst, WebRtcAgcConfig* config); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 204 | |
| 205 | /* |
Bjorn Volcker | 9345e86 | 2015-06-10 21:43:36 +0200 | [diff] [blame] | 206 | * This function creates and returns an AGC instance, which will contain the |
| 207 | * state information for one (duplex) channel. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 208 | */ |
Mirko Bonadei | d757356 | 2018-03-19 16:23:48 +0100 | [diff] [blame] | 209 | void* WebRtcAgc_Create(void); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 210 | |
| 211 | /* |
| 212 | * This function frees the AGC instance created at the beginning. |
| 213 | * |
| 214 | * Input: |
| 215 | * - agcInst : AGC instance. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 216 | */ |
Bjorn Volcker | f6a99e6 | 2015-04-10 07:56:57 +0200 | [diff] [blame] | 217 | void WebRtcAgc_Free(void* agcInst); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 218 | |
| 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 | */ |
minyue | cac94aa | 2016-05-20 08:42:22 -0700 | [diff] [blame] | 235 | int WebRtcAgc_Init(void* agcInst, |
pbos@webrtc.org | b7192b8 | 2013-04-10 07:50:54 +0000 | [diff] [blame] | 236 | int32_t minLevel, |
| 237 | int32_t maxLevel, |
| 238 | int16_t agcMode, |
| 239 | uint32_t fs); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 240 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 241 | #if defined(__cplusplus) |
| 242 | } |
| 243 | #endif |
| 244 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 245 | #endif // MODULES_AUDIO_PROCESSING_AGC_LEGACY_GAIN_CONTROL_H_ |