func_speex.c File Reference

Noise reduction and automatic gain control (AGC). More...

#include "asterisk.h"
#include <speex/speex_preprocess.h>
#include "asterisk/module.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/utils.h"
#include "asterisk/audiohook.h"

Include dependency graph for func_speex.c:

Go to the source code of this file.

Data Structures

struct  speex_direction_info
struct  speex_info

Defines

#define DEFAULT_AGC_LEVEL   8000.0

Functions

static void __reg_module (void)
static void __unreg_module (void)
static void destroy_callback (void *data)
static int load_module (void)
static int speex_callback (struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction)
static int speex_read (struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
static int speex_write (struct ast_channel *chan, const char *cmd, char *data, const char *value)
static int unload_module (void)

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Noise reduction and Automatic Gain Control (AGC)" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
static struct ast_custom_function agc_function
static struct ast_module_infoast_module_info = &__mod_info
static struct ast_custom_function denoise_function
static struct ast_datastore_info speex_datastore


Detailed Description

Noise reduction and automatic gain control (AGC).

Author:
Brian Degenhardt <bmd@digium.com>

Brett Bryant <bbryant@digium.com>

The Speex 1.2 library - http://www.speex.org
Note:
Requires the 1.2 version of the Speex library (which might not be what you find in Linux packages)

Definition in file func_speex.c.


Define Documentation

#define DEFAULT_AGC_LEVEL   8000.0

Definition at line 51 of file func_speex.c.

Referenced by speex_write().


Function Documentation

static void __reg_module ( void   )  [static]

Definition at line 384 of file func_speex.c.

static void __unreg_module ( void   )  [static]

Definition at line 384 of file func_speex.c.

static void destroy_callback ( void *  data  )  [static]

Definition at line 113 of file func_speex.c.

References ast_audiohook_destroy(), ast_free, speex_info::audiohook, speex_info::rx, speex_direction_info::state, and speex_info::tx.

00114 {
00115    struct speex_info *si = data;
00116 
00117    ast_audiohook_destroy(&si->audiohook);
00118 
00119    if (si->rx && si->rx->state) {
00120       speex_preprocess_state_destroy(si->rx->state);
00121    }
00122 
00123    if (si->tx && si->tx->state) {
00124       speex_preprocess_state_destroy(si->tx->state);
00125    }
00126 
00127    if (si->rx) {
00128       ast_free(si->rx);
00129    }
00130 
00131    if (si->tx) {
00132       ast_free(si->tx);
00133    }
00134 
00135    ast_free(data);
00136 };

static int load_module ( void   )  [static]

static int speex_callback ( struct ast_audiohook audiohook,
struct ast_channel chan,
struct ast_frame frame,
enum ast_audiohook_direction  direction 
) [static]

Definition at line 143 of file func_speex.c.

References speex_direction_info::agc, speex_direction_info::agclevel, AST_AUDIOHOOK_DIRECTION_READ, AST_AUDIOHOOK_STATUS_DONE, ast_channel_datastore_find(), ast_format_get_sample_rate(), AST_FRAME_VOICE, ast_free, AST_MALLOCD_SRC, ast_strdup, ast_frame::data, ast_datastore::data, speex_direction_info::denoise, ast_frame_subclass::format, ast_frame::frametype, speex_info::lastrate, ast_frame::mallocd, NULL, ast_frame::ptr, speex_info::rx, ast_frame::samples, speex_direction_info::samples, ast_frame::src, speex_direction_info::state, ast_audiohook::status, ast_frame::subclass, and speex_info::tx.

Referenced by speex_write().

00144 {
00145    struct ast_datastore *datastore = NULL;
00146    struct speex_direction_info *sdi = NULL;
00147    struct speex_info *si = NULL;
00148    char source[80];
00149 
00150    /* If the audiohook is stopping it means the channel is shutting down.... but we let the datastore destroy take care of it */
00151    if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE || frame->frametype != AST_FRAME_VOICE) {
00152       return -1;
00153    }
00154 
00155    /* We are called with chan already locked */
00156    if (!(datastore = ast_channel_datastore_find(chan, &speex_datastore, NULL))) {
00157       return -1;
00158    }
00159 
00160    si = datastore->data;
00161 
00162    sdi = (direction == AST_AUDIOHOOK_DIRECTION_READ) ? si->rx : si->tx;
00163 
00164    if (!sdi) {
00165       return -1;
00166    }
00167 
00168    if ((sdi->samples != frame->samples) || (ast_format_get_sample_rate(frame->subclass.format) != si->lastrate)) {
00169       si->lastrate = ast_format_get_sample_rate(frame->subclass.format);
00170       if (sdi->state) {
00171          speex_preprocess_state_destroy(sdi->state);
00172       }
00173 
00174       if (!(sdi->state = speex_preprocess_state_init((sdi->samples = frame->samples), si->lastrate))) {
00175          return -1;
00176       }
00177 
00178       speex_preprocess_ctl(sdi->state, SPEEX_PREPROCESS_SET_AGC, &sdi->agc);
00179 
00180       if (sdi->agc) {
00181          speex_preprocess_ctl(sdi->state, SPEEX_PREPROCESS_SET_AGC_LEVEL, &sdi->agclevel);
00182       }
00183 
00184       speex_preprocess_ctl(sdi->state, SPEEX_PREPROCESS_SET_DENOISE, &sdi->denoise);
00185    }
00186 
00187    speex_preprocess(sdi->state, frame->data.ptr, NULL);
00188    snprintf(source, sizeof(source), "%s/speex", frame->src);
00189    if (frame->mallocd & AST_MALLOCD_SRC) {
00190       ast_free((char *) frame->src);
00191    }
00192    frame->src = ast_strdup(source);
00193    frame->mallocd |= AST_MALLOCD_SRC;
00194 
00195    return 0;
00196 }

