Wed Aug 11 01:07:59 2010

Asterisk developer's documentation


dsp.c File Reference

Convenience Signal Processing routines. More...

#include "asterisk.h"
#include <math.h>
#include "asterisk/frame.h"
#include "asterisk/channel.h"
#include "asterisk/dsp.h"
#include "asterisk/ulaw.h"
#include "asterisk/alaw.h"
#include "asterisk/utils.h"
#include "asterisk/options.h"
#include "asterisk/config.h"

Include dependency graph for dsp.c:

Go to the source code of this file.

Data Structures

struct  ast_dsp
struct  digit_detect_state_t
struct  dtmf_detect_state_t
struct  fragment_t
struct  goertzel_result_t
struct  goertzel_state_t
struct  mf_detect_state_t
struct  progalias
struct  progress
struct  tone_detect_state_t

Defines

#define BELL_MF_RELATIVE_PEAK   12.6
#define BELL_MF_THRESHOLD   1.6e9
#define BELL_MF_TWIST   4.0
#define CONFIG_FILE_NAME   "dsp.conf"
#define DEFAULT_THRESHOLD   512
 This value is the minimum threshold, calculated by averaging all of the samples within a frame, for which a frame is determined to either be silence (below the threshold) or noise (above the threshold). Please note that while the default threshold is an even exponent of 2, there is no requirement that it be so. The threshold will accept any value between 0 and 32767.
#define DSP_HISTORY   15
#define DTMF_2ND_HARMONIC_COL   63.1
#define DTMF_2ND_HARMONIC_ROW   (relax ? 1.7 : 2.5)
#define DTMF_GSIZE   102
#define DTMF_HITS_TO_BEGIN   2
#define DTMF_MISSES_TO_END   3
#define DTMF_NORMAL_TWIST   6.3
#define DTMF_RELATIVE_PEAK_COL   6.3
#define DTMF_RELATIVE_PEAK_ROW   6.3
#define DTMF_REVERSE_TWIST   (relax ? 4.0 : 2.5)
#define DTMF_THRESHOLD   8.0e7
#define DTMF_TO_TOTAL_ENERGY   42.0
#define FAX_2ND_HARMONIC   2.0
#define FAX_THRESHOLD   8.0e7
#define FAX_TONE_CED_DB   16
#define FAX_TONE_CED_DURATION   2600
#define FAX_TONE_CED_FREQ   2100
#define FAX_TONE_CNG_DB   16
#define FAX_TONE_CNG_DURATION   500
#define FAX_TONE_CNG_FREQ   1100
#define MAX_DTMF_DIGITS   128
#define MF_GSIZE   120
#define SAMPLE_RATE   8000
#define SAMPLES_IN_FRAME   160
#define TONE_MIN_THRESH   1e8
#define TONE_THRESH   10.0

Enumerations

enum  busy_detect {
  BUSY_PERCENT = 10, BUSY_PAT_PERCENT = 7, BUSY_THRESHOLD = 100, BUSY_MIN = 75,
  BUSY_MAX = 3100
}
enum  freq_index {
  HZ_350 = 0, HZ_440, HZ_480, HZ_620,
  HZ_950, HZ_1400, HZ_1800, HZ_425 = 0,
  HZ_350UK = 0, HZ_400UK, HZ_440UK
}
enum  gsamp_size { GSAMP_SIZE_NA = 183, GSAMP_SIZE_CR = 188, GSAMP_SIZE_UK = 160 }
enum  gsamp_thresh {
  THRESH_RING = 8, THRESH_TALK = 2, THRESH_BUSY = 4, THRESH_CONGESTION = 4,
  THRESH_HANGUP = 60, THRESH_RING2ANSWER = 300
}
enum  prog_mode { PROG_MODE_NA = 0, PROG_MODE_CR, PROG_MODE_UK }

Functions

static int __ast_dsp_call_progress (struct ast_dsp *dsp, short *s, int len)
static int __ast_dsp_silence_noise (struct ast_dsp *dsp, short *s, int len, int *totalsilence, int *totalnoise)
static int _dsp_init (int reload)
static void ast_digit_detect_init (digit_detect_state_t *s, int mf)
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.
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_init (void)
 Load dsp settings from dsp.conf.
struct ast_dsp * ast_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_frame * ast_dsp_process (struct ast_channel *chan, struct ast_dsp *dsp, struct ast_frame *af)
 Return AST_FRAME_NULL frames when there is silence, AST_FRAME_BUSY on busies, and call progress, all dependent upon which features are enabled.
static void ast_dsp_prog_reset (struct ast_dsp *dsp)
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.
static void ast_dtmf_detect_init (dtmf_detect_state_t *s)
static void ast_fax_detect_init (struct ast_dsp *s)
static void ast_mf_detect_init (mf_detect_state_t *s)
static void ast_tone_detect_init (tone_detect_state_t *s, int freq, int duration, int amp)
static int dtmf_detect (struct ast_dsp *dsp, digit_detect_state_t *s, int16_t amp[], int samples, int squelch, int relax)
static void goertzel_init (goertzel_state_t *s, float freq, int samples)
static void goertzel_reset (goertzel_state_t *s)
static float goertzel_result (goertzel_state_t *s)
static void goertzel_sample (goertzel_state_t *s, short sample)
static void goertzel_update (goertzel_state_t *s, short *samps, int count)
static int mf_detect (struct ast_dsp *dsp, digit_detect_state_t *s, int16_t amp[], int samples, int squelch, int relax)
static void mute_fragment (struct ast_dsp *dsp, fragment_t *fragment)
static int pair_there (float p1, float p2, float i1, float i2, float e)
static void store_digit (digit_detect_state_t *s, char digit)
static int tone_detect (struct ast_dsp *dsp, tone_detect_state_t *s, int16_t *amp, int samples)

Variables

static struct progalias aliases []
static char bell_mf_positions [] = "1247C-358A--69*---0B----#"
static const int DEFAULT_SILENCE_THRESHOLD = 256
 The default silence threshold we will use if an alternate configured value is not present or is invalid.
static float dtmf_col []
static char dtmf_positions [] = "123A" "456B" "789C" "*0#D"
static float dtmf_row []
static float mf_tones []
static struct progress modes []
static int thresholds [THRESHOLD_MAX]


Detailed Description

Convenience Signal Processing routines.

Author:
Mark Spencer <markster@digium.com>

Steve Underwood <steveu@coppice.org>

Definition in file dsp.c.


Define Documentation

#define BELL_MF_RELATIVE_PEAK   12.6

Definition at line 174 of file dsp.c.

Referenced by mf_detect().

#define BELL_MF_THRESHOLD   1.6e9

Definition at line 172 of file dsp.c.

Referenced by mf_detect().

#define BELL_MF_TWIST   4.0

Definition at line 173 of file dsp.c.

Referenced by mf_detect().

#define CONFIG_FILE_NAME   "dsp.conf"

Definition at line 222 of file dsp.c.

#define DEFAULT_THRESHOLD   512

This value is the minimum threshold, calculated by averaging all of the samples within a frame, for which a frame is determined to either be silence (below the threshold) or noise (above the threshold). Please note that while the default threshold is an even exponent of 2, there is no requirement that it be so. The threshold will accept any value between 0 and 32767.

Definition at line 117 of file dsp.c.

Referenced by ast_dsp_new().

#define DSP_HISTORY   15

Remember last 15 units

Definition at line 128 of file dsp.c.

Referenced by __ast_dsp_silence_noise(), ast_dsp_busydetect(), ast_dsp_new(), and ast_dsp_set_busy_count().

#define DTMF_2ND_HARMONIC_COL   63.1

Definition at line 169 of file dsp.c.

#define DTMF_2ND_HARMONIC_ROW   (relax ? 1.7 : 2.5)

Definition at line 168 of file dsp.c.

#define DTMF_GSIZE   102

Definition at line 209 of file dsp.c.

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

#define DTMF_HITS_TO_BEGIN   2

Definition at line 212 of file dsp.c.

Referenced by ast_dtmf_detect_init().

#define DTMF_MISSES_TO_END   3

Definition at line 214 of file dsp.c.

Referenced by ast_dtmf_detect_init().

#define DTMF_NORMAL_TWIST   6.3

Definition at line 160 of file dsp.c.

Referenced by dtmf_detect().

#define DTMF_RELATIVE_PEAK_COL   6.3

Definition at line 167 of file dsp.c.

Referenced by dtmf_detect().

#define DTMF_RELATIVE_PEAK_ROW   6.3

Definition at line 166 of file dsp.c.

Referenced by dtmf_detect().

#define DTMF_REVERSE_TWIST   (relax ? 4.0 : 2.5)

Definition at line 164 of file dsp.c.

Referenced by dtmf_detect().

#define DTMF_THRESHOLD   8.0e7

Definition at line 157 of file dsp.c.

Referenced by dtmf_detect().

#define DTMF_TO_TOTAL_ENERGY   42.0

Definition at line 170 of file dsp.c.

Referenced by dtmf_detect().

#define FAX_2ND_HARMONIC   2.0

Definition at line 159 of file dsp.c.

#define FAX_THRESHOLD   8.0e7

Definition at line 158 of file dsp.c.

#define FAX_TONE_CED_DB   16

Definition at line 193 of file dsp.c.

Referenced by ast_fax_detect_init().

#define FAX_TONE_CED_DURATION   2600

Definition at line 192 of file dsp.c.

