gain_control.h 9.1 KB

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