static int speex_read ( struct ast_channel chan,
const char *  cmd,
char *  data,
char *  buf,
size_t  len 
) [static]

Definition at line 312 of file func_speex.c.

References speex_direction_info::agclevel, ast_channel_datastore_find(), ast_channel_lock, ast_channel_unlock, ast_log, ast_datastore::data, speex_direction_info::denoise, LOG_ERROR, NULL, speex_info::rx, and speex_info::tx.

00313 {
00314    struct ast_datastore *datastore = NULL;
00315    struct speex_info *si = NULL;
00316    struct speex_direction_info *sdi = NULL;
00317 
00318    if (!chan) {
00319       ast_log(LOG_ERROR, "%s cannot be used without a channel!\n", cmd);
00320       return -1;
00321    }
00322 
00323    ast_channel_lock(chan);
00324    if (!(datastore = ast_channel_datastore_find(chan, &speex_datastore, NULL))) {
00325       ast_channel_unlock(chan);
00326       return -1;
00327    }
00328    ast_channel_unlock(chan);
00329 
00330    si = datastore->data;
00331 
00332    if (!strcasecmp(data, "tx"))
00333       sdi = si->tx;
00334    else if (!strcasecmp(data, "rx"))
00335       sdi = si->rx;
00336    else {
00337       ast_log(LOG_ERROR, "%s(%s) must either \"tx\" or \"rx\"\n", cmd, data);
00338       return -1;
00339    }
00340 
00341    if (!strcasecmp(cmd, "agc"))
00342       snprintf(buf, len, "%.01f", sdi ? sdi->agclevel : 0.0);
00343    else
00344       snprintf(buf, len, "%d", sdi ? sdi->denoise : 0);
00345 
00346    return 0;
00347 }

static int speex_write ( struct ast_channel chan,
const char *  cmd,
char *  data,
const char *  value 
) [static]

Definition at line 198 of file func_speex.c.

References ast_audiohook_attach(), ast_audiohook_detach(), ast_audiohook_init(), AST_AUDIOHOOK_MANIPULATE_ALL_RATES, ast_audiohook_remove(), AST_AUDIOHOOK_TYPE_MANIPULATE, ast_calloc, ast_channel_datastore_add(), ast_channel_datastore_find(), ast_channel_datastore_remove(), ast_channel_lock, ast_channel_unlock, ast_datastore_alloc, ast_datastore_free(), ast_free, ast_log, ast_true(), speex_info::audiohook, ast_datastore::data, DEFAULT_AGC_LEVEL, speex_info::lastrate, LOG_ERROR, LOG_WARNING, ast_audiohook::manipulate_callback, NULL, speex_info::rx, speex_direction_info::samples, speex_callback(), and speex_info::tx.

