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