Thu May 24 14:22:26 2007

Asterisk developer's documentation


dsp.h File Reference

Convenient Signal Processing routines. More...

This graph shows which files directly or indirectly include this file:

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_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_DTMF_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

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.
int ast_dsp_digitmode (struct ast_dsp *dsp, int digitmode)
 Set digit mode.
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_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.
ast_dspast_dsp_new (void)
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.
void ast_dsp_reset (struct ast_dsp *dsp)
 Reset total silence count.
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)
 Set expected lengths of the busy tone.
int ast_dsp_set_call_progress_zone (struct ast_dsp *dsp, char *zone)
 Set zone for doing progress detection.
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.


Detailed Description

Convenient Signal Processing routines.

Definition in file dsp.h.


Define Documentation

#define DSP_DIGITMODE_DTMF   0

Definition at line 31 of file dsp.h.

Referenced by ast_dsp_digitmode(), i4l_answer(), i4l_startrec(), mkif(), sip_new(), ss_thread(), zt_hangup(), zt_new(), and zt_setoption().

#define DSP_DIGITMODE_MF   1

Definition at line 32 of file dsp.h.

Referenced by __ast_dsp_digitdetect(), ast_dsp_digitmode(), ast_dsp_digitreset(), ast_dsp_getdigits(), ast_dsp_process(), and ss_thread().

#define DSP_DIGITMODE_MUTECONF   (1 << 9)

Definition at line 35 of file dsp.h.

Referenced by ast_dsp_digitmode(), ast_dsp_process(), and zt_setoption().

#define DSP_DIGITMODE_MUTEMAX   (1 << 10)

Definition at line 36 of file dsp.h.

Referenced by ast_dsp_digitmode(), ast_dsp_process(), and zt_setoption().

#define DSP_DIGITMODE_NOQUELCH   (1 << 8)

Definition at line 34 of file dsp.h.

Referenced by dtmf_detect(), mf_detect(), and mgcp_new().

#define DSP_DIGITMODE_RELAXDTMF   (1 << 11)

Definition at line 37 of file dsp.h.

Referenced by __ast_dsp_digitdetect(), mkif(), setup_zap(), sip_new(), and zt_setoption().

#define DSP_FEATURE_BUSY_DETECT   (1 << 1)

Definition at line 27 of file dsp.h.

Referenced by ast_dsp_process(), and zt_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 __ast_dsp_call_progress(), ast_dsp_process(), and zt_new().

#define DSP_FEATURE_DTMF_DETECT   (1 << 3)

Definition at line 28 of file dsp.h.

Referenced by __oh323_new(), ast_dsp_process(), disable_dtmf_detect(), enable_dtmf_detect(), i4l_answer(), i4l_startrec(), mgcp_new(), misdn_set_opt_exec(), mkif(), sip_dtmfmode(), sip_new(), and zt_new().

#define DSP_FEATURE_FAX_DETECT   (1 << 4)

Definition at line 29 of file dsp.h.

Referenced by __ast_dsp_digitdetect(), i4l_answer(), i4l_startrec(), misdn_set_opt_exec(), and zt_new().

#define DSP_FEATURE_SILENCE_SUPPRESS   (1 << 0)

Definition at line 26 of file dsp.h.

Referenced by ast_dsp_new(), and ast_dsp_process().

#define DSP_PROGRESS_BUSY   (1 << 18)

Definition at line 41 of file dsp.h.

Referenced by __ast_dsp_call_progress().

#define DSP_PROGRESS_CONGESTION   (1 << 19)

Definition at line 42 of file dsp.h.

Referenced by __ast_dsp_call_progress().

#define DSP_PROGRESS_RINGING   (1 << 17)

Definition at line 40 of file dsp.h.

Referenced by __ast_dsp_call_progress().

#define DSP_PROGRESS_TALK   (1 << 16)

Definition at line 39 of file dsp.h.

Referenced by __ast_dsp_call_progress(), and zt_new().

#define DSP_TONE_STATE_BUSY   4

Definition at line 49 of file dsp.h.

Referenced by __ast_dsp_call_progress().

#define DSP_TONE_STATE_DIALTONE   2

Definition at line 47 of file dsp.h.

Referenced by __ast_dsp_call_progress().

#define DSP_TONE_STATE_HUNGUP   8

Definition at line 53 of file dsp.h.

Referenced by __ast_dsp_call_progress().

#define DSP_TONE_STATE_RINGING   1

Definition at line 46 of file dsp.h.

