codec_speex.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  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 Translate between signed linear and Speex (Open Codec)
  22. *
  23. * \note This work was motivated by Jeremy McNamara
  24. * hacked to be configurable by anthm and bkw 9/28/2004
  25. *
  26. * \ingroup codecs
  27. *
  28. * The Speex library - http://www.speex.org
  29. *
  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.h>
  40. /* We require a post 1.1.8 version of Speex to enable preprocessing
  41. * and better type handling
  42. */
  43. #ifdef _SPEEX_TYPES_H
  44. #include <speex/speex_preprocess.h>
  45. #endif
  46. #include "asterisk/translate.h"
  47. #include "asterisk/module.h"
  48. #include "asterisk/config.h"
  49. #include "asterisk/utils.h"
  50. /* codec variables */
  51. static int quality = 3;
  52. static int complexity = 2;
  53. static int enhancement = 0;
  54. static int vad = 0;
  55. static int vbr = 0;
  56. static float vbr_quality = 4;
  57. static int abr = 0;
  58. static int dtx = 0; /* set to 1 to enable silence detection */
  59. static int preproc = 0;
  60. static int pp_vad = 0;
  61. static int pp_agc = 0;
  62. static float pp_agc_level = 8000; /* XXX what is this 8000 ? */
  63. static int pp_denoise = 0;
  64. static int pp_dereverb = 0;
  65. static float pp_dereverb_decay = 0.4;
  66. static float pp_dereverb_level = 0.3;
  67. #define TYPE_SILENCE 0x2
  68. #define TYPE_HIGH 0x0
  69. #define TYPE_LOW 0x1
  70. #define TYPE_MASK 0x3
  71. #define BUFFER_SAMPLES 8000
  72. #define SPEEX_SAMPLES 160
  73. /* Sample frame data */
  74. #include "asterisk/slin.h"
  75. #include "ex_speex.h"
  76. struct speex_coder_pvt {
  77. void *speex;
  78. SpeexBits bits;
  79. int framesize;
  80. int silent_state;
  81. #ifdef _SPEEX_TYPES_H
  82. SpeexPreprocessState *pp;
  83. spx_int16_t buf[BUFFER_SAMPLES];
  84. #else
  85. int16_t buf[BUFFER_SAMPLES]; /* input, waiting to be compressed */
  86. #endif
  87. };
  88. static int speex_encoder_construct(struct ast_trans_pvt *pvt, const SpeexMode *profile, int sampling_rate)
  89. {
  90. struct speex_coder_pvt *tmp = pvt->pvt;
  91. if (!(tmp->speex = speex_encoder_init(profile)))
  92. return -1;
  93. speex_bits_init(&tmp->bits);
  94. speex_bits_reset(&tmp->bits);
  95. speex_encoder_ctl(tmp->speex, SPEEX_GET_FRAME_SIZE, &tmp->framesize);
  96. speex_encoder_ctl(tmp->speex, SPEEX_SET_COMPLEXITY, &complexity);
  97. #ifdef _SPEEX_TYPES_H
  98. if (preproc) {
  99. tmp->pp = speex_preprocess_state_init(tmp->framesize, sampling_rate);
  100. speex_preprocess_ctl(tmp->pp, SPEEX_PREPROCESS_SET_VAD, &pp_vad);
  101. speex_preprocess_ctl(tmp->pp, SPEEX_PREPROCESS_SET_AGC, &pp_agc);
  102. speex_preprocess_ctl(tmp->pp, SPEEX_PREPROCESS_SET_AGC_LEVEL, &pp_agc_level);
  103. speex_preprocess_ctl(tmp->pp, SPEEX_PREPROCESS_SET_DENOISE, &pp_denoise);
  104. speex_preprocess_ctl(tmp->pp, SPEEX_PREPROCESS_SET_DEREVERB, &pp_dereverb);
  105. speex_preprocess_ctl(tmp->pp, SPEEX_PREPROCESS_SET_DEREVERB_DECAY, &pp_dereverb_decay);
  106. speex_preprocess_ctl(tmp->pp, SPEEX_PREPROCESS_SET_DEREVERB_LEVEL, &pp_dereverb_level);
  107. }
  108. #endif
  109. if (!abr && !vbr) {
  110. speex_encoder_ctl(tmp->speex, SPEEX_SET_QUALITY, &quality);
  111. if (vad)
  112. speex_encoder_ctl(tmp->speex, SPEEX_SET_VAD, &vad);
  113. }
  114. if (vbr) {
  115. speex_encoder_ctl(tmp->speex, SPEEX_SET_VBR, &vbr);
  116. speex_encoder_ctl(tmp->speex, SPEEX_SET_VBR_QUALITY, &vbr_quality);
  117. }
  118. if (abr)
  119. speex_encoder_ctl(tmp->speex, SPEEX_SET_ABR, &abr);
  120. if (dtx)
  121. speex_encoder_ctl(tmp->speex, SPEEX_SET_DTX, &dtx);
  122. tmp->silent_state = 0;
  123. return 0;
  124. }
  125. static int lintospeex_new(struct ast_trans_pvt *pvt)
  126. {
  127. return speex_encoder_construct(pvt, &speex_nb_mode, 8000);
  128. }
  129. static int lin16tospeexwb_new(struct ast_trans_pvt *pvt)
  130. {
  131. return speex_encoder_construct(pvt, &speex_wb_mode, 16000);
  132. }
  133. static int lin32tospeexuwb_new(struct ast_trans_pvt *pvt)
  134. {
  135. return speex_encoder_construct(pvt, &speex_uwb_mode, 32000);
  136. }
  137. static int speex_decoder_construct(struct ast_trans_pvt *pvt, const SpeexMode *profile)
  138. {
  139. struct speex_coder_pvt *tmp = pvt->pvt;
  140. if (!(tmp->speex = speex_decoder_init(profile)))
  141. return -1;
  142. speex_bits_init(&tmp->bits);
  143. speex_decoder_ctl(tmp->speex, SPEEX_GET_FRAME_SIZE, &tmp->framesize);
  144. if (enhancement)
  145. speex_decoder_ctl(tmp->speex, SPEEX_SET_ENH, &enhancement);
  146. return 0;
  147. }
  148. static int speextolin_new(struct ast_trans_pvt *pvt)
  149. {
  150. return speex_decoder_construct(pvt, &speex_nb_mode);
  151. }
  152. static int speexwbtolin16_new(struct ast_trans_pvt *pvt)
  153. {
  154. return speex_decoder_construct(pvt, &speex_wb_mode);
  155. }
  156. static int speexuwbtolin32_new(struct ast_trans_pvt *pvt)
  157. {
  158. return speex_decoder_construct(pvt, &speex_uwb_mode);
  159. }
  160. /*! \brief convert and store into outbuf */
  161. static int speextolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
  162. {
  163. struct speex_coder_pvt *tmp = pvt->pvt;
  164. /* Assuming there's space left, decode into the current buffer at
  165. the tail location. Read in as many frames as there are */
  166. int x;
  167. int res;
  168. int16_t *dst = pvt->outbuf.i16;
  169. /* XXX fout is a temporary buffer, may have different types */
  170. #ifdef _SPEEX_TYPES_H
  171. spx_int16_t fout[1024];
  172. #else
  173. float fout[1024];
  174. #endif
  175. if (f->datalen == 0) { /* Native PLC interpolation */
  176. if (pvt->samples + tmp->framesize > BUFFER_SAMPLES) {
  177. ast_log(LOG_WARNING, "Out of buffer space\n");
  178. return -1;
  179. }
  180. #ifdef _SPEEX_TYPES_H
  181. speex_decode_int(tmp->speex, NULL, dst + pvt->samples);
  182. #else
  183. speex_decode(tmp->speex, NULL, fout);
  184. for (x=0;x<tmp->framesize;x++) {
  185. dst[pvt->samples + x] = (int16_t)fout[x];
  186. }
  187. #endif
  188. pvt->samples += tmp->framesize;
  189. pvt->datalen += 2 * tmp->framesize; /* 2 bytes/sample */
  190. return 0;
  191. }
  192. /* Read in bits */
  193. speex_bits_read_from(&tmp->bits, f->data.ptr, f->datalen);
  194. for (;;) {
  195. #ifdef _SPEEX_TYPES_H
  196. res = speex_decode_int(tmp->speex, &tmp->bits, fout);
  197. #else
  198. res = speex_decode(tmp->speex, &tmp->bits, fout);
  199. #endif
  200. if (res < 0)
  201. break;
  202. if (pvt->samples + tmp->framesize > BUFFER_SAMPLES) {
  203. ast_log(LOG_WARNING, "Out of buffer space\n");
  204. return -1;
  205. }
  206. for (x = 0 ; x < tmp->framesize; x++)
  207. dst[pvt->samples + x] = (int16_t)fout[x];
  208. pvt->samples += tmp->framesize;
  209. pvt->datalen += 2 * tmp->framesize; /* 2 bytes/sample */
  210. }
  211. return 0;
  212. }
  213. /*! \brief store input frame in work buffer */
  214. static int lintospeex_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
  215. {
  216. struct speex_coder_pvt *tmp = pvt->pvt;
  217. /* XXX We should look at how old the rest of our stream is, and if it
  218. is too old, then we should overwrite it entirely, otherwise we can
  219. get artifacts of earlier talk that do not belong */
  220. memcpy(tmp->buf + pvt->samples, f->data.ptr, f->datalen);
  221. pvt->samples += f->samples;
  222. return 0;
  223. }
  224. /*! \brief convert work buffer and produce output frame */
  225. static struct ast_frame *lintospeex_frameout(struct ast_trans_pvt *pvt)
  226. {
  227. struct speex_coder_pvt *tmp = pvt->pvt;
  228. int is_speech=1;
  229. int datalen = 0; /* output bytes */
  230. int samples = 0; /* output samples */
  231. /* We can't work on anything less than a frame in size */
  232. if (pvt->samples < tmp->framesize)
  233. return NULL;
  234. speex_bits_reset(&tmp->bits);
  235. while (pvt->samples >= tmp->framesize) {
  236. #ifdef _SPEEX_TYPES_H
  237. /* Preprocess audio */
  238. if (preproc)
  239. is_speech = speex_preprocess(tmp->pp, tmp->buf + samples, NULL);
  240. /* Encode a frame of data */
  241. if (is_speech) {
  242. /* If DTX enabled speex_encode returns 0 during silence */
  243. is_speech = speex_encode_int(tmp->speex, tmp->buf + samples, &tmp->bits) || !dtx;
  244. } else {
  245. /* 5 zeros interpreted by Speex as silence (submode 0) */
  246. speex_bits_pack(&tmp->bits, 0, 5);
  247. }
  248. #else
  249. {
  250. float fbuf[1024];
  251. int x;
  252. /* Convert to floating point */
  253. for (x = 0; x < tmp->framesize; x++)
  254. fbuf[x] = tmp->buf[samples + x];
  255. /* Encode a frame of data */
  256. is_speech = speex_encode(tmp->speex, fbuf, &tmp->bits) || !dtx;
  257. }
  258. #endif
  259. samples += tmp->framesize;
  260. pvt->samples -= tmp->framesize;
  261. }
  262. /* Move the data at the end of the buffer to the front */
  263. if (pvt->samples)
  264. memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
  265. /* Use AST_FRAME_CNG to signify the start of any silence period */
  266. if (is_speech) {
  267. tmp->silent_state = 0;
  268. } else {
  269. if (tmp->silent_state) {
  270. return NULL;
  271. } else {
  272. struct ast_frame frm = {
  273. .frametype = AST_FRAME_CNG,
  274. .src = pvt->t->name,
  275. };
  276. /*
  277. * XXX I don't think the AST_FRAME_CNG code has ever
  278. * really worked for speex. There doesn't seem to be
  279. * any consumers of the frame type. Everyone that
  280. * references the type seems to pass the frame on.
  281. */
  282. tmp->silent_state = 1;
  283. /* XXX what now ? format etc... */
  284. return ast_frisolate(&frm);
  285. }
  286. }
  287. /* Terminate bit stream */
  288. speex_bits_pack(&tmp->bits, 15, 5);
  289. datalen = speex_bits_write(&tmp->bits, pvt->outbuf.c, pvt->t->buf_size);
  290. return ast_trans_frameout(pvt, datalen, samples);
  291. }
  292. static void speextolin_destroy(struct ast_trans_pvt *arg)
  293. {
  294. struct speex_coder_pvt *pvt = arg->pvt;
  295. speex_decoder_destroy(pvt->speex);
  296. speex_bits_destroy(&pvt->bits);
  297. }
  298. static void lintospeex_destroy(struct ast_trans_pvt *arg)
  299. {
  300. struct speex_coder_pvt *pvt = arg->pvt;
  301. #ifdef _SPEEX_TYPES_H
  302. if (preproc)
  303. speex_preprocess_state_destroy(pvt->pp);
  304. #endif
  305. speex_encoder_destroy(pvt->speex);
  306. speex_bits_destroy(&pvt->bits);
  307. }
  308. static struct ast_translator speextolin = {
  309. .name = "speextolin",
  310. .src_codec = {
  311. .name = "speex",
  312. .type = AST_MEDIA_TYPE_AUDIO,
  313. .sample_rate = 8000,
  314. },
  315. .dst_codec = {
  316. .name = "slin",
  317. .type = AST_MEDIA_TYPE_AUDIO,
  318. .sample_rate = 8000,
  319. },
  320. .format = "slin",
  321. .newpvt = speextolin_new,
  322. .framein = speextolin_framein,
  323. .destroy = speextolin_destroy,
  324. .sample = speex_sample,
  325. .desc_size = sizeof(struct speex_coder_pvt),
  326. .buffer_samples = BUFFER_SAMPLES,
  327. .buf_size = BUFFER_SAMPLES * 2,
  328. .native_plc = 1,
  329. };
  330. static struct ast_translator lintospeex = {
  331. .name = "lintospeex",
  332. .src_codec = {
  333. .name = "slin",
  334. .type = AST_MEDIA_TYPE_AUDIO,
  335. .sample_rate = 8000,
  336. },
  337. .dst_codec = {
  338. .name = "speex",
  339. .type = AST_MEDIA_TYPE_AUDIO,
  340. .sample_rate = 8000,
  341. },
  342. .format = "speex",
  343. .newpvt = lintospeex_new,
  344. .framein = lintospeex_framein,
  345. .frameout = lintospeex_frameout,
  346. .destroy = lintospeex_destroy,
  347. .sample = slin8_sample,
  348. .desc_size = sizeof(struct speex_coder_pvt),
  349. .buffer_samples = BUFFER_SAMPLES,
  350. .buf_size = BUFFER_SAMPLES * 2, /* XXX maybe a lot less ? */
  351. };
  352. static struct ast_translator speexwbtolin16 = {
  353. .name = "speexwbtolin16",
  354. .src_codec = {
  355. .name = "speex",
  356. .type = AST_MEDIA_TYPE_AUDIO,
  357. .sample_rate = 16000,
  358. },
  359. .dst_codec = {
  360. .name = "slin",
  361. .type = AST_MEDIA_TYPE_AUDIO,
  362. .sample_rate = 16000,
  363. },
  364. .format = "slin16",
  365. .newpvt = speexwbtolin16_new,
  366. .framein = speextolin_framein,
  367. .destroy = speextolin_destroy,
  368. .sample = speex16_sample,
  369. .desc_size = sizeof(struct speex_coder_pvt),
  370. .buffer_samples = BUFFER_SAMPLES,
  371. .buf_size = BUFFER_SAMPLES * 2,
  372. .native_plc = 1,
  373. };
  374. static struct ast_translator lin16tospeexwb = {
  375. .name = "lin16tospeexwb",
  376. .src_codec = {
  377. .name = "slin",
  378. .type = AST_MEDIA_TYPE_AUDIO,
  379. .sample_rate = 16000,
  380. },
  381. .dst_codec = {
  382. .name = "speex",
  383. .type = AST_MEDIA_TYPE_AUDIO,
  384. .sample_rate = 16000,
  385. },
  386. .format = "speex16",
  387. .newpvt = lin16tospeexwb_new,
  388. .framein = lintospeex_framein,
  389. .frameout = lintospeex_frameout,
  390. .destroy = lintospeex_destroy,
  391. .sample = slin16_sample,
  392. .desc_size = sizeof(struct speex_coder_pvt),
  393. .buffer_samples = BUFFER_SAMPLES,
  394. .buf_size = BUFFER_SAMPLES * 2, /* XXX maybe a lot less ? */
  395. };
  396. static struct ast_translator speexuwbtolin32 = {
  397. .name = "speexuwbtolin32",
  398. .src_codec = {
  399. .name = "speex",
  400. .type = AST_MEDIA_TYPE_AUDIO,
  401. .sample_rate = 32000,
  402. },
  403. .dst_codec = {
  404. .name = "slin",
  405. .type = AST_MEDIA_TYPE_AUDIO,
  406. .sample_rate = 32000,
  407. },
  408. .format = "slin32",
  409. .newpvt = speexuwbtolin32_new,
  410. .framein = speextolin_framein,
  411. .destroy = speextolin_destroy,
  412. .desc_size = sizeof(struct speex_coder_pvt),
  413. .buffer_samples = BUFFER_SAMPLES,
  414. .buf_size = BUFFER_SAMPLES * 2,
  415. .native_plc = 1,
  416. };
  417. static struct ast_translator lin32tospeexuwb = {
  418. .name = "lin32tospeexuwb",
  419. .src_codec = {
  420. .name = "slin",
  421. .type = AST_MEDIA_TYPE_AUDIO,
  422. .sample_rate = 32000,
  423. },
  424. .dst_codec = {
  425. .name = "speex",
  426. .type = AST_MEDIA_TYPE_AUDIO,
  427. .sample_rate = 32000,
  428. },
  429. .format = "speex32",
  430. .newpvt = lin32tospeexuwb_new,
  431. .framein = lintospeex_framein,
  432. .frameout = lintospeex_frameout,
  433. .destroy = lintospeex_destroy,
  434. .desc_size = sizeof(struct speex_coder_pvt),
  435. .buffer_samples = BUFFER_SAMPLES,
  436. .buf_size = BUFFER_SAMPLES * 2, /* XXX maybe a lot less ? */
  437. };
  438. static int parse_config(int reload)
  439. {
  440. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  441. struct ast_config *cfg = ast_config_load("codecs.conf", config_flags);
  442. struct ast_variable *var;
  443. int res;
  444. float res_f;
  445. if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID)
  446. return 0;
  447. for (var = ast_variable_browse(cfg, "speex"); var; var = var->next) {
  448. if (!strcasecmp(var->name, "quality")) {
  449. res = abs(atoi(var->value));
  450. if (res > -1 && res < 11) {
  451. ast_verb(3, "CODEC SPEEX: Setting Quality to %d\n",res);
  452. quality = res;
  453. } else
  454. ast_log(LOG_ERROR,"Error Quality must be 0-10\n");
  455. } else if (!strcasecmp(var->name, "complexity")) {
  456. res = abs(atoi(var->value));
  457. if (res > -1 && res < 11) {
  458. ast_verb(3, "CODEC SPEEX: Setting Complexity to %d\n",res);
  459. complexity = res;
  460. } else
  461. ast_log(LOG_ERROR,"Error! Complexity must be 0-10\n");
  462. } else if (!strcasecmp(var->name, "vbr_quality")) {
  463. if (sscanf(var->value, "%30f", &res_f) == 1 && res_f >= 0 && res_f <= 10) {
  464. ast_verb(3, "CODEC SPEEX: Setting VBR Quality to %f\n",res_f);
  465. vbr_quality = res_f;
  466. } else
  467. ast_log(LOG_ERROR,"Error! VBR Quality must be 0-10\n");
  468. } else if (!strcasecmp(var->name, "abr_quality")) {
  469. ast_log(LOG_ERROR,"Error! ABR Quality setting obsolete, set ABR to desired bitrate\n");
  470. } else if (!strcasecmp(var->name, "enhancement")) {
  471. enhancement = ast_true(var->value) ? 1 : 0;
  472. ast_verb(3, "CODEC SPEEX: Perceptual Enhancement Mode. [%s]\n",enhancement ? "on" : "off");
  473. } else if (!strcasecmp(var->name, "vbr")) {
  474. vbr = ast_true(var->value) ? 1 : 0;
  475. ast_verb(3, "CODEC SPEEX: VBR Mode. [%s]\n",vbr ? "on" : "off");
  476. } else if (!strcasecmp(var->name, "abr")) {
  477. res = abs(atoi(var->value));
  478. if (res >= 0) {
  479. if (res > 0)
  480. ast_verb(3, "CODEC SPEEX: Setting ABR target bitrate to %d\n",res);
  481. else
  482. ast_verb(3, "CODEC SPEEX: Disabling ABR\n");
  483. abr = res;
  484. } else
  485. ast_log(LOG_ERROR,"Error! ABR target bitrate must be >= 0\n");
  486. } else if (!strcasecmp(var->name, "vad")) {
  487. vad = ast_true(var->value) ? 1 : 0;
  488. ast_verb(3, "CODEC SPEEX: VAD Mode. [%s]\n",vad ? "on" : "off");
  489. } else if (!strcasecmp(var->name, "dtx")) {
  490. dtx = ast_true(var->value) ? 1 : 0;
  491. ast_verb(3, "CODEC SPEEX: DTX Mode. [%s]\n",dtx ? "on" : "off");
  492. } else if (!strcasecmp(var->name, "preprocess")) {
  493. preproc = ast_true(var->value) ? 1 : 0;
  494. ast_verb(3, "CODEC SPEEX: Preprocessing. [%s]\n",preproc ? "on" : "off");
  495. } else if (!strcasecmp(var->name, "pp_vad")) {
  496. pp_vad = ast_true(var->value) ? 1 : 0;
  497. ast_verb(3, "CODEC SPEEX: Preprocessor VAD. [%s]\n",pp_vad ? "on" : "off");
  498. } else if (!strcasecmp(var->name, "pp_agc")) {
  499. pp_agc = ast_true(var->value) ? 1 : 0;
  500. ast_verb(3, "CODEC SPEEX: Preprocessor AGC. [%s]\n",pp_agc ? "on" : "off");
  501. } else if (!strcasecmp(var->name, "pp_agc_level")) {
  502. if (sscanf(var->value, "%30f", &res_f) == 1 && res_f >= 0) {
  503. ast_verb(3, "CODEC SPEEX: Setting preprocessor AGC Level to %f\n",res_f);
  504. pp_agc_level = res_f;
  505. } else
  506. ast_log(LOG_ERROR,"Error! Preprocessor AGC Level must be >= 0\n");
  507. } else if (!strcasecmp(var->name, "pp_denoise")) {
  508. pp_denoise = ast_true(var->value) ? 1 : 0;
  509. ast_verb(3, "CODEC SPEEX: Preprocessor Denoise. [%s]\n",pp_denoise ? "on" : "off");
  510. } else if (!strcasecmp(var->name, "pp_dereverb")) {
  511. pp_dereverb = ast_true(var->value) ? 1 : 0;
  512. ast_verb(3, "CODEC SPEEX: Preprocessor Dereverb. [%s]\n",pp_dereverb ? "on" : "off");
  513. } else if (!strcasecmp(var->name, "pp_dereverb_decay")) {
  514. if (sscanf(var->value, "%30f", &res_f) == 1 && res_f >= 0) {
  515. ast_verb(3, "CODEC SPEEX: Setting preprocessor Dereverb Decay to %f\n",res_f);
  516. pp_dereverb_decay = res_f;
  517. } else
  518. ast_log(LOG_ERROR,"Error! Preprocessor Dereverb Decay must be >= 0\n");
  519. } else if (!strcasecmp(var->name, "pp_dereverb_level")) {
  520. if (sscanf(var->value, "%30f", &res_f) == 1 && res_f >= 0) {
  521. ast_verb(3, "CODEC SPEEX: Setting preprocessor Dereverb Level to %f\n",res_f);
  522. pp_dereverb_level = res_f;
  523. } else
  524. ast_log(LOG_ERROR,"Error! Preprocessor Dereverb Level must be >= 0\n");
  525. }
  526. }
  527. ast_config_destroy(cfg);
  528. return 0;
  529. }
  530. static int reload(void)
  531. {
  532. if (parse_config(1))
  533. return AST_MODULE_LOAD_DECLINE;
  534. return AST_MODULE_LOAD_SUCCESS;
  535. }
  536. static int unload_module(void)
  537. {
  538. int res = 0;
  539. res |= ast_unregister_translator(&speextolin);
  540. res |= ast_unregister_translator(&lintospeex);
  541. res |= ast_unregister_translator(&speexwbtolin16);
  542. res |= ast_unregister_translator(&lin16tospeexwb);
  543. res |= ast_unregister_translator(&speexuwbtolin32);
  544. res |= ast_unregister_translator(&lin32tospeexuwb);
  545. return res;
  546. }
  547. static int load_module(void)
  548. {
  549. int res = 0;
  550. if (parse_config(0))
  551. return AST_MODULE_LOAD_DECLINE;
  552. res |= ast_register_translator(&speextolin);
  553. res |= ast_register_translator(&lintospeex);
  554. res |= ast_register_translator(&speexwbtolin16);
  555. res |= ast_register_translator(&lin16tospeexwb);
  556. res |= ast_register_translator(&speexuwbtolin32);
  557. res |= ast_register_translator(&lin32tospeexuwb);
  558. if (res) {
  559. unload_module();
  560. return res;
  561. }
  562. return res;
  563. }
  564. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Speex Coder/Decoder",
  565. .support_level = AST_MODULE_SUPPORT_CORE,
  566. .load = load_module,
  567. .unload = unload_module,
  568. .reload = reload,
  569. );