func_speex.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2008, Digium, Inc.
  5. *
  6. * Brian Degenhardt <bmd@digium.com>
  7. * Brett Bryant <bbryant@digium.com>
  8. *
  9. * See http://www.asterisk.org for more information about
  10. * the Asterisk project. Please do not directly contact
  11. * any of the maintainers of this project for assistance;
  12. * the project provides a web site, mailing lists and IRC
  13. * channels for your use.
  14. *
  15. * This program is free software, distributed under the terms of
  16. * the GNU General Public License Version 2. See the LICENSE file
  17. * at the top of the source tree.
  18. */
  19. /*! \file
  20. *
  21. * \brief Noise reduction and automatic gain control (AGC)
  22. *
  23. * \author Brian Degenhardt <bmd@digium.com>
  24. * \author Brett Bryant <bbryant@digium.com>
  25. *
  26. * \ingroup functions
  27. *
  28. * The Speex 1.2 library - http://www.speex.org
  29. * \note Requires the 1.2 version of the Speex library (which might not be what you find in Linux packages)
  30. */
  31. /*** MODULEINFO
  32. <depend>speex</depend>
  33. <depend>speex_preprocess</depend>
  34. <use type="external">speexdsp</use>
  35. <support_level>core</support_level>
  36. ***/
  37. #include "asterisk.h"
  38. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  39. #include <speex/speex_preprocess.h>
  40. #include "asterisk/module.h"
  41. #include "asterisk/channel.h"
  42. #include "asterisk/pbx.h"
  43. #include "asterisk/utils.h"
  44. #include "asterisk/audiohook.h"
  45. #define DEFAULT_AGC_LEVEL 8000.0
  46. /*** DOCUMENTATION
  47. <function name="AGC" language="en_US">
  48. <synopsis>
  49. Apply automatic gain control to audio on a channel.
  50. </synopsis>
  51. <syntax>
  52. <parameter name="channeldirection" required="true">
  53. <para>This can be either <literal>rx</literal> or <literal>tx</literal></para>
  54. </parameter>
  55. </syntax>
  56. <description>
  57. <para>The AGC function will apply automatic gain control to the audio on the
  58. channel that it is executed on. Using <literal>rx</literal> for audio received
  59. and <literal>tx</literal> for audio transmitted to the channel. When using this
  60. function you set a target audio level. It is primarily intended for use with
  61. analog lines, but could be useful for other channels as well. The target volume
  62. is set with a number between <literal>1-32768</literal>. The larger the number
  63. the louder (more gain) the channel will receive.</para>
  64. <para>Examples:</para>
  65. <para>exten => 1,1,Set(AGC(rx)=8000)</para>
  66. <para>exten => 1,2,Set(AGC(tx)=off)</para>
  67. </description>
  68. </function>
  69. <function name="DENOISE" language="en_US">
  70. <synopsis>
  71. Apply noise reduction to audio on a channel.
  72. </synopsis>
  73. <syntax>
  74. <parameter name="channeldirection" required="true">
  75. <para>This can be either <literal>rx</literal> or <literal>tx</literal>
  76. the values that can be set to this are either <literal>on</literal> and
  77. <literal>off</literal></para>
  78. </parameter>
  79. </syntax>
  80. <description>
  81. <para>The DENOISE function will apply noise reduction to audio on the channel
  82. that it is executed on. It is very useful for noisy analog lines, especially
  83. when adjusting gains or using AGC. Use <literal>rx</literal> for audio received from the channel
  84. and <literal>tx</literal> to apply the filter to the audio being sent to the channel.</para>
  85. <para>Examples:</para>
  86. <para>exten => 1,1,Set(DENOISE(rx)=on)</para>
  87. <para>exten => 1,2,Set(DENOISE(tx)=off)</para>
  88. </description>
  89. </function>
  90. ***/
  91. struct speex_direction_info {
  92. SpeexPreprocessState *state; /*!< speex preprocess state object */
  93. int agc; /*!< audio gain control is enabled or not */
  94. int denoise; /*!< denoise is enabled or not */
  95. int samples; /*!< n of 8Khz samples in last frame */
  96. float agclevel; /*!< audio gain control level [1.0 - 32768.0] */
  97. };
  98. struct speex_info {
  99. struct ast_audiohook audiohook;
  100. int lastrate;
  101. struct speex_direction_info *tx, *rx;
  102. };
  103. static void destroy_callback(void *data)
  104. {
  105. struct speex_info *si = data;
  106. ast_audiohook_destroy(&si->audiohook);
  107. if (si->rx && si->rx->state) {
  108. speex_preprocess_state_destroy(si->rx->state);
  109. }
  110. if (si->tx && si->tx->state) {
  111. speex_preprocess_state_destroy(si->tx->state);
  112. }
  113. if (si->rx) {
  114. ast_free(si->rx);
  115. }
  116. if (si->tx) {
  117. ast_free(si->tx);
  118. }
  119. ast_free(data);
  120. };
  121. static const struct ast_datastore_info speex_datastore = {
  122. .type = "speex",
  123. .destroy = destroy_callback
  124. };
  125. static int speex_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
  126. {
  127. struct ast_datastore *datastore = NULL;
  128. struct speex_direction_info *sdi = NULL;
  129. struct speex_info *si = NULL;
  130. char source[80];
  131. /* If the audiohook is stopping it means the channel is shutting down.... but we let the datastore destroy take care of it */
  132. if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE || frame->frametype != AST_FRAME_VOICE) {
  133. return -1;
  134. }
  135. /* We are called with chan already locked */
  136. if (!(datastore = ast_channel_datastore_find(chan, &speex_datastore, NULL))) {
  137. return -1;
  138. }
  139. si = datastore->data;
  140. sdi = (direction == AST_AUDIOHOOK_DIRECTION_READ) ? si->rx : si->tx;
  141. if (!sdi) {
  142. return -1;
  143. }
  144. if ((sdi->samples != frame->samples) || (ast_format_get_sample_rate(frame->subclass.format) != si->lastrate)) {
  145. si->lastrate = ast_format_get_sample_rate(frame->subclass.format);
  146. if (sdi->state) {
  147. speex_preprocess_state_destroy(sdi->state);
  148. }
  149. if (!(sdi->state = speex_preprocess_state_init((sdi->samples = frame->samples), si->lastrate))) {
  150. return -1;
  151. }
  152. speex_preprocess_ctl(sdi->state, SPEEX_PREPROCESS_SET_AGC, &sdi->agc);
  153. if (sdi->agc) {
  154. speex_preprocess_ctl(sdi->state, SPEEX_PREPROCESS_SET_AGC_LEVEL, &sdi->agclevel);
  155. }
  156. speex_preprocess_ctl(sdi->state, SPEEX_PREPROCESS_SET_DENOISE, &sdi->denoise);
  157. }
  158. speex_preprocess(sdi->state, frame->data.ptr, NULL);
  159. snprintf(source, sizeof(source), "%s/speex", frame->src);
  160. if (frame->mallocd & AST_MALLOCD_SRC) {
  161. ast_free((char *) frame->src);
  162. }
  163. frame->src = ast_strdup(source);
  164. frame->mallocd |= AST_MALLOCD_SRC;
  165. return 0;
  166. }
  167. static int speex_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
  168. {
  169. struct ast_datastore *datastore = NULL;
  170. struct speex_info *si = NULL;
  171. struct speex_direction_info **sdi = NULL;
  172. int is_new = 0;
  173. if (!chan) {
  174. ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
  175. return -1;
  176. }
  177. if (strcasecmp(data, "rx") && strcasecmp(data, "tx")) {
  178. ast_log(LOG_ERROR, "Invalid argument provided to the %s function\n", cmd);
  179. return -1;
  180. }
  181. ast_channel_lock(chan);
  182. if (!(datastore = ast_channel_datastore_find(chan, &speex_datastore, NULL))) {
  183. ast_channel_unlock(chan);
  184. if (!(datastore = ast_datastore_alloc(&speex_datastore, NULL))) {
  185. return 0;
  186. }
  187. if (!(si = ast_calloc(1, sizeof(*si)))) {
  188. ast_datastore_free(datastore);
  189. return 0;
  190. }
  191. ast_audiohook_init(&si->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "speex", AST_AUDIOHOOK_MANIPULATE_ALL_RATES);
  192. si->audiohook.manipulate_callback = speex_callback;
  193. si->lastrate = 8000;
  194. is_new = 1;
  195. } else {
  196. ast_channel_unlock(chan);
  197. si = datastore->data;
  198. }
  199. if (!strcasecmp(data, "rx")) {
  200. sdi = &si->rx;
  201. } else {
  202. sdi = &si->tx;
  203. }
  204. if (!*sdi) {
  205. if (!(*sdi = ast_calloc(1, sizeof(**sdi)))) {
  206. return 0;
  207. }
  208. /* Right now, the audiohooks API will _only_ provide us 8 kHz slinear
  209. * audio. When it supports 16 kHz (or any other sample rates, we will
  210. * have to take that into account here. */
  211. (*sdi)->samples = -1;
  212. }
  213. if (!strcasecmp(cmd, "agc")) {
  214. if (!sscanf(value, "%30f", &(*sdi)->agclevel))
  215. (*sdi)->agclevel = ast_true(value) ? DEFAULT_AGC_LEVEL : 0.0;
  216. if ((*sdi)->agclevel > 32768.0) {
  217. ast_log(LOG_WARNING, "AGC(%s)=%.01f is greater than 32768... setting to 32768 instead\n",
  218. ((*sdi == si->rx) ? "rx" : "tx"), (*sdi)->agclevel);
  219. (*sdi)->agclevel = 32768.0;
  220. }
  221. (*sdi)->agc = !!((*sdi)->agclevel);
  222. if ((*sdi)->state) {
  223. speex_preprocess_ctl((*sdi)->state, SPEEX_PREPROCESS_SET_AGC, &(*sdi)->agc);
  224. if ((*sdi)->agc) {
  225. speex_preprocess_ctl((*sdi)->state, SPEEX_PREPROCESS_SET_AGC_LEVEL, &(*sdi)->agclevel);
  226. }
  227. }
  228. } else if (!strcasecmp(cmd, "denoise")) {
  229. (*sdi)->denoise = (ast_true(value) != 0);
  230. if ((*sdi)->state) {
  231. speex_preprocess_ctl((*sdi)->state, SPEEX_PREPROCESS_SET_DENOISE, &(*sdi)->denoise);
  232. }
  233. }
  234. if (!(*sdi)->agc && !(*sdi)->denoise) {
  235. if ((*sdi)->state)
  236. speex_preprocess_state_destroy((*sdi)->state);
  237. ast_free(*sdi);
  238. *sdi = NULL;
  239. }
  240. if (!si->rx && !si->tx) {
  241. if (is_new) {
  242. is_new = 0;
  243. } else {
  244. ast_channel_lock(chan);
  245. ast_channel_datastore_remove(chan, datastore);
  246. ast_channel_unlock(chan);
  247. ast_audiohook_remove(chan, &si->audiohook);
  248. ast_audiohook_detach(&si->audiohook);
  249. }
  250. ast_datastore_free(datastore);
  251. }
  252. if (is_new) {
  253. datastore->data = si;
  254. ast_channel_lock(chan);
  255. ast_channel_datastore_add(chan, datastore);
  256. ast_channel_unlock(chan);
  257. ast_audiohook_attach(chan, &si->audiohook);
  258. }
  259. return 0;
  260. }
  261. static int speex_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
  262. {
  263. struct ast_datastore *datastore = NULL;
  264. struct speex_info *si = NULL;
  265. struct speex_direction_info *sdi = NULL;
  266. if (!chan) {
  267. ast_log(LOG_ERROR, "%s cannot be used without a channel!\n", cmd);
  268. return -1;
  269. }
  270. ast_channel_lock(chan);
  271. if (!(datastore = ast_channel_datastore_find(chan, &speex_datastore, NULL))) {
  272. ast_channel_unlock(chan);
  273. return -1;
  274. }
  275. ast_channel_unlock(chan);
  276. si = datastore->data;
  277. if (!strcasecmp(data, "tx"))
  278. sdi = si->tx;
  279. else if (!strcasecmp(data, "rx"))
  280. sdi = si->rx;
  281. else {
  282. ast_log(LOG_ERROR, "%s(%s) must either \"tx\" or \"rx\"\n", cmd, data);
  283. return -1;
  284. }
  285. if (!strcasecmp(cmd, "agc"))
  286. snprintf(buf, len, "%.01f", sdi ? sdi->agclevel : 0.0);
  287. else
  288. snprintf(buf, len, "%d", sdi ? sdi->denoise : 0);
  289. return 0;
  290. }
  291. static struct ast_custom_function agc_function = {
  292. .name = "AGC",
  293. .write = speex_write,
  294. .read = speex_read,
  295. .read_max = 22,
  296. };
  297. static struct ast_custom_function denoise_function = {
  298. .name = "DENOISE",
  299. .write = speex_write,
  300. .read = speex_read,
  301. .read_max = 22,
  302. };
  303. static int unload_module(void)
  304. {
  305. ast_custom_function_unregister(&agc_function);
  306. ast_custom_function_unregister(&denoise_function);
  307. return 0;
  308. }
  309. static int load_module(void)
  310. {
  311. if (ast_custom_function_register(&agc_function)) {
  312. return AST_MODULE_LOAD_DECLINE;
  313. }
  314. if (ast_custom_function_register(&denoise_function)) {
  315. ast_custom_function_unregister(&agc_function);
  316. return AST_MODULE_LOAD_DECLINE;
  317. }
  318. return AST_MODULE_LOAD_SUCCESS;
  319. }
  320. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Noise reduction and Automatic Gain Control (AGC)");