gain_control.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
  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. #ifndef WEBRTC_MODULES_AUDIO_PROCESSING_AGC_INCLUDE_GAIN_CONTROL_H_
  11. #define WEBRTC_MODULES_AUDIO_PROCESSING_AGC_INCLUDE_GAIN_CONTROL_H_
  12. #include "typedefs.h"
  13. // Errors
  14. #define AGC_UNSPECIFIED_ERROR 18000
  15. #define AGC_UNSUPPORTED_FUNCTION_ERROR 18001
  16. #define AGC_UNINITIALIZED_ERROR 18002
  17. #define AGC_NULL_POINTER_ERROR 18003
  18. #define AGC_BAD_PARAMETER_ERROR 18004
  19. // Warnings
  20. #define AGC_BAD_PARAMETER_WARNING 18050
  21. enum
  22. {
  23. kAgcModeUnchanged,
  24. kAgcModeAdaptiveAnalog,
  25. kAgcModeAdaptiveDigital,
  26. kAgcModeFixedDigital
  27. };
  28. enum
  29. {
  30. kAgcFalse = 0,
  31. kAgcTrue
  32. };
  33. typedef struct
  34. {
  35. int16_t targetLevelDbfs; // default 3 (-3 dBOv)
  36. int16_t compressionGaindB; // default 9 dB
  37. uint8_t limiterEnable; // default kAgcTrue (on)
  38. } WebRtcAgc_config_t;
  39. #if defined(__cplusplus)
  40. extern "C"
  41. {
  42. #endif
  43. /*
  44. * This function processes a 10/20ms frame of far-end speech to determine
  45. * if there is active speech. Far-end speech length can be either 10ms or
  46. * 20ms. The length of the input speech vector must be given in samples
  47. * (80/160 when FS=8000, and 160/320 when FS=16000 or FS=32000).
  48. *
  49. * Input:
  50. * - agcInst : AGC instance.
  51. * - inFar : Far-end input speech vector (10 or 20ms)
  52. * - samples : Number of samples in input vector
  53. *
  54. * Return value:
  55. * : 0 - Normal operation.
  56. * : -1 - Error
  57. */
  58. int WebRtcAgc_AddFarend(void* agcInst,
  59. const int16_t* inFar,
  60. int16_t samples);
  61. /*
  62. * This function processes a 10/20ms frame of microphone speech to determine
  63. * if there is active speech. Microphone speech length can be either 10ms or
  64. * 20ms. The length of the input speech vector must be given in samples
  65. * (80/160 when FS=8000, and 160/320 when FS=16000 or FS=32000). For very low
  66. * input levels, the input signal is increased in level by multiplying and
  67. * overwriting the samples in inMic[].
  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.
  74. * - inMic : Microphone input speech vector (10 or 20 ms) for
  75. * L band
  76. * - inMic_H : Microphone input speech vector (10 or 20 ms) for
  77. * H band
  78. * - samples : Number of samples in input vector
  79. *
  80. * Return value:
  81. * : 0 - Normal operation.
  82. * : -1 - Error
  83. */
  84. int WebRtcAgc_AddMic(void* agcInst,
  85. int16_t* inMic,
  86. int16_t* inMic_H,
  87. int16_t samples);
  88. /*
  89. * This function replaces the analog microphone with a virtual one.
  90. * It is a digital gain applied to the input signal and is used in the
  91. * agcAdaptiveDigital mode where no microphone level is adjustable.
  92. * Microphone speech length can be either 10ms or 20ms. The length of the
  93. * input speech vector must be given in samples (80/160 when FS=8000, and
  94. * 160/320 when FS=16000 or FS=32000).
  95. *
  96. * Input:
  97. * - agcInst : AGC instance.
  98. * - inMic : Microphone input speech vector for (10 or 20 ms)
  99. * L band
  100. * - inMic_H : Microphone input speech vector for (10 or 20 ms)
  101. * H band
  102. * - samples : Number of samples in input vector
  103. * - micLevelIn : Input level of microphone (static)
  104. *
  105. * Output:
  106. * - inMic : Microphone output after processing (L band)
  107. * - inMic_H : Microphone output after processing (H band)
  108. * - micLevelOut : Adjusted microphone level after processing
  109. *
  110. * Return value:
  111. * : 0 - Normal operation.
  112. * : -1 - Error
  113. */
  114. int WebRtcAgc_VirtualMic(void* agcInst,
  115. int16_t* inMic,
  116. int16_t* inMic_H,
  117. int16_t samples,
  118. int32_t micLevelIn,
  119. int32_t* micLevelOut);
  120. /*
  121. * This function processes a 10/20ms frame and adjusts (normalizes) the gain
  122. * both analog and digitally. The gain adjustments are done only during
  123. * active periods of speech. The input speech length can be either 10ms or
  124. * 20ms and the output is of the same length. The length of the speech
  125. * vectors must be given in samples (80/160 when FS=8000, and 160/320 when
  126. * FS=16000 or FS=32000). The echo parameter can be used to ensure the AGC will
  127. * not adjust upward in the presence of echo.
  128. *
  129. * This function should be called after processing the near-end microphone
  130. * signal, in any case after any echo cancellation.
  131. *
  132. * Input:
  133. * - agcInst : AGC instance
  134. * - inNear : Near-end input speech vector (10 or 20 ms) for
  135. * L band
  136. * - inNear_H : Near-end input speech vector (10 or 20 ms) for
  137. * H band
  138. * - samples : Number of samples in input/output vector
  139. * - inMicLevel : Current microphone volume level
  140. * - echo : Set to 0 if the signal passed to add_mic is
  141. * almost certainly free of echo; otherwise set
  142. * to 1. If you have no information regarding echo
  143. * set to 0.
  144. *
  145. * Output:
  146. * - outMicLevel : Adjusted microphone volume level
  147. * - out : Gain-adjusted near-end speech vector (L band)
  148. * : May be the same vector as the input.
  149. * - out_H : Gain-adjusted near-end speech vector (H band)
  150. * - saturationWarning : A returned value of 1 indicates a saturation event
  151. * has occurred and the volume cannot be further
  152. * reduced. Otherwise will be set to 0.
  153. *
  154. * Return value:
  155. * : 0 - Normal operation.
  156. * : -1 - Error
  157. */
  158. int WebRtcAgc_Process(void* agcInst,
  159. const int16_t* inNear,
  160. const int16_t* inNear_H,
  161. int16_t samples,
  162. int16_t* out,
  163. int16_t* out_H,
  164. int32_t inMicLevel,
  165. int32_t* outMicLevel,
  166. int16_t echo,
  167. uint8_t* saturationWarning);
  168. /*
  169. * This function sets the config parameters (targetLevelDbfs,
  170. * compressionGaindB and limiterEnable).
  171. *
  172. * Input:
  173. * - agcInst : AGC instance
  174. * - config : config struct
  175. *
  176. * Output:
  177. *
  178. * Return value:
  179. * : 0 - Normal operation.
  180. * : -1 - Error
  181. */
  182. int WebRtcAgc_set_config(void* agcInst, WebRtcAgc_config_t config);
  183. /*
  184. * This function returns the config parameters (targetLevelDbfs,
  185. * compressionGaindB and limiterEnable).
  186. *
  187. * Input:
  188. * - agcInst : AGC instance
  189. *
  190. * Output:
  191. * - config : config struct
  192. *
  193. * Return value:
  194. * : 0 - Normal operation.
  195. * : -1 - Error
  196. */
  197. int WebRtcAgc_get_config(void* agcInst, WebRtcAgc_config_t* config);
  198. /*
  199. * This function creates an AGC instance, which will contain the state
  200. * information for one (duplex) channel.
  201. *
  202. * Return value : AGC instance if successful
  203. * : 0 (i.e., a NULL pointer) if unsuccessful
  204. */
  205. int WebRtcAgc_Create(void **agcInst);
  206. /*
  207. * This function frees the AGC instance created at the beginning.
  208. *
  209. * Input:
  210. * - agcInst : AGC instance.
  211. *
  212. * Return value : 0 - Ok
  213. * -1 - Error
  214. */
  215. int WebRtcAgc_Free(void *agcInst);
  216. /*
  217. * This function initializes an AGC instance.
  218. *
  219. * Input:
  220. * - agcInst : AGC instance.
  221. * - minLevel : Minimum possible mic level
  222. * - maxLevel : Maximum possible mic level
  223. * - agcMode : 0 - Unchanged
  224. * : 1 - Adaptive Analog Automatic Gain Control -3dBOv
  225. * : 2 - Adaptive Digital Automatic Gain Control -3dBOv
  226. * : 3 - Fixed Digital Gain 0dB
  227. * - fs : Sampling frequency
  228. *
  229. * Return value : 0 - Ok
  230. * -1 - Error
  231. */
  232. int WebRtcAgc_Init(void *agcInst,
  233. int32_t minLevel,
  234. int32_t maxLevel,
  235. int16_t agcMode,
  236. uint32_t fs);
  237. #if defined(__cplusplus)
  238. }
  239. #endif
  240. #endif // WEBRTC_MODULES_AUDIO_PROCESSING_AGC_INCLUDE_GAIN_CONTROL_H_