Referenced by __ast_dsp_call_progress().

#define DSP_TONE_STATE_SILENCE   0

Definition at line 45 of file dsp.h.

Referenced by __ast_dsp_call_progress().

#define DSP_TONE_STATE_SPECIAL1   5

Definition at line 50 of file dsp.h.

Referenced by __ast_dsp_call_progress().

#define DSP_TONE_STATE_SPECIAL2   6

Definition at line 51 of file dsp.h.

Referenced by __ast_dsp_call_progress().

#define DSP_TONE_STATE_SPECIAL3   7

Definition at line 52 of file dsp.h.

Referenced by __ast_dsp_call_progress().

#define DSP_TONE_STATE_TALKING   3

Definition at line 48 of file dsp.h.

Referenced by __ast_dsp_call_progress().


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 1213 of file dsp.c.

References ast_dsp_busydetect(), ast_log(), BUSY_MAX, BUSY_MIN, BUSY_PAT_PERCENT, BUSY_PERCENT, ast_dsp::busy_quietlength, BUSY_THRESHOLD, ast_dsp::busy_tonelength, ast_dsp::busycount, ast_dsp::busymaybe, dsp, DSP_HISTORY, ast_dsp::historicnoise, ast_dsp::historicsilence, LOG_DEBUG, and LOG_NOTICE.

Referenced by ast_dsp_busydetect(), and ast_dsp_process().

