codec_speex.c 17 KB

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