| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
#include "asterisk.h" |
| 41 |
|
| 42 |
#include <speex/speex_preprocess.h> |
| 43 |
#include "asterisk/module.h" |
| 44 |
#include "asterisk/channel.h" |
| 45 |
#include "asterisk/pbx.h" |
| 46 |
#include "asterisk/utils.h" |
| 47 |
#include "asterisk/audiohook.h" |
| 48 |
|
| 49 |
#define DEFAULT_AGC_LEVEL 8000.0 |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
struct speex_direction_info { |
| 106 |
SpeexPreprocessState *state; |
| 107 |
int agc; |
| 108 |
int denoise; |
| 109 |
int samples; |
| 110 |
float agclevel; |
| 111 |
}; |
| 112 |
|
| 113 |
struct speex_info { |
| 114 |
struct ast_audiohook audiohook; |
| 115 |
int lastrate; |
| 116 |
struct speex_direction_info *tx, *rx; |
| 117 |
}; |
| 118 |
|
| 119 |
static void destroy_callback(void *data) |
| 120 |
{ |
| 121 |
struct speex_info *si = data; |
| 122 |
|
| 123 |
ast_audiohook_destroy(&si->audiohook); |
| 124 |
|
| 125 |
if (si->rx && si->rx->state) { |
| 126 |
speex_preprocess_state_destroy(si->rx->state); |
| 127 |
} |
| 128 |
|
| 129 |
if (si->tx && si->tx->state) { |
| 130 |
speex_preprocess_state_destroy(si->tx->state); |
| 131 |
} |
| 132 |
|
| 133 |
if (si->rx) { |
| 134 |
ast_free(si->rx); |
| 135 |
} |
| 136 |
|
| 137 |
if (si->tx) { |
| 138 |
ast_free(si->tx); |
| 139 |
} |
| 140 |
|
| 141 |
ast_free(data); |
| 142 |
}; |
| 143 |
|
| 144 |
static const struct ast_datastore_info speex_datastore = { |
| 145 |
.type = "speex", |
| 146 |
.destroy = destroy_callback |
| 147 |
}; |
| 148 |
|
| 149 |
static int speex_callback(struct ast_audiohook *audiohook, struct ast_channel *chan, struct ast_frame *frame, enum ast_audiohook_direction direction) |
| 150 |
{ |
| 151 |
struct ast_datastore *datastore = NULL; |
| 152 |
struct speex_direction_info *sdi = NULL; |
| 153 |
struct speex_info *si = NULL; |
| 154 |
char source[80]; |
| 155 |
|
| 156 |
|
| 157 |
if (audiohook->status == AST_AUDIOHOOK_STATUS_DONE || frame->frametype != AST_FRAME_VOICE) { |
| 158 |
return -1; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
if (!(datastore = ast_channel_datastore_find(chan, &speex_datastore, NULL))) { |
| 163 |
return -1; |
| 164 |
} |
| 165 |
|
| 166 |
si = datastore->data; |
| 167 |
|
| 168 |
sdi = (direction == AST_AUDIOHOOK_DIRECTION_READ) ? si->rx : si->tx; |
| 169 |
|
| 170 |
if (!sdi) { |
| 171 |
return -1; |
| 172 |
} |
| 173 |
|
| 174 |
if ((sdi->samples != frame->samples) || (ast_format_get_sample_rate(frame->subclass.format) != si->lastrate)) { |
| 175 |
si->lastrate = ast_format_get_sample_rate(frame->subclass.format); |
| 176 |
if (sdi->state) { |
| 177 |
speex_preprocess_state_destroy(sdi->state); |
| 178 |
} |
| 179 |
|
| 180 |
if (!(sdi->state = speex_preprocess_state_init((sdi->samples = frame->samples), si->lastrate))) { |
| 181 |
return -1; |
| 182 |
} |
| 183 |
|
| 184 |
speex_preprocess_ctl(sdi->state, SPEEX_PREPROCESS_SET_AGC, &sdi->agc); |
| 185 |
|
| 186 |
if (sdi->agc) { |
| 187 |
speex_preprocess_ctl(sdi->state, SPEEX_PREPROCESS_SET_AGC_LEVEL, &sdi->agclevel); |
| 188 |
} |
| 189 |
|
| 190 |
speex_preprocess_ctl(sdi->state, SPEEX_PREPROCESS_SET_DENOISE, &sdi->denoise); |
| 191 |
} |
| 192 |
|
| 193 |
speex_preprocess(sdi->state, frame->data.ptr, NULL); |
| 194 |
snprintf(source, sizeof(source), "%s/speex", frame->src); |
| 195 |
if (frame->mallocd & AST_MALLOCD_SRC) { |
| 196 |
ast_free((char *) frame->src); |
| 197 |
} |
| 198 |
frame->src = ast_strdup(source); |
| 199 |
frame->mallocd |= AST_MALLOCD_SRC; |
| 200 |
|
| 201 |
return 0; |
| 202 |
} |
| 203 |
|
| 204 |
static int speex_write(struct ast_channel *chan, const char *cmd, char *data, const char *value) |
| 205 |
{ |
| 206 |
struct ast_datastore *datastore = NULL; |
| 207 |
struct speex_info *si = NULL; |
| 208 |
struct speex_direction_info **sdi = NULL; |
| 209 |
int is_new = 0; |
| 210 |
|
| 211 |
if (!chan) { |
| 212 |
ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd); |
| 213 |
return -1; |
| 214 |
} |
| 215 |
|
| 216 |
if (strcasecmp(data, "rx") && strcasecmp(data, "tx")) { |
| 217 |
ast_log(LOG_ERROR, "Invalid argument provided to the %s function\n", cmd); |
| 218 |
return -1; |
| 219 |
} |
| 220 |
|
| 221 |
ast_channel_lock(chan); |
| 222 |
if (!(datastore = ast_channel_datastore_find(chan, &speex_datastore, NULL))) { |
| 223 |
ast_channel_unlock(chan); |
| 224 |
|
| 225 |
if (!(datastore = ast_datastore_alloc(&speex_datastore, NULL))) { |
| 226 |
return 0; |
| 227 |
} |
| 228 |
|
| 229 |
if (!(si = ast_calloc(1, sizeof(*si)))) { |
| 230 |
ast_datastore_free(datastore); |
| 231 |
return 0; |
| 232 |
} |
| 233 |
|
| 234 |
ast_audiohook_init(&si->audiohook, AST_AUDIOHOOK_TYPE_MANIPULATE, "speex", AST_AUDIOHOOK_MANIPULATE_ALL_RATES); |
| 235 |
si->audiohook.manipulate_callback = speex_callback; |
| 236 |
si->lastrate = 8000; |
| 237 |
is_new = 1; |
| 238 |
} else { |
| 239 |
ast_channel_unlock(chan); |
| 240 |
si = datastore->data; |
| 241 |
} |
| 242 |
|
| 243 |
if (!strcasecmp(data, "rx")) { |
| 244 |
sdi = &si->rx; |
| 245 |
} else { |
| 246 |
sdi = &si->tx; |
| 247 |
} |
| 248 |
|
| 249 |
if (!*sdi) { |
| 250 |
if (!(*sdi = ast_calloc(1, sizeof(**sdi)))) { |
| 251 |
return 0; |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
(*sdi)->samples = -1; |
| 257 |
} |
| 258 |
|
| 259 |
if (!strcasecmp(cmd, "agc")) { |
| 260 |
if (!sscanf(value, "%30f", &(*sdi)->agclevel)) |
| 261 |
(*sdi)->agclevel = ast_true(value) ? DEFAULT_AGC_LEVEL : 0.0; |
| 262 |
|
| 263 |
if ((*sdi)->agclevel > 32768.0) { |
| 264 |
ast_log(LOG_WARNING, "AGC(%s)=%.01f is greater than 32768... setting to 32768 instead\n", |
| 265 |
((*sdi == si->rx) ? "rx" : "tx"), (*sdi)->agclevel); |
| 266 |
(*sdi)->agclevel = 32768.0; |
| 267 |
} |
| 268 |
|
| 269 |
(*sdi)->agc = !!((*sdi)->agclevel); |
| 270 |
|
| 271 |
if ((*sdi)->state) { |
| 272 |
speex_preprocess_ctl((*sdi)->state, SPEEX_PREPROCESS_SET_AGC, &(*sdi)->agc); |
| 273 |
if ((*sdi)->agc) { |
| 274 |
speex_preprocess_ctl((*sdi)->state, SPEEX_PREPROCESS_SET_AGC_LEVEL, &(*sdi)->agclevel); |
| 275 |
} |
| 276 |
} |
| 277 |
} else if (!strcasecmp(cmd, "denoise")) { |
| 278 |
(*sdi)->denoise = (ast_true(value) != 0); |
| 279 |
|
| 280 |
if ((*sdi)->state) { |
| 281 |
speex_preprocess_ctl((*sdi)->state, SPEEX_PREPROCESS_SET_DENOISE, &(*sdi)->denoise); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
if (!(*sdi)->agc && !(*sdi)->denoise) { |
| 286 |
if ((*sdi)->state) |
| 287 |
speex_preprocess_state_destroy((*sdi)->state); |
| 288 |
|
| 289 |
ast_free(*sdi); |
| 290 |
*sdi = NULL; |
| 291 |
} |
| 292 |
|
| 293 |
if (!si->rx && !si->tx) { |
| 294 |
if (is_new) { |
| 295 |
is_new = 0; |
| 296 |
} else { |
| 297 |
ast_channel_lock(chan); |
| 298 |
ast_channel_datastore_remove(chan, datastore); |
| 299 |
ast_channel_unlock(chan); |
| 300 |
ast_audiohook_remove(chan, &si->audiohook); |
| 301 |
ast_audiohook_detach(&si->audiohook); |
| 302 |
} |
| 303 |
|
| 304 |
ast_datastore_free(datastore); |
| 305 |
} |
| 306 |
|
| 307 |
if (is_new) { |
| 308 |
datastore->data = si; |
| 309 |
ast_channel_lock(chan); |
| 310 |
ast_channel_datastore_add(chan, datastore); |
| 311 |
ast_channel_unlock(chan); |
| 312 |
ast_audiohook_attach(chan, &si->audiohook); |
| 313 |
} |
| 314 |
|
| 315 |
return 0; |
| 316 |
} |
| 317 |
|
| 318 |
static int speex_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) |
| 319 |
{ |
| 320 |
struct ast_datastore *datastore = NULL; |
| 321 |
struct speex_info *si = NULL; |
| 322 |
struct speex_direction_info *sdi = NULL; |
| 323 |
|
| 324 |
if (!chan) { |
| 325 |
ast_log(LOG_ERROR, "%s cannot be used without a channel!\n", cmd); |
| 326 |
return -1; |
| 327 |
} |
| 328 |
|
| 329 |
ast_channel_lock(chan); |
| 330 |
if (!(datastore = ast_channel_datastore_find(chan, &speex_datastore, NULL))) { |
| 331 |
ast_channel_unlock(chan); |
| 332 |
return -1; |
| 333 |
} |
| 334 |
ast_channel_unlock(chan); |
| 335 |
|
| 336 |
si = datastore->data; |
| 337 |
|
| 338 |
if (!strcasecmp(data, "tx")) |
| 339 |
sdi = si->tx; |
| 340 |
else if (!strcasecmp(data, "rx")) |
| 341 |
sdi = si->rx; |
| 342 |
else { |
| 343 |
ast_log(LOG_ERROR, "%s(%s) must either \"tx\" or \"rx\"\n", cmd, data); |
| 344 |
return -1; |
| 345 |
} |
| 346 |
|
| 347 |
if (!strcasecmp(cmd, "agc")) |
| 348 |
snprintf(buf, len, "%.01f", sdi ? sdi->agclevel : 0.0); |
| 349 |
else |
| 350 |
snprintf(buf, len, "%d", sdi ? sdi->denoise : 0); |
| 351 |
|
| 352 |
return 0; |
| 353 |
} |
| 354 |
|
| 355 |
static struct ast_custom_function agc_function = { |
| 356 |
.name = "AGC", |
| 357 |
.write = speex_write, |
| 358 |
.read = speex_read, |
| 359 |
.read_max = 22, |
| 360 |
}; |
| 361 |
|
| 362 |
static struct ast_custom_function denoise_function = { |
| 363 |
.name = "DENOISE", |
| 364 |
.write = speex_write, |
| 365 |
.read = speex_read, |
| 366 |
.read_max = 22, |
| 367 |
}; |
| 368 |
|
| 369 |
static int unload_module(void) |
| 370 |
{ |
| 371 |
ast_custom_function_unregister(&agc_function); |
| 372 |
ast_custom_function_unregister(&denoise_function); |
| 373 |
return 0; |
| 374 |
} |
| 375 |
|
| 376 |
static int load_module(void) |
| 377 |
{ |
| 378 |
if (ast_custom_function_register(&agc_function)) { |
| 379 |
return AST_MODULE_LOAD_DECLINE; |
| 380 |
} |
| 381 |
|
| 382 |
if (ast_custom_function_register(&denoise_function)) { |
| 383 |
ast_custom_function_unregister(&agc_function); |
| 384 |
return AST_MODULE_LOAD_DECLINE; |
| 385 |
} |
| 386 |
|
| 387 |
return AST_MODULE_LOAD_SUCCESS; |
| 388 |
} |
| 389 |
|
| 390 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Noise reduction and Automatic Gain Control (AGC)"); |