Referenced by ast_fax_detect_init().

#define FAX_TONE_CED_FREQ   2100

Definition at line 191 of file dsp.c.

Referenced by ast_fax_detect_init().

#define FAX_TONE_CNG_DB   16

Definition at line 185 of file dsp.c.

Referenced by ast_fax_detect_init().

#define FAX_TONE_CNG_DURATION   500

Definition at line 184 of file dsp.c.

Referenced by ast_fax_detect_init().

#define FAX_TONE_CNG_FREQ   1100

Definition at line 183 of file dsp.c.

Referenced by ast_fax_detect_init().

#define MAX_DTMF_DIGITS   128

Definition at line 143 of file dsp.c.

Referenced by store_digit().

#define MF_GSIZE   120

Definition at line 206 of file dsp.c.

Referenced by mf_detect().

#define SAMPLE_RATE   8000

Definition at line 195 of file dsp.c.

#define SAMPLES_IN_FRAME   160

Definition at line 203 of file dsp.c.

Referenced by ast_tone_detect_init().

#define TONE_MIN_THRESH   1e8

How much tone there should be at least to attempt

Definition at line 131 of file dsp.c.

Referenced by __ast_dsp_call_progress(), and pair_there().

#define TONE_THRESH   10.0

How much louder the tone should be than channel energy

Definition at line 130 of file dsp.c.

Referenced by __ast_dsp_call_progress(), and pair_there().


Enumeration Type Documentation

Enumerator:
BUSY_PERCENT  The percentage difference between the two last silence periods
BUSY_PAT_PERCENT  The percentage difference between measured and actual pattern
BUSY_THRESHOLD  Max number of ms difference between max and min times in busy
BUSY_MIN  Busy must be at least 80 ms in half-cadence
BUSY_MAX  Busy can't be longer than 3100 ms in half-cadence

Definition at line 119 of file dsp.c.

00119                  {
00120    BUSY_PERCENT = 10,      /*!< The percentage difference between the two last silence periods */
00121    BUSY_PAT_PERCENT = 7,   /*!< The percentage difference between measured and actual pattern */
00122    BUSY_THRESHOLD = 100,   /*!< Max number of ms difference between max and min times in busy */
00123    BUSY_MIN = 75,          /*!< Busy must be at least 80 ms in half-cadence */
00124    BUSY_MAX =3100          /*!< Busy can't be longer than 3100 ms in half-cadence */
00125 };

enum freq_index

Enumerator:
HZ_350  For US modes {
HZ_440 
HZ_480 
HZ_620 
HZ_950 
HZ_1400 
HZ_1800  }
HZ_425  For CR/BR modes
HZ_350UK  For UK mode
HZ_400UK 
HZ_440UK 

Definition at line 71 of file dsp.c.

00071                 { 
00072    /*! For US modes { */
00073    HZ_350 = 0,
00074    HZ_440,
00075    HZ_480,
00076    HZ_620,
00077    HZ_950,
00078    HZ_1400,
00079    HZ_1800, /*!< } */
00080 
00081    /*! For CR/BR modes */
00082    HZ_425 = 0,
00083 
00084    /*! For UK mode */
00085    HZ_350UK = 0,
00086    HZ_400UK,
00087    HZ_440UK
00088 };

enum gsamp_size

Number of goertzels for progress detect

Enumerator:
GSAMP_SIZE_NA  North America - 350, 440, 480, 620, 950, 1400, 1800 Hz
GSAMP_SIZE_CR  Costa Rica, Brazil - Only care about 425 Hz
GSAMP_SIZE_UK  UK disconnect goertzel feed - should trigger 400hz

Definition at line 59 of file dsp.c.

00059                 {
00060    GSAMP_SIZE_NA = 183,       /*!< North America - 350, 440, 480, 620, 950, 1400, 1800 Hz */
00061    GSAMP_SIZE_CR = 188,       /*!< Costa Rica, Brazil - Only care about 425 Hz */
00062    GSAMP_SIZE_UK = 160        /*!< UK disconnect goertzel feed - should trigger 400hz */
00063 };

All THRESH_XXX values are in GSAMP_SIZE chunks (us = 22ms)

Enumerator:
THRESH_RING  Need at least 150ms ring to accept
THRESH_TALK  Talk detection does not work continuously
THRESH_BUSY  Need at least 80ms to accept
THRESH_CONGESTION  Need at least 80ms to accept
THRESH_HANGUP  Need at least 1300ms to accept hangup
THRESH_RING2ANSWER  Timeout from start of ring to answer (about 6600 ms)

Definition at line 134 of file dsp.c.

00134                   {
00135    THRESH_RING = 8,           /*!< Need at least 150ms ring to accept */
00136    THRESH_TALK = 2,           /*!< Talk detection does not work continuously */
00137    THRESH_BUSY = 4,           /*!< Need at least 80ms to accept */
00138    THRESH_CONGESTION = 4,     /*!< Need at least 80ms to accept */
00139    THRESH_HANGUP = 60,        /*!< Need at least 1300ms to accept hangup */
00140    THRESH_RING2ANSWER = 300   /*!< Timeout from start of ring to answer (about 6600 ms) */
00141 };

enum prog_mode

Enumerator:
PROG_MODE_NA 
PROG_MODE_CR 
PROG_MODE_UK 

Definition at line 65 of file dsp.c.

00065                {
00066    PROG_MODE_NA = 0,
00067    PROG_MODE_CR,
00068    PROG_MODE_UK
00069 };


Function Documentation

static int __ast_dsp_call_progress ( struct ast_dsp *  dsp,
short *  s,
int  len 
) [static]

Definition at line 973 of file dsp.c.

References AST_CONTROL_ANSWER, AST_CONTROL_BUSY, AST_CONTROL_CONGESTION, AST_CONTROL_HANGUP, AST_CONTROL_RINGING, ast_debug, ast_log(), DSP_FEATURE_CALL_PROGRESS, DSP_PROGRESS_BUSY, DSP_PROGRESS_CONGESTION, DSP_PROGRESS_RINGING, DSP_PROGRESS_TALK, DSP_TONE_STATE_BUSY, DSP_TONE_STATE_DIALTONE, DSP_TONE_STATE_HUNGUP, DSP_TONE_STATE_RINGING, DSP_TONE_STATE_SILENCE, DSP_TONE_STATE_SPECIAL1, DSP_TONE_STATE_SPECIAL2, DSP_TONE_STATE_SPECIAL3, DSP_TONE_STATE_TALKING, ast_dsp::features, ast_dsp::freqcount, ast_dsp::freqs, ast_dsp::genergy, goertzel_result(), goertzel_sample(), ast_dsp::gsamp_size, ast_dsp::gsamps, HZ_1400, HZ_1800, HZ_350, HZ_350UK, HZ_400UK, HZ_425, HZ_440, HZ_440UK, HZ_480, HZ_620, HZ_950, LOG_WARNING, pair_there(), pass, PROG_MODE_CR, PROG_MODE_NA, PROG_MODE_UK, ast_dsp::progmode, ast_dsp::ringtimeout, ast_dsp::tcount, THRESH_BUSY, THRESH_CONGESTION, THRESH_HANGUP, THRESH_RING, THRESH_RING2ANSWER, THRESH_TALK, TONE_MIN_THRESH, TONE_THRESH, ast_dsp::tstate, goertzel_state_t::v2, and goertzel_state_t::v3.

Referenced by ast_dsp_call_progress(), and ast_dsp_process().

