Wed Aug 18 22:34:22 2010

Asterisk developer's documentation


dsp.h File Reference

Convenient Signal Processing routines. More...

Go to the source code of this file.

Defines

#define DSP_DIGITMODE_DTMF   0
#define DSP_DIGITMODE_MF   1
#define DSP_DIGITMODE_MUTECONF   (1 << 9)
#define DSP_DIGITMODE_MUTEMAX   (1 << 10)
#define DSP_DIGITMODE_NOQUELCH   (1 << 8)
#define DSP_DIGITMODE_RELAXDTMF   (1 << 11)
#define DSP_FAXMODE_DETECT_ALL   (DSP_FAXMODE_DETECT_CNG | DSP_FAXMODE_DETECT_CED)
#define DSP_FAXMODE_DETECT_CED   (1 << 1)
#define DSP_FAXMODE_DETECT_CNG   (1 << 0)
#define DSP_FEATURE_BUSY_DETECT   (1 << 1)
#define DSP_FEATURE_CALL_PROGRESS   (DSP_PROGRESS_TALK | DSP_PROGRESS_RINGING | DSP_PROGRESS_BUSY | DSP_PROGRESS_CONGESTION)
#define DSP_FEATURE_DIGIT_DETECT   (1 << 3)
#define DSP_FEATURE_FAX_DETECT   (1 << 4)
#define DSP_FEATURE_SILENCE_SUPPRESS   (1 << 0)
#define DSP_PROGRESS_BUSY   (1 << 18)
#define DSP_PROGRESS_CONGESTION   (1 << 19)
#define DSP_PROGRESS_RINGING   (1 << 17)
#define DSP_PROGRESS_TALK   (1 << 16)
#define DSP_TONE_STATE_BUSY   4
#define DSP_TONE_STATE_DIALTONE   2
#define DSP_TONE_STATE_HUNGUP   8
#define DSP_TONE_STATE_RINGING   1
#define DSP_TONE_STATE_SILENCE   0
#define DSP_TONE_STATE_SPECIAL1   5
#define DSP_TONE_STATE_SPECIAL2   6
#define DSP_TONE_STATE_SPECIAL3   7
#define DSP_TONE_STATE_TALKING   3

Enumerations

enum  threshold { THRESHOLD_SILENCE = 0, THRESHOLD_MAX = 1 }

Functions

int ast_dsp_busydetect (struct ast_dsp *dsp)
 Return non-zero if historically this should be a busy, request that ast_dsp_silence has already been called.
int ast_dsp_call_progress (struct ast_dsp *dsp, struct ast_frame *inf)
 Scans for progress indication in audio.
int ast_dsp_digitdetect (struct ast_dsp *dsp, struct ast_frame *f)
 Return non-zero if DTMF hit was found.
void ast_dsp_digitreset (struct ast_dsp *dsp)
 Reset DTMF detector.
void ast_dsp_free (struct ast_dsp *dsp)
int ast_dsp_get_tcount (struct ast_dsp *dsp)
 Get tcount (Threshold counter).
int ast_dsp_get_threshold_from_settings (enum threshold which)
 Get silence threshold from dsp.conf.
int ast_dsp_get_tstate (struct ast_dsp *dsp)
 Get tstate (Tone State).
int ast_dsp_getdigits (struct ast_dsp *dsp, char *buf, int max)
 Get pending DTMF/MF digits.
int ast_dsp_init (void)
 Load dsp settings from dsp.conf.
ast_dspast_dsp_new (void)
int ast_dsp_noise (struct ast_dsp *dsp, struct ast_frame *f, int *totalnoise)
 Return non-zero if this is noise. Updates "totalnoise" with the total number of seconds of noise.
ast_frameast_dsp_process (struct ast_channel *chan, struct ast_dsp *dsp, struct ast_frame *inf)
 Return AST_FRAME_NULL frames when there is silence, AST_FRAME_BUSY on busies, and call progress, all dependent upon which features are enabled.
int ast_dsp_reload (void)
 Reloads dsp settings from dsp.conf.
void ast_dsp_reset (struct ast_dsp *dsp)
 Reset total silence count.
void ast_dsp_set_busy_compare (struct ast_dsp *dsp, int compare)
 Set if silence and noice lengths must be compared for busy.
void ast_dsp_set_busy_count (struct ast_dsp *dsp, int cadences)
 Set number of required cadences for busy.
void ast_dsp_set_busy_pattern (struct ast_dsp *dsp, int tonelength, int quietlength, int fuzzy)
 Set expected lengths of the busy tones.
int ast_dsp_set_call_progress_zone (struct ast_dsp *dsp, char *zone)
 Set zone for doing progress detection.
int ast_dsp_set_digitmode (struct ast_dsp *dsp, int digitmode)
 Set digit mode.
int ast_dsp_set_faxmode (struct ast_dsp *dsp, int faxmode)
 Set fax mode.
void ast_dsp_set_features (struct ast_dsp *dsp, int features)
 Select feature set.
void ast_dsp_set_threshold (struct ast_dsp *dsp, int threshold)
 Set threshold value for silence.
int ast_dsp_silence (struct ast_dsp *dsp, struct ast_frame *f, int *totalsilence)
 Return non-zero if this is silence. Updates "totalsilence" with the total number of seconds of silence.
