gain_control.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 MODULES_AUDIO_PROCESSING_AGC_LEGACY_GAIN_CONTROL_H_
  11. #define MODULES_AUDIO_PROCESSING_AGC_LEGACY_GAIN_CONTROL_H_
  12. namespace webrtc {
  13. enum {
  14. kAgcModeUnchanged,
  15. kAgcModeAdaptiveAnalog,
  16. kAgcModeAdaptiveDigital,
  17. kAgcModeFixedDigital
  18. };
  19. enum { kAgcFalse = 0, kAgcTrue };
  20. typedef struct {
  21. int16_t targetLevelDbfs; // default 3 (-3 dBOv)
  22. int16_t compressionGaindB; // default 9 dB
  23. uint8_t limiterEnable; // default kAgcTrue (on)
  24. } WebRtcAgcConfig;
  25. /*
  26. * This function analyses the number of samples passed to
  27. * farend and produces any error code that could arise.
  28. *
  29. * Input:
  30. * - agcInst : AGC instance.
  31. * - samples : Number of samples in input vector.
  32. *
  33. * Return value:
  34. * : 0 - Normal operation.
  35. * : -1 - Error.
  36. */
  37. int WebRtcAgc_GetAddFarendError(void* state, size_t samples);
  38. /*
  39. * This function processes a 10 ms frame of far-end speech to determine
  40. * if there is active speech. The length of the input speech vector must be
  41. * given in samples (80 when FS=8000, and 160 when FS=16000, FS=32000 or
  42. * FS=48000).
  43. *
  44. * Input:
  45. * - agcInst : AGC instance.
  46. * - inFar : Far-end input speech vector
  47. * - samples : Number of samples in input vector
  48. *
  49. * Return value:
  50. * : 0 - Normal operation.
  51. * : -1 - Error
  52. */
  53. int WebRtcAgc_AddFarend(void* agcInst, const int16_t* inFar, size_t samples);
  54. /*
  55. * This function processes a 10 ms frame of microphone speech to determine
  56. * if there is active speech. The length of the input speech vector must be
  57. * given in samples (80 when FS=8000, and 160 when FS=16000, FS=32000 or
  58. * FS=48000). For very low input levels, the input signal is increased in level
  59. * by multiplying and overwriting the samples in inMic[].
  60. *
  61. * This function should be called before any further processing of the
  62. * near-end microphone signal.
  63. *
  64. * Input:
  65. * - agcInst : AGC instance.
  66. * - inMic : Microphone input speech vector for each band
  67. * - num_bands : Number of bands in input vector
  68. * - samples : Number of samples in input vector
  69. *
  70. * Return value:
  71. * : 0 - Normal operation.
  72. * : -1 - Error
  73. */
  74. int WebRtcAgc_AddMic(void* agcInst,
  75. int16_t* const* inMic,
  76. size_t num_bands,
  77. size_t samples);
  78. /*
  79. * This function replaces the analog microphone with a virtual one.
  80. * It is a digital gain applied to the input signal and is used in the
  81. * agcAdaptiveDigital mode where no microphone level is adjustable. The length
  82. * of the input speech vector must be given in samples (80 when FS=8000, and 160
  83. * when FS=16000, FS=32000 or FS=48000).
  84. *
  85. * Input:
  86. * - agcInst : AGC instance.
  87. * - inMic : Microphone input speech vector for each band
  88. * - num_bands : Number of bands in input vector
  89. * - samples : Number of samples in input vector
  90. * - micLevelIn : Input level of microphone (static)
  91. *
  92. * Output:
  93. * - inMic : Microphone output after processing (L band)
  94. * - inMic_H : Microphone output after processing (H band)
  95. * - micLevelOut : Adjusted microphone level after processing
  96. *
  97. * Return value:
  98. * : 0 - Normal operation.
  99. * : -1 - Error
  100. */
  101. int WebRtcAgc_VirtualMic(void* agcInst,
  102. int16_t* const* inMic,
  103. size_t num_bands,
  104. size_t samples,
  105. int32_t micLevelIn,
  106. int32_t* micLevelOut);
  107. /*
  108. * This function analyses a 10 ms frame and produces the analog and digital
  109. * gains required to normalize the signal. The gain adjustments are done only
  110. * during active periods of speech. The length of the speech vectors must be
  111. * given in samples (80 when FS=8000, and 160 when FS=16000, FS=32000 or
  112. * FS=48000). The echo parameter can be used to ensure the AGC will not adjust
  113. * upward in the presence of echo.
  114. *
  115. * This function should be called after processing the near-end microphone
  116. * signal, in any case after any echo cancellation.
  117. *
  118. * Input:
  119. * - agcInst : AGC instance
  120. * - inNear : Near-end input speech vector for each band
  121. * - num_bands : Number of bands in input/output vector
  122. * - samples : Number of samples in input/output vector
  123. * - inMicLevel : Current microphone volume level
  124. * - echo : Set to 0 if the signal passed to add_mic is
  125. * almost certainly free of echo; otherwise set
  126. * to 1. If you have no information regarding echo
  127. * set to 0.
  128. *
  129. * Output:
  130. * - outMicLevel : Adjusted microphone volume level
  131. * - saturationWarning : A returned value of 1 indicates a saturation event
  132. * has occurred and the volume cannot be further
  133. * reduced. Otherwise will be set to 0.
  134. * - gains : Vector of gains to apply for digital normalization
  135. *
  136. * Return value:
  137. * : 0 - Normal operation.
  138. * : -1 - Error
  139. */
  140. int WebRtcAgc_Analyze(void* agcInst,
  141. const int16_t* const* inNear,
  142. size_t num_bands,
  143. size_t samples,
  144. int32_t inMicLevel,
  145. int32_t* outMicLevel,
  146. int16_t echo,
  147. uint8_t* saturationWarning,
  148. int32_t gains[11]);
  149. /*
  150. * This function processes a 10 ms frame by applying precomputed digital gains.
  151. *
  152. * Input:
  153. * - agcInst : AGC instance
  154. * - gains : Vector of gains to apply for digital normalization
  155. * - in_near : Near-end input speech vector for each band
  156. * - num_bands : Number of bands in input/output vector
  157. *
  158. * Output:
  159. * - out : Gain-adjusted near-end speech vector
  160. * : May be the same vector as the input.
  161. *
  162. * Return value:
  163. * : 0 - Normal operation.
  164. * : -1 - Error
  165. */
  166. int WebRtcAgc_Process(const void* agcInst,
  167. const int32_t gains[11],
  168. const int16_t* const* in_near,
  169. size_t num_bands,
  170. int16_t* const* out);
  171. /*
  172. * This function sets the config parameters (targetLevelDbfs,
  173. * compressionGaindB and limiterEnable).
  174. *
  175. * Input:
  176. * - agcInst : AGC instance
  177. * - config : config struct
  178. *
  179. * Output:
  180. *
  181. * Return value:
  182. * : 0 - Normal operation.
  183. * : -1 - Error
  184. */
  185. int WebRtcAgc_set_config(void* agcInst, WebRtcAgcConfig config);
  186. /*
  187. * This function returns the config parameters (targetLevelDbfs,
  188. * compressionGaindB and limiterEnable).
  189. *
  190. * Input:
  191. * - agcInst : AGC instance
  192. *
  193. * Output:
  194. * - config : config struct
  195. *
  196. * Return value:
  197. * : 0 - Normal operation.
  198. * : -1 - Error
  199. */
  200. int WebRtcAgc_get_config(void* agcInst, WebRtcAgcConfig* config);
  201. /*
  202. * This function creates and returns an AGC instance, which will contain the
  203. * state information for one (duplex) channel.
  204. */
  205. void* WebRtcAgc_Create(void);
  206. /*
  207. * This function frees the AGC instance created at the beginning.
  208. *
  209. * Input:
  210. * - agcInst : AGC instance.
  211. */
  212. void 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. int32_t minLevel,
  231. int32_t maxLevel,
  232. int16_t agcMode,
  233. uint32_t fs);
  234. } // namespace webrtc
  235. #endif // MODULES_AUDIO_PROCESSING_AGC_LEGACY_GAIN_CONTROL_H_