00199 {
00200    struct ast_datastore *datastore = NULL;
00201    struct speex_info *si = NULL;
00202    struct speex_direction_info **sdi = NULL;
00203    int is_new = 0;
00204 
00205    if (!chan) {
00206       ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
00207       return -1;
00208    }
00209 
00210    if (strcasecmp(data, "rx") && strcasecmp(data, "tx")) {
00211       ast_log(LOG_ERROR, "Invalid argument provided to the %s function\n", cmd);
00212       return -1;
00213    }
00214 
00215    ast_channel_lock(chan);
00216    if (!(datastore = ast_channel_datastore_find(chan, &speex_datastore, NULL))) {
00217       ast_channel_unlock(chan);
00218 
00219       if (!(datastore = ast_datastore_alloc(&speex_datastore, NULL))) {
00220          return 0;
00221       }
00222 
00223       if (!(si = ast_calloc(1, sizeof(*si)))) {
00224          ast_datastore_free(datastore);
00225          return 0;
00226       }
00227 
00228       ast_audiohook_init(&si->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "speex", AST_AUDIOHOOK_MANIPULATE_ALL_RATES);
00229       si->audiohook.manipulate_callback = speex_callback;
00230       si->lastrate = 8000;
00231       is_new = 1;
00232    } else {
00233       ast_channel_unlock(chan);
00234       si = datastore->data;
00235    }
00236 
00237    if (!strcasecmp(data, "rx")) {
00238       sdi = &si->rx;
00239    } else {
00240       sdi = &si->tx;
00241    }
00242 
00243    if (!*sdi) {
00244       if (!(*sdi = ast_calloc(1, sizeof(**sdi)))) {
00245          return 0;
00246       }
00247       /* Right now, the audiohooks API will _only_ provide us 8 kHz slinear
00248        * audio.  When it supports 16 kHz (or any other sample rates, we will
00249        * have to take that into account here. */
00250       (*sdi)->samples = -1;
00251    }
00252 
00253    if (!strcasecmp(cmd, "agc")) {
00254       if (!sscanf(value, "%30f", &(*sdi)->agclevel))
00255          (*sdi)->agclevel = ast_true(value) ? DEFAULT_AGC_LEVEL : 0.0;
00256    
00257       if ((*sdi)->agclevel > 32768.0) {
00258          ast_log(LOG_WARNING, "AGC(%s)=%.01f is greater than 32768... setting to 32768 instead\n", 
00259                ((*sdi == si->rx) ? "rx" : "tx"), (*sdi)->agclevel);
00260          (*sdi)->agclevel = 32768.0;
00261       }
00262    
00263       (*sdi)->agc = !!((*sdi)->agclevel);
00264 
00265       if ((*sdi)->state) {
00266          speex_preprocess_ctl((*sdi)->state, SPEEX_PREPROCESS_SET_AGC, &(*sdi)->agc);
00267          if ((*sdi)->agc) {
00268             speex_preprocess_ctl((*sdi)->state, SPEEX_PREPROCESS_SET_AGC_LEVEL, &(*sdi)->agclevel);
00269          }
00270       }
00271    } else if (!strcasecmp(cmd, "denoise")) {
00272       (*sdi)->denoise = (ast_true(value) != 0);
00273 
00274       if ((*sdi)->state) {
00275          speex_preprocess_ctl((*sdi)->state, SPEEX_PREPROCESS_SET_DENOISE, &(*sdi)->denoise);
00276       }
00277    }
00278 
00279    if (!(*sdi)->agc && !(*sdi)->denoise) {
00280       if ((*sdi)->state)
00281          speex_preprocess_state_destroy((*sdi)->state);
00282 
00283       ast_free(*sdi);
00284       *sdi = NULL;
00285    }
00286 
00287    if (!si->rx && !si->tx) {
00288       if (is_new) {
00289          is_new = 0;
00290       } else {
00291          ast_channel_lock(chan);
00292          ast_channel_datastore_remove(chan, datastore);
00293          ast_channel_unlock(chan);
00294          ast_audiohook_remove(chan, &si->audiohook);
00295          ast_audiohook_detach(&si->audiohook);
00296       }
00297       
00298       ast_datastore_free(datastore);
00299    }
00300 
00301    if (is_new) { 
00302       datastore->data = si;
00303       ast_channel_lock(chan);
00304       ast_channel_datastore_add(chan, datastore);
00305       ast_channel_unlock(chan);
00306       ast_audiohook_attach(chan, &si->audiohook);
00307    }
00308 
00309    return 0;
00310 }

static int unload_module ( void   )  [static]

Definition at line 363 of file func_speex.c.

References ast_custom_function_unregister().


Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Noise reduction and Automatic Gain Control (AGC)" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, } [static]

Definition at line 384 of file func_speex.c.

Definition at line 349 of file func_speex.c.

Definition at line 384 of file func_speex.c.

Definition at line 356 of file func_speex.c.

Initial value:

 {
   .type = "speex",
   .destroy = destroy_callback
}

Definition at line 138 of file func_speex.c.


Generated on Thu Apr 16 06:31:25 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6