01214 {
01215    int res = 0, x;
01216 #ifndef BUSYDETECT_TONEONLY
01217    int avgsilence = 0, hitsilence = 0;
01218 #endif
01219    int avgtone = 0, hittone = 0;
01220    if (!dsp->busymaybe)
01221       return res;
01222    for (x=DSP_HISTORY - dsp->busycount;x<DSP_HISTORY;x++) {
01223 #ifndef BUSYDETECT_TONEONLY
01224       avgsilence += dsp->historicsilence[x];
01225 #endif
01226       avgtone += dsp->historicnoise[x];
01227    }
01228 #ifndef BUSYDETECT_TONEONLY
01229    avgsilence /= dsp->busycount;
01230 #endif
01231    avgtone /= dsp->busycount;
01232    for (x=DSP_HISTORY - dsp->busycount;x<DSP_HISTORY;x++) {
01233 #ifndef BUSYDETECT_TONEONLY
01234       if (avgsilence > dsp->historicsilence[x]) {
01235          if (avgsilence - (avgsilence*BUSY_PERCENT/100) <= dsp->historicsilence[x])
01236             hitsilence++;
01237       } else {
01238          if (avgsilence + (avgsilence*BUSY_PERCENT/100) >= dsp->historicsilence[x])
01239             hitsilence++;
01240       }
01241 #endif
01242       if (avgtone > dsp->historicnoise[x]) {
01243          if (avgtone - (avgtone*BUSY_PERCENT/100) <= dsp->historicnoise[x])
01244             hittone++;
01245       } else {
01246          if (avgtone + (avgtone*BUSY_PERCENT/100) >= dsp->historicnoise[x])
01247             hittone++;
01248       }
01249    }
01250 #ifndef BUSYDETECT_TONEONLY
01251    if ((hittone >= dsp->busycount - 1) && (hitsilence >= dsp->busycount - 1) && 
01252        (avgtone >= BUSY_MIN && avgtone <= BUSY_MAX) && 
01253        (avgsilence >= BUSY_MIN && avgsilence <= BUSY_MAX)) {
01254 #else
01255    if ((hittone >= dsp->busycount - 1) && (avgtone >= BUSY_MIN && avgtone <= BUSY_MAX)) {
01256 #endif
01257 #ifdef BUSYDETECT_COMPARE_TONE_AND_SILENCE
01258 #ifdef BUSYDETECT_TONEONLY
01259 #error You cant use BUSYDETECT_TONEONLY together with BUSYDETECT_COMPARE_TONE_AND_SILENCE
01260 #endif
01261       if (avgtone > avgsilence) {
01262          if (avgtone - avgtone*BUSY_PERCENT/100 <= avgsilence)
01263             res = 1;
01264       } else {
01265          if (avgtone + avgtone*BUSY_PERCENT/100 >= avgsilence)
01266             res = 1;
01267       }
01268 #else
01269       res = 1;
01270 #endif
01271    }
01272    /* If we know the expected busy tone length, check we are in the range */
01273    if (res && (dsp->busy_tonelength > 0)) {
01274       if (abs(avgtone - dsp->busy_tonelength) > (dsp->busy_tonelength*BUSY_PAT_PERCENT/100)) {
01275 #if 0
01276          ast_log(LOG_NOTICE, "busy detector: avgtone of %d not close enough to desired %d\n",
01277                   avgtone, dsp->busy_tonelength);
01278 #endif
01279          res = 0;
01280       }
01281    }
01282 #ifndef BUSYDETECT_TONEONLY
01283    /* If we know the expected busy tone silent-period length, check we are in the range */
01284    if (res && (dsp->busy_quietlength > 0)) {
01285       if (abs(avgsilence - dsp->busy_quietlength) > (dsp->busy_quietlength*BUSY_PAT_PERCENT/100)) {
01286 #if 0
01287          ast_log(LOG_NOTICE, "busy detector: avgsilence of %d not close enough to desired %d\n",
01288                   avgsilence, dsp->busy_quietlength);
01289 #endif
01290          res = 0;
01291       }
01292    }
01293 #endif
01294 #if 1
01295    if (res)
01296       ast_log(LOG_DEBUG, "ast_dsp_busydetect detected busy, avgtone: %d, avgsilence %d\n", avgtone, avgsilence);
01297 #endif
01298    return res;
01299 }

int ast_dsp_call_progress ( struct ast_dsp dsp,
struct ast_frame inf 
)

Scans for progress indication in audio.

Definition at line 1144 of file dsp.c.

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

01145 {
01146    if (inf->frametype != AST_FRAME_VOICE) {
01147       ast_log(LOG_WARNING, "Can't check call progress of non-voice frames\n");
01148       return 0;
01149    }
01150    if (inf->subclass != AST_FORMAT_SLINEAR) {
01151       ast_log(LOG_WARNING, "Can only check call progress in signed-linear frames\n");
01152       return 0;
01153    }
01154    return __ast_dsp_call_progress(dsp, inf->data, inf->datalen / 2);
01155 }

int ast_dsp_digitdetect ( struct ast_dsp dsp,
struct ast_frame f 
)

Return non-zero if DTMF hit was found.

Definition at line 967 of file dsp.c.

References __ast_dsp_digitdetect(), AST_FORMAT_SLINEAR, AST_FRAME_VOICE, ast_log(), ast_frame::data, ast_frame::datalen, dsp, ast_frame::frametype, LOG_WARNING, s, and ast_frame::subclass.

00968 {
00969    short *s;
00970    int len;
00971    int ign=0;
00972 
00973    if (inf->frametype != AST_FRAME_VOICE) {
00974       ast_log(LOG_WARNING, "Can't check call progress of non-voice frames\n");
00975       return 0;
00976    }
00977    if (inf->subclass != AST_FORMAT_SLINEAR) {
00978       ast_log(LOG_WARNING, "Can only check call progress in signed-linear frames\n");
00979       return 0;
00980    }
00981    s = inf->data;
00982    len = inf->datalen / 2;
00983    return __ast_dsp_digitdetect(dsp, s, len, &ign);
00984 }

int ast_dsp_digitmode ( struct ast_dsp dsp,
int  digitmode 
)

Set digit mode.

Definition at line 1686 of file dsp.c.

References ast_dtmf_detect_init(), ast_mf_detect_init(), ast_dsp::digitmode, dsp, DSP_DIGITMODE_DTMF, DSP_DIGITMODE_MF, DSP_DIGITMODE_MUTECONF, DSP_DIGITMODE_MUTEMAX, ast_dsp::dtmf, ast_dsp::mf, and ast_dsp::td.

Referenced by i4l_answer(), i4l_startrec(), mgcp_new(), mkif(), sip_new(), ss_thread(), zt_hangup(), zt_new(), and zt_setoption().

01687 {
01688    int new;
01689    int old;
01690    
01691    old = dsp->digitmode & (DSP_DIGITMODE_DTMF | DSP_DIGITMODE_MF | DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX);
01692    new = digitmode & (DSP_DIGITMODE_DTMF | DSP_DIGITMODE_MF | DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX);
01693    if (old != new) {
01694       /* Must initialize structures if switching from MF to DTMF or vice-versa */
01695       if (new & DSP_DIGITMODE_MF)
01696          ast_mf_detect_init(&dsp->td.mf);
01697       else
01698          ast_dtmf_detect_init(&dsp->td.dtmf);
01699    }
01700    dsp->digitmode = digitmode;
01701    return 0;
01702 }

void ast_dsp_digitreset ( struct ast_dsp dsp  ) 

Reset DTMF detector.

Definition at line 1624 of file dsp.c.

References dtmf_detect_state_t::col_out, dtmf_detect_state_t::current_digits, mf_detect_state_t::current_digits, dtmf_detect_state_t::current_sample, mf_detect_state_t::current_sample, ast_dsp::digitmode, dtmf_detect_state_t::digits, mf_detect_state_t::digits, dsp, DSP_DIGITMODE_MF, ast_dsp::dtmf, dtmf_detect_state_t::energy, dtmf_detect_state_t::fax_tone, goertzel_reset(), dtmf_detect_state_t::hits, mf_detect_state_t::hits, ast_dsp::mf, dtmf_detect_state_t::mhit, mf_detect_state_t::mhit, dtmf_detect_state_t::row_out, ast_dsp::td, ast_dsp::thinkdigit, and mf_detect_state_t::tone_out.

Referenced by ss_thread().

01625 {
01626    int i;
01627    
01628    dsp->thinkdigit = 0;
01629    if (dsp->digitmode & DSP_DIGITMODE_MF) {
01630       memset(dsp->td.mf.digits, 0, sizeof(dsp->td.mf.digits));
01631       dsp->td.mf.current_digits = 0;
01632       /* Reinitialise the detector for the next block */
01633       for (i = 0;  i < 6;  i++) {
01634          goertzel_reset(&dsp->td.mf.tone_out[i]);
01635 #ifdef OLD_DSP_ROUTINES
01636          goertzel_reset(&dsp->td.mf.tone_out2nd[i]);
01637 #endif         
01638       }
01639 #ifdef OLD_DSP_ROUTINES
01640       dsp->td.mf.energy = 0.0;
01641       dsp->td.mf.hit1 = dsp->td.mf.hit2 = dsp->td.mf.hit3 = dsp->td.mf.hit4 = dsp->td.mf.mhit = 0;
01642 #else
01643       dsp->td.mf.hits[4] = dsp->td.mf.hits[3] = dsp->td.mf.hits[2] = dsp->td.mf.hits[1] = dsp->td.mf.hits[0] = dsp->td.mf.mhit = 0;
01644 #endif      
01645       dsp->td.mf.current_sample = 0;
01646    } else {
01647       memset(dsp->td.dtmf.digits, 0, sizeof(dsp->td.dtmf.digits));
01648       dsp->td.dtmf.current_digits = 0;
01649       /* Reinitialise the detector for the next block */
01650       for (i = 0;  i < 4;  i++) {
01651          goertzel_reset(&dsp->td.dtmf.row_out[i]);
01652          goertzel_reset(&dsp->td.dtmf.col_out[i]);
01653 #ifdef OLD_DSP_ROUTINES
01654          goertzel_reset(&dsp->td.dtmf.row_out2nd[i]);
01655          goertzel_reset(&dsp->td.dtmf.col_out2nd[i]);
01656 #endif         
01657       }
01658 #ifdef FAX_DETECT
01659       goertzel_reset (&dsp->td.dtmf.fax_tone);
01660 #endif
01661 #ifdef OLD_DSP_ROUTINES
01662 #ifdef FAX_DETECT
01663       goertzel_reset (&dsp->td.dtmf.fax_tone2nd);
01664 #endif
01665       dsp->td.dtmf.hit1 = dsp->td.dtmf.hit2 = dsp->td.dtmf.hit3 = dsp->td.dtmf.hit4 = dsp->td.dtmf.mhit = 0;
01666 #else
01667       dsp->td.dtmf.hits[2] = dsp->td.dtmf.hits[1] = dsp->td.dtmf.hits[0] =  dsp->td.dtmf.mhit = 0;
01668 #endif      
01669       dsp->td.dtmf.energy = 0.0;
01670       dsp->td.dtmf.current_sample = 0;
01671    }
01672 }

void ast_dsp_free ( struct ast_dsp dsp  ) 

Definition at line 1598 of file dsp.c.

References dsp, and free.

Referenced by __oh323_destroy(), ast_app_getvoice(), ast_play_and_prepend(), ast_play_and_record_full(), background_detect_exec(), cl_dequeue_chan(), cleanup_connection(), do_waiting(), handle_recordfile(), i4l_hangup(), mgcp_hangup(), sip_dtmfmode(), sip_hangup(), ss_thread(), vpb_hangup(), and zt_hangup().

01599 {
01600    free(dsp);
01601 }

int ast_dsp_get_tcount ( struct ast_dsp dsp  ) 

Get tcount (Threshold counter).

Definition at line 1723 of file dsp.c.

References dsp, and ast_dsp::tcount.

01724 {
01725    return dsp->tcount;
01726 }

int ast_dsp_get_tstate ( struct ast_dsp dsp  ) 

Get tstate (Tone State).

Definition at line 1718 of file dsp.c.

References dsp, and ast_dsp::tstate.

01719 {
01720    return dsp->tstate;
01721 }

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

Get pending DTMF/MF digits.

Definition at line 1006 of file dsp.c.

References dtmf_detect_state_t::current_digits, mf_detect_state_t::current_digits, ast_dsp::digitmode, dtmf_detect_state_t::digits, mf_detect_state_t::digits, dsp, DSP_DIGITMODE_MF, ast_dsp::dtmf, ast_dsp::mf, and ast_dsp::td.

01007 {
01008    if (dsp->digitmode & DSP_DIGITMODE_MF) {
01009       if (max > dsp->td.mf.current_digits)
01010          max = dsp->td.mf.current_digits;
01011       if (max > 0) {
01012          memcpy(buf, dsp->td.mf.digits, max);
01013          memmove(dsp->td.mf.digits, dsp->td.mf.digits + max, dsp->td.mf.current_digits - max);
01014          dsp->td.mf.current_digits -= max;
01015       }
01016       buf[max] = '\0';
01017       return  max;
01018    } else {
01019       if (max > dsp->td.dtmf.current_digits)
01020          max = dsp->td.dtmf.current_digits;
01021       if (max > 0) {
01022          memcpy (buf, dsp->td.dtmf.digits, max);
01023          memmove (dsp->td.dtmf.digits, dsp->td.dtmf.digits + max, dsp->td.dtmf.current_digits - max);
01024          dsp->td.dtmf.current_digits -= max;
01025       }
01026       buf[max] = '\0';
01027       return  max;
01028    }
01029 }

struct ast_dsp* ast_dsp_new ( void   ) 

Definition at line 1575 of file dsp.c.

References ast_dsp_prog_reset(), ast_dtmf_detect_init(), DEFAULT_THRESHOLD, dsp, DSP_FEATURE_SILENCE_SUPPRESS, DSP_HISTORY, and malloc.

Referenced by __oh323_new(), ast_app_getvoice(), ast_play_and_prepend(), ast_play_and_record_full(), background_detect_exec(), conf_run(), do_waiting(), handle_recordfile(), i4l_answer(), i4l_startrec(), mgcp_new(), misdn_set_opt_exec(), mkif(), sip_dtmfmode(), sip_new(), and zt_new().

01576 {
01577    struct ast_dsp *dsp;
01578 
01579    dsp = malloc(sizeof(struct ast_dsp));
01580    if (dsp) {
01581       memset(dsp, 0, sizeof(struct ast_dsp));
01582       dsp->threshold = DEFAULT_THRESHOLD;
01583       dsp->features = DSP_FEATURE_SILENCE_SUPPRESS;
01584       dsp->busycount = DSP_HISTORY;
01585       /* Initialize DTMF detector */
01586       ast_dtmf_detect_init(&dsp->td.dtmf);
01587       /* Initialize initial DSP progress detect parameters */
01588       ast_dsp_prog_reset(dsp);
01589    }
01590    return dsp;
01591 }

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 1365 of file dsp.c.

References __ast_dsp_call_progress(), __ast_dsp_digitdetect(), __ast_dsp_silence(), ast_channel::_softhangup, AST_ALAW, AST_CONTROL_ANSWER, AST_CONTROL_BUSY, AST_CONTROL_CONGESTION, AST_CONTROL_HANGUP, AST_CONTROL_RINGING, ast_dsp_busydetect(), AST_FORMAT_ALAW, AST_FORMAT_SLINEAR, AST_FORMAT_ULAW, AST_FRAME_CONTROL, AST_FRAME_DTMF, AST_FRAME_NULL, AST_FRAME_VOICE, ast_frfree(), ast_getformatname(), ast_log(), AST_MULAW, ast_queue_frame(), AST_SOFTHANGUP_DEV, dtmf_detect_state_t::current_digits, mf_detect_state_t::current_digits, ast_frame::data, ast_frame::datalen, ast_dsp::digitmode, dtmf_detect_state_t::digits, mf_detect_state_t::digits, dsp, DSP_DIGITMODE_MF, DSP_DIGITMODE_MUTECONF, DSP_DIGITMODE_MUTEMAX, DSP_FEATURE_BUSY_DETECT, DSP_FEATURE_CALL_PROGRESS, DSP_FEATURE_DTMF_DETECT, DSP_FEATURE_SILENCE_SUPPRESS, ast_dsp::dtmf, ast_dsp::f, ast_dsp::features, FIX_INF, ast_frame::frametype, LOG_DEBUG, LOG_WARNING, ast_dsp::mf, ast_channel::name, silence, ast_frame::src, ast_frame::subclass, ast_dsp::td, and ast_dsp::thinkdigit.

Referenced by do_chanreads(), mgcp_rtp_read(), oh323_rtp_read(), process_ast_dsp(), sip_rtp_read(), and zt_read().

01366 {
01367    int silence;
01368    int res;
01369    int digit;
01370    int x;
01371    short *shortdata;
01372    unsigned char *odata;
01373    int len;
01374    int writeback = 0;
01375 
01376 #define FIX_INF(inf) do { \
01377       if (writeback) { \
01378          switch(inf->subclass) { \
01379          case AST_FORMAT_SLINEAR: \
01380             break; \
01381          case AST_FORMAT_ULAW: \
01382             for (x=0;x<len;x++) \
01383                odata[x] = AST_LIN2MU((unsigned short)shortdata[x]); \
01384             break; \
01385          case AST_FORMAT_ALAW: \
01386             for (x=0;x<len;x++) \
01387                odata[x] = AST_LIN2A((unsigned short)shortdata[x]); \
01388             break; \
01389          } \
01390       } \
01391    } while(0) 
01392 
01393    if (!af)
01394       return NULL;
01395    if (af->frametype != AST_FRAME_VOICE)
01396       return af;
01397    odata = af->data;
01398    len = af->datalen;
01399    /* Make sure we have short data */
01400    switch(af->subclass) {
01401    case AST_FORMAT_SLINEAR:
01402       shortdata = af->data;
01403       len = af->datalen / 2;
01404       break;
01405    case AST_FORMAT_ULAW:
01406       shortdata = alloca(af->datalen * 2);
01407       if (!shortdata) {
01408          ast_log(LOG_WARNING, "Unable to allocate stack space for data: %s\n", strerror(errno));
01409          return af;
01410       }
01411       for (x=0;x<len;x++) 
01412          shortdata[x] = AST_MULAW(odata[x]);
01413       break;
01414    case AST_FORMAT_ALAW:
01415       shortdata = alloca(af->datalen * 2);
01416       if (!shortdata) {
01417          ast_log(LOG_WARNING, "Unable to allocate stack space for data: %s\n", strerror(errno));
01418          return af;
01419       }
01420       for (x=0;x<len;x++) 
01421          shortdata[x] = AST_ALAW(odata[x]);
01422       break;
01423    default:
01424       ast_log(LOG_WARNING, "Inband DTMF is not supported on codec %s. Use RFC2833\n", ast_getformatname(af->subclass));
01425       return af;
01426    }
01427    silence = __ast_dsp_silence(dsp, shortdata, len, NULL);
01428    if ((dsp->features & DSP_FEATURE_SILENCE_SUPPRESS) && silence) {
01429       memset(&dsp->f, 0, sizeof(dsp->f));
01430       dsp->f.frametype = AST_FRAME_NULL;
01431       return &dsp->f;
01432    }
01433    if ((dsp->features & DSP_FEATURE_BUSY_DETECT) && ast_dsp_busydetect(dsp)) {
01434       chan->_softhangup |= AST_SOFTHANGUP_DEV;
01435       memset(&dsp->f, 0, sizeof(dsp->f));
01436       dsp->f.frametype = AST_FRAME_CONTROL;
01437       dsp->f.subclass = AST_CONTROL_BUSY;
01438       ast_log(LOG_DEBUG, "Requesting Hangup because the busy tone was detected on channel %s\n", chan->name);
01439       return &dsp->f;
01440    }
01441    if ((dsp->features & DSP_FEATURE_DTMF_DETECT)) {
01442       digit = __ast_dsp_digitdetect(dsp, shortdata, len, &writeback);
01443 #if 0
01444       if (digit)
01445          printf("Performing digit detection returned %d, digitmode is %d\n", digit, dsp->digitmode);
01446 #endif         
01447       if (dsp->digitmode & (DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX)) {
01448          if (!dsp->thinkdigit) {
01449             if (digit) {
01450                /* Looks like we might have something.  
01451                 * Request a conference mute for the moment */
01452                memset(&dsp->f, 0, sizeof(dsp->f));
01453                dsp->f.frametype = AST_FRAME_DTMF;
01454                dsp->f.subclass = 'm';
01455                dsp->thinkdigit = 'x';
01456                FIX_INF(af);
01457                if (chan)
01458                   ast_queue_frame(chan, af);
01459                ast_frfree(af);
01460                return &dsp->f;
01461             }
01462          } else {
01463             if (digit) {
01464                /* Thought we saw one last time.  Pretty sure we really have now */
01465                if (dsp->thinkdigit) {
01466                   if ((dsp->thinkdigit != 'x') && (dsp->thinkdigit != digit)) {
01467                      /* If we found a digit, and we're changing digits, go
01468                         ahead and send this one, but DON'T stop confmute because
01469                         we're detecting something else, too... */
01470                      memset(&dsp->f, 0, sizeof(dsp->f));
01471                      dsp->f.frametype = AST_FRAME_DTMF;
01472                      dsp->f.subclass = dsp->thinkdigit;
01473                      FIX_INF(af);
01474                      if (chan)
01475                         ast_queue_frame(chan, af);
01476                      ast_frfree(af);
01477                   }
01478                   dsp->thinkdigit = digit;
01479                   return &dsp->f;
01480                }
01481                dsp->thinkdigit = digit;
01482             } else {
01483                if (dsp->thinkdigit) {
01484                   memset(&dsp->f, 0, sizeof(dsp->f));
01485                   if (dsp->thinkdigit != 'x') {
01486                      /* If we found a digit, send it now */
01487                      dsp->f.frametype = AST_FRAME_DTMF;
01488                      dsp->f.subclass = dsp->thinkdigit;
01489                      dsp->thinkdigit = 0;
01490                   } else {
01491                      dsp->f.frametype = AST_FRAME_DTMF;
01492                      dsp->f.subclass = 'u';
01493                      dsp->thinkdigit = 0;
01494                   }
01495                   FIX_INF(af);
01496                   if (chan)
01497                      ast_queue_frame(chan, af);
01498                   ast_frfree(af);
01499                   return &dsp->f;
01500                }
01501             }
01502          }
01503       } else if (!digit) {
01504          /* Only check when there is *not* a hit... */
01505          if (dsp->digitmode & DSP_DIGITMODE_MF) {
01506             if (dsp->td.mf.current_digits) {
01507                memset(&dsp->f, 0, sizeof(dsp->f));
01508                dsp->f.frametype = AST_FRAME_DTMF;
01509                dsp->f.subclass = dsp->td.mf.digits[0];
01510                memmove(dsp->td.mf.digits, dsp->td.mf.digits + 1, dsp->td.mf.current_digits);
01511                dsp->td.mf.current_digits--;
01512                FIX_INF(af);
01513                if (chan)
01514                   ast_queue_frame(chan, af);
01515                ast_frfree(af);
01516                return &dsp->f;
01517             }
01518          } else {
01519             if (dsp->td.dtmf.current_digits) {
01520                memset(&dsp->f, 0, sizeof(dsp->f));
01521                dsp->f.frametype = AST_FRAME_DTMF;
01522                dsp->f.subclass = dsp->td.dtmf.digits[0];
01523                memmove(dsp->td.dtmf.digits, dsp->td.dtmf.digits + 1, dsp->td.dtmf.current_digits);
01524                dsp->td.dtmf.current_digits--;
01525                FIX_INF(af);
01526                if (chan)
01527                   ast_queue_frame(chan, af);
01528                ast_frfree(af);
01529                return &dsp->f;
01530             }
01531          }
01532       }
01533    }
01534    if ((dsp->features & DSP_FEATURE_CALL_PROGRESS)) {
01535       res = __ast_dsp_call_progress(dsp, shortdata, len);
01536       if (res) {
01537          switch(res) {
01538          case AST_CONTROL_ANSWER:
01539          case AST_CONTROL_BUSY:
01540          case AST_CONTROL_RINGING:
01541          case AST_CONTROL_CONGESTION:
01542          case AST_CONTROL_HANGUP:
01543             memset(&dsp->f, 0, sizeof(dsp->f));
01544             dsp->f.frametype = AST_FRAME_CONTROL;
01545             dsp->f.subclass = res;
01546             dsp->f.src = "dsp_progress";
01547             if (chan) 
01548                ast_queue_frame(chan, &dsp->f);
01549             break;
01550          default:
01551             ast_log(LOG_WARNING, "Don't know how to represent call progress message %d\n", res);
01552          }
01553       }
01554    }
01555    FIX_INF(af);
01556    return af;
01557 }