int ast_dsp_was_muted (struct ast_dsp *dsp)
 Returns true if DSP code was muting any fragment of the last processed frame. Muting (squelching) happens when DSP code removes DTMF/MF/generic tones from the audio.


Detailed Description

Convenient Signal Processing routines.

Definition in file dsp.h.


Define Documentation

#define DSP_DIGITMODE_DTMF   0

Detect DTMF digits

Definition at line 31 of file dsp.h.

Referenced by ast_dsp_new(), ast_dsp_set_digitmode(), dahdi_hangup(), dahdi_new(), dahdi_setoption(), sip_new(), ss_thread(), and store_config().

#define DSP_DIGITMODE_MF   1

Detect MF digits

Definition at line 32 of file dsp.h.

Referenced by ss_thread().

#define DSP_DIGITMODE_MUTECONF   (1 << 9)

Mute conference

Definition at line 35 of file dsp.h.

Referenced by ast_dsp_set_digitmode(), dahdi_setoption(), and store_config().

#define DSP_DIGITMODE_MUTEMAX   (1 << 10)

Delay audio by a frame to try to extra quelch

Definition at line 36 of file dsp.h.

Referenced by ast_dsp_set_digitmode(), and dahdi_setoption().

#define DSP_DIGITMODE_NOQUELCH   (1 << 8)

Do not quelch DTMF from in-band

Definition at line 34 of file dsp.h.

Referenced by mgcp_new().

#define DSP_DIGITMODE_RELAXDTMF   (1 << 11)

"Radio" mode (relaxed DTMF)

Definition at line 37 of file dsp.h.

Referenced by dahdi_setoption(), sip_new(), and store_config().

#define DSP_FAXMODE_DETECT_ALL   (DSP_FAXMODE_DETECT_CNG | DSP_FAXMODE_DETECT_CED)

Definition at line 47 of file dsp.h.

#define DSP_FAXMODE_DETECT_CED   (1 << 1)

Definition at line 46 of file dsp.h.

#define DSP_FAXMODE_DETECT_CNG   (1 << 0)

Definition at line 45 of file dsp.h.

#define DSP_FEATURE_BUSY_DETECT   (1 << 1)

Definition at line 27 of file dsp.h.

Referenced by ast_dsp_process(), and dahdi_new().

#define DSP_FEATURE_CALL_PROGRESS   (DSP_PROGRESS_TALK | DSP_PROGRESS_RINGING | DSP_PROGRESS_BUSY | DSP_PROGRESS_CONGESTION)

Definition at line 43 of file dsp.h.

Referenced by dahdi_new().

#define DSP_FEATURE_DIGIT_DETECT   (1 << 3)

Definition at line 28 of file dsp.h.

Referenced by __oh323_new(), dahdi_new(), disable_dtmf_detect(), enable_dtmf_detect(), mgcp_new(), misdn_set_opt_exec(), read_config(), sip_dtmfmode(), sip_new(), sip_rtp_read(), and store_config().

#define DSP_FEATURE_FAX_DETECT   (1 << 4)

Definition at line 29 of file dsp.h.

Referenced by dahdi_new(), misdn_set_opt_exec(), read_config(), and sip_new().

#define DSP_FEATURE_SILENCE_SUPPRESS   (1 << 0)

Definition at line 26 of file dsp.h.

Referenced by ast_dsp_process().

#define DSP_PROGRESS_BUSY   (1 << 18)

Enable busy tone detection

Definition at line 41 of file dsp.h.

#define DSP_PROGRESS_CONGESTION   (1 << 19)

Enable congestion tone detection

Definition at line 42 of file dsp.h.

#define DSP_PROGRESS_RINGING   (1 << 17)

Enable calling tone detection

Definition at line 40 of file dsp.h.

#define DSP_PROGRESS_TALK   (1 << 16)

Enable talk detection

Definition at line 39 of file dsp.h.

Referenced by dahdi_new().

#define DSP_TONE_STATE_BUSY   4

Definition at line 53 of file dsp.h.

Referenced by __ast_dsp_call_progress().

#define DSP_TONE_STATE_DIALTONE   2

Definition at line 51 of file dsp.h.

#define DSP_TONE_STATE_HUNGUP   8

Definition at line 57 of file dsp.h.

#define DSP_TONE_STATE_RINGING   1

Definition at line 50 of file dsp.h.

Referenced by __ast_dsp_call_progress().

#define DSP_TONE_STATE_SILENCE   0

Definition at line 49 of file dsp.h.

Referenced by __ast_dsp_call_progress().

#define DSP_TONE_STATE_SPECIAL1   5

Definition at line 54 of file dsp.h.

#define DSP_TONE_STATE_SPECIAL2   6

Definition at line 55 of file dsp.h.

#define DSP_TONE_STATE_SPECIAL3   7

Definition at line 56 of file dsp.h.

#define DSP_TONE_STATE_TALKING   3

Definition at line 52 of file dsp.h.


Enumeration Type Documentation

enum threshold

Enumerator:
THRESHOLD_SILENCE 
THRESHOLD_MAX 

Definition at line 61 of file dsp.h.

00061                {
00062    /* Array offsets */
00063    THRESHOLD_SILENCE = 0,
00064    /* Always the last */
00065    THRESHOLD_MAX = 1,
00066 };


