Wed Mar 3 22:54:12 2010

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_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_frame_freed (struct ast_frame *fr)
 Hint that a frame from a dsp was freed.
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.
struct 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.
struct 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_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.
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(), mkintf(), 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 ast_dsp_digitreset(), ast_dsp_new(), ast_dsp_process(), ast_dsp_set_digitmode(), and 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 ast_dsp_process(), and mgcp_new().

#define DSP_DIGITMODE_RELAXDTMF   (1 << 11)

"Radio" mode (relaxed DTMF)

Definition at line 37 of file dsp.h.

Referenced by ast_dsp_process(), dahdi_setoption(), process_dahdi(), 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.

Referenced by ast_dsp_process().

#define DSP_FAXMODE_DETECT_CNG   (1 << 0)

Definition at line 45 of file dsp.h.

Referenced by ast_dsp_new(), and ast_dsp_process().

#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 __ast_dsp_call_progress(), ast_dsp_process(), and dahdi_new().

#define DSP_FEATURE_DIGIT_DETECT   (1 << 3)
#define DSP_FEATURE_FAX_DETECT   (1 << 4)

Definition at line 29 of file dsp.h.

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

#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)

Enable busy tone detection

Definition at line 41 of file dsp.h.

Referenced by __ast_dsp_call_progress().

#define DSP_PROGRESS_CONGESTION   (1 << 19)

Enable congestion tone detection

Definition at line 42 of file dsp.h.

Referenced by __ast_dsp_call_progress().

#define DSP_PROGRESS_RINGING   (1 << 17)

Enable calling tone detection

Definition at line 40 of file dsp.h.

Referenced by __ast_dsp_call_progress(), and pri_dchannel().

#define DSP_PROGRESS_TALK   (1 << 16)

Enable talk detection

Definition at line 39 of file dsp.h.

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

Referenced by __ast_dsp_call_progress().

#define DSP_TONE_STATE_HUNGUP   8

Definition at line 57 of file dsp.h.

Referenced by __ast_dsp_call_progress().

#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.

Referenced by __ast_dsp_call_progress().

#define DSP_TONE_STATE_SPECIAL2   6

Definition at line 55 of file dsp.h.

Referenced by __ast_dsp_call_progress().

#define DSP_TONE_STATE_SPECIAL3   7

Definition at line 56 of file dsp.h.

Referenced by __ast_dsp_call_progress().

#define DSP_TONE_STATE_TALKING   3

Definition at line 52 of file dsp.h.

Referenced by __ast_dsp_call_progress().


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

References ast_debug, BUSY_MAX, BUSY_MIN, BUSY_PAT_PERCENT, BUSY_PERCENT, ast_dsp::busy_quietlength, ast_dsp::busy_tonelength, ast_dsp::busycount, ast_dsp::busymaybe, DSP_HISTORY, ast_dsp::historicnoise, and ast_dsp::historicsilence.

Referenced by ast_dsp_process().