void ast_dsp_reset ( struct ast_dsp dsp  ) 

Reset total silence count.

Definition at line 1674 of file dsp.c.

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

01675 {
01676    int x;
01677    
01678    dsp->totalsilence = 0;
01679    dsp->gsamps = 0;
01680    for (x=0;x<4;x++)
01681       dsp->freqs[x].v2 = dsp->freqs[x].v3 = 0.0;
01682    memset(dsp->historicsilence, 0, sizeof(dsp->historicsilence));
01683    memset(dsp->historicnoise, 0, sizeof(dsp->historicnoise));  
01684 }

void ast_dsp_set_busy_count ( struct ast_dsp dsp,
int  cadences 
)

Set number of required cadences for busy.

Definition at line 1608 of file dsp.c.

References ast_dsp::busycount, dsp, and DSP_HISTORY.

Referenced by zt_new().

01609 {
01610    if (cadences < 4)
01611       cadences = 4;
01612    if (cadences > DSP_HISTORY)
01613       cadences = DSP_HISTORY;
01614    dsp->busycount = cadences;
01615 }

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

Set expected lengths of the busy tone.

Definition at line 1617 of file dsp.c.

References ast_log(), ast_dsp::busy_quietlength, ast_dsp::busy_tonelength, dsp, and LOG_DEBUG.