Function Documentation

int ast_dsp_busydetect ( struct ast_dsp dsp  ) 

Return non-zero if historically this should be a busy, request that ast_dsp_silence has already been called.

Definition at line 1149 of file dsp.c.

References ast_debug, ast_log(), buf, BUSY_MAX, BUSY_MIN, ast_dsp::busy_pattern_fuzzy, BUSY_PERCENT, ast_dsp::busy_quietlength, ast_dsp::busy_tonelength, ast_dsp::busycompare, ast_dsp::busycount, ast_dsp::busymaybe, ast_dsp::busytoneonly, DSP_HISTORY, ast_dsp::historicnoise, ast_dsp::historicsilence, LOG_ERROR, LOG_NOTICE, and option_debug.

01150 {
01151    int res = 0, x;
01152    int avgsilence = 0, hitsilence = 0;
01153    int avgtone = 0, hittone = 0;
01154 #ifdef DEBUG_DSP_BUSYDETECT
01155    char buf[16];
01156    char silence_list[64]="", tone_list[64]="";
01157 #endif
01158    
01159    if (!dsp->busymaybe)
01160       return res;
01161    dsp->busymaybe = 0;
01162 
01163    for (x=DSP_HISTORY - dsp->busycount;x<DSP_HISTORY;x++) {
01164       avgsilence += dsp->historicsilence[x];
01165       avgtone += dsp->historicnoise[x];
01166    }
01167    avgsilence /= dsp->busycount;
01168    avgtone /= dsp->busycount;
01169 #ifdef DEBUG_DSP_BUSYDETECT
01170    sprintf(silence_list,"Silences: ");
01171    sprintf(tone_list,"Tones:    ");
01172 #endif
01173    for (x=DSP_HISTORY - dsp->busycount; x<DSP_HISTORY; x++) {
01174 #ifdef DEBUG_DSP_BUSYDETECT
01175       snprintf(buf, sizeof(buf), "%5d ", dsp->historicsilence[x]);
01176       strcat(silence_list, buf);
01177       snprintf(buf, sizeof(buf), "%5d ", dsp->historicnoise[x]);
01178       strcat(tone_list, buf); 
01179 #endif
01180       if (!dsp->busytoneonly) {
01181          if (avgsilence > dsp->historicsilence[x]) {
01182             if (avgsilence - (avgsilence*BUSY_PERCENT/100) <= dsp->historicsilence[x])
01183                hitsilence++;
01184          } else {
01185             if (avgsilence + (avgsilence*BUSY_PERCENT/100) >= dsp->historicsilence[x])
01186                hitsilence++;
01187          }
01188       }
01189       if (avgtone > dsp->historicnoise[x]) {
01190          if (avgtone - (avgtone*BUSY_PERCENT/100) <= dsp->historicnoise[x])
01191             hittone++;
01192       } else {
01193          if (avgtone + (avgtone*BUSY_PERCENT/100) >= dsp->historicnoise[x])
01194             hittone++;
01195       }
01196    }
01197 #ifdef DEBUG_DSP_BUSYDETECT
01198    fprintf(stderr, "BUSY DETECTOR\n");   
01199    fprintf(stderr, "%s\n", tone_list);
01200    fprintf(stderr, "%s\n", silence_list)
01201 #endif
01202    if ((dsp->busytoneonly || 
01203        (hitsilence >= dsp->busycount - 1 && avgsilence >= BUSY_MIN && avgsilence <= BUSY_MAX)) &&
01204       (hittone >= dsp->busycount - 1 && avgtone >= BUSY_MIN && avgtone <= BUSY_MAX)) {
01205       if (dsp->busycompare) {
01206            if (dsp->busytoneonly) {
01207              res = 1;
01208             ast_log(LOG_ERROR, "You can't use busytoneonly together with busycompare");
01209          } else {
01210               if (avgtone > avgsilence) {
01211                  if (avgtone - avgtone*BUSY_PERCENT/100 <= avgsilence)
01212                     res = 1;
01213             } else {
01214                  if (avgtone + avgtone*BUSY_PERCENT/100 >= avgsilence)
01215                    res = 1;
01216             }
01217          }
01218       } else {
01219          res = 1;
01220       }
01221    }
01222    /* If we know the expected busy tone length, check we are in the range */
01223    if (res && (dsp->busy_tonelength > 0)) {
01224       if (abs(avgtone - dsp->busy_tonelength) > (dsp->busy_tonelength*dsp->busy_pattern_fuzzy/100)) {
01225 #ifdef BUSYDETECT_DEBUG
01226             ast_debug(5, "busy detector: avgtone of %d not close enough to desired %d\n", avgtone, dsp->busy_tonelength);
01227 #endif
01228          res = 0;
01229       }
01230    }
01231    /* If we know the expected busy tone silent-period length, check we are in the range */
01232    if (res && (!dsp->busytoneonly) && (dsp->busy_quietlength > 0)) {
01233       if (abs(avgsilence - dsp->busy_quietlength) > (dsp->busy_quietlength*dsp->busy_pattern_fuzzy/100)) {
01234 #ifdef BUSYDETECT_DEBUG
01235             ast_debug(5, "busy detector: avgsilence of %d not close enough to desired %d\n", avgsilence, dsp->busy_quietlength);
01236 #endif
01237          res = 0;
01238       }
01239    }
01240    if (res) {
01241       if (option_debug)
01242          ast_log(LOG_NOTICE, "ast_dsp_busydetect detected busy sequence, avgtone: %d, avgsilence %d\n", avgtone, avgsilence);
01243    } else {
01244          ast_debug(5, "busy detector: FAILED with avgtone: %d, avgsilence %d\n", avgtone, avgsilence);
01245    }
01246    return res;
01247 }

