44 #include <speex/speex.h>
49 #include <speex/speex_preprocess.h>
76 #define TYPE_SILENCE 0x2
81 #define BUFFER_SAMPLES 8000
82 #define SPEEX_SAMPLES 160
94 SpeexPreprocessState *pp;
105 if (!(tmp->
speex = speex_encoder_init(profile)))
108 speex_bits_init(&tmp->
bits);
109 speex_bits_reset(&tmp->
bits);
110 speex_encoder_ctl(tmp->
speex, SPEEX_GET_FRAME_SIZE, &tmp->
framesize);
111 speex_encoder_ctl(tmp->
speex, SPEEX_SET_COMPLEXITY, &complexity);
112 #ifdef _SPEEX_TYPES_H
114 tmp->pp = speex_preprocess_state_init(tmp->
framesize, sampling_rate);
115 speex_preprocess_ctl(tmp->pp, SPEEX_PREPROCESS_SET_VAD, &pp_vad);
116 speex_preprocess_ctl(tmp->pp, SPEEX_PREPROCESS_SET_AGC, &pp_agc);
117 speex_preprocess_ctl(tmp->pp, SPEEX_PREPROCESS_SET_AGC_LEVEL, &pp_agc_level);
118 speex_preprocess_ctl(tmp->pp, SPEEX_PREPROCESS_SET_DENOISE, &pp_denoise);
119 speex_preprocess_ctl(tmp->pp, SPEEX_PREPROCESS_SET_DEREVERB, &pp_dereverb);
120 speex_preprocess_ctl(tmp->pp, SPEEX_PREPROCESS_SET_DEREVERB_DECAY, &pp_dereverb_decay);
121 speex_preprocess_ctl(tmp->pp, SPEEX_PREPROCESS_SET_DEREVERB_LEVEL, &pp_dereverb_level);
125 speex_encoder_ctl(tmp->
speex, SPEEX_SET_QUALITY, &quality);
127 speex_encoder_ctl(tmp->
speex, SPEEX_SET_VAD, &vad);
130 speex_encoder_ctl(tmp->
speex, SPEEX_SET_VBR, &vbr);
131 speex_encoder_ctl(tmp->
speex, SPEEX_SET_VBR_QUALITY, &vbr_quality);
134 speex_encoder_ctl(tmp->
speex, SPEEX_SET_ABR, &abr);
136 speex_encoder_ctl(tmp->
speex, SPEEX_SET_DTX, &dtx);
156 if (!(tmp->
speex = speex_decoder_init(profile)))
159 speex_bits_init(&tmp->
bits);
160 speex_decoder_ctl(tmp->
speex, SPEEX_GET_FRAME_SIZE, &tmp->
framesize);
162 speex_decoder_ctl(tmp->
speex, SPEEX_SET_ENH, &enhancement);
188 #ifdef _SPEEX_TYPES_H
189 spx_int16_t fout[1024];
199 #ifdef _SPEEX_TYPES_H
202 speex_decode(tmp->
speex, NULL, fout);
204 dst[pvt->
samples + x] = (int16_t)fout[x];
215 #ifdef _SPEEX_TYPES_H
216 res = speex_decode_int(tmp->
speex, &tmp->
bits, fout);
218 res = speex_decode(tmp->
speex, &tmp->
bits, fout);
227 dst[pvt->
samples + x] = (int16_t)fout[x];
258 speex_bits_reset(&tmp->
bits);
260 #ifdef _SPEEX_TYPES_H
263 is_speech = speex_preprocess(tmp->pp, tmp->
buf + samples, NULL);
267 is_speech = speex_encode_int(tmp->
speex, tmp->
buf + samples, &tmp->
bits) || !
dtx;
270 speex_bits_pack(&tmp->
bits, 0, 5);
278 fbuf[x] = tmp->
buf[samples + x];
280 is_speech = speex_encode(tmp->
speex, fbuf, &tmp->
bits) || !
dtx;
299 speex_bits_reset(&tmp->
bits);
300 memset(&pvt->
f, 0,
sizeof(pvt->
f));
308 speex_bits_pack(&tmp->
bits, 15, 5);
317 speex_decoder_destroy(pvt->
speex);
318 speex_bits_destroy(&pvt->
bits);
324 #ifdef _SPEEX_TYPES_H
326 speex_preprocess_state_destroy(pvt->pp);
328 speex_encoder_destroy(pvt->
speex);
329 speex_bits_destroy(&pvt->
bits);
333 .
name =
"speextolin",
347 .
name =
"lintospeex",
361 .
name =
"speexwbtolin16",
375 .
name =
"lin16tospeexwb",
400 if (!strcasecmp(var->
name,
"quality")) {
401 res = abs(atoi(var->
value));
402 if (res > -1 && res < 11) {
403 ast_verb(3,
"CODEC SPEEX: Setting Quality to %d\n",res);
407 }
else if (!strcasecmp(var->
name,
"complexity")) {
408 res = abs(atoi(var->
value));
409 if (res > -1 && res < 11) {
410 ast_verb(3,
"CODEC SPEEX: Setting Complexity to %d\n",res);
414 }
else if (!strcasecmp(var->
name,
"vbr_quality")) {
415 if (sscanf(var->
value,
"%30f", &res_f) == 1 && res_f >= 0 && res_f <= 10) {
416 ast_verb(3,
"CODEC SPEEX: Setting VBR Quality to %f\n",res_f);
420 }
else if (!strcasecmp(var->
name,
"abr_quality")) {
421 ast_log(
LOG_ERROR,
"Error! ABR Quality setting obsolete, set ABR to desired bitrate\n");
422 }
else if (!strcasecmp(var->
name,
"enhancement")) {
424 ast_verb(3,
"CODEC SPEEX: Perceptual Enhancement Mode. [%s]\n",enhancement ?
"on" :
"off");
425 }
else if (!strcasecmp(var->
name,
"vbr")) {
427 ast_verb(3,
"CODEC SPEEX: VBR Mode. [%s]\n",vbr ?
"on" :
"off");
428 }
else if (!strcasecmp(var->
name,
"abr")) {
429 res = abs(atoi(var->
value));
432 ast_verb(3,
"CODEC SPEEX: Setting ABR target bitrate to %d\n",res);
434 ast_verb(3,
"CODEC SPEEX: Disabling ABR\n");
438 }
else if (!strcasecmp(var->
name,
"vad")) {
440 ast_verb(3,
"CODEC SPEEX: VAD Mode. [%s]\n",vad ?
"on" :
"off");
441 }
else if (!strcasecmp(var->
name,
"dtx")) {
443 ast_verb(3,
"CODEC SPEEX: DTX Mode. [%s]\n",dtx ?
"on" :
"off");
444 }
else if (!strcasecmp(var->
name,
"preprocess")) {
446 ast_verb(3,
"CODEC SPEEX: Preprocessing. [%s]\n",preproc ?
"on" :
"off");
447 }
else if (!strcasecmp(var->
name,
"pp_vad")) {
449 ast_verb(3,
"CODEC SPEEX: Preprocessor VAD. [%s]\n",pp_vad ?
"on" :
"off");
450 }
else if (!strcasecmp(var->
name,
"pp_agc")) {
452 ast_verb(3,
"CODEC SPEEX: Preprocessor AGC. [%s]\n",pp_agc ?
"on" :
"off");
453 }
else if (!strcasecmp(var->
name,
"pp_agc_level")) {
454 if (sscanf(var->
value,
"%30f", &res_f) == 1 && res_f >= 0) {
455 ast_verb(3,
"CODEC SPEEX: Setting preprocessor AGC Level to %f\n",res_f);
456 pp_agc_level = res_f;
459 }
else if (!strcasecmp(var->
name,
"pp_denoise")) {
461 ast_verb(3,
"CODEC SPEEX: Preprocessor Denoise. [%s]\n",pp_denoise ?
"on" :
"off");
462 }
else if (!strcasecmp(var->
name,
"pp_dereverb")) {
464 ast_verb(3,
"CODEC SPEEX: Preprocessor Dereverb. [%s]\n",pp_dereverb ?
"on" :
"off");
465 }
else if (!strcasecmp(var->
name,
"pp_dereverb_decay")) {
466 if (sscanf(var->
value,
"%30f", &res_f) == 1 && res_f >= 0) {
467 ast_verb(3,
"CODEC SPEEX: Setting preprocessor Dereverb Decay to %f\n",res_f);
468 pp_dereverb_decay = res_f;
471 }
else if (!strcasecmp(var->
name,
"pp_dereverb_level")) {
472 if (sscanf(var->
value,
"%30f", &res_f) == 1 && res_f >= 0) {
473 ast_verb(3,
"CODEC SPEEX: Setting preprocessor Dereverb Level to %f\n",res_f);
474 pp_dereverb_level = res_f;
static int speexwbtolin16_new(struct ast_trans_pvt *pvt)
struct ast_frame * ast_trans_frameout(struct ast_trans_pvt *pvt, int datalen, int samples)
generic frameout function
int datalen
actual space used in outbuf
static float pp_dereverb_decay
static void lintospeex_destroy(struct ast_trans_pvt *arg)
Asterisk main include file. File version handling, generic pbx functions.
union ast_trans_pvt::@213 outbuf
Descriptor of a translator.
static struct ast_translator lin16tospeexwb
Support for translation of data formats. translate.c.
static int speex_decoder_construct(struct ast_trans_pvt *pvt, const SpeexMode *profile)
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category)
Goes through variables.
static int lin16tospeexwb_new(struct ast_trans_pvt *pvt)
Structure for variables, used for configurations and for channel variables.
static struct ast_frame * lintospeex_frameout(struct ast_trans_pvt *pvt)
convert work buffer and produce output frame
Configuration File Parser.
static int lintospeex_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
store input frame in work buffer
int16_t buf[BUFFER_SAMPLES]
static int load_module(void)
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
static void speextolin_destroy(struct ast_trans_pvt *arg)
static struct ast_frame * slin8_sample(void)
static int parse_config(int reload)
#define ast_verb(level,...)
void ast_config_destroy(struct ast_config *config)
Destroys a config.
static struct ast_frame * slin16_sample(void)
#define CONFIG_STATUS_FILEMISSING
static float pp_dereverb_level
static int speextolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
convert and store into outbuf
#define ast_config_load(filename, flags)
Load a config file.
#define ast_register_translator(t)
See __ast_register_translator()
int ast_unregister_translator(struct ast_translator *t)
Unregister a translator Unregisters the given tranlator.
static int speextolin_new(struct ast_trans_pvt *pvt)
int buf_size
size of outbuf, in bytes. Mandatory. The wrapper code will also allocate an AST_FRIENDLY_OFFSET space...
#define AST_FORMAT_SLINEAR16
static struct ast_frame * speex_sample(void)
static int unload_module(void)
static struct ast_translator lintospeex
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".
Default structure for translators, with the basic fields and buffers, all allocated as part of the sa...
static struct ast_translator speexwbtolin16
static int speex_encoder_construct(struct ast_trans_pvt *pvt, const SpeexMode *profile, int sampling_rate)
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
static struct ast_frame * speex16_sample(void)
static int lintospeex_new(struct ast_trans_pvt *pvt)
Structure used to handle boolean flags.
#define AST_FORMAT_SLINEAR
Data structure associated with a single frame of data.
struct ast_translator * t
enum ast_frame_type frametype
struct ast_variable * next
#define CONFIG_STATUS_FILEINVALID
static float pp_agc_level
static struct ast_translator speextolin
#define ASTERISK_GPL_KEY
The text the key() function should return.
Asterisk module definitions.
union ast_frame::@172 data
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
#define CONFIG_STATUS_FILEUNCHANGED
#define AST_FORMAT_SPEEX16