Referenced by zt_new().

01618 {
01619    dsp->busy_tonelength = tonelength;
01620    dsp->busy_quietlength = quietlength;
01621    ast_log(LOG_DEBUG, "dsp busy pattern set to %d,%d\n", tonelength, quietlength);
01622 }

int ast_dsp_set_call_progress_zone ( struct ast_dsp dsp,
char *  zone 
)

Set zone for doing progress detection.

Definition at line 1704 of file dsp.c.

References aliases, ast_dsp_prog_reset(), dsp, name, and ast_dsp::progmode.

Referenced by zt_new().

01705 {
01706    int x;
01707    
01708    for (x=0;x<sizeof(aliases) / sizeof(aliases[0]);x++) {
01709       if (!strcasecmp(aliases[x].name, zone)) {
01710          dsp->progmode = aliases[x].mode;
01711          ast_dsp_prog_reset(dsp);
01712          return 0;
01713       }
01714    }
01715    return -1;
01716 }

void ast_dsp_set_features ( struct ast_dsp dsp,
int  features 
)

Select feature set.

Definition at line 1593 of file dsp.c.

References dsp, and ast_dsp::features.

Referenced by __oh323_new(), disable_dtmf_detect(), enable_dtmf_detect(), i4l_answer(), i4l_startrec(), mgcp_new(), misdn_set_opt_exec(), mkif(), sip_dtmfmode(), sip_new(), and zt_new().