int ast_dsp_call_progress ( struct ast_dsp dsp,
struct ast_frame inf 
)

Scans for progress indication in audio.

Definition at line 1077 of file dsp.c.

References __ast_dsp_call_progress(), AST_FORMAT_SLINEAR, AST_FRAME_VOICE, ast_log(), ast_frame::data, ast_frame::datalen, ast_frame::frametype, LOG_WARNING, ast_frame::ptr, and ast_frame::subclass.

01078 {
01079    if (inf->frametype != AST_FRAME_VOICE) {
01080       ast_log(LOG_WARNING, "Can't check call progress of non-voice frames\n");
01081       return 0;
01082    }
01083    if (inf->subclass != AST_FORMAT_SLINEAR) {
01084       ast_log(LOG_WARNING, "Can only check call progress in signed-linear frames\n");
01085       return 0;
01086    }
01087    return __ast_dsp_call_progress(dsp, inf->data.ptr, inf->datalen / 2);
01088 }

int ast_dsp_digitdetect ( struct ast_dsp dsp,
struct ast_frame f 
)

Return non-zero if DTMF hit was found.

void ast_dsp_digitreset ( struct ast_dsp dsp  ) 

Reset DTMF detector.

Definition at line 1569 of file dsp.c.

References digit_detect_state_t::current_digits, ast_dsp::digit_state, ast_dsp::digitmode, digit_detect_state_t::digits, digit_detect_state_t::dtmf, ast_dsp::dtmf_began, goertzel_reset(), digit_detect_state_t::mf, s, and digit_detect_state_t::td.

Referenced by ss_thread().

01570 {
01571    int i;
01572    
01573    dsp->dtmf_began = 0;
01574    if (dsp->digitmode & DSP_DIGITMODE_MF) {
01575       mf_detect_state_t *s = &dsp->digit_state.td.mf;
01576       /* Reinitialise the detector for the next block */
01577       for (i = 0;  i < 6;  i++) {
01578          goertzel_reset(&s->tone_out[i]);
01579       }
01580       s->hits[4] = s->hits[3] = s->hits[2] = s->hits[1] = s->hits[0] = s->current_hit = 0;
01581       s->current_sample = 0;
01582    } else {
01583       dtmf_detect_state_t *s = &dsp->digit_state.td.dtmf;
01584       /* Reinitialise the detector for the next block */
01585       for (i = 0;  i < 4;  i++) {
01586          goertzel_reset(&s->row_out[i]);
01587          goertzel_reset(&s->col_out[i]);
01588       }
01589       s->lasthit = s->current_hit = 0;
01590       s->energy = 0.0;
01591       s->current_sample = 0;
01592       s->hits = 0;
01593       s->misses = 0;
01594    }
01595 
01596    dsp->digit_state.digits[0] = '\0';
01597    dsp->digit_state.current_digits = 0;
01598 }

void ast_dsp_free ( struct ast_dsp dsp  ) 

Definition at line 1527 of file dsp.c.

References ast_free.

Referenced by __ast_play_and_record(), __oh323_destroy(), background_detect_exec(), cl_dequeue_chan(), cleanup_connection(), conf_run(), dahdi_hangup(), do_waiting(), handle_recordfile(), isAnsweringMachine(), mgcp_hangup(), sip_dtmfmode(), sip_hangup(), sip_rtp_read(), ss_thread(), and unload_module().

01528 {
01529    ast_free(dsp);
01530 }

int ast_dsp_get_tcount ( struct ast_dsp dsp  ) 

Get tcount (Threshold counter).

Definition at line 1661 of file dsp.c.

References ast_dsp::tcount.

01662 {
01663    return dsp->tcount;
01664 }

int ast_dsp_get_threshold_from_settings ( enum threshold  which  ) 

Get silence threshold from dsp.conf.

Since:
1.6.1

Definition at line 1688 of file dsp.c.

References thresholds.

Referenced by app_exec(), ast_record_review(), conf_run(), do_waiting(), handle_recordfile(), load_config(), and setup_privacy_args().

01689 {
01690    return thresholds[which];
01691 }

int ast_dsp_get_tstate ( struct ast_dsp dsp  ) 

Get tstate (Tone State).

Definition at line 1656 of file dsp.c.

References ast_dsp::tstate.

01657 {
01658    return dsp->tstate;
01659 }

int ast_dsp_getdigits ( struct ast_dsp dsp,
char *  buf,
int  max 
)

Get pending DTMF/MF digits.

int ast_dsp_init ( void   ) 

Load dsp settings from dsp.conf.

Since:
1.6.1

Definition at line 1693 of file dsp.c.

References _dsp_init().

Referenced by main().

01694 {
01695    return _dsp_init(0);
01696 }

struct ast_dsp* ast_dsp_new ( void   ) 

Definition at line 1487 of file dsp.c.