00974 {
00975    int x;
00976    int y;
00977    int pass;
00978    int newstate = DSP_TONE_STATE_SILENCE;
00979    int res = 0;
00980    while (len) {
00981       /* Take the lesser of the number of samples we need and what we have */
00982       pass = len;
00983       if (pass > dsp->gsamp_size - dsp->gsamps) {
00984          pass = dsp->gsamp_size - dsp->gsamps;
00985       }
00986       for (x = 0; x < pass; x++) {
00987          for (y = 0; y < dsp->freqcount; y++) {
00988             goertzel_sample(&dsp->freqs[y], s[x]);
00989          }
00990          dsp->genergy += s[x] * s[x];
00991       }
00992       s += pass;
00993       dsp->gsamps += pass;
00994       len -= pass;
00995       if (dsp->gsamps == dsp->gsamp_size) {
00996          float hz[7];
00997          for (y = 0; y < 7; y++) {
00998             hz[y] = goertzel_result(&dsp->freqs[y]);
00999          }
01000          switch (dsp->progmode) {
01001          case PROG_MODE_NA:
01002             if (pair_there(hz[HZ_480], hz[HZ_620], hz[HZ_350], hz[HZ_440], dsp->genergy)) {
01003                newstate = DSP_TONE_STATE_BUSY;
01004             } else if (pair_there(hz[HZ_440], hz[HZ_480], hz[HZ_350], hz[HZ_620], dsp->genergy)) {
01005                newstate = DSP_TONE_STATE_RINGING;
01006             } else if (pair_there(hz[HZ_350], hz[HZ_440], hz[HZ_480], hz[HZ_620], dsp->genergy)) {
01007                newstate = DSP_TONE_STATE_DIALTONE;
01008             } else if (hz[HZ_950] > TONE_MIN_THRESH * TONE_THRESH) {
01009                newstate = DSP_TONE_STATE_SPECIAL1;
01010             } else if (hz[HZ_1400] > TONE_MIN_THRESH * TONE_THRESH) {
01011                /* End of SPECIAL1 or middle of SPECIAL2 */
01012                if (dsp->tstate == DSP_TONE_STATE_SPECIAL1 || dsp->tstate == DSP_TONE_STATE_SPECIAL2) {
01013                   newstate = DSP_TONE_STATE_SPECIAL2;
01014                }
01015             } else if (hz[HZ_1800] > TONE_MIN_THRESH * TONE_THRESH) {
01016                /* End of SPECIAL2 or middle of SPECIAL3 */
01017                if (dsp->tstate == DSP_TONE_STATE_SPECIAL2 || dsp->tstate == DSP_TONE_STATE_SPECIAL3) {
01018                   newstate = DSP_TONE_STATE_SPECIAL3;
01019                }
01020             } else if (dsp->genergy > TONE_MIN_THRESH * TONE_THRESH) {
01021                newstate = DSP_TONE_STATE_TALKING;
01022             } else {
01023                newstate = DSP_TONE_STATE_SILENCE;
01024             }
01025             break;
01026          case PROG_MODE_CR:
01027             if (hz[HZ_425] > TONE_MIN_THRESH * TONE_THRESH) {
01028                newstate = DSP_TONE_STATE_RINGING;
01029             } else if (dsp->genergy > TONE_MIN_THRESH * TONE_THRESH) {
01030                newstate = DSP_TONE_STATE_TALKING;
01031             } else {
01032                newstate = DSP_TONE_STATE_SILENCE;
01033             }
01034             break;
01035          case PROG_MODE_UK:
01036             if (hz[HZ_400UK] > TONE_MIN_THRESH * TONE_THRESH) {
01037                newstate = DSP_TONE_STATE_HUNGUP;
01038             } else if (pair_there(hz[HZ_350UK], hz[HZ_440UK], hz[HZ_400UK], hz[HZ_400UK], dsp->genergy)) {
01039                newstate = DSP_TONE_STATE_DIALTONE;
01040             }
01041             break;
01042          default:
01043             ast_log(LOG_WARNING, "Can't process in unknown prog mode '%d'\n", dsp->progmode);
01044          }
01045          if (newstate == dsp->tstate) {
01046             dsp->tcount++;
01047             if (dsp->ringtimeout) {
01048                dsp->ringtimeout++;
01049             }
01050             switch (dsp->tstate) {
01051             case DSP_TONE_STATE_RINGING:
01052                if ((dsp->features & DSP_PROGRESS_RINGING) &&
01053                    (dsp->tcount == THRESH_RING)) {
01054                   res = AST_CONTROL_RINGING;
01055                   dsp->ringtimeout = 1;
01056                }
01057                break;
01058             case DSP_TONE_STATE_BUSY:
01059                if ((dsp->features & DSP_PROGRESS_BUSY) &&
01060                    (dsp->tcount == THRESH_BUSY)) {
01061                   res = AST_CONTROL_BUSY;
01062                   dsp->features &= ~DSP_FEATURE_CALL_PROGRESS;
01063                }
01064                break;
01065             case DSP_TONE_STATE_TALKING:
01066                if ((dsp->features & DSP_PROGRESS_TALK) &&
01067                    (dsp->tcount == THRESH_TALK)) {
01068                   res = AST_CONTROL_ANSWER;
01069                   dsp->features &= ~DSP_FEATURE_CALL_PROGRESS;
01070                }
01071                break;
01072             case DSP_TONE_STATE_SPECIAL3:
01073                if ((dsp->features & DSP_PROGRESS_CONGESTION) &&
01074                    (dsp->tcount == THRESH_CONGESTION)) {
01075                   res = AST_CONTROL_CONGESTION;
01076                   dsp->features &= ~DSP_FEATURE_CALL_PROGRESS;
01077                }
01078                break;
01079             case DSP_TONE_STATE_HUNGUP:
01080                if ((dsp->features & DSP_FEATURE_CALL_PROGRESS) &&
01081                    (dsp->tcount == THRESH_HANGUP)) {
01082                   res = AST_CONTROL_HANGUP;
01083                   dsp->features &= ~DSP_FEATURE_CALL_PROGRESS;
01084                }
01085                break;
01086             }
01087             if (dsp->ringtimeout == THRESH_RING2ANSWER) {
01088                ast_debug(1, "Consider call as answered because of timeout after last ring\n");
01089                res = AST_CONTROL_ANSWER;
01090                dsp->features &= ~DSP_FEATURE_CALL_PROGRESS;
01091             }
01092          } else {
01093             ast_debug(5, "Stop state %d with duration %d\n", dsp->tstate, dsp->tcount);
01094             ast_debug(5, "Start state %d\n", newstate);
01095             dsp->tstate = newstate;
01096             dsp->tcount = 1;
01097          }
01098 
01099          /* Reset goertzel */
01100          for (x = 0; x < 7; x++) {
01101             dsp->freqs[x].v2 = dsp->freqs[x].v3 = 0.0;
01102          }
01103          dsp->gsamps = 0;
01104          dsp->genergy = 0.0;
01105       }
01106    }
01107 
01108    return res;
01109 }

static int __ast_dsp_silence_noise ( struct ast_dsp *  dsp,
short *  s,
int  len,
int *  totalsilence,
int *  totalnoise 
) [static]

Definition at line 1124 of file dsp.c.

References BUSY_PERCENT, ast_dsp::busycount, ast_dsp::busymaybe, DSP_HISTORY, ast_dsp::historicnoise, ast_dsp::historicsilence, ast_dsp::totalnoise, and ast_dsp::totalsilence.

Referenced by ast_dsp_noise(), ast_dsp_process(), and ast_dsp_silence().

01125 {
01126    int accum;
01127    int x;
01128    int res = 0;
01129 
01130    if (!len) {
01131       return 0;
01132    }
01133    accum = 0;
01134    for (x = 0; x < len; x++) {
01135       accum += abs(s[x]);
01136    }
01137    accum /= len;
01138    if (accum < dsp->threshold) {
01139       /* Silent */
01140       dsp->totalsilence += len / 8;
01141       if (dsp->totalnoise) {
01142          /* Move and save history */
01143          memmove(dsp->historicnoise + DSP_HISTORY - dsp->busycount, dsp->historicnoise + DSP_HISTORY - dsp->busycount + 1, dsp->busycount * sizeof(dsp->historicnoise[0]));
01144          dsp->historicnoise[DSP_HISTORY - 1] = dsp->totalnoise;
01145 /* we don't want to check for busydetect that frequently */
01146 #if 0
01147          dsp->busymaybe = 1;
01148 #endif
01149       }
01150       dsp->totalnoise = 0;
01151       res = 1;
01152    } else {
01153       /* Not silent */
01154       dsp->totalnoise += len / 8;
01155       if (dsp->totalsilence) {
01156          int silence1 = dsp->historicsilence[DSP_HISTORY - 1];
01157          int silence2 = dsp->historicsilence[DSP_HISTORY - 2];
01158          /* Move and save history */
01159          memmove(dsp->historicsilence + DSP_HISTORY - dsp->busycount, dsp->historicsilence + DSP_HISTORY - dsp->busycount + 1, dsp->busycount * sizeof(dsp->historicsilence[0]));
01160          dsp->historicsilence[DSP_HISTORY - 1] = dsp->totalsilence;
01161          /* check if the previous sample differs only by BUSY_PERCENT from the one before it */
01162          if (silence1 < silence2) {
01163             if (silence1 + silence1 * BUSY_PERCENT / 100 >= silence2) {
01164                dsp->busymaybe = 1;
01165             } else {
01166                dsp->busymaybe = 0;
01167             }
01168          } else {
01169             if (silence1 - silence1 * BUSY_PERCENT / 100 <= silence2) {
01170                dsp->busymaybe = 1;
01171             } else {
01172                dsp->busymaybe = 0;
01173             }
01174          }
01175       }
01176       dsp->totalsilence = 0;
01177    }
01178    if (totalsilence) {
01179       *totalsilence = dsp->totalsilence;
01180    }
01181    if (totalnoise) {
01182       *totalnoise = dsp->totalnoise;
01183    }
01184    return res;
01185 }

static int _dsp_init ( int  reload  )  [static]

Definition at line 1697 of file dsp.c.

References ast_config_destroy(), ast_config_load2(), ast_variable_retrieve(), ast_verb, CONFIG_FILE_NAME, CONFIG_FLAG_FILEUNCHANGED, CONFIG_STATUS_FILEINVALID, CONFIG_STATUS_FILEMISSING, CONFIG_STATUS_FILEUNCHANGED, and THRESHOLD_SILENCE.

Referenced by ast_dsp_init(), and ast_dsp_reload().

01698 {
01699    struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
01700    struct ast_config *cfg;
01701 
01702    cfg = ast_config_load2(CONFIG_FILE_NAME, "dsp", config_flags);
01703    if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) {
01704       ast_verb(5, "Can't find dsp config file %s. Assuming default silencethreshold of %d.\n", CONFIG_FILE_NAME, DEFAULT_SILENCE_THRESHOLD);
01705       thresholds[THRESHOLD_SILENCE] = DEFAULT_SILENCE_THRESHOLD;
01706       return 0;
01707    }
01708 
01709    if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
01710       return 0;
01711    }
01712 
01713    if (cfg) {
01714       const char *value;
01715 
01716       value = ast_variable_retrieve(cfg, "default", "silencethreshold");
01717       if (value && sscanf(value, "%30d", &thresholds[THRESHOLD_SILENCE]) != 1) {
01718          ast_verb(5, "%s: '%s' is not a valid silencethreshold value\n", CONFIG_FILE_NAME, value);
01719          thresholds[THRESHOLD_SILENCE] = DEFAULT_SILENCE_THRESHOLD;
01720       } else if (!value) {
01721          thresholds[THRESHOLD_SILENCE] = DEFAULT_SILENCE_THRESHOLD;
01722       }
01723 
01724       ast_config_destroy(cfg);
01725    }
01726    return 0;
01727 }

