7d7871e218e040aef42d550f14485e38e4e941c2
[asterisk/asterisk.git] / codecs / codec_speex.c
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
20 /*! \file
21  *
22  * \brief Translate between signed linear and Speex (Open Codec)
23  *
24  * http://www.speex.org
25  * \note This work was motivated by Jeremy McNamara 
26  * hacked to be configurable by anthm and bkw 9/28/2004
27  * \ingroup codecs
28  */
29
30 #include <fcntl.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <netinet/in.h>
34 #include <string.h>
35 #include <stdio.h>
36 #include <speex.h>
37
38 /* We require a post 1.1.8 version of Speex to enable preprocessing
39    and better type handling */   
40 #ifdef _SPEEX_TYPES_H
41 #include <speex/speex_preprocess.h>
42 #endif
43
44 static int quality = 3;
45 static int complexity = 2;
46 static int enhancement = 0;
47 static int vad = 0;
48 static int vbr = 0;
49 static float vbr_quality = 4;
50 static int abr = 0;
51 static int dtx = 0;
52
53 static int preproc = 0;
54 static int pp_vad = 0;
55 static int pp_agc = 0;
56 static float pp_agc_level = 8000;
57 static int pp_denoise = 0;
58 static int pp_dereverb = 0;
59 static float pp_dereverb_decay = 0.4;
60 static float pp_dereverb_level = 0.3;
61
62 #define TYPE_SILENCE     0x2
63 #define TYPE_HIGH        0x0
64 #define TYPE_LOW         0x1
65 #define TYPE_MASK        0x3
66
67 #include "asterisk.h"
68
69 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
70
71 #include "asterisk/lock.h"
72 #include "asterisk/translate.h"
73 #include "asterisk/module.h"
74 #include "asterisk/config.h"
75 #include "asterisk/options.h"
76 #include "asterisk/logger.h"
77 #include "asterisk/channel.h"
78 #include "asterisk/utils.h"
79
80 /* Sample frame data */
81 #include "slin_speex_ex.h"
82 #include "speex_slin_ex.h"
83
84 AST_MUTEX_DEFINE_STATIC(localuser_lock);
85 static int localusecnt=0;
86
87 static char *tdesc = "Speex/PCM16 (signed linear) Codec Translator";
88
89 struct ast_translator_pvt {
90         void *speex;
91         struct ast_frame f;
92         SpeexBits bits;
93         int framesize;
94         /* Space to build offset */
95         char offset[AST_FRIENDLY_OFFSET];
96 #ifdef _SPEEX_TYPES_H
97         SpeexPreprocessState *pp;
98         /* Buffer for our outgoing frame */
99         spx_int16_t outbuf[8000];
100         /* Enough to store a full second */
101         spx_int16_t buf[8000];
102 #else
103         short outbuf[8000];
104         short buf[8000];
105 #endif
106
107         int tail;
108         int silent_state;
109 };
110
111 #define speex_coder_pvt ast_translator_pvt
112
113 static struct ast_translator_pvt *lintospeex_new(void)
114 {
115         struct speex_coder_pvt *tmp;    
116         if ((tmp = ast_malloc(sizeof(*tmp)))) {
117                 if (!(tmp->speex = speex_encoder_init(&speex_nb_mode))) {
118                         free(tmp);
119                         tmp = NULL;
120                 } else {
121                         speex_bits_init(&tmp->bits);
122                         speex_bits_reset(&tmp->bits);
123                         speex_encoder_ctl(tmp->speex, SPEEX_GET_FRAME_SIZE, &tmp->framesize);
124                         speex_encoder_ctl(tmp->speex, SPEEX_SET_COMPLEXITY, &complexity);
125 #ifdef _SPEEX_TYPES_H
126                         if (preproc) {
127                                 tmp->pp = speex_preprocess_state_init(tmp->framesize, 8000);
128                                 speex_preprocess_ctl(tmp->pp, SPEEX_PREPROCESS_SET_VAD, &pp_vad);
129                                 speex_preprocess_ctl(tmp->pp, SPEEX_PREPROCESS_SET_AGC, &pp_agc);
130                                 speex_preprocess_ctl(tmp->pp, SPEEX_PREPROCESS_SET_AGC_LEVEL, &pp_agc_level);
131                                 speex_preprocess_ctl(tmp->pp, SPEEX_PREPROCESS_SET_DENOISE, &pp_denoise);
132                                 speex_preprocess_ctl(tmp->pp, SPEEX_PREPROCESS_SET_DEREVERB, &pp_dereverb);
133                                 speex_preprocess_ctl(tmp->pp, SPEEX_PREPROCESS_SET_DEREVERB_DECAY, &pp_dereverb_decay);
134                                 speex_preprocess_ctl(tmp->pp, SPEEX_PREPROCESS_SET_DEREVERB_LEVEL, &pp_dereverb_level);
135                         }
136 #endif
137                         if (!abr && !vbr) {
138                                 speex_encoder_ctl(tmp->speex, SPEEX_SET_QUALITY, &quality);
139                                 if (vad)
140                                         speex_encoder_ctl(tmp->speex, SPEEX_SET_VAD, &vad);
141                         }
142                         if (vbr) {
143                                 speex_encoder_ctl(tmp->speex, SPEEX_SET_VBR, &vbr);
144                                 speex_encoder_ctl(tmp->speex, SPEEX_SET_VBR_QUALITY, &vbr_quality);
145                         }
146                         if (abr) {
147                                 speex_encoder_ctl(tmp->speex, SPEEX_SET_ABR, &abr);
148                         }
149                         if (dtx)
150                                 speex_encoder_ctl(tmp->speex, SPEEX_SET_DTX, &dtx); 
151                         tmp->tail = 0;
152                         tmp->silent_state = 0;
153                 }
154                 localusecnt++;
155         }
156         return tmp;
157 }
158
159 static struct ast_translator_pvt *speextolin_new(void)
160 {
161         struct speex_coder_pvt *tmp;    
162         if ((tmp = ast_malloc(sizeof(*tmp)))) {
163                 if (!(tmp->speex = speex_decoder_init(&speex_nb_mode))) {
164                         free(tmp);
165                         tmp = NULL;
166                 } else {
167                         speex_bits_init(&tmp->bits);
168                         speex_decoder_ctl(tmp->speex, SPEEX_GET_FRAME_SIZE, &tmp->framesize);
169                         if (enhancement)
170                                 speex_decoder_ctl(tmp->speex, SPEEX_SET_ENH, &enhancement);
171                         tmp->tail = 0;
172                 }
173                 localusecnt++;
174         }
175         return tmp;
176 }
177
178 static struct ast_frame *lintospeex_sample(void)
179 {
180         static struct ast_frame f;
181         f.frametype = AST_FRAME_VOICE;
182         f.subclass = AST_FORMAT_SLINEAR;
183         f.datalen = sizeof(slin_speex_ex);
184         /* Assume 8000 Hz */
185         f.samples = sizeof(slin_speex_ex)/2;
186         f.mallocd = 0;
187         f.offset = 0;
188         f.src = __PRETTY_FUNCTION__;
189         f.data = slin_speex_ex;
190         return &f;
191 }
192
193 static struct ast_frame *speextolin_sample(void)
194 {
195         static struct ast_frame f;
196         f.frametype = AST_FRAME_VOICE;
197         f.subclass = AST_FORMAT_SPEEX;
198         f.datalen = sizeof(speex_slin_ex);
199         /* All frames are 20 ms long */
200         f.samples = 160;
201         f.mallocd = 0;
202         f.offset = 0;
203         f.src = __PRETTY_FUNCTION__;
204         f.data = speex_slin_ex;
205         return &f;
206 }
207
208 static struct ast_frame *speextolin_frameout(struct ast_translator_pvt *tmp)
209 {
210         if (!tmp->tail)
211                 return NULL;
212         /* Signed linear is no particular frame size, so just send whatever
213            we have in the buffer in one lump sum */
214         tmp->f.frametype = AST_FRAME_VOICE;
215         tmp->f.subclass = AST_FORMAT_SLINEAR;
216         tmp->f.datalen = tmp->tail * 2;
217         /* Assume 8000 Hz */
218         tmp->f.samples = tmp->tail;
219         tmp->f.mallocd = 0;
220         tmp->f.offset = AST_FRIENDLY_OFFSET;
221         tmp->f.src = __PRETTY_FUNCTION__;
222         tmp->f.data = tmp->buf;
223         /* Reset tail pointer */
224         tmp->tail = 0;
225         return &tmp->f; 
226 }
227
228 static int speextolin_framein(struct ast_translator_pvt *tmp, struct ast_frame *f)
229 {
230         /* Assuming there's space left, decode into the current buffer at
231            the tail location.  Read in as many frames as there are */
232         int x;
233         int res;
234 #ifdef _SPEEX_TYPES_H
235         spx_int16_t out[1024];
236 #else
237         float fout[1024];
238 #endif
239
240         if (f->datalen == 0) {  /* Native PLC interpolation */
241                 if (tmp->tail + tmp->framesize > sizeof(tmp->buf) / 2) {
242                         ast_log(LOG_WARNING, "Out of buffer space\n");
243                         return -1;
244                 }
245 #ifdef _SPEEX_TYPES_H
246                 speex_decode_int(tmp->speex, NULL, tmp->buf + tmp->tail);
247 #else
248                 speex_decode(tmp->speex, NULL, fout);
249                 for (x=0;x<tmp->framesize;x++) {
250                         tmp->buf[tmp->tail + x] = fout[x];
251                 }
252 #endif
253                 tmp->tail += tmp->framesize;
254                 return 0;
255         }
256
257         /* Read in bits */
258         speex_bits_read_from(&tmp->bits, f->data, f->datalen);
259         for(;;) {
260 #ifdef _SPEEX_TYPES_H
261                 res = speex_decode_int(tmp->speex, &tmp->bits, out);
262 #else
263                 res = speex_decode(tmp->speex, &tmp->bits, fout);
264 #endif
265                 if (res < 0)
266                         break;
267                 if (tmp->tail + tmp->framesize < sizeof(tmp->buf) / 2) {
268                         for (x=0;x<tmp->framesize;x++) {
269 #ifdef _SPEEX_TYPES_H
270                                 tmp->buf[tmp->tail + x] = out[x];
271 #else
272                                 tmp->buf[tmp->tail + x] = fout[x];
273 #endif
274                         }
275                         tmp->tail += tmp->framesize;
276                 } else {
277                         ast_log(LOG_WARNING, "Out of buffer space\n");
278                         return -1;
279                 }
280                 
281         }
282         return 0;
283 }
284
285 static int lintospeex_framein(struct ast_translator_pvt *tmp, struct ast_frame *f)
286 {
287         /* Just add the frames to our stream */
288         /* XXX We should look at how old the rest of our stream is, and if it
289            is too old, then we should overwrite it entirely, otherwise we can
290            get artifacts of earlier talk that do not belong */
291         if (tmp->tail + f->datalen/2 < sizeof(tmp->buf) / 2) {
292                 memcpy((tmp->buf + tmp->tail), f->data, f->datalen);
293                 tmp->tail += f->datalen/2;
294         } else {
295                 ast_log(LOG_WARNING, "Out of buffer space\n");
296                 return -1;
297         }
298         return 0;
299 }
300
301 static struct ast_frame *lintospeex_frameout(struct ast_translator_pvt *tmp)
302 {
303 #ifndef _SPEEX_TYPES_H
304         float fbuf[1024];
305         int x;
306 #endif
307         int len;
308         int y=0;
309         int is_speech=1;
310         /* We can't work on anything less than a frame in size */
311         if (tmp->tail < tmp->framesize)
312                 return NULL;
313         tmp->f.frametype = AST_FRAME_VOICE;
314         tmp->f.subclass = AST_FORMAT_SPEEX;
315         tmp->f.mallocd = 0;
316         tmp->f.offset = AST_FRIENDLY_OFFSET;
317         tmp->f.src = __PRETTY_FUNCTION__;
318         tmp->f.data = tmp->outbuf;
319         speex_bits_reset(&tmp->bits);
320         while(tmp->tail >= tmp->framesize) {
321 #ifdef _SPEEX_TYPES_H
322                 /* Preprocess audio */
323                 if(preproc)
324                         is_speech = speex_preprocess(tmp->pp, tmp->buf, NULL);
325                 /* Encode a frame of data */
326                 if (is_speech) {
327                         /* If DTX enabled speex_encode returns 0 during silence */
328                         is_speech = speex_encode_int(tmp->speex, tmp->buf, &tmp->bits) || !dtx;
329                 } else {
330                         /* 5 zeros interpreted by Speex as silence (submode 0) */
331                         speex_bits_pack(&tmp->bits, 0, 5);
332                 }
333 #else
334                 /* Convert to floating point */
335                 for (x=0;x<tmp->framesize;x++)
336                         fbuf[x] = tmp->buf[x];
337                 /* Encode a frame of data */
338                 is_speech = speex_encode(tmp->speex, fbuf, &tmp->bits) || !dtx;
339 #endif
340                 /* Assume 8000 Hz -- 20 ms */
341                 tmp->tail -= tmp->framesize;
342                 /* Move the data at the end of the buffer to the front */
343                 if (tmp->tail)
344                         memmove(tmp->buf, tmp->buf + tmp->framesize, tmp->tail * 2);
345                 y++;
346         }
347
348         /* Use AST_FRAME_CNG to signify the start of any silence period */
349         if (!is_speech) {
350                 if (tmp->silent_state) {
351                         return NULL;
352                 } else {
353                         tmp->silent_state = 1;
354                         speex_bits_reset(&tmp->bits);
355                         tmp->f.frametype = AST_FRAME_CNG;
356                 }
357         } else {
358                 tmp->silent_state = 0;
359         }
360
361         /* Terminate bit stream */
362         speex_bits_pack(&tmp->bits, 15, 5);
363         len = speex_bits_write(&tmp->bits, (char *)tmp->outbuf, sizeof(tmp->outbuf));
364         tmp->f.datalen = len;
365         tmp->f.samples = y * 160;
366 #if 0
367         {
368                 static int fd = -1;
369                 if (fd < 0) {
370                         fd = open("speex.raw", O_WRONLY|O_TRUNC|O_CREAT);
371                         if (fd > -1) {
372                                 write(fd, tmp->f.data, tmp->f.datalen);
373                                 close(fd);
374                         }
375                 }
376         }
377 #endif
378         return &tmp->f; 
379 }
380
381 static void speextolin_destroy(struct ast_translator_pvt *pvt)
382 {
383         speex_decoder_destroy(pvt->speex);
384         speex_bits_destroy(&pvt->bits);
385         free(pvt);
386         localusecnt--;
387 }
388
389 static void lintospeex_destroy(struct ast_translator_pvt *pvt)
390 {
391 #ifdef _SPEEX_TYPES_H
392         if (preproc)
393                 speex_preprocess_state_destroy(pvt->pp);
394 #endif
395         speex_encoder_destroy(pvt->speex);
396         speex_bits_destroy(&pvt->bits);
397         free(pvt);
398         localusecnt--;
399 }
400
401 static struct ast_translator speextolin =
402         { "speextolin", 
403            AST_FORMAT_SPEEX, AST_FORMAT_SLINEAR,
404            speextolin_new,
405            speextolin_framein,
406            speextolin_frameout,
407            speextolin_destroy,
408            speextolin_sample
409            };
410
411 static struct ast_translator lintospeex =
412         { "lintospeex", 
413            AST_FORMAT_SLINEAR, AST_FORMAT_SPEEX,
414            lintospeex_new,
415            lintospeex_framein,
416            lintospeex_frameout,
417            lintospeex_destroy,
418            lintospeex_sample
419         };
420
421
422 static void parse_config(void) 
423 {
424         struct ast_config *cfg;
425         struct ast_variable *var;
426         int res;
427         float res_f;
428
429         if ((cfg = ast_config_load("codecs.conf"))) {
430                 if ((var = ast_variable_browse(cfg, "speex"))) {
431                         while (var) {
432                                 if (!strcasecmp(var->name, "quality")) {
433                                         res = abs(atoi(var->value));
434                                         if (res > -1 && res < 11) {
435                                                 if (option_verbose > 2)
436                                                         ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: Setting Quality to %d\n",res);
437                                                 ast_mutex_lock(&localuser_lock);
438                                                 quality = res;
439                                                 ast_mutex_unlock(&localuser_lock);
440                                         } else 
441                                                 ast_log(LOG_ERROR,"Error Quality must be 0-10\n");
442                                 } else if (!strcasecmp(var->name, "complexity")) {
443                                         res = abs(atoi(var->value));
444                                         if (res > -1 && res < 11) {
445                                                 if (option_verbose > 2)
446                                                         ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: Setting Complexity to %d\n",res);
447                                                 ast_mutex_lock(&localuser_lock);
448                                                 complexity = res;
449                                                 ast_mutex_unlock(&localuser_lock);
450                                         } else 
451                                                 ast_log(LOG_ERROR,"Error! Complexity must be 0-10\n");
452                                 } else if (!strcasecmp(var->name, "vbr_quality")) {
453                                         if (sscanf(var->value, "%f", &res_f) == 1 && res_f >= 0 && res_f <= 10) {
454                                                 if (option_verbose > 2)
455                                                         ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: Setting VBR Quality to %f\n",res_f);
456                                                 ast_mutex_lock(&localuser_lock);
457                                                 vbr_quality = res_f;
458                                                 ast_mutex_unlock(&localuser_lock);
459                                         } else
460                                                 ast_log(LOG_ERROR,"Error! VBR Quality must be 0-10\n");
461                                 } else if (!strcasecmp(var->name, "abr_quality")) {
462                                         ast_log(LOG_ERROR,"Error! ABR Quality setting obsolete, set ABR to desired bitrate\n");
463                                 } else if (!strcasecmp(var->name, "enhancement")) {
464                                         ast_mutex_lock(&localuser_lock);
465                                         enhancement = ast_true(var->value) ? 1 : 0;
466                                         if (option_verbose > 2)
467                                                 ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: Perceptual Enhancement Mode. [%s]\n",enhancement ? "on" : "off");
468                                         ast_mutex_unlock(&localuser_lock);
469                                 } else if (!strcasecmp(var->name, "vbr")) {
470                                         ast_mutex_lock(&localuser_lock);
471                                         vbr = ast_true(var->value) ? 1 : 0;
472                                         if (option_verbose > 2)
473                                                 ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: VBR Mode. [%s]\n",vbr ? "on" : "off");
474                                         ast_mutex_unlock(&localuser_lock);
475                                 } else if (!strcasecmp(var->name, "abr")) {
476                                         res = abs(atoi(var->value));
477                                         if (res >= 0) {
478                                                 if (option_verbose > 2) {
479                                                         if (res > 0)
480                                                                 ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: Setting ABR target bitrate to %d\n",res);
481                                                         else
482                                                                 ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: Disabling ABR\n");
483                                                 }
484                                                 ast_mutex_lock(&localuser_lock);
485                                                 abr = res;
486                                                 ast_mutex_unlock(&localuser_lock);
487                                         } else 
488                                                 ast_log(LOG_ERROR,"Error! ABR target bitrate must be >= 0\n");
489                                 } else if (!strcasecmp(var->name, "vad")) {
490                                         ast_mutex_lock(&localuser_lock);
491                                         vad = ast_true(var->value) ? 1 : 0;
492                                         if (option_verbose > 2)
493                                                 ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: VAD Mode. [%s]\n",vad ? "on" : "off");
494                                         ast_mutex_unlock(&localuser_lock);
495                                 } else if (!strcasecmp(var->name, "dtx")) {
496                                         ast_mutex_lock(&localuser_lock);
497                                         dtx = ast_true(var->value) ? 1 : 0;
498                                         if (option_verbose > 2)
499                                                 ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: DTX Mode. [%s]\n",dtx ? "on" : "off");
500                                         ast_mutex_unlock(&localuser_lock);
501                                 } else if (!strcasecmp(var->name, "preprocess")) {
502                                         ast_mutex_lock(&localuser_lock);
503                                         preproc = ast_true(var->value) ? 1 : 0;
504                                         if (option_verbose > 2)
505                                                 ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: Preprocessing. [%s]\n",preproc ? "on" : "off");
506                                         ast_mutex_unlock(&localuser_lock);
507                                 } else if (!strcasecmp(var->name, "pp_vad")) {
508                                         ast_mutex_lock(&localuser_lock);
509                                         pp_vad = ast_true(var->value) ? 1 : 0;
510                                         if (option_verbose > 2)
511                                                 ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: Preprocessor VAD. [%s]\n",pp_vad ? "on" : "off");
512                                         ast_mutex_unlock(&localuser_lock);
513                                 } else if (!strcasecmp(var->name, "pp_agc")) {
514                                         ast_mutex_lock(&localuser_lock);
515                                         pp_agc = ast_true(var->value) ? 1 : 0;
516                                         if (option_verbose > 2)
517                                                 ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: Preprocessor AGC. [%s]\n",pp_agc ? "on" : "off");
518                                         ast_mutex_unlock(&localuser_lock);
519                                 } else if (!strcasecmp(var->name, "pp_agc_level")) {
520                                         if (sscanf(var->value, "%f", &res_f) == 1 && res_f >= 0) {
521                                                 if (option_verbose > 2)
522                                                         ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: Setting preprocessor AGC Level to %f\n",res_f);
523                                                 ast_mutex_lock(&localuser_lock);
524                                                 pp_agc_level = res_f;
525                                                 ast_mutex_unlock(&localuser_lock);
526                                         } else
527                                                 ast_log(LOG_ERROR,"Error! Preprocessor AGC Level must be >= 0\n");
528                                 } else if (!strcasecmp(var->name, "pp_denoise")) {
529                                         ast_mutex_lock(&localuser_lock);
530                                         pp_denoise = ast_true(var->value) ? 1 : 0;
531                                         if (option_verbose > 2)
532                                                 ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: Preprocessor Denoise. [%s]\n",pp_denoise ? "on" : "off");
533                                         ast_mutex_unlock(&localuser_lock);
534                                 } else if (!strcasecmp(var->name, "pp_dereverb")) {
535                                         ast_mutex_lock(&localuser_lock);
536                                         pp_dereverb = ast_true(var->value) ? 1 : 0;
537                                         if (option_verbose > 2)
538                                                 ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: Preprocessor Dereverb. [%s]\n",pp_dereverb ? "on" : "off");
539                                         ast_mutex_unlock(&localuser_lock);
540                                 } else if (!strcasecmp(var->name, "pp_dereverb_decay")) {
541                                         if (sscanf(var->value, "%f", &res_f) == 1 && res_f >= 0) {
542                                                 if (option_verbose > 2)
543                                                         ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: Setting preprocessor Dereverb Decay to %f\n",res_f);
544                                                 ast_mutex_lock(&localuser_lock);
545                                                 pp_dereverb_decay = res_f;
546                                                 ast_mutex_unlock(&localuser_lock);
547                                         } else
548                                                 ast_log(LOG_ERROR,"Error! Preprocessor Dereverb Decay must be >= 0\n");
549                                 } else if (!strcasecmp(var->name, "pp_dereverb_level")) {
550                                         if (sscanf(var->value, "%f", &res_f) == 1 && res_f >= 0) {
551                                                 if (option_verbose > 2)
552                                                         ast_verbose(VERBOSE_PREFIX_3 "CODEC SPEEX: Setting preprocessor Dereverb Level to %f\n",res_f);
553                                                 ast_mutex_lock(&localuser_lock);
554                                                 pp_dereverb_level = res_f;
555                                                 ast_mutex_unlock(&localuser_lock);
556                                         } else
557                                                 ast_log(LOG_ERROR,"Error! Preprocessor Dereverb Level must be >= 0\n");
558                                 }
559                                 var = var->next;
560                         }
561                 }
562                 ast_config_destroy(cfg);
563         }
564 }
565
566 int reload(void) 
567 {
568         parse_config();
569         return 0;
570 }
571
572 int unload_module(void)
573 {
574         int res;
575         ast_mutex_lock(&localuser_lock);
576         res = ast_unregister_translator(&lintospeex);
577         if (!res)
578                 res = ast_unregister_translator(&speextolin);
579         if (localusecnt)
580                 res = -1;
581         ast_mutex_unlock(&localuser_lock);
582         return res;
583 }
584
585 int load_module(void)
586 {
587         int res;
588         parse_config();
589         res=ast_register_translator(&speextolin);
590         if (!res) 
591                 res=ast_register_translator(&lintospeex);
592         else
593                 ast_unregister_translator(&speextolin);
594         return res;
595 }
596
597 char *description(void)
598 {
599         return tdesc;
600 }
601
602 int usecount(void)
603 {
604         int res;
605         STANDARD_USECOUNT(res);
606         return res;
607 }
608
609 char *key()
610 {
611         return ASTERISK_GPL_KEY;
612 }