References ast_calloc, ast_digit_detect_init(), ast_dsp_prog_reset(), ast_fax_detect_init(), BUSY_PAT_PERCENT, DEFAULT_THRESHOLD, DSP_DIGITMODE_DTMF, DSP_HISTORY, and ast_dsp::threshold.

Referenced by __ast_play_and_record(), __oh323_new(), background_detect_exec(), conf_run(), dahdi_new(), do_waiting(), handle_recordfile(), isAnsweringMachine(), mgcp_new(), misdn_set_opt_exec(), read_config(), sip_dtmfmode(), sip_new(), and store_config().

01488 {
01489    struct ast_dsp *dsp;
01490    
01491    if ((dsp = ast_calloc(1, sizeof(*dsp)))) {      
01492       dsp->threshold = DEFAULT_THRESHOLD;
01493       dsp->features = DSP_FEATURE_SILENCE_SUPPRESS;
01494       dsp->busycount = DSP_HISTORY;
01495       dsp->digitmode = DSP_DIGITMODE_DTMF;
01496       dsp->faxmode = DSP_FAXMODE_DETECT_CNG;
01497       dsp->busy_pattern_fuzzy = BUSY_PAT_PERCENT;
01498 #ifdef BUSYDETECT_TONEONLY
01499       dsp->busytoneonly = 1;
01500 #ifdef BUSYDETECT_COMPARE_TONE_AND_SILENCE
01501 #error "You can't use BUSYDETECT_TONEONLY together with BUSYDETECT_COMPARE_TONE_AND_SILENCE");
01502 #endif
01503 #else
01504    dsp->busytoneonly = 0;
01505 #ifdef BUSYDETECT_COMPARE_TONE_AND_SILENCE
01506    dsp->busycompare = 1;
01507 #else
01508    dsp->busycompare = 0;
01509 #endif
01510 #endif
01511       /* Initialize digit detector */
01512       ast_digit_detect_init(&dsp->digit_state, dsp->digitmode & DSP_DIGITMODE_MF);
01513       dsp->display_inband_dtmf_warning = 1;
01514       /* Initialize initial DSP progress detect parameters */
01515       ast_dsp_prog_reset(dsp);
01516       /* Initialize fax detector */
01517       ast_fax_detect_init(dsp);
01518    }
01519    return dsp;
01520 }

int ast_dsp_noise ( struct ast_dsp dsp,
struct ast_frame f,
int *  totalnoise 
)

Return non-zero if this is noise. Updates "totalnoise" with the total number of seconds of noise.

Since:
1.6.1

Definition at line 1267 of file dsp.c.

References __ast_dsp_silence_noise(), AST_FORMAT_SLINEAR, AST_FRAME_VOICE, ast_log(), f, len(), LOG_WARNING, and s.

Referenced by do_waiting().

01268 {
01269        short *s;
01270        int len;
01271 
01272        if (f->frametype != AST_FRAME_VOICE) {
01273                ast_log(LOG_WARNING, "Can't calculate noise on a non-voice frame\n");
01274                return 0;
01275        }
01276        if (f->subclass != AST_FORMAT_SLINEAR) {
01277                ast_log(LOG_WARNING, "Can only calculate noise on signed-linear frames :(\n");
01278                return 0;
01279        }
01280        s = f->data.ptr;
01281        len = f->datalen/2;
01282        return __ast_dsp_silence_noise(dsp, s, len, NULL, totalnoise);
01283 }

struct ast_frame* ast_dsp_process ( struct ast_channel chan,
struct ast_dsp dsp,
struct ast_frame inf 
)

Return AST_FRAME_NULL frames when there is silence, AST_FRAME_BUSY on busies, and call progress, all dependent upon which features are enabled.

Definition at line 1286 of file dsp.c.

References __ast_dsp_silence_noise(), AST_ALAW, AST_FORMAT_ALAW, AST_FORMAT_SLINEAR, AST_FORMAT_ULAW, AST_FRAME_VOICE, ast_getformatname(), ast_log(), AST_MULAW, ast_frame::data, ast_frame::datalen, ast_dsp::display_inband_dtmf_warning, DSP_FEATURE_BUSY_DETECT, DSP_FEATURE_SILENCE_SUPPRESS, ast_dsp::features, ast_frame::frametype, len(), LOG_WARNING, ast_dsp::mute_fragments, ast_frame::ptr, and ast_frame::subclass.

Referenced by dahdi_read(), mgcp_rtp_read(), oh323_rtp_read(), process_ast_dsp(), sip_rtp_read(), and usbradio_read().

