codec_speex.c 18 KB

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