static void ast_digit_detect_init ( digit_detect_state_t *  s,
int  mf 
) [static]

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 1187 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().

01188 {
01189    int res = 0, x;
01190 #ifndef BUSYDETECT_TONEONLY
01191    int avgsilence = 0, hitsilence = 0;
01192 #endif
01193    int avgtone = 0, hittone = 0;
01194    if (!dsp->busymaybe) {
01195       return res;
01196    }
01197    for (x = DSP_HISTORY - dsp->busycount; x < DSP_HISTORY; x++) {
01198 #ifndef BUSYDETECT_TONEONLY
01199       avgsilence += dsp->historicsilence[x];
01200 #endif
01201       avgtone += dsp->historicnoise[x];
01202    }
01203 #ifndef BUSYDETECT_TONEONLY
01204    avgsilence /= dsp->busycount;
01205 #endif
01206    avgtone /= dsp->busycount;
01207    for (x = DSP_HISTORY - dsp->busycount; x < DSP_HISTORY; x++) {
01208 #ifndef BUSYDETECT_TONEONLY
01209       if (avgsilence > dsp->historicsilence[x]) {
01210          if (avgsilence - (avgsilence * BUSY_PERCENT / 100) <= dsp->historicsilence[x]) {
01211             hitsilence++;
01212          }
01213       } else {
01214          if (avgsilence + (avgsilence * BUSY_PERCENT / 100) >= dsp->historicsilence[x]) {
01215             hitsilence++;
01216          }
01217       }
01218 #endif
01219       if (avgtone > dsp->historicnoise[x]) {
01220          if (avgtone - (avgtone * BUSY_PERCENT / 100) <= dsp->historicnoise[x]) {
01221             hittone++;
01222          }
01223       } else {
01224          if (avgtone + (avgtone * BUSY_PERCENT / 100) >= dsp->historicnoise[x]) {
01225             hittone++;
01226          }
01227       }
01228    }
01229 #ifndef BUSYDETECT_TONEONLY
01230    if ((hittone >= dsp->busycount - 1) && (hitsilence >= dsp->busycount - 1) && 
01231        (avgtone >= BUSY_MIN && avgtone <= BUSY_MAX) && 
01232        (avgsilence >= BUSY_MIN && avgsilence <= BUSY_MAX)) {
01233 #else
01234    if ((hittone >= dsp->busycount - 1) && (avgtone >= BUSY_MIN && avgtone <= BUSY_MAX)) {
01235 #endif
01236 #ifdef BUSYDETECT_COMPARE_TONE_AND_SILENCE
01237       if (avgtone > avgsilence) {
01238          if (avgtone - avgtone*BUSY_PERCENT/100 <= avgsilence) {
01239             res = 1;
01240          }
01241       } else {
01242          if (avgtone + avgtone*BUSY_PERCENT/100 >= avgsilence) {
01243             res = 1;
01244          }
01245       }
01246 #else
01247       res = 1;
01248 #endif
01249    }
01250    /* If we know the expected busy tone length, check we are in the range */
01251    if (res && (dsp->busy_tonelength > 0)) {
01252       if (abs(avgtone - dsp->busy_tonelength) > (dsp->busy_tonelength*BUSY_PAT_PERCENT/100)) {
01253 #ifdef BUSYDETECT_DEBUG
01254          ast_debug(5, "busy detector: avgtone of %d not close enough to desired %d\n",
01255             avgtone, dsp->busy_tonelength);
01256 #endif
01257          res = 0;
01258       }
01259    }
01260 #ifndef BUSYDETECT_TONEONLY
01261    /* If we know the expected busy tone silent-period length, check we are in the range */
01262    if (res && (dsp->busy_quietlength > 0)) {
01263       if (abs(avgsilence - dsp->busy_quietlength) > (dsp->busy_quietlength*BUSY_PAT_PERCENT/100)) {
01264 #ifdef BUSYDETECT_DEBUG
01265       ast_debug(5, "busy detector: avgsilence of %d not close enough to desired %d\n",
01266          avgsilence, dsp->busy_quietlength);
01267 #endif
01268          res = 0;
01269       }
01270    }
01271 #endif
01272 #if !defined(BUSYDETECT_TONEONLY) && defined(BUSYDETECT_DEBUG)
01273    if (res) {
01274       ast_debug(5, "ast_dsp_busydetect detected busy, avgtone: %d, avgsilence %d\n", avgtone, avgsilence);
01275    } else {
01276       ast_debug(5, "busy detector: FAILED with avgtone: %d, avgsilence %d\n", avgtone, avgsilence);
01277    }
01278 #endif
01279    return res;
01280 }

int ast_dsp_call_progress ( struct ast_dsp *  dsp,
struct ast_frame *  inf 
)

Scans for progress indication in audio.

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

01112 {
01113    if (inf->frametype != AST_FRAME_VOICE) {
01114       ast_log(LOG_WARNING, "Can't check call progress of non-voice frames\n");
01115       return 0;
01116    }
01117    if (inf->subclass != AST_FORMAT_SLINEAR) {
01118       ast_log(LOG_WARNING, "Can only check call progress in signed-linear frames\n");
01119       return 0;
01120    }
01121    return __ast_dsp_call_progress(dsp, inf->data.ptr, inf->datalen / 2);
01122 }

void ast_dsp_digitreset ( struct ast_dsp *  dsp  ) 

Reset DTMF detector.

Definition at line 1599 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().

01600 {
01601    int i;
01602    
01603    dsp->dtmf_began = 0;
01604    if (dsp->digitmode & DSP_DIGITMODE_MF) {
01605       mf_detect_state_t *s = &dsp->digit_state.td.mf;
01606       /* Reinitialise the detector for the next block */
01607       for (i = 0;  i < 6;  i++) {
01608          goertzel_reset(&s->tone_out[i]);
01609       }
01610       s->hits[4] = s->hits[3] = s->hits[2] = s->hits[1] = s->hits[0] = s->current_hit = 0;
01611       s->current_sample = 0;
01612    } else {
01613       dtmf_detect_state_t *s = &dsp->digit_state.td.dtmf;
01614       /* Reinitialise the detector for the next block */
01615       for (i = 0;  i < 4;  i++) {
01616          goertzel_reset(&s->row_out[i]);
01617          goertzel_reset(&s->col_out[i]);
01618       }
01619       s->lasthit = s->current_hit = 0;
01620       s->energy = 0.0;
01621       s->current_sample = 0;
01622       s->hits = 0;
01623       s->misses = 0;
01624    }
01625 
01626    dsp->digit_state.digits[0] = '\0';
01627    dsp->digit_state.current_digits = 0;
01628 }

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

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

Referenced by __frame_free().

01745 {
01746    struct ast_dsp *dsp;
01747 
01748    ast_clear_flag(fr, AST_FRFLAG_FROM_DSP);
01749 
01750    dsp = (struct ast_dsp *) (((char *) fr) - offsetof(struct ast_dsp, f));
01751 
01752    if (!dsp->destroy)
01753       return;
01754    
01755    ast_dsp_free(dsp);
01756 }

void ast_dsp_free ( struct ast_dsp *  dsp  ) 

Definition at line 1561 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(), sip_rtp_read(), ss_thread(), and unload_module().

01562 {
01563    if (ast_test_flag(&dsp->f, AST_FRFLAG_FROM_DSP)) {
01564       /* If this flag is still set, that means that the dsp's destruction 
01565        * been torn down, while we still have a frame out there being used.
01566        * When ast_frfree() gets called on that frame, this ast_trans_pvt
01567        * will get destroyed, too. */
01568 
01569       dsp->destroy = 1;
01570 
01571       return;
01572    }
01573    ast_free(dsp);
01574 }

int ast_dsp_get_tcount ( struct ast_dsp *  dsp  ) 

Get tcount (Threshold counter).

Definition at line 1692 of file dsp.c.

References ast_dsp::tcount.

Referenced by dahdi_read().

01693 {
01694    return dsp->tcount;
01695 }

int ast_dsp_get_threshold_from_settings ( enum threshold  which  ) 

Get silence threshold from dsp.conf.

Since:
1.6.1

Definition at line 1729 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().

01730 {
01731    return thresholds[which];
01732 }

int ast_dsp_get_tstate ( struct ast_dsp *  dsp  ) 

Get tstate (Tone State).

Definition at line 1687 of file dsp.c.

References ast_dsp::tstate.

Referenced by dahdi_read().

01688 {
01689    return dsp->tstate;
01690 }

int ast_dsp_init ( void   ) 

Load dsp settings from dsp.conf.

Since:
1.6.1

Definition at line 1734 of file dsp.c.

References _dsp_init().

Referenced by main().

01735 {
01736    return _dsp_init(0);
01737 }

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 1300 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().

01301 {
01302        short *s;
01303        int len;
01304 
01305        if (f->frametype != AST_FRAME_VOICE) {
01306                ast_log(LOG_WARNING, "Can't calculate noise on a non-voice frame\n");
01307                return 0;
01308        }
01309        if (f->subclass != AST_FORMAT_SLINEAR) {
01310                ast_log(LOG_WARNING, "Can only calculate noise on signed-linear frames :(\n");
01311                return 0;
01312        }
01313        s = f->data.ptr;
01314        len = f->datalen/2;
01315        return __ast_dsp_silence_noise(dsp, s, len, NULL, totalnoise);
01316 }

struct ast_frame* ast_dsp_process ( struct ast_channel *  chan,
struct ast_dsp *  dsp,
struct ast_frame *  af 
) [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 1319 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, digit_detect_state_t::digitlen, ast_dsp::digitmode, digit_detect_state_t::digits, ast_dsp::display_inband_dtmf_warning, 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, DSP_FEATURE_WAITDIALTONE, ast_dsp::dtmf_began, dtmf_detect(), fragment_t::end, ast_dsp::f, ast_dsp::faxmode, ast_dsp::features, ast_frame::frametype, ast_dsp::historicnoise, ast_dsp::historicsilence, ast_frame::len, len(), LOG_WARNING, mf_detect(), ast_dsp::mute_data, ast_dsp::mute_fragments, ast_channel::name, ast_frame::ptr, SAMPLE_RATE, 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().

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

static void ast_dsp_prog_reset ( struct ast_dsp *  dsp  )  [static]

Definition at line 1518 of file dsp.c.

References ARRAY_LEN, ast_dsp::freqcount, ast_dsp::freqs, progress::freqs, goertzel_init(), ast_dsp::gsamp_size, ast_dsp::gsamps, modes, ast_dsp::progmode, ast_dsp::ringtimeout, and progress::size.

Referenced by ast_dsp_new(), and ast_dsp_set_call_progress_zone().

01519 {
01520    int max = 0;
01521    int x;
01522    
01523    dsp->gsamp_size = modes[dsp->progmode].size;
01524    dsp->gsamps = 0;
01525    for (x = 0; x < ARRAY_LEN(modes[dsp->progmode].freqs); x++) {
01526       if (modes[dsp->progmode].freqs[x]) {
01527          goertzel_init(&dsp->freqs[x], (float)modes[dsp->progmode].freqs[x], dsp->gsamp_size);
01528          max = x + 1;
01529       }
01530    }
01531    dsp->freqcount = max;
01532    dsp->ringtimeout= 0;
01533 }

int ast_dsp_reload ( void   ) 

Reloads dsp settings from dsp.conf.

Since:
1.6.1

Definition at line 1739 of file dsp.c.

References _dsp_init().

01740 {
01741    return _dsp_init(1);
01742 }

void ast_dsp_reset ( struct ast_dsp *  dsp  ) 

Reset total silence count.

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

01631 {
01632    int x;
01633    
01634    dsp->totalsilence = 0;
01635    dsp->gsamps = 0;
01636    for (x = 0; x < 4; x++) {
01637       dsp->freqs[x].v2 = dsp->freqs[x].v3 = 0.0;
01638    }
01639    memset(dsp->historicsilence, 0, sizeof(dsp->historicsilence));
01640    memset(dsp->historicnoise, 0, sizeof(dsp->historicnoise));  
01641    dsp->ringtimeout= 0;
01642 }

void ast_dsp_set_busy_count ( struct ast_dsp *  dsp,
int  cadences 
)

Set number of required cadences for busy.

Definition at line 1581 of file dsp.c.

References ast_dsp::busycount, and DSP_HISTORY.

Referenced by dahdi_new().

01582 {
01583    if (cadences < 4) {
01584       cadences = 4;
01585    }
01586    if (cadences > DSP_HISTORY) {
01587       cadences = DSP_HISTORY;
01588    }
01589    dsp->busycount = cadences;
01590 }

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

Set expected lengths of the busy tone.

Definition at line 1592 of file dsp.c.

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

Referenced by dahdi_new().

01593 {
01594    dsp->busy_tonelength = tonelength;
01595    dsp->busy_quietlength = quietlength;
01596    ast_debug(1, "dsp busy pattern set to %d,%d\n", tonelength, quietlength);
01597 }

int ast_dsp_set_call_progress_zone ( struct ast_dsp *  dsp,
char *  zone 
)

Set zone for doing progress detection.

Definition at line 1668 of file dsp.c.

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

Referenced by dahdi_new().

01669 {
01670    int x;
01671    
01672    for (x = 0; x < ARRAY_LEN(aliases); x++) {
01673       if (!strcasecmp(aliases[x].name, zone)) {
01674          dsp->progmode = aliases[x].mode;
01675          ast_dsp_prog_reset(dsp);
01676          return 0;
01677       }
01678    }
01679    return -1;
01680 }

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 1644 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().

01645 {
01646    int new;
01647    int old;
01648    
01649    old = dsp->digitmode & (DSP_DIGITMODE_DTMF | DSP_DIGITMODE_MF | DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX);
01650    new = digitmode & (DSP_DIGITMODE_DTMF | DSP_DIGITMODE_MF | DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX);
01651    if (old != new) {
01652       /* Must initialize structures if switching from MF to DTMF or vice-versa */
01653       ast_digit_detect_init(&dsp->digit_state, new & DSP_DIGITMODE_MF);
01654    }
01655    dsp->digitmode = digitmode;
01656    return 0;
01657 }

int ast_dsp_set_faxmode ( struct ast_dsp *  dsp,
int  faxmode 
)

Set fax mode.

Definition at line 1659 of file dsp.c.

References ast_fax_detect_init(), and ast_dsp::faxmode.

01660 {
01661    if (dsp->faxmode != faxmode) {
01662       ast_fax_detect_init(dsp);
01663    }
01664    dsp->faxmode = faxmode;
01665    return 0;
01666 }

void ast_dsp_set_features ( struct ast_dsp *  dsp,
int  features 
)

void ast_dsp_set_threshold ( struct ast_dsp *  dsp,
int  threshold 
)

Set threshold value for silence.

Definition at line 1576 of file dsp.c.

References ast_dsp::threshold.

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

01577 {
01578    dsp->threshold = threshold;
01579 }

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 1282 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().

01283 {
01284    short *s;
01285    int len;
01286    
01287    if (f->frametype != AST_FRAME_VOICE) {
01288       ast_log(LOG_WARNING, "Can't calculate silence on a non-voice frame\n");
01289       return 0;
01290    }
01291    if (f->subclass != AST_FORMAT_SLINEAR) {
01292       ast_log(LOG_WARNING, "Can only calculate silence on signed-linear frames :(\n");
01293       return 0;
01294    }
01295    s = f->data.ptr;
01296    len = f->datalen/2;
01297    return __ast_dsp_silence_noise(dsp, s, len, totalsilence, NULL);
01298 }

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

References ast_dsp::mute_fragments.

Referenced by dahdi_read().

01683 {
01684    return (dsp->mute_fragments > 0);
01685 }

static void ast_dtmf_detect_init ( dtmf_detect_state_t *  s  )  [static]

static void ast_fax_detect_init ( struct ast_dsp *  s  )  [static]

static void ast_mf_detect_init ( mf_detect_state_t *  s  )  [static]

Definition at line 507 of file dsp.c.

References mf_detect_state_t::current_hit, mf_detect_state_t::current_sample, goertzel_init(), mf_detect_state_t::hits, and mf_detect_state_t::tone_out.

Referenced by ast_digit_detect_init().

00508 {
00509    int i;
00510    s->hits[0] = s->hits[1] = s->hits[2] = s->hits[3] = s->hits[4] = 0;
00511    for (i = 0;  i < 6;  i++) {
00512       goertzel_init (&s->tone_out[i], mf_tones[i], 160);
00513    }
00514    s->current_sample = 0;
00515    s->current_hit = 0;
00516 }

static void ast_tone_detect_init ( tone_detect_state_t *  s,
int  freq,
int  duration,
int  amp 
) [static]

Definition at line 422 of file dsp.c.

References ast_debug, tone_detect_state_t::block_size, tone_detect_state_t::energy, tone_detect_state_t::freq, goertzel_init(), tone_detect_state_t::hit_count, tone_detect_state_t::hits_required, tone_detect_state_t::last_hit, SAMPLE_RATE, SAMPLES_IN_FRAME, tone_detect_state_t::samples_pending, tone_detect_state_t::squelch, tone_detect_state_t::threshold, and tone_detect_state_t::tone.

Referenced by ast_fax_detect_init().

00423 {
00424    int duration_samples;
00425    float x;
00426    int periods_in_block;
00427 
00428    s->freq = freq;
00429 
00430    /* Desired tone duration in samples */
00431    duration_samples = duration * SAMPLE_RATE / 1000;
00432    /* We want to allow 10% deviation of tone duration */
00433    duration_samples = duration_samples * 9 / 10;
00434 
00435    /* If we want to remove tone, it is important to have block size not
00436       to exceed frame size. Otherwise by the moment tone is detected it is too late
00437       to squelch it from previous frames */
00438    s->block_size = SAMPLES_IN_FRAME;
00439 
00440    periods_in_block = s->block_size * freq / SAMPLE_RATE;
00441 
00442    /* Make sure we will have at least 5 periods at target frequency for analisys.
00443       This may make block larger than expected packet and will make squelching impossible
00444       but at least we will be detecting the tone */
00445    if (periods_in_block < 5)
00446       periods_in_block = 5;
00447 
00448    /* Now calculate final block size. It will contain integer number of periods */
00449    s->block_size = periods_in_block * SAMPLE_RATE / freq;
00450 
00451    /* tone_detect is currently only used to detect fax tones and we
00452       do not need suqlching the fax tones */
00453    s->squelch = 0;
00454 
00455    /* Account for the first and the last block to be incomplete
00456       and thus no tone will be detected in them */
00457    s->hits_required = (duration_samples - (s->block_size - 1)) / s->block_size;
00458 
00459    goertzel_init(&s->tone, freq, s->block_size);
00460 
00461    s->samples_pending = s->block_size;
00462    s->hit_count = 0;
00463    s->last_hit = 0;
00464    s->energy = 0.0;
00465 
00466    /* We want tone energy to be amp decibels above the rest of the signal (the noise).
00467       According to Parseval's theorem the energy computed in time domain equals to energy
00468       computed in frequency domain. So subtracting energy in the frequency domain (Goertzel result)
00469       from the energy in the time domain we will get energy of the remaining signal (without the tone
00470       we are detecting). We will be checking that
00471       10*log(Ew / (Et - Ew)) > amp
00472       Calculate threshold so that we will be actually checking
00473       Ew > Et * threshold
00474    */
00475 
00476    x = pow(10.0, amp / 10.0);
00477    s->threshold = x / (x + 1);
00478 
00479    ast_debug(1, "Setup tone %d Hz, %d ms, block_size=%d, hits_required=%d\n", freq, duration, s->block_size, s->hits_required);
00480 }

static int dtmf_detect ( struct ast_dsp *  dsp,
digit_detect_state_t *  s,
int16_t  amp[],
int  samples,
int  squelch,
int  relax 
) [static]

Definition at line 649 of file dsp.c.

References dtmf_detect_state_t::col_out, digit_detect_state_t::current_digits, dtmf_detect_state_t::current_hit, dtmf_detect_state_t::current_sample, digit_detect_state_t::digitlen, digit_detect_state_t::dtmf, DTMF_GSIZE, DTMF_NORMAL_TWIST, DTMF_RELATIVE_PEAK_COL, DTMF_RELATIVE_PEAK_ROW, DTMF_REVERSE_TWIST, DTMF_THRESHOLD, DTMF_TO_TOTAL_ENERGY, fragment_t::end, dtmf_detect_state_t::energy, goertzel_reset(), goertzel_result(), goertzel_sample(), dtmf_detect_state_t::hits, dtmf_detect_state_t::hits_to_begin, dtmf_detect_state_t::lasthit, dtmf_detect_state_t::misses, dtmf_detect_state_t::misses_to_end, mute_fragment(), dtmf_detect_state_t::mute_samples, dtmf_detect_state_t::row_out, fragment_t::start, store_digit(), and digit_detect_state_t::td.

Referenced by ast_dsp_process().

00650 {
00651    float row_energy[4];
00652    float col_energy[4];
00653    float famp;
00654    int i;
00655    int j;
00656    int sample;
00657    int best_row;
00658    int best_col;
00659    int hit;
00660    int limit;
00661    fragment_t mute = {0, 0};
00662 
00663    if (squelch && s->td.dtmf.mute_samples > 0) {
00664       mute.end = (s->td.dtmf.mute_samples < samples) ? s->td.dtmf.mute_samples : samples;
00665       s->td.dtmf.mute_samples -= mute.end;
00666    }
00667 
00668    hit = 0;
00669    for (sample = 0; sample < samples; sample = limit) {
00670       /* DTMF_GSIZE is optimised to meet the DTMF specs. */
00671       if ((samples - sample) >= (DTMF_GSIZE - s->td.dtmf.current_sample)) {
00672          limit = sample + (DTMF_GSIZE - s->td.dtmf.current_sample);
00673       } else {
00674          limit = samples;
00675       }
00676       /* The following unrolled loop takes only 35% (rough estimate) of the 
00677          time of a rolled loop on the machine on which it was developed */
00678       for (j = sample; j < limit; j++) {
00679          famp = amp[j];
00680          s->td.dtmf.energy += famp*famp;
00681          /* With GCC 2.95, the following unrolled code seems to take about 35%
00682             (rough estimate) as long as a neat little 0-3 loop */
00683          goertzel_sample(s->td.dtmf.row_out, amp[j]);
00684          goertzel_sample(s->td.dtmf.col_out, amp[j]);
00685          goertzel_sample(s->td.dtmf.row_out + 1, amp[j]);
00686          goertzel_sample(s->td.dtmf.col_out + 1, amp[j]);
00687          goertzel_sample(s->td.dtmf.row_out + 2, amp[j]);
00688          goertzel_sample(s->td.dtmf.col_out + 2, amp[j]);
00689          goertzel_sample(s->td.dtmf.row_out + 3, amp[j]);
00690          goertzel_sample(s->td.dtmf.col_out + 3, amp[j]);
00691       }
00692       s->td.dtmf.current_sample += (limit - sample);
00693       if (s->td.dtmf.current_sample < DTMF_GSIZE) {
00694          continue;
00695       }
00696       /* We are at the end of a DTMF detection block */
00697       /* Find the peak row and the peak column */
00698       row_energy[0] = goertzel_result (&s->td.dtmf.row_out[0]);
00699       col_energy[0] = goertzel_result (&s->td.dtmf.col_out[0]);
00700 
00701       for (best_row = best_col = 0, i = 1;  i < 4;  i++) {
00702          row_energy[i] = goertzel_result (&s->td.dtmf.row_out[i]);
00703          if (row_energy[i] > row_energy[best_row]) {
00704             best_row = i;
00705          }
00706          col_energy[i] = goertzel_result (&s->td.dtmf.col_out[i]);
00707          if (col_energy[i] > col_energy[best_col]) {
00708             best_col = i;
00709          }
00710       }
00711       hit = 0;
00712       /* Basic signal level test and the twist test */
00713       if (row_energy[best_row] >= DTMF_THRESHOLD && 
00714           col_energy[best_col] >= DTMF_THRESHOLD &&
00715           col_energy[best_col] < row_energy[best_row] * DTMF_REVERSE_TWIST &&
00716           col_energy[best_col] * DTMF_NORMAL_TWIST > row_energy[best_row]) {
00717          /* Relative peak test */
00718          for (i = 0;  i < 4;  i++) {
00719             if ((i != best_col &&
00720                 col_energy[i] * DTMF_RELATIVE_PEAK_COL > col_energy[best_col]) ||
00721                 (i != best_row 
00722                  && row_energy[i] * DTMF_RELATIVE_PEAK_ROW > row_energy[best_row])) {
00723                break;
00724             }
00725          }
00726          /* ... and fraction of total energy test */
00727          if (i >= 4 &&
00728              (row_energy[best_row] + col_energy[best_col]) > DTMF_TO_TOTAL_ENERGY * s->td.dtmf.energy) {
00729             /* Got a hit */
00730             hit = dtmf_positions[(best_row << 2) + best_col];
00731          }
00732       } 
00733 
00734       if (s->td.dtmf.current_hit) {
00735          /* We are in the middle of a digit already */
00736          if (hit != s->td.dtmf.current_hit) {
00737             s->td.dtmf.misses++;
00738             if (s->td.dtmf.misses == s->td.dtmf.misses_to_end) {
00739                /* There were enough misses to consider digit ended */
00740                s->td.dtmf.current_hit = 0;
00741             }
00742          } else {
00743             s->td.dtmf.misses = 0;
00744             /* Current hit was same as last, so increment digit duration (of last digit) */
00745             s->digitlen[s->current_digits - 1] += DTMF_GSIZE;
00746          }
00747       }
00748 
00749       /* Look for a start of a new digit no matter if we are already in the middle of some
00750          digit or not. This is because hits_to_begin may be smaller than misses_to_end
00751          and we may find begin of new digit before we consider last one ended. */
00752       if (hit) {
00753          if (hit == s->td.dtmf.lasthit) {
00754             s->td.dtmf.hits++;
00755          } else {
00756             s->td.dtmf.hits = 1;
00757          }
00758 
00759          if (s->td.dtmf.hits == s->td.dtmf.hits_to_begin && hit != s->td.dtmf.current_hit) {
00760             store_digit(s, hit);
00761             s->td.dtmf.current_hit = hit;
00762             s->td.dtmf.misses = 0;
00763          }
00764       } else {
00765          s->td.dtmf.hits = 0;
00766       }
00767 
00768       s->td.dtmf.lasthit = hit;
00769 
00770       /* If we had a hit in this block, include it into mute fragment */
00771       if (squelch && hit) {
00772          if (mute.end < sample - DTMF_GSIZE) {
00773             /* There is a gap between fragments */
00774             mute_fragment(dsp, &mute);
00775             mute.start = (sample > DTMF_GSIZE) ? (sample - DTMF_GSIZE) : 0;
00776          }
00777          mute.end = limit + DTMF_GSIZE;
00778       }
00779 
00780       /* Reinitialise the detector for the next block */
00781       for (i = 0; i < 4; i++) {
00782          goertzel_reset(&s->td.dtmf.row_out[i]);
00783          goertzel_reset(&s->td.dtmf.col_out[i]);
00784       }
00785       s->td.dtmf.energy = 0.0;
00786       s->td.dtmf.current_sample = 0;
00787    }
00788 
00789    if (squelch && mute.end) {
00790       if (mute.end > samples) {
00791          s->td.dtmf.mute_samples = mute.end - samples;
00792          mute.end = samples;
00793       }
00794       mute_fragment(dsp, &mute);
00795    }
00796 
00797    return (s->td.dtmf.current_hit); /* return the debounced hit */
00798 }

static void goertzel_init ( goertzel_state_t *  s,
float  freq,
int  samples 
) [inline, static]

Definition at line 349 of file dsp.c.

References goertzel_state_t::chunky, cos, goertzel_state_t::fac, SAMPLE_RATE, goertzel_state_t::samples, goertzel_state_t::v2, and goertzel_state_t::v3.

Referenced by ast_dsp_prog_reset(), ast_dtmf_detect_init(), ast_mf_detect_init(), and ast_tone_detect_init().

00350 {
00351    s->v2 = s->v3 = s->chunky = 0.0;
00352    s->fac = (int)(32768.0 * 2.0 * cos(2.0 * M_PI * freq / SAMPLE_RATE));
00353    s->samples = samples;
00354 }

static void goertzel_reset ( goertzel_state_t *  s  )  [inline, static]

Definition at line 356 of file dsp.c.

References goertzel_state_t::chunky, goertzel_state_t::v2, and goertzel_state_t::v3.

Referenced by ast_dsp_digitreset(), dtmf_detect(), mf_detect(), and tone_detect().

00357 {
00358    s->v2 = s->v3 = s->chunky = 0.0;
00359 }

static float goertzel_result ( goertzel_state_t *  s  )  [inline, static]

Definition at line 340 of file dsp.c.

References goertzel_state_t::chunky, goertzel_state_t::fac, goertzel_result_t::power, goertzel_state_t::v2, goertzel_state_t::v3, and goertzel_result_t::value.

Referenced by __ast_dsp_call_progress(), dtmf_detect(), mf_detect(), and tone_detect().

00341 {
00342    goertzel_result_t r;
00343    r.value = (s->v3 * s->v3) + (s->v2 * s->v2);
00344    r.value -= ((s->v2 * s->v3) >> 15) * s->fac;
00345    r.power = s->chunky * 2;
00346    return (float)r.value * (float)(1 << r.power);
00347 }

static void goertzel_sample ( goertzel_state_t *  s,
short  sample 
) [inline, static]

Definition at line 313 of file dsp.c.

References goertzel_state_t::chunky, goertzel_state_t::fac, goertzel_state_t::v2, and goertzel_state_t::v3.

Referenced by __ast_dsp_call_progress(), dtmf_detect(), goertzel_update(), mf_detect(), and tone_detect().

00314 {
00315    int v1;
00316    
00317    v1 = s->v2;
00318    s->v2 = s->v3;
00319    
00320    s->v3 = (s->fac * s->v2) >> 15;
00321    s->v3 = s->v3 - v1 + (sample >> s->chunky);
00322    if (abs(s->v3) > 32768) {
00323       s->chunky++;
00324       s->v3 = s->v3 >> 1;
00325       s->v2 = s->v2 >> 1;
00326       v1 = v1 >> 1;
00327    }
00328 }

static void goertzel_update ( goertzel_state_t *  s,
short *  samps,
int  count 
) [inline, static]

Definition at line 330 of file dsp.c.

References goertzel_sample().

00331 {
00332    int i;
00333    
00334    for (i = 0; i < count; i++) {
00335       goertzel_sample(s, samps[i]);
00336    }
00337 }

static int mf_detect ( struct ast_dsp *  dsp,
digit_detect_state_t *  s,
int16_t  amp[],
int  samples,
int  squelch,
int  relax 
) [static]

Definition at line 800 of file dsp.c.

References BELL_MF_RELATIVE_PEAK, BELL_MF_THRESHOLD, BELL_MF_TWIST, mf_detect_state_t::current_hit, mf_detect_state_t::current_sample, DTMF_GSIZE, fragment_t::end, goertzel_reset(), goertzel_result(), goertzel_sample(), mf_detect_state_t::hits, digit_detect_state_t::mf, MF_GSIZE, mute_fragment(), mf_detect_state_t::mute_samples, fragment_t::start, store_digit(), digit_detect_state_t::td, and mf_detect_state_t::tone_out.

Referenced by ast_dsp_process().

00802 {
00803    float energy[6];
00804    int best;
00805    int second_best;
00806    float famp;
00807    int i;
00808    int j;
00809    int sample;
00810    int hit;
00811    int limit;
00812    fragment_t mute = {0, 0};
00813 
00814    if (squelch && s->td.mf.mute_samples > 0) {
00815       mute.end = (s->td.mf.mute_samples < samples) ? s->td.mf.mute_samples : samples;
00816       s->td.mf.mute_samples -= mute.end;
00817    }
00818 
00819    hit = 0;
00820    for (sample = 0;  sample < samples;  sample = limit) {
00821       /* 80 is optimised to meet the MF specs. */
00822       /* XXX So then why is MF_GSIZE defined as 120? */
00823       if ((samples - sample) >= (MF_GSIZE - s->td.mf.current_sample)) {
00824          limit = sample + (MF_GSIZE - s->td.mf.current_sample);
00825       } else {
00826          limit = samples;
00827       }
00828       /* The following unrolled loop takes only 35% (rough estimate) of the 
00829          time of a rolled loop on the machine on which it was developed */
00830       for (j = sample;  j < limit;  j++) {
00831          famp = amp[j];
00832          /* With GCC 2.95, the following unrolled code seems to take about 35%
00833             (rough estimate) as long as a neat little 0-3 loop */
00834          goertzel_sample(s->td.mf.tone_out, amp[j]);
00835          goertzel_sample(s->td.mf.tone_out + 1, amp[j]);
00836          goertzel_sample(s->td.mf.tone_out + 2, amp[j]);
00837          goertzel_sample(s->td.mf.tone_out + 3, amp[j]);
00838          goertzel_sample(s->td.mf.tone_out + 4, amp[j]);
00839          goertzel_sample(s->td.mf.tone_out + 5, amp[j]);
00840       }
00841       s->td.mf.current_sample += (limit - sample);
00842       if (s->td.mf.current_sample < MF_GSIZE) {
00843          continue;
00844       }
00845       /* We're at the end of an MF detection block.  */
00846       /* Find the two highest energies. The spec says to look for
00847          two tones and two tones only. Taking this literally -ie
00848          only two tones pass the minimum threshold - doesn't work
00849          well. The sinc function mess, due to rectangular windowing
00850          ensure that! Find the two highest energies and ensure they
00851          are considerably stronger than any of the others. */
00852       energy[0] = goertzel_result(&s->td.mf.tone_out[0]);
00853       energy[1] = goertzel_result(&s->td.mf.tone_out[1]);
00854       if (energy[0] > energy[1]) {
00855          best = 0;
00856          second_best = 1;
00857       } else {
00858          best = 1;
00859          second_best = 0;
00860       }
00861       /*endif*/
00862       for (i = 2; i < 6; i++) {
00863          energy[i] = goertzel_result(&s->td.mf.tone_out[i]);
00864          if (energy[i] >= energy[best]) {
00865             second_best = best;
00866             best = i;
00867          } else if (energy[i] >= energy[second_best]) {
00868             second_best = i;
00869          }
00870       }
00871       /* Basic signal level and twist tests */
00872       hit = 0;
00873       if (energy[best] >= BELL_MF_THRESHOLD && energy[second_best] >= BELL_MF_THRESHOLD
00874                && energy[best] < energy[second_best]*BELL_MF_TWIST
00875                && energy[best] * BELL_MF_TWIST > energy[second_best]) {
00876          /* Relative peak test */
00877          hit = -1;
00878          for (i = 0; i < 6; i++) {
00879             if (i != best && i != second_best) {
00880                if (energy[i]*BELL_MF_RELATIVE_PEAK >= energy[second_best]) {
00881                   /* The best two are not clearly the best */
00882                   hit = 0;
00883                   break;
00884                }
00885             }
00886          }
00887       }
00888       if (hit) {
00889          /* Get the values into ascending order */
00890          if (second_best < best) {
00891             i = best;
00892             best = second_best;
00893             second_best = i;
00894          }
00895          best = best * 5 + second_best - 1;
00896          hit = bell_mf_positions[best];
00897          /* Look for two successive similar results */
00898          /* The logic in the next test is:
00899             For KP we need 4 successive identical clean detects, with
00900             two blocks of something different preceeding it. For anything
00901             else we need two successive identical clean detects, with
00902             two blocks of something different preceeding it. */
00903          if (hit == s->td.mf.hits[4] && hit == s->td.mf.hits[3] &&
00904             ((hit != '*' && hit != s->td.mf.hits[2] && hit != s->td.mf.hits[1])||
00905              (hit == '*' && hit == s->td.mf.hits[2] && hit != s->td.mf.hits[1] && 
00906              hit != s->td.mf.hits[0]))) {
00907             store_digit(s, hit);
00908          }
00909       }
00910 
00911 
00912       if (hit != s->td.mf.hits[4] && hit != s->td.mf.hits[3]) {
00913          /* Two successive block without a hit terminate current digit */
00914          s->td.mf.current_hit = 0;
00915       }
00916 
00917       s->td.mf.hits[0] = s->td.mf.hits[1];
00918       s->td.mf.hits[1] = s->td.mf.hits[2];
00919       s->td.mf.hits[2] = s->td.mf.hits[3];
00920       s->td.mf.hits[3] = s->td.mf.hits[4];
00921       s->td.mf.hits[4] = hit;
00922 
00923       /* If we had a hit in this block, include it into mute fragment */
00924       if (squelch && hit) {
00925          if (mute.end < sample - MF_GSIZE) {
00926             /* There is a gap between fragments */
00927             mute_fragment(dsp, &mute);
00928             mute.start = (sample > MF_GSIZE) ? (sample - MF_GSIZE) : 0;
00929          }
00930          mute.end = limit + DTMF_GSIZE;
00931       }
00932 
00933       /* Reinitialise the detector for the next block */
00934       for (i = 0;  i < 6;  i++)
00935          goertzel_reset(&s->td.mf.tone_out[i]);
00936       s->td.mf.current_sample = 0;
00937    }
00938 
00939    if (squelch && mute.end) {
00940       if (mute.end > samples) {
00941          s->td.mf.mute_samples = mute.end - samples;
00942          mute.end = samples;
00943       }
00944       mute_fragment(dsp, &mute);
00945    }
00946 
00947    return (s->td.mf.current_hit); /* return the debounced hit */
00948 }

static void mute_fragment ( struct ast_dsp *  dsp,
fragment_t *  fragment 
) [static]

Definition at line 412 of file dsp.c.

References ARRAY_LEN, ast_log(), LOG_ERROR, ast_dsp::mute_data, and ast_dsp::mute_fragments.

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

00413 {
00414    if (dsp->mute_fragments >= ARRAY_LEN(dsp->mute_data)) {
00415       ast_log(LOG_ERROR, "Too many fragments to mute. Ignoring\n");
00416       return;
00417    }
00418 
00419    dsp->mute_data[dsp->mute_fragments++] = *fragment;
00420 }

static int pair_there ( float  p1,
float  p2,
float  i1,
float  i2,
float  e 
) [inline, static]

Definition at line 950 of file dsp.c.

References TONE_MIN_THRESH, and TONE_THRESH.

Referenced by __ast_dsp_call_progress().

00951 {
00952    /* See if p1 and p2 are there, relative to i1 and i2 and total energy */
00953    /* Make sure absolute levels are high enough */
00954    if ((p1 < TONE_MIN_THRESH) || (p2 < TONE_MIN_THRESH)) {
00955       return 0;
00956    }
00957    /* Amplify ignored stuff */
00958    i2 *= TONE_THRESH;
00959    i1 *= TONE_THRESH;
00960    e *= TONE_THRESH;
00961    /* Check first tone */
00962    if ((p1 < i1) || (p1 < i2) || (p1 < e)) {
00963       return 0;
00964    }
00965    /* And second */
00966    if ((p2 < i1) || (p2 < i2) || (p2 < e)) {
00967       return 0;
00968    }
00969    /* Guess it's there... */
00970    return 1;
00971 }

static void store_digit ( digit_detect_state_t *  s,
char  digit 
) [static]

Definition at line 636 of file dsp.c.

References ast_log(), digit_detect_state_t::current_digits, digit_detect_state_t::detected_digits, digit_detect_state_t::digitlen, digit_detect_state_t::digits, LOG_WARNING, digit_detect_state_t::lost_digits, and MAX_DTMF_DIGITS.

Referenced by dtmf_detect(), and mf_detect().

00637 {
00638    s->detected_digits++;
00639    if (s->current_digits < MAX_DTMF_DIGITS) {
00640       s->digitlen[s->current_digits] = 0;
00641       s->digits[s->current_digits++] = digit;
00642       s->digits[s->current_digits] = '\0';
00643    } else {
00644       ast_log(LOG_WARNING, "Digit lost due to full buffer\n");
00645       s->lost_digits++;
00646    }
00647 }

static int tone_detect ( struct ast_dsp *  dsp,
tone_detect_state_t *  s,
int16_t *  amp,
int  samples 
) [static]

Definition at line 532 of file dsp.c.

References ast_debug, tone_detect_state_t::block_size, fragment_t::end, tone_detect_state_t::energy, tone_detect_state_t::freq, goertzel_reset(), goertzel_result(), goertzel_sample(), tone_detect_state_t::hit_count, tone_detect_state_t::hits_required, tone_detect_state_t::last_hit, mute_fragment(), tone_detect_state_t::mute_samples, ast_frame::ptr, tone_detect_state_t::samples_pending, tone_detect_state_t::squelch, fragment_t::start, tone_detect_state_t::threshold, and tone_detect_state_t::tone.

Referenced by ast_dsp_process().

00533 {
00534    float tone_energy;
00535    int i;
00536    int hit = 0;
00537    int limit;
00538    int res = 0;
00539    int16_t *ptr;
00540    int start, end;
00541    fragment_t mute = {0, 0};
00542 
00543    if (s->squelch && s->mute_samples > 0) {
00544       mute.end = (s->mute_samples < samples) ? s->mute_samples : samples;
00545       s->mute_samples -= mute.end;
00546    }
00547 
00548    for (start = 0;  start < samples;  start = end) {
00549       /* Process in blocks. */
00550       limit = samples - start;
00551       if (limit > s->samples_pending) {
00552          limit = s->samples_pending;
00553       }
00554       end = start + limit;
00555 
00556       for (i = limit, ptr = amp ; i > 0; i--, ptr++) {
00557          /* signed 32 bit int should be enough to suqare any possible signed 16 bit value */
00558          s->energy += (int32_t) *ptr * (int32_t) *ptr;
00559 
00560          goertzel_sample(&s->tone, *ptr);
00561       }
00562 
00563       s->samples_pending -= limit;
00564 
00565       if (s->samples_pending) {
00566          /* Finished incomplete (last) block */
00567          break;
00568       }
00569 
00570       tone_energy = goertzel_result(&s->tone);
00571 
00572       /* Scale to make comparable */
00573       tone_energy *= 2.0;
00574       s->energy *= s->block_size;
00575 
00576       ast_debug(10, "tone %d, Ew=%.2E, Et=%.2E, s/n=%10.2f\n", s->freq, tone_energy, s->energy, tone_energy / (s->energy - tone_energy));
00577       hit = 0;
00578       if (tone_energy > s->energy * s->threshold) {
00579          ast_debug(10, "Hit! count=%d\n", s->hit_count);
00580          hit = 1;
00581       }
00582 
00583       if (s->hit_count) {
00584          s->hit_count++;
00585       }
00586 
00587       if (hit == s->last_hit) {
00588          if (!hit) {
00589             /* Two successive misses. Tone ended */
00590             s->hit_count = 0;
00591          } else if (!s->hit_count) {
00592             s->hit_count++;
00593          }
00594 
00595       }
00596 
00597       if (s->hit_count == s->hits_required) {
00598          ast_debug(1, "%d Hz done detected\n", s->freq);
00599          res = 1;
00600       }
00601 
00602       s->last_hit = hit;
00603 
00604       /* If we had a hit in this block, include it into mute fragment */
00605       if (s->squelch && hit) {
00606          if (mute.end < start - s->block_size) {
00607             /* There is a gap between fragments */
00608             mute_fragment(dsp, &mute);
00609             mute.start = (start > s->block_size) ? (start - s->block_size) : 0;
00610          }
00611          mute.end = end + s->block_size;
00612       }
00613 
00614       /* Reinitialise the detector for the next block */
00615       /* Reset for the next block */
00616       goertzel_reset(&s->tone);
00617 
00618       /* Advance to the next block */
00619       s->energy = 0.0;
00620       s->samples_pending = s->block_size;
00621 
00622       amp += limit;
00623    }
00624 
00625    if (s->squelch && mute.end) {
00626       if (mute.end > samples) {
00627          s->mute_samples = mute.end - samples;
00628          mute.end = samples;
00629       }
00630       mute_fragment(dsp, &mute);
00631    }
00632 
00633    return res;
00634 }


Variable Documentation

struct progalias aliases[] [static]

char bell_mf_positions[] = "1247C-358A--69*---0B----#" [static]

Definition at line 309 of file dsp.c.

const int DEFAULT_SILENCE_THRESHOLD = 256 [static]

The default silence threshold we will use if an alternate configured value is not present or is invalid.

Definition at line 220 of file dsp.c.

float dtmf_col[] [static]

Initial value:

{
   1209.0, 1336.0, 1477.0, 1633.0
}

Definition at line 297 of file dsp.c.

char dtmf_positions[] = "123A" "456B" "789C" "*0#D" [static]

Definition at line 307 of file dsp.c.

float dtmf_row[] [static]

Initial value:

{
   697.0,  770.0,  852.0,  941.0
}

Definition at line 293 of file dsp.c.

float mf_tones[] [static]

Initial value:

{
   700.0, 900.0, 1100.0, 1300.0, 1500.0, 1700.0
}

Definition at line 302 of file dsp.c.

struct progress modes[] [static]

Referenced by ast_dsp_prog_reset(), and setrem().

int thresholds[THRESHOLD_MAX] [static]

Definition at line 311 of file dsp.c.


Generated on Wed Aug 11 01:08:00 2010 for Asterisk - the Open Source PBX by  doxygen 1.5.9