01594 {
01595    dsp->features = features;
01596 }

void ast_dsp_set_threshold ( struct ast_dsp dsp,
int  threshold 
)

Set threshold value for silence.

Definition at line 1603 of file dsp.c.

References dsp, and ast_dsp::threshold.

Referenced by ast_play_and_prepend(), ast_play_and_record_full(), do_waiting(), and handle_recordfile().

01604 {
01605    dsp->threshold = threshold;
01606 }

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 1347 of file dsp.c.

References __ast_dsp_silence(), AST_FORMAT_SLINEAR, AST_FRAME_VOICE, ast_log(), ast_frame::data, ast_frame::datalen, dsp, ast_frame::frametype, LOG_WARNING, s, and ast_frame::subclass.

Referenced by ast_app_getvoice(), ast_play_and_prepend(), ast_play_and_record_full(), background_detect_exec(), conf_run(), do_waiting(), and handle_recordfile().

01348 {
01349    short *s;
01350    int len;
01351    
01352    if (f->frametype != AST_FRAME_VOICE) {
01353       ast_log(LOG_WARNING, "Can't calculate silence on a non-voice frame\n");
01354       return 0;
01355    }
01356    if (f->subclass != AST_FORMAT_SLINEAR) {
01357       ast_log(LOG_WARNING, "Can only calculate silence on signed-linear frames :(\n");
01358       return 0;
01359    }
01360    s = f->data;
01361    len = f->datalen/2;
01362    return __ast_dsp_silence(dsp, s, len, totalsilence);
01363 }


Generated on Thu May 24 14:22:26 2007 for Asterisk - the Open Source PBX by  doxygen 1.4.7