01287 {
01288    int silence;
01289    int res;
01290    int digit = 0, fax_digit = 0;
01291    int x;
01292    short *shortdata;
01293    unsigned char *odata;
01294    int len;
01295    struct ast_frame *outf = NULL;
01296 
01297    if (!af)
01298       return NULL;
01299    if (af->frametype != AST_FRAME_VOICE)
01300       return af;
01301 
01302    odata = af->data.ptr;
01303    len = af->datalen;
01304    /* Make sure we have short data */
01305    switch (af->subclass) {
01306    case AST_FORMAT_SLINEAR:
01307       shortdata = af->data.ptr;
01308       len = af->datalen / 2;
01309       break;
01310    case AST_FORMAT_ULAW:
01311       shortdata = alloca(af->datalen * 2);
01312       for (x = 0;x < len; x++) 
01313          shortdata[x] = AST_MULAW(odata[x]);
01314       break;
01315    case AST_FORMAT_ALAW:
01316       shortdata = alloca(af->datalen * 2);
01317       for (x = 0; x < len; x++) 
01318          shortdata[x] = AST_ALAW(odata[x]);
01319       break;
01320    default:
01321       /*Display warning only once. Otherwise you would get hundreds of warnings every second */
01322       if (dsp->display_inband_dtmf_warning)
01323          ast_log(LOG_WARNING, "Inband DTMF is not supported on codec %s. Use RFC2833\n", ast_getformatname(af->subclass));
01324       dsp->display_inband_dtmf_warning = 0;
01325       return af;
01326    }
01327 
01328    /* Initially we do not want to mute anything */
01329    dsp->mute_fragments = 0;
01330 
01331    /* Need to run the silence detection stuff for silence suppression and busy detection */
01332    if ((dsp->features & DSP_FEATURE_SILENCE_SUPPRESS) || (dsp->features & DSP_FEATURE_BUSY_DETECT)) {
01333       res = __ast_dsp_silence_noise(dsp, shortdata, len, &silence, NULL);
01334    }
01335 
01336    if ((dsp->features & DSP_FEATURE_SILENCE_SUPPRESS) && silence) {
01337       memset(&dsp->f, 0, sizeof(dsp->f));
01338       dsp->f.frametype = AST_FRAME_NULL;
01339       ast_frfree(af);
01340       return ast_frisolate(&dsp->f);
01341    }
01342    if ((dsp->features & DSP_FEATURE_BUSY_DETECT) && ast_dsp_busydetect(dsp)) {
01343       chan->_softhangup |= AST_SOFTHANGUP_DEV;
01344       memset(&dsp->f, 0, sizeof(dsp->f));
01345       dsp->f.frametype = AST_FRAME_CONTROL;
01346       dsp->f.subclass = AST_CONTROL_BUSY;
01347       ast_frfree(af);
01348       ast_debug(1, "Requesting Hangup because the busy tone was detected on channel %s\n", chan->name);
01349       return ast_frisolate(&dsp->f);
01350    }
01351 
01352    if ((dsp->features & DSP_FEATURE_FAX_DETECT)) {
01353       if ((dsp->faxmode & DSP_FAXMODE_DETECT_CNG) && tone_detect(dsp, &dsp->cng_tone_state, shortdata, len)) {
01354          fax_digit = 'f';
01355       }
01356 
01357       if ((dsp->faxmode & DSP_FAXMODE_DETECT_CED) && tone_detect(dsp, &dsp->ced_tone_state, shortdata, len)) {
01358          fax_digit = 'e';
01359       }
01360    }
01361 
01362    if (dsp->features & (DSP_FEATURE_DIGIT_DETECT | DSP_FEATURE_BUSY_DETECT)) {
01363       if (dsp->digitmode & DSP_DIGITMODE_MF)
01364          digit = mf_detect(dsp, &dsp->digit_state, shortdata, len, (dsp->digitmode & DSP_DIGITMODE_NOQUELCH) == 0, (dsp->digitmode & DSP_DIGITMODE_RELAXDTMF));
01365       else
01366          digit = dtmf_detect(dsp, &dsp->digit_state, shortdata, len, (dsp->digitmode & DSP_DIGITMODE_NOQUELCH) == 0, (dsp->digitmode & DSP_DIGITMODE_RELAXDTMF));
01367 
01368       if (dsp->digit_state.current_digits) {
01369          int event = 0;
01370          char event_digit = 0;
01371 
01372          if (!dsp->dtmf_began) {
01373             /* We have not reported DTMF_BEGIN for anything yet */
01374 
01375             if (dsp->features & DSP_FEATURE_DIGIT_DETECT) {
01376                event = AST_FRAME_DTMF_BEGIN;
01377                event_digit = dsp->digit_state.digits[0];
01378             }
01379             dsp->dtmf_began = 1;
01380 
01381          } else if (dsp->digit_state.current_digits > 1 || digit != dsp->digit_state.digits[0]) {
01382             /* Digit changed. This means digit we have reported with DTMF_BEGIN ended */
01383             if (dsp->features & DSP_FEATURE_DIGIT_DETECT) {
01384                event = AST_FRAME_DTMF_END;
01385                event_digit = dsp->digit_state.digits[0];
01386             }
01387             memmove(dsp->digit_state.digits, dsp->digit_state.digits + 1, dsp->digit_state.current_digits);
01388             dsp->digit_state.current_digits--;
01389             dsp->dtmf_began = 0;
01390 
01391             if (dsp->features & DSP_FEATURE_BUSY_DETECT) {
01392                /* Reset Busy Detector as we have some confirmed activity */ 
01393                memset(dsp->historicsilence, 0, sizeof(dsp->historicsilence));
01394                memset(dsp->historicnoise, 0, sizeof(dsp->historicnoise));
01395                ast_debug(1, "DTMF Detected - Reset busydetector\n");
01396             }
01397          }
01398 
01399          if (event) {
01400             memset(&dsp->f, 0, sizeof(dsp->f));
01401             dsp->f.frametype = event;
01402             dsp->f.subclass = event_digit;
01403             outf = &dsp->f;
01404             goto done;
01405          }
01406       }
01407    }
01408 
01409    if (fax_digit) {
01410       /* Fax was detected - digit is either 'f' or 'e' */
01411 
01412       memset(&dsp->f, 0, sizeof(dsp->f));
01413       dsp->f.frametype = AST_FRAME_DTMF;
01414       dsp->f.subclass = fax_digit;
01415       outf = &dsp->f;
01416       goto done;
01417    }
01418 
01419    if ((dsp->features & DSP_FEATURE_CALL_PROGRESS)) {
01420       res = __ast_dsp_call_progress(dsp, shortdata, len);
01421       if (res) {
01422          switch (res) {
01423          case AST_CONTROL_ANSWER:
01424          case AST_CONTROL_BUSY:
01425          case AST_CONTROL_RINGING:
01426          case AST_CONTROL_CONGESTION:
01427          case AST_CONTROL_HANGUP:
01428             memset(&dsp->f, 0, sizeof(dsp->f));
01429             dsp->f.frametype = AST_FRAME_CONTROL;
01430             dsp->f.subclass = res;
01431             dsp->f.src = "dsp_progress";
01432             if (chan) 
01433                ast_queue_frame(chan, &dsp->f);
01434             break;
01435          default:
01436             ast_log(LOG_WARNING, "Don't know how to represent call progress message %d\n", res);
01437          }
01438       }
01439    }
01440 
01441 done:
01442    /* Mute fragment of the frame */
01443    for (x = 0; x < dsp->mute_fragments; x++) {
01444       memset(shortdata + dsp->mute_data[x].start, 0, sizeof(int16_t) * (dsp->mute_data[x].end - dsp->mute_data[x].start));
01445    }
01446 
01447    switch (af->subclass) {
01448    case AST_FORMAT_SLINEAR:
01449       break;
01450    case AST_FORMAT_ULAW:
01451       for (x = 0; x < len; x++)
01452          odata[x] = AST_LIN2MU((unsigned short) shortdata[x]);
01453       break;
01454    case AST_FORMAT_ALAW:
01455       for (x = 0; x < len; x++)
01456          odata[x] = AST_LIN2A((unsigned short) shortdata[x]);
01457       break;
01458    }
01459 
01460    if (outf) {
01461       if (chan) 
01462          ast_queue_frame(chan, af);
01463       ast_frfree(af);
01464       return ast_frisolate(outf);
01465    } else {
01466       return af;
01467    }
01468 }