01145 {
01146    int res = 0, x;
01147 #ifndef BUSYDETECT_TONEONLY
01148    int avgsilence = 0, hitsilence = 0;
01149 #endif
01150    int avgtone = 0, hittone = 0;
01151    if (!dsp->busymaybe)
01152       return res;
01153    for (x=DSP_HISTORY - dsp->busycount;x<DSP_HISTORY;x++) {
01154 #ifndef BUSYDETECT_TONEONLY
01155       avgsilence += dsp->historicsilence[x];
01156 #endif
01157       avgtone += dsp->historicnoise[x];
01158    }
01159 #ifndef BUSYDETECT_TONEONLY
01160    avgsilence /= dsp->busycount;
01161 #endif
01162    avgtone /= dsp->busycount;
01163    for (x=DSP_HISTORY - dsp->busycount;x<DSP_HISTORY;x++) {
01164 #ifndef BUSYDETECT_TONEONLY
01165       if (avgsilence > dsp->historicsilence[x]) {
01166          if (avgsilence - (avgsilence*BUSY_PERCENT/100) <= dsp->historicsilence[x])
01167             hitsilence++;
01168       } else {
01169          if (avgsilence + (avgsilence*BUSY_PERCENT/100) >= dsp->historicsilence[x])
01170             hitsilence++;
01171       }
01172 #endif
01173       if (avgtone > dsp->historicnoise[x]) {
01174          if (avgtone - (avgtone*BUSY_PERCENT/100) <= dsp->historicnoise[x])
01175             hittone++;
01176       } else {
01177          if (avgtone + (avgtone*BUSY_PERCENT/100) >= dsp->historicnoise[x])
01178             hittone++;
01179       }
01180    }
01181 #ifndef BUSYDETECT_TONEONLY
01182    if ((hittone >= dsp->busycount - 1) && (hitsilence >= dsp->busycount - 1) && 
01183        (avgtone >= BUSY_MIN && avgtone <= BUSY_MAX) && 
01184        (avgsilence >= BUSY_MIN && avgsilence <= BUSY_MAX)) {
01185 #else
01186    if ((hittone >= dsp->busycount - 1) && (avgtone >= BUSY_MIN && avgtone <= BUSY_MAX)) {
01187 #endif
01188 #ifdef BUSYDETECT_COMPARE_TONE_AND_SILENCE
01189       if (avgtone > avgsilence) {
01190          if (avgtone - avgtone*BUSY_PERCENT/100 <= avgsilence)
01191             res = 1;
01192       } else {
01193          if (avgtone + avgtone*BUSY_PERCENT/100 >= avgsilence)
01194             res = 1;
01195       }
01196 #else
01197       res = 1;
01198 #endif
01199    }
01200    /* If we know the expected busy tone length, check we are in the range */
01201    if (res && (dsp->busy_tonelength > 0)) {
01202       if (abs(avgtone - dsp->busy_tonelength) > (dsp->busy_tonelength*BUSY_PAT_PERCENT/100)) {
01203 #ifdef BUSYDETECT_DEBUG
01204          ast_debug(5, "busy detector: avgtone of %d not close enough to desired %d\n",
01205             avgtone, dsp->busy_tonelength);
01206 #endif
01207          res = 0;
01208       }
01209    }
01210 #ifndef BUSYDETECT_TONEONLY
01211    /* If we know the expected busy tone silent-period length, check we are in the range */
01212    if (res && (dsp->busy_quietlength > 0)) {
01213       if (abs(avgsilence - dsp->busy_quietlength) > (dsp->busy_quietlength*BUSY_PAT_PERCENT/100)) {
01214 #ifdef BUSYDETECT_DEBUG
01215       ast_debug(5, "busy detector: avgsilence of %d not close enough to desired %d\n",
01216          avgsilence, dsp->busy_quietlength);
01217 #endif
01218          res = 0;
01219       }
01220    }
01221 #endif
01222 #if !defined(BUSYDETECT_TONEONLY) && defined(BUSYDETECT_DEBUG)
01223    if (res) {
01224       ast_debug(5, "ast_dsp_busydetect detected busy, avgtone: %d, avgsilence %d\n", avgtone, avgsilence);
01225    } else {
01226       ast_debug(5, "busy detector: FAILED with avgtone: %d, avgsilence %d\n", avgtone, avgsilence);
01227    }
01228 #endif
01229    return res;
01230 }

int ast_dsp_call_progress ( struct ast_dsp dsp,
struct ast_frame inf 
)

Scans for progress indication in audio.

Definition at line 1074 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.

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

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

References dtmf_detect_state_t::col_out, digit_detect_state_t::current_digits, dtmf_detect_state_t::current_hit, mf_detect_state_t::current_hit, dtmf_detect_state_t::current_sample, mf_detect_state_t::current_sample, ast_dsp::digit_state, ast_dsp::digitmode, digit_detect_state_t::digits, DSP_DIGITMODE_MF, digit_detect_state_t::dtmf, ast_dsp::dtmf_began, dtmf_detect_state_t::energy, goertzel_reset(), dtmf_detect_state_t::hits, mf_detect_state_t::hits, dtmf_detect_state_t::lasthit, digit_detect_state_t::mf, dtmf_detect_state_t::misses, dtmf_detect_state_t::row_out, s, digit_detect_state_t::td, and mf_detect_state_t::tone_out.

Referenced by ss_thread().

01522 {
01523    int i;
01524    
01525    dsp->dtmf_began = 0;
01526    if (dsp->digitmode & DSP_DIGITMODE_MF) {
01527       mf_detect_state_t *s = &dsp->digit_state.td.mf;
01528       /* Reinitialise the detector for the next block */
01529       for (i = 0;  i < 6;  i++) {
01530          goertzel_reset(&s->tone_out[i]);
01531       }
01532       s->hits[4] = s->hits[3] = s->hits[2] = s->hits[1] = s->hits[0] = s->current_hit = 0;
01533       s->current_sample = 0;
01534    } else {
01535       dtmf_detect_state_t *s = &dsp->digit_state.td.dtmf;
01536       /* Reinitialise the detector for the next block */
01537       for (i = 0;  i < 4;  i++) {
01538          goertzel_reset(&s->row_out[i]);
01539          goertzel_reset(&s->col_out[i]);
01540       }
01541       s->lasthit = s->current_hit = 0;
01542       s->energy = 0.0;
01543       s->current_sample = 0;
01544       s->hits = 0;
01545       s->misses = 0;
01546    }
01547 
01548    dsp->digit_state.digits[0] = '\0';
01549    dsp->digit_state.current_digits = 0;
01550 }

void ast_dsp_frame_freed ( struct ast_frame fr  ) 

Hint that a frame from a dsp was freed.

This is called from ast_frame_free if AST_FRFLAG_FROM_DSP is set. This occurs because it is possible for the dsp to be freed while someone still holds a reference to the frame that is in that dsp. This has been known to happen when the dsp on a DAHDI channel detects a busy signal. The channel is hung up, and the application that read the frame to begin with still has a reference to the frame.

Returns:
nothing

Definition at line 1655 of file dsp.c.

References ast_clear_flag, ast_dsp_free(), AST_FRFLAG_FROM_DSP, ast_dsp::destroy, and f.

Referenced by __frame_free().

01656 {
01657    struct ast_dsp *dsp;
01658 
01659    ast_clear_flag(fr, AST_FRFLAG_FROM_DSP);
01660 
01661    dsp = (struct ast_dsp *) (((char *) fr) - offsetof(struct ast_dsp, f));
01662 
01663    if (!dsp->destroy)
01664       return;
01665    
01666    ast_dsp_free(dsp);
01667 }

void ast_dsp_free ( struct ast_dsp dsp  ) 

Definition at line 1485 of file dsp.c.

References ast_free, AST_FRFLAG_FROM_DSP, ast_test_flag, ast_dsp::destroy, and ast_dsp::f.

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

01486 {
01487    if (ast_test_flag(&dsp->f, AST_FRFLAG_FROM_DSP)) {
01488       /* If this flag is still set, that means that the dsp's destruction 
01489        * been torn down, while we still have a frame out there being used.
01490        * When ast_frfree() gets called on that frame, this ast_trans_pvt
01491        * will get destroyed, too. */
01492 
01493       dsp->destroy = 1;
01494 
01495       return;
01496    }
01497    ast_free(dsp);
01498 }

int ast_dsp_get_tcount ( struct ast_dsp dsp  ) 

Get tcount (Threshold counter).

Definition at line 1613 of file dsp.c.

References ast_dsp::tcount.

01614 {
01615    return dsp->tcount;
01616 }

int ast_dsp_get_threshold_from_settings ( enum threshold  which  ) 

Get silence threshold from dsp.conf.

Since:
1.6.1

Definition at line 1640 of file dsp.c.

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

01641 {
01642    return thresholds[which];
01643 }

int ast_dsp_get_tstate ( struct ast_dsp dsp  ) 

Get tstate (Tone State).

Definition at line 1608 of file dsp.c.

References ast_dsp::tstate.

01609 {
01610    return dsp->tstate;
01611 }

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

References _dsp_init().

Referenced by main().

01646 {
01647    return _dsp_init(0);
01648 }

struct ast_dsp* ast_dsp_new ( void   )  [read]
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 1250 of file dsp.c.

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

Referenced by do_waiting().

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

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

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

References __ast_dsp_call_progress(), __ast_dsp_silence_noise(), ast_channel::_softhangup, AST_ALAW, AST_CONTROL_ANSWER, AST_CONTROL_BUSY, AST_CONTROL_CONGESTION, AST_CONTROL_HANGUP, AST_CONTROL_RINGING, ast_debug, ast_dsp_busydetect(), AST_FORMAT_ALAW, AST_FORMAT_SLINEAR, AST_FORMAT_ULAW, AST_FRAME_CONTROL, AST_FRAME_DTMF, AST_FRAME_DTMF_BEGIN, AST_FRAME_DTMF_END, AST_FRAME_NULL, AST_FRAME_VOICE, AST_FRFLAG_FROM_DSP, ast_frfree, ast_getformatname(), AST_LIN2A, AST_LIN2MU, ast_log(), AST_MULAW, ast_queue_frame(), ast_set_flag, AST_SOFTHANGUP_DEV, ast_dsp::ced_tone_state, ast_dsp::cng_tone_state, digit_detect_state_t::current_digits, ast_frame::data, ast_frame::datalen, ast_dsp::digit_state, ast_dsp::digitmode, digit_detect_state_t::digits, DSP_DIGITMODE_MF, DSP_DIGITMODE_NOQUELCH, DSP_DIGITMODE_RELAXDTMF, DSP_FAXMODE_DETECT_CED, DSP_FAXMODE_DETECT_CNG, DSP_FEATURE_BUSY_DETECT, DSP_FEATURE_CALL_PROGRESS, DSP_FEATURE_DIGIT_DETECT, DSP_FEATURE_FAX_DETECT, DSP_FEATURE_SILENCE_SUPPRESS, ast_dsp::dtmf_began, dtmf_detect(), fragment_t::end, ast_dsp::f, ast_dsp::faxmode, ast_dsp::features, ast_frame::frametype, len(), LOG_WARNING, mf_detect(), ast_dsp::mute_data, ast_dsp::mute_fragments, ast_frame::ptr, ast_frame::src, fragment_t::start, ast_frame::subclass, and tone_detect().

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

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

int ast_dsp_reload ( void   ) 

Reloads dsp settings from dsp.conf.

Since:
1.6.1

Definition at line 1650 of file dsp.c.

References _dsp_init().

01651 {
01652    return _dsp_init(1);
01653 }

void ast_dsp_reset ( struct ast_dsp dsp  ) 

Reset total silence count.

Definition at line 1552 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.

01553 {
01554    int x;
01555    
01556    dsp->totalsilence = 0;
01557    dsp->gsamps = 0;
01558    for (x=0;x<4;x++)
01559       dsp->freqs[x].v2 = dsp->freqs[x].v3 = 0.0;
01560    memset(dsp->historicsilence, 0, sizeof(dsp->historicsilence));
01561    memset(dsp->historicnoise, 0, sizeof(dsp->historicnoise));  
01562    dsp->ringtimeout= 0;
01563 }

void ast_dsp_set_busy_count ( struct ast_dsp dsp,
int  cadences 
)

Set number of required cadences for busy.

Definition at line 1505 of file dsp.c.

References ast_dsp::busycount, and DSP_HISTORY.

Referenced by dahdi_new().

01506 {
01507    if (cadences < 4)
01508       cadences = 4;
01509    if (cadences > DSP_HISTORY)
01510       cadences = DSP_HISTORY;
01511    dsp->busycount = cadences;
01512 }

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

Set expected lengths of the busy tone.

Definition at line 1514 of file dsp.c.

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

Referenced by dahdi_new().

01515 {
01516    dsp->busy_tonelength = tonelength;
01517    dsp->busy_quietlength = quietlength;
01518    ast_debug(1, "dsp busy pattern set to %d,%d\n", tonelength, quietlength);
01519 }

int ast_dsp_set_call_progress_zone ( struct ast_dsp dsp,
char *  zone 
)

Set zone for doing progress detection.

Definition at line 1589 of file dsp.c.

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

Referenced by dahdi_new().

01590 {
01591    int x;
01592    
01593    for (x = 0; x < ARRAY_LEN(aliases); x++) {
01594       if (!strcasecmp(aliases[x].name, zone)) {
01595          dsp->progmode = aliases[x].mode;
01596          ast_dsp_prog_reset(dsp);
01597          return 0;
01598       }
01599    }
01600    return -1;
01601 }

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

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

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

01566 {
01567    int new;
01568    int old;
01569    
01570    old = dsp->digitmode & (DSP_DIGITMODE_DTMF | DSP_DIGITMODE_MF | DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX);
01571    new = digitmode & (DSP_DIGITMODE_DTMF | DSP_DIGITMODE_MF | DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX);
01572    if (old != new) {
01573       /* Must initialize structures if switching from MF to DTMF or vice-versa */
01574       ast_digit_detect_init(&dsp->digit_state, new & DSP_DIGITMODE_MF);
01575    }
01576    dsp->digitmode = digitmode;
01577    return 0;
01578 }

int ast_dsp_set_faxmode ( struct ast_dsp dsp,
int  faxmode 
)

Set fax mode.

Definition at line 1580 of file dsp.c.

References ast_fax_detect_init(), and ast_dsp::faxmode.

01581 {
01582    if (dsp->faxmode != faxmode) {
01583       ast_fax_detect_init(dsp);
01584    }
01585    dsp->faxmode = faxmode;
01586    return 0;
01587 }

void ast_dsp_set_features ( struct ast_dsp dsp,
int  features 
)

Select feature set.

Definition at line 1480 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(), pri_dchannel(), read_config(), sip_dtmfmode(), sip_new(), ss7_linkset(), and store_config().

01481 {
01482    dsp->features = features;
01483 }

void ast_dsp_set_threshold ( struct ast_dsp dsp,
int  threshold 
)

Set threshold value for silence.

Definition at line 1500 of file dsp.c.

References ast_dsp::threshold.

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

01501 {
01502    dsp->threshold = threshold;
01503 }

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

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

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

01233 {
01234    short *s;
01235    int len;
01236    
01237    if (f->frametype != AST_FRAME_VOICE) {
01238       ast_log(LOG_WARNING, "Can't calculate silence on a non-voice frame\n");
01239       return 0;
01240    }
01241    if (f->subclass != AST_FORMAT_SLINEAR) {
01242       ast_log(LOG_WARNING, "Can only calculate silence on signed-linear frames :(\n");
01243       return 0;
01244    }
01245    s = f->data.ptr;
01246    len = f->datalen/2;
01247    return __ast_dsp_silence_noise(dsp, s, len, totalsilence, NULL);
01248 }

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

References ast_dsp::mute_fragments.

Referenced by dahdi_read().

01604 {
01605    return (dsp->mute_fragments > 0);
01606 }


Generated on 3 Mar 2010 for Asterisk - the Open Source PBX by  doxygen 1.6.1