42 #include <speex/speex_preprocess.h>
49 #define DEFAULT_AGC_LEVEL 8000.0
119 if (si->rx && si->rx->
state) {
120 speex_preprocess_state_destroy(si->rx->
state);
123 if (si->tx && si->tx->
state) {
124 speex_preprocess_state_destroy(si->tx->
state);
160 si = datastore->
data;
171 speex_preprocess_state_destroy(sdi->
state);
174 if (!(sdi->
state = speex_preprocess_state_init((sdi->
samples = frame->
samples), si->lastrate))) {
178 speex_preprocess_ctl(sdi->
state, SPEEX_PREPROCESS_SET_AGC, &sdi->
agc);
181 speex_preprocess_ctl(sdi->
state, SPEEX_PREPROCESS_SET_AGC_LEVEL, &sdi->
agclevel);
184 speex_preprocess_ctl(sdi->
state, SPEEX_PREPROCESS_SET_DENOISE, &sdi->
denoise);
187 speex_preprocess(sdi->
state, frame->
data.ptr, NULL);
188 snprintf(source,
sizeof(source),
"%s/speex", frame->
src);
190 ast_free((
char *) frame->
src);
198 static int speex_write(
struct ast_channel *chan,
const char *cmd,
char *data,
const char *value)
206 ast_log(LOG_WARNING,
"No channel was provided to %s function.\n", cmd);
210 if (strcasecmp(data,
"rx") && strcasecmp(data,
"tx")) {
211 ast_log(LOG_ERROR,
"Invalid argument provided to the %s function\n", cmd);
215 ast_channel_lock(chan);
217 ast_channel_unlock(chan);
219 if (!(datastore = ast_datastore_alloc(&speex_datastore, NULL))) {
233 ast_channel_unlock(chan);
234 si = datastore->
data;
237 if (!strcasecmp(data,
"rx")) {
253 if (!strcasecmp(cmd,
"agc")) {
254 if (!sscanf(value,
"%30f", &(*sdi)->agclevel))
255 (*sdi)->agclevel =
ast_true(value) ? DEFAULT_AGC_LEVEL : 0.0;
257 if ((*sdi)->agclevel > 32768.0) {
258 ast_log(LOG_WARNING,
"AGC(%s)=%.01f is greater than 32768... setting to 32768 instead\n",
259 ((*sdi == si->rx) ?
"rx" :
"tx"), (*sdi)->agclevel);
260 (*sdi)->agclevel = 32768.0;
263 (*sdi)->agc = !!((*sdi)->agclevel);
266 speex_preprocess_ctl((*sdi)->state, SPEEX_PREPROCESS_SET_AGC, &(*sdi)->agc);
268 speex_preprocess_ctl((*sdi)->state, SPEEX_PREPROCESS_SET_AGC_LEVEL, &(*sdi)->agclevel);
271 }
else if (!strcasecmp(cmd,
"denoise")) {
272 (*sdi)->denoise = (
ast_true(value) != 0);
275 speex_preprocess_ctl((*sdi)->state, SPEEX_PREPROCESS_SET_DENOISE, &(*sdi)->denoise);
279 if (!(*sdi)->agc && !(*sdi)->denoise) {
281 speex_preprocess_state_destroy((*sdi)->state);
287 if (!si->rx && !si->tx) {
291 ast_channel_lock(chan);
293 ast_channel_unlock(chan);
302 datastore->
data = si;
303 ast_channel_lock(chan);
305 ast_channel_unlock(chan);
312 static int speex_read(
struct ast_channel *chan,
const char *cmd,
char *data,
char *buf,
size_t len)
319 ast_log(LOG_ERROR,
"%s cannot be used without a channel!\n", cmd);
323 ast_channel_lock(chan);
325 ast_channel_unlock(chan);
328 ast_channel_unlock(chan);
330 si = datastore->
data;
332 if (!strcasecmp(data,
"tx"))
334 else if (!strcasecmp(data,
"rx"))
337 ast_log(LOG_ERROR,
"%s(%s) must either \"tx\" or \"rx\"\n", cmd, data);
341 if (!strcasecmp(cmd,
"agc"))
342 snprintf(buf, len,
"%.01f", sdi ? sdi->
agclevel : 0.0);
344 snprintf(buf, len,
"%d", sdi ? sdi->
denoise : 0);
351 .write = speex_write,
358 .write = speex_write,
363 static int unload_module(
void)
370 static int load_module(
void)
384 AST_MODULE_INFO_STANDARD(
ASTERISK_GPL_KEY,
"Noise reduction and Automatic Gain Control (AGC)");
Main Channel structure associated with a channel.
Asterisk main include file. File version handling, generic pbx functions.
int ast_audiohook_remove(struct ast_channel *chan, struct ast_audiohook *audiohook)
Remove an audiohook from a specified channel.
Structure for a data store type.
int ast_audiohook_attach(struct ast_channel *chan, struct ast_audiohook *audiohook)
Attach audiohook to channel.
#define ast_strdup(str)
A wrapper for strdup()
Structure for a data store object.
struct ast_datastore * ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
Find a datastore on a channel.
int ast_audiohook_destroy(struct ast_audiohook *audiohook)
Destroys an audiohook structure.
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
struct ast_frame_subclass subclass
ast_audiohook_manipulate_callback manipulate_callback
int ast_audiohook_init(struct ast_audiohook *audiohook, enum ast_audiohook_type type, const char *source, enum ast_audiohook_init_flags flags)
Initialize an audiohook structure.
General Asterisk PBX channel definitions.
Data structure associated with a custom dialplan function.
Core PBX routines and definitions.
int ast_audiohook_detach(struct ast_audiohook *audiohook)
Detach audiohook from channel.
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true". This function checks to see whether a string passed to it is an indication of an "true" value. It checks to see if the string is "yes", "true", "y", "t", "on" or "1".
static void destroy_callback(void *data)
Helper function used by datastores to destroy the speech structure upon hangup.
union ast_frame::@224 data
#define ast_calloc(num, len)
A wrapper for calloc()
Module has failed to load, may be in an inconsistent state.
Data structure associated with a single frame of data.
enum ast_audiohook_status status
enum ast_frame_type frametype
struct ast_format * format
#define ASTERISK_GPL_KEY
The text the key() function should return.
Asterisk module definitions.
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
SpeexPreprocessState * state
int ast_channel_datastore_remove(struct ast_channel *chan, struct ast_datastore *datastore)
Remove a datastore from a channel.
#define ast_custom_function_register(acf)
Register a custom function.