int ast_dsp_reload ( void   ) 

Reloads dsp settings from dsp.conf.

Since:
1.6.1

Definition at line 1698 of file dsp.c.

References _dsp_init().

01699 {
01700    return _dsp_init(1);
01701 }

void ast_dsp_reset ( struct ast_dsp dsp  ) 

Reset total silence count.

Definition at line 1600 of file dsp.c.

References ast_dsp::freqs, ast_dsp::gsamps, ast_dsp::historicnoise, ast_dsp::historicsilence, ast_dsp::ringtimeout, ast_dsp::totalsilence, goertzel_state_t::v2, and goertzel_state_t::v3.

01601 {
01602    int x;
01603    
01604    dsp->totalsilence = 0;
01605    dsp->gsamps = 0;
01606    for (x=0;x<4;x++)
01607       dsp->freqs[x].v2 = dsp->freqs[x].v3 = 0.0;
01608    memset(dsp->historicsilence, 0, sizeof(dsp->historicsilence));
01609    memset(dsp->historicnoise, 0, sizeof(dsp->historicnoise));  
01610    dsp->ringtimeout= 0;
01611 }

void ast_dsp_set_busy_compare ( struct ast_dsp dsp,
int  compare 
)

Set if silence and noice lengths must be compared for busy.

Definition at line 1549 of file dsp.c.

References ast_dsp::busycompare.

Referenced by dahdi_new().

01550 {
01551   if (compare > 0)
01552       dsp->busycompare = 1;
01553   else
01554       dsp->busycompare = 0;
01555 }

void ast_dsp_set_busy_count ( struct ast_dsp dsp,
int  cadences 
)

Set number of required cadences for busy.

Definition at line 1540 of file dsp.c.

References ast_dsp::busycount, and DSP_HISTORY.

Referenced by dahdi_new().

01541 {
01542    if (cadences < 4)
01543       cadences = 4;
01544    if (cadences > DSP_HISTORY)
01545       cadences = DSP_HISTORY;
01546    dsp->busycount = cadences;
01547 }

void ast_dsp_set_busy_pattern ( struct ast_dsp dsp,
int  tonelength,
int  quietlength,
int  fuzzy 
)

Set expected lengths of the busy tones.

Definition at line 1557 of file dsp.c.

References ast_debug, ast_dsp::busy_pattern_fuzzy, ast_dsp::busy_quietlength, ast_dsp::busy_tonelength, and ast_dsp::busytoneonly.

Referenced by dahdi_new().

01558 {
01559    dsp->busy_tonelength = tonelength;
01560    if (quietlength > 0)
01561       dsp->busy_quietlength = quietlength;
01562    else 
01563      dsp->busytoneonly = 1;
01564    ast_debug(1, "dsp busy pattern set to %d,%d\n", tonelength, quietlength);
01565    if( fuzzy > 0 && fuzzy < 50 ) 
01566       dsp->busy_pattern_fuzzy = fuzzy;
01567 }

int ast_dsp_set_call_progress_zone ( struct ast_dsp dsp,
char *  zone 
)

Set zone for doing progress detection.

Definition at line 1637 of file dsp.c.

References aliases, ARRAY_LEN, ast_dsp_prog_reset(), progalias::mode, name, and ast_dsp::progmode.

Referenced by dahdi_new().

01638 {
01639    int x;
01640    
01641    for (x = 0; x < ARRAY_LEN(aliases); x++) {
01642       if (!strcasecmp(aliases[x].name, zone)) {
01643          dsp->progmode = aliases[x].mode;
01644          ast_dsp_prog_reset(dsp);
01645          return 0;
01646       }
01647    }
01648    return -1;
01649 }

int ast_dsp_set_digitmode ( struct ast_dsp dsp,
int  digitmode 
)

Set digit mode.

Version:
1.6.1 renamed from ast_dsp_digitmode to ast_dsp_set_digitmode

Definition at line 1613 of file dsp.c.

References ast_digit_detect_init(), ast_dsp::digit_state, ast_dsp::digitmode, DSP_DIGITMODE_DTMF, DSP_DIGITMODE_MUTECONF, and DSP_DIGITMODE_MUTEMAX.

Referenced by dahdi_hangup(), dahdi_new(), dahdi_setoption(), mgcp_new(), sip_new(), ss_thread(), and store_config().

01614 {
01615    int new;
01616    int old;
01617    
01618    old = dsp->digitmode & (DSP_DIGITMODE_DTMF | DSP_DIGITMODE_MF | DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX);
01619    new = digitmode & (DSP_DIGITMODE_DTMF | DSP_DIGITMODE_MF | DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX);
01620    if (old != new) {
01621       /* Must initialize structures if switching from MF to DTMF or vice-versa */
01622       ast_digit_detect_init(&dsp->digit_state, new & DSP_DIGITMODE_MF);
01623    }
01624    dsp->digitmode = digitmode;
01625    return 0;
01626 }

int ast_dsp_set_faxmode ( struct ast_dsp dsp,
int  faxmode 
)

Set fax mode.

Definition at line 1628 of file dsp.c.

References ast_fax_detect_init(), and ast_dsp::faxmode.

01629 {
01630    if (dsp->faxmode != faxmode) {
01631       ast_fax_detect_init(dsp);
01632    }
01633    dsp->faxmode = faxmode;
01634    return 0;
01635 }

void ast_dsp_set_features ( struct ast_dsp dsp,
int  features 
)

Select feature set.

Definition at line 1522 of file dsp.c.

References ast_dsp::features.

Referenced by __oh323_new(), dahdi_new(), disable_dtmf_detect(), enable_dtmf_detect(), mgcp_new(), misdn_set_opt_exec(), read_config(), sip_dtmfmode(), sip_new(), sip_rtp_read(), and store_config().

01523 {
01524    dsp->features = features;
01525 }

void ast_dsp_set_threshold ( struct ast_dsp dsp,
int  threshold 
)

Set threshold value for silence.

Definition at line 1532 of file dsp.c.

References ast_dsp::threshold.

Referenced by __ast_play_and_record(), dahdi_new(), do_waiting(), handle_recordfile(), and isAnsweringMachine().

01533 {
01534    if (threshold < 256)
01535      dsp->threshold = 256;
01536    else
01537      dsp->threshold = threshold;
01538 }

int ast_dsp_silence ( struct ast_dsp dsp,
struct ast_frame f,
int *  totalsilence 
)

Return non-zero if this is silence. Updates "totalsilence" with the total number of seconds of silence.

Definition at line 1249 of file dsp.c.

References __ast_dsp_silence_noise(), AST_FORMAT_SLINEAR, AST_FRAME_VOICE, ast_log(), f, len(), LOG_WARNING, and s.

Referenced by __ast_play_and_record(), background_detect_exec(), conf_run(), do_waiting(), handle_recordfile(), and isAnsweringMachine().

01250 {
01251    short *s;
01252    int len;
01253    
01254    if (f->frametype != AST_FRAME_VOICE) {
01255       ast_log(LOG_WARNING, "Can't calculate silence on a non-voice frame\n");
01256       return 0;
01257    }
01258    if (f->subclass != AST_FORMAT_SLINEAR) {
01259       ast_log(LOG_WARNING, "Can only calculate silence on signed-linear frames :(\n");
01260       return 0;
01261    }
01262    s = f->data.ptr;
01263    len = f->datalen/2;
01264    return __ast_dsp_silence_noise(dsp, s, len, totalsilence, NULL);
01265 }

int ast_dsp_was_muted ( struct ast_dsp dsp  ) 

Returns true if DSP code was muting any fragment of the last processed frame. Muting (squelching) happens when DSP code removes DTMF/MF/generic tones from the audio.

Since:
1.6.1

Definition at line 1651 of file dsp.c.

References ast_dsp::mute_fragments.

Referenced by dahdi_read().

01652 {
01653    return (dsp->mute_fragments > 0);
01654 }


Generated on Wed Aug 18 22:34:22 2010 for Asterisk - the Open Source PBX by  doxygen 1.4.7