Fix MF detector, merge code from Steve Underwood for optional new detectors

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@1858 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Mark Spencer
2003-12-15 15:12:28 +00:00
parent 6fb9ea4ea5
commit 843ae2cc2c

341
dsp.c
View File

@@ -79,6 +79,11 @@
#define MAX_DTMF_DIGITS 128 #define MAX_DTMF_DIGITS 128
/*
* Comment out the following line to use the new DSP routines.
*/
#define OLD_DSP_ROUTINES
/* Basic DTMF specs: /* Basic DTMF specs:
* *
* Minimum tone on = 40ms * Minimum tone on = 40ms
@@ -100,55 +105,76 @@
#define DTMF_RELATIVE_PEAK_COL 6.3 /* 8dB */ #define DTMF_RELATIVE_PEAK_COL 6.3 /* 8dB */
#define DTMF_2ND_HARMONIC_ROW ((digitmode & DSP_DIGITMODE_RELAXDTMF) ? 1.7 : 2.5) /* 4dB normal */ #define DTMF_2ND_HARMONIC_ROW ((digitmode & DSP_DIGITMODE_RELAXDTMF) ? 1.7 : 2.5) /* 4dB normal */
#define DTMF_2ND_HARMONIC_COL 63.1 /* 18dB */ #define DTMF_2ND_HARMONIC_COL 63.1 /* 18dB */
#ifndef OLD_DSP_ROUTINES
#define DTMF_TO_TOTAL_ENERGY 42.0
#endif
#ifdef OLD_DSP_ROUTINES
#define MF_THRESHOLD 8.0e7 #define MF_THRESHOLD 8.0e7
#define MF_NORMAL_TWIST 5.3 /* 8dB */ #define MF_NORMAL_TWIST 5.3 /* 8dB */
#define MF_REVERSE_TWIST 4.0 /* was 2.5 */ #define MF_REVERSE_TWIST 4.0 /* was 2.5 */
#define MF_RELATIVE_PEAK 5.3 /* 8dB */ #define MF_RELATIVE_PEAK 5.3 /* 8dB */
#define MF_2ND_HARMONIC 1.7 /* was 2.5 */ #define MF_2ND_HARMONIC 1.7 /* was 2.5 */
#else
#define BELL_MF_THRESHOLD 1.6e9
#define BELL_MF_TWIST 4.0 /* 6dB */
#define BELL_MF_RELATIVE_PEAK 12.6 /* 11dB */
#endif
typedef struct { typedef struct {
float v2; float v2;
float v3; float v3;
float fac; float fac;
#ifndef OLD_DSP_ROUTINES
int samples;
#endif
} goertzel_state_t; } goertzel_state_t;
typedef struct typedef struct
{ {
goertzel_state_t row_out[4];
goertzel_state_t col_out[4];
goertzel_state_t fax_tone;
#ifdef OLD_DSP_ROUTINES
goertzel_state_t row_out2nd[4];
goertzel_state_t col_out2nd[4];
goertzel_state_t fax_tone2nd;
int hit1; int hit1;
int hit2; int hit2;
int hit3; int hit3;
int hit4; int hit4;
#else
int hits[3];
#endif
int mhit; int mhit;
goertzel_state_t row_out[4];
goertzel_state_t col_out[4];
goertzel_state_t row_out2nd[4];
goertzel_state_t col_out2nd[4];
goertzel_state_t fax_tone;
goertzel_state_t fax_tone2nd;
float energy; float energy;
int current_sample; int current_sample;
char digits[MAX_DTMF_DIGITS + 1]; char digits[MAX_DTMF_DIGITS + 1];
int current_digits; int current_digits;
int detected_digits; int detected_digits;
int lost_digits; int lost_digits;
int digit_hits[16]; int digit_hits[16];
int fax_hits; int fax_hits;
} dtmf_detect_state_t; } dtmf_detect_state_t;
typedef struct typedef struct
{ {
goertzel_state_t tone_out[6];
int mhit;
#ifdef OLD_DSP_ROUTINES
int hit1; int hit1;
int hit2; int hit2;
int hit3; int hit3;
int hit4; int hit4;
int mhit;
goertzel_state_t tone_out[6];
goertzel_state_t tone_out2nd[6]; goertzel_state_t tone_out2nd[6];
float energy; float energy;
#else
int hits[5];
#endif
int current_sample; int current_sample;
char digits[MAX_DTMF_DIGITS + 1]; char digits[MAX_DTMF_DIGITS + 1];
@@ -176,6 +202,7 @@ static float fax_freq = 1100.0;
static char dtmf_positions[] = "123A" "456B" "789C" "*0#D"; static char dtmf_positions[] = "123A" "456B" "789C" "*0#D";
#ifdef OLD_DSP_ROUTINES
static char mf_hit[6][6] = { static char mf_hit[6][6] = {
/* 700 + */ { 0, '1', '2', '4', '7', 'C' }, /* 700 + */ { 0, '1', '2', '4', '7', 'C' },
/* 900 + */ { '1', 0, '3', '5', '8', 'A' }, /* 900 + */ { '1', 0, '3', '5', '8', 'A' },
@@ -184,6 +211,9 @@ static char mf_hit[6][6] = {
/* 1500 + */ { '7', '8', '9', '0', 0, '#' }, /* 1500 + */ { '7', '8', '9', '0', 0, '#' },
/* 1700 + */ { 'C', 'A', '*', 'B', '#', 0 }, /* 1700 + */ { 'C', 'A', '*', 'B', '#', 0 },
}; };
#else
static char bell_mf_positions[] = "1247C-358A--69*---0B----#";
#endif
static inline void goertzel_sample(goertzel_state_t *s, short sample) static inline void goertzel_sample(goertzel_state_t *s, short sample)
{ {
@@ -207,10 +237,13 @@ static inline float goertzel_result(goertzel_state_t *s)
return s->v3 * s->v3 + s->v2 * s->v2 - s->v2 * s->v3 * s->fac; return s->v3 * s->v3 + s->v2 * s->v2 - s->v2 * s->v3 * s->fac;
} }
static inline void goertzel_init(goertzel_state_t *s, float freq) static inline void goertzel_init(goertzel_state_t *s, float freq, int samples)
{ {
s->v2 = s->v3 = 0.0; s->v2 = s->v3 = 0.0;
s->fac = 2.0 * cos(2.0 * M_PI * (freq / 8000.0)); s->fac = 2.0 * cos(2.0 * M_PI * (freq / 8000.0));
#ifndef OLD_DSP_ROUTINES
s->samples = samples;
#endif
} }
static inline void goertzel_reset(goertzel_state_t *s) static inline void goertzel_reset(goertzel_state_t *s)
@@ -245,25 +278,34 @@ static void ast_dtmf_detect_init (dtmf_detect_state_t *s)
{ {
int i; int i;
#ifdef OLD_DSP_ROUTINES
s->hit1 = s->hit1 =
s->mhit =
s->hit3 =
s->hit4 =
s->hit2 = 0; s->hit2 = 0;
#else
s->hits[0] = s->hits[1] = s->hits[2] = 0;
#endif
for (i = 0; i < 4; i++) for (i = 0; i < 4; i++)
{ {
goertzel_init (&s->row_out[i], dtmf_row[i]); goertzel_init (&s->row_out[i], dtmf_row[i], 102);
goertzel_init (&s->col_out[i], dtmf_col[i]); goertzel_init (&s->col_out[i], dtmf_col[i], 102);
goertzel_init (&s->row_out2nd[i], dtmf_row[i] * 2.0); #ifdef OLD_DSP_ROUTINES
goertzel_init (&s->col_out2nd[i], dtmf_col[i] * 2.0); goertzel_init (&s->row_out2nd[i], dtmf_row[i] * 2.0, 102);
goertzel_init (&s->col_out2nd[i], dtmf_col[i] * 2.0, 102);
#endif
s->energy = 0.0; s->energy = 0.0;
} }
/* Same for the fax dector */ /* Same for the fax dector */
goertzel_init (&s->fax_tone, fax_freq); goertzel_init (&s->fax_tone, fax_freq, 102);
#ifdef OLD_DSP_ROUTINES
/* Same for the fax dector 2nd harmonic */ /* Same for the fax dector 2nd harmonic */
goertzel_init (&s->fax_tone2nd, fax_freq * 2.0); goertzel_init (&s->fax_tone2nd, fax_freq * 2.0, 102);
#endif
s->current_sample = 0; s->current_sample = 0;
s->detected_digits = 0; s->detected_digits = 0;
@@ -271,23 +313,27 @@ static void ast_dtmf_detect_init (dtmf_detect_state_t *s)
memset(&s->digits, 0, sizeof(s->digits)); memset(&s->digits, 0, sizeof(s->digits));
s->lost_digits = 0; s->lost_digits = 0;
s->digits[0] = '\0'; s->digits[0] = '\0';
s->mhit = 0;
} }
static void ast_mf_detect_init (mf_detect_state_t *s) static void ast_mf_detect_init (mf_detect_state_t *s)
{ {
int i; int i;
#ifdef OLD_DSP_ROUTINES
s->hit1 = s->hit1 =
s->hit2 = 0; s->hit2 = 0;
#else
s->hits[0] = s->hits[1] = s->hits[2] = s->hits[3] = s->hits[4] = 0;
#endif
for (i = 0; i < 6; i++) for (i = 0; i < 6; i++)
{ {
goertzel_init (&s->tone_out[i], mf_tones[i]); goertzel_init (&s->tone_out[i], mf_tones[i], 160);
goertzel_init (&s->tone_out2nd[i], mf_tones[i] * 2.0); #ifdef OLD_DSP_ROUTINES
goertzel_init (&s->tone_out2nd[i], mf_tones[i] * 2.0, 160);
s->energy = 0.0; s->energy = 0.0;
#endif
} }
s->current_digits = 0; s->current_digits = 0;
@@ -308,7 +354,9 @@ static int dtmf_detect (dtmf_detect_state_t *s,
float row_energy[4]; float row_energy[4];
float col_energy[4]; float col_energy[4];
float fax_energy; float fax_energy;
#ifdef OLD_DSP_ROUTINES
float fax_energy_2nd; float fax_energy_2nd;
#endif
float famp; float famp;
float v1; float v1;
int i; int i;
@@ -330,8 +378,10 @@ static int dtmf_detect (dtmf_detect_state_t *s,
#if defined(USE_3DNOW) #if defined(USE_3DNOW)
_dtmf_goertzel_update (s->row_out, amp + sample, limit - sample); _dtmf_goertzel_update (s->row_out, amp + sample, limit - sample);
_dtmf_goertzel_update (s->col_out, amp + sample, limit - sample); _dtmf_goertzel_update (s->col_out, amp + sample, limit - sample);
#ifdef OLD_DSP_ROUTINES
_dtmf_goertzel_update (s->row_out2nd, amp + sample, limit2 - sample); _dtmf_goertzel_update (s->row_out2nd, amp + sample, limit2 - sample);
_dtmf_goertzel_update (s->col_out2nd, amp + sample, limit2 - sample); _dtmf_goertzel_update (s->col_out2nd, amp + sample, limit2 - sample);
#endif
/* XXX Need to fax detect for 3dnow too XXX */ /* XXX Need to fax detect for 3dnow too XXX */
#warning "Fax Support Broken" #warning "Fax Support Broken"
#else #else
@@ -377,6 +427,12 @@ static int dtmf_detect (dtmf_detect_state_t *s,
s->col_out[3].v2 = s->col_out[3].v3; s->col_out[3].v2 = s->col_out[3].v3;
s->col_out[3].v3 = s->col_out[3].fac*s->col_out[3].v2 - v1 + famp; s->col_out[3].v3 = s->col_out[3].fac*s->col_out[3].v2 - v1 + famp;
/* Update fax tone */
v1 = s->fax_tone.v2;
s->fax_tone.v2 = s->fax_tone.v3;
s->fax_tone.v3 = s->fax_tone.fac*s->fax_tone.v2 - v1 + famp;
#ifdef OLD_DSP_ROUTINES
v1 = s->col_out2nd[0].v2; v1 = s->col_out2nd[0].v2;
s->col_out2nd[0].v2 = s->col_out2nd[0].v3; s->col_out2nd[0].v2 = s->col_out2nd[0].v3;
s->col_out2nd[0].v3 = s->col_out2nd[0].fac*s->col_out2nd[0].v2 - v1 + famp; s->col_out2nd[0].v3 = s->col_out2nd[0].fac*s->col_out2nd[0].v2 - v1 + famp;
@@ -409,14 +465,10 @@ static int dtmf_detect (dtmf_detect_state_t *s,
s->row_out2nd[3].v2 = s->row_out2nd[3].v3; s->row_out2nd[3].v2 = s->row_out2nd[3].v3;
s->row_out2nd[3].v3 = s->row_out2nd[3].fac*s->row_out2nd[3].v2 - v1 + famp; s->row_out2nd[3].v3 = s->row_out2nd[3].fac*s->row_out2nd[3].v2 - v1 + famp;
/* Update fax tone */
v1 = s->fax_tone.v2;
s->fax_tone.v2 = s->fax_tone.v3;
s->fax_tone.v3 = s->fax_tone.fac*s->fax_tone.v2 - v1 + famp;
v1 = s->fax_tone.v2; v1 = s->fax_tone.v2;
s->fax_tone2nd.v2 = s->fax_tone2nd.v3; s->fax_tone2nd.v2 = s->fax_tone2nd.v3;
s->fax_tone2nd.v3 = s->fax_tone2nd.fac*s->fax_tone2nd.v2 - v1 + famp; s->fax_tone2nd.v3 = s->fax_tone2nd.fac*s->fax_tone2nd.v2 - v1 + famp;
#endif
} }
#endif #endif
s->current_sample += (limit - sample); s->current_sample += (limit - sample);
@@ -468,6 +520,7 @@ static int dtmf_detect (dtmf_detect_state_t *s,
break; break;
} }
} }
#ifdef OLD_DSP_ROUTINES
/* ... and second harmonic test */ /* ... and second harmonic test */
if (i >= 4 if (i >= 4
&& &&
@@ -476,6 +529,12 @@ static int dtmf_detect (dtmf_detect_state_t *s,
goertzel_result (&s->col_out2nd[best_col])*DTMF_2ND_HARMONIC_COL < col_energy[best_col] goertzel_result (&s->col_out2nd[best_col])*DTMF_2ND_HARMONIC_COL < col_energy[best_col]
&& &&
goertzel_result (&s->row_out2nd[best_row])*DTMF_2ND_HARMONIC_ROW < row_energy[best_row]) goertzel_result (&s->row_out2nd[best_row])*DTMF_2ND_HARMONIC_ROW < row_energy[best_row])
#else
/* ... and fraction of total energy test */
if (i >= 4
&&
(row_energy[best_row] + col_energy[best_col]) > DTMF_TO_TOTAL_ENERGY*s->energy)
#endif
{ {
/* Got a hit */ /* Got a hit */
hit = dtmf_positions[(best_row << 2) + best_col]; hit = dtmf_positions[(best_row << 2) + best_col];
@@ -492,6 +551,8 @@ static int dtmf_detect (dtmf_detect_state_t *s,
back to back differing digits. More importantly, it back to back differing digits. More importantly, it
can work with nasty phones that give a very wobbly start can work with nasty phones that give a very wobbly start
to a digit. */ to a digit. */
#ifdef OLD_DSP_ROUTINES
if (hit == s->hit3 && s->hit3 != s->hit2) if (hit == s->hit3 && s->hit3 != s->hit2)
{ {
s->mhit = hit; s->mhit = hit;
@@ -507,9 +568,28 @@ static int dtmf_detect (dtmf_detect_state_t *s,
s->lost_digits++; s->lost_digits++;
} }
} }
#else
if (hit == s->hits[2] && hit != s->hits[1] && hit != s->hits[0])
{
s->mhit = hit;
s->digit_hits[(best_row << 2) + best_col]++;
s->detected_digits++;
if (s->current_digits < MAX_DTMF_DIGITS)
{
s->digits[s->current_digits++] = hit;
s->digits[s->current_digits] = '\0';
}
else
{
s->lost_digits++;
} }
} }
#endif
}
}
#ifdef OLD_DSP_ROUTINES
if (!hit && (fax_energy >= FAX_THRESHOLD) && (fax_energy > s->energy * 21.0)) { if (!hit && (fax_energy >= FAX_THRESHOLD) && (fax_energy > s->energy * 21.0)) {
fax_energy_2nd = goertzel_result(&s->fax_tone2nd);
fax_energy_2nd = goertzel_result(&s->fax_tone2nd); fax_energy_2nd = goertzel_result(&s->fax_tone2nd);
if (fax_energy_2nd * FAX_2ND_HARMONIC < fax_energy) { if (fax_energy_2nd * FAX_2ND_HARMONIC < fax_energy) {
#if 0 #if 0
@@ -519,7 +599,18 @@ static int dtmf_detect (dtmf_detect_state_t *s,
hit = 'f'; hit = 'f';
s->fax_hits++; s->fax_hits++;
} /* Don't reset fax hits counter */ } /* Don't reset fax hits counter */
} else { }
#else /* OLD_DSP_ROUTINES */
if (!hit && (fax_energy >= DTMF_TO_TOTAL_ENERGY*s->energy)) {
#if 0
printf("Fax energy/Second Harmonic: %f\n", fax_energy);
#endif
/* XXX Probably need better checking than just this the energy XXX */
hit = 'f';
s->fax_hits++;
}
#endif /* OLD_DSP_ROUTINES */
else {
if (s->fax_hits > 5) { if (s->fax_hits > 5) {
hit = 'f'; hit = 'f';
s->mhit = 'f'; s->mhit = 'f';
@@ -536,19 +627,29 @@ static int dtmf_detect (dtmf_detect_state_t *s,
} }
s->fax_hits = 0; s->fax_hits = 0;
} }
#ifdef OLD_DSP_ROUTINES
s->hit1 = s->hit2; s->hit1 = s->hit2;
s->hit2 = s->hit3; s->hit2 = s->hit3;
s->hit3 = hit; s->hit3 = hit;
#else
s->hits[0] = s->hits[1];
s->hits[1] = s->hits[2];
s->hits[2] = hit;
#endif
/* Reinitialise the detector for the next block */ /* Reinitialise the detector for the next block */
for (i = 0; i < 4; i++) for (i = 0; i < 4; i++)
{ {
goertzel_reset(&s->row_out[i]); goertzel_reset(&s->row_out[i]);
goertzel_reset(&s->col_out[i]); goertzel_reset(&s->col_out[i]);
#ifdef OLD_DSP_ROUTINES
goertzel_reset(&s->row_out2nd[i]); goertzel_reset(&s->row_out2nd[i]);
goertzel_reset(&s->col_out2nd[i]); goertzel_reset(&s->col_out2nd[i]);
#endif
} }
goertzel_reset (&s->fax_tone); goertzel_reset (&s->fax_tone);
#ifdef OLD_DSP_ROUTINES
goertzel_reset (&s->fax_tone2nd); goertzel_reset (&s->fax_tone2nd);
#endif
s->energy = 0.0; s->energy = 0.0;
s->current_sample = 0; s->current_sample = 0;
} }
@@ -561,7 +662,11 @@ static int dtmf_detect (dtmf_detect_state_t *s,
} }
/* MF goertzel size */ /* MF goertzel size */
#ifdef OLD_DSP_ROUTINES
#define MF_GSIZE 160 #define MF_GSIZE 160
#else
#define MF_GSIZE 120
#endif
static int mf_detect (mf_detect_state_t *s, static int mf_detect (mf_detect_state_t *s,
int16_t amp[], int16_t amp[],
@@ -569,18 +674,24 @@ static int mf_detect (mf_detect_state_t *s,
int digitmode, int *writeback) int digitmode, int *writeback)
{ {
#ifdef OLD_DSP_ROUTINES
float tone_energy[6]; float tone_energy[6];
int best1;
int best2;
float max;
int sofarsogood;
#else
float energy[6];
int best;
int second_best;
#endif
float famp; float famp;
float v1; float v1;
int i; int i;
int j; int j;
int sample; int sample;
int best1;
int best2;
float max;
int hit; int hit;
int limit; int limit;
int sofarsogood;
hit = 0; hit = 0;
for (sample = 0; sample < samples; sample = limit) for (sample = 0; sample < samples; sample = limit)
@@ -593,8 +704,10 @@ static int mf_detect (mf_detect_state_t *s,
#if defined(USE_3DNOW) #if defined(USE_3DNOW)
_dtmf_goertzel_update (s->row_out, amp + sample, limit - sample); _dtmf_goertzel_update (s->row_out, amp + sample, limit - sample);
_dtmf_goertzel_update (s->col_out, amp + sample, limit - sample); _dtmf_goertzel_update (s->col_out, amp + sample, limit - sample);
#ifdef OLD_DSP_ROUTINES
_dtmf_goertzel_update (s->row_out2nd, amp + sample, limit2 - sample); _dtmf_goertzel_update (s->row_out2nd, amp + sample, limit2 - sample);
_dtmf_goertzel_update (s->col_out2nd, amp + sample, limit2 - sample); _dtmf_goertzel_update (s->col_out2nd, amp + sample, limit2 - sample);
#endif
/* XXX Need to fax detect for 3dnow too XXX */ /* XXX Need to fax detect for 3dnow too XXX */
#warning "Fax Support Broken" #warning "Fax Support Broken"
#else #else
@@ -604,7 +717,9 @@ static int mf_detect (mf_detect_state_t *s,
{ {
famp = amp[j]; famp = amp[j];
#ifdef OLD_DSP_ROUTINES
s->energy += famp*famp; s->energy += famp*famp;
#endif
/* With GCC 2.95, the following unrolled code seems to take about 35% /* With GCC 2.95, the following unrolled code seems to take about 35%
(rough estimate) as long as a neat little 0-3 loop */ (rough estimate) as long as a neat little 0-3 loop */
@@ -632,6 +747,7 @@ static int mf_detect (mf_detect_state_t *s,
s->tone_out[5].v2 = s->tone_out[5].v3; s->tone_out[5].v2 = s->tone_out[5].v3;
s->tone_out[5].v3 = s->tone_out[5].fac*s->tone_out[5].v2 - v1 + famp; s->tone_out[5].v3 = s->tone_out[5].fac*s->tone_out[5].v2 - v1 + famp;
#ifdef OLD_DSP_ROUTINES
v1 = s->tone_out2nd[0].v2; v1 = s->tone_out2nd[0].v2;
s->tone_out2nd[0].v2 = s->tone_out2nd[0].v3; s->tone_out2nd[0].v2 = s->tone_out2nd[0].v3;
s->tone_out2nd[0].v3 = s->tone_out2nd[0].fac*s->tone_out2nd[0].v2 - v1 + famp; s->tone_out2nd[0].v3 = s->tone_out2nd[0].fac*s->tone_out2nd[0].v2 - v1 + famp;
@@ -655,7 +771,7 @@ static int mf_detect (mf_detect_state_t *s,
v1 = s->tone_out2nd[3].v2; v1 = s->tone_out2nd[3].v2;
s->tone_out2nd[5].v2 = s->tone_out2nd[6].v3; s->tone_out2nd[5].v2 = s->tone_out2nd[6].v3;
s->tone_out2nd[5].v3 = s->tone_out2nd[6].fac*s->tone_out2nd[3].v2 - v1 + famp; s->tone_out2nd[5].v3 = s->tone_out2nd[6].fac*s->tone_out2nd[3].v2 - v1 + famp;
#endif
} }
#endif #endif
s->current_sample += (limit - sample); s->current_sample += (limit - sample);
@@ -670,12 +786,13 @@ static int mf_detect (mf_detect_state_t *s,
continue; continue;
} }
#ifdef OLD_DSP_ROUTINES
/* We're at the end of an MF detection block. Go ahead and calculate /* We're at the end of an MF detection block. Go ahead and calculate
all the energies. */ all the energies. */
for (i=0;i<6;i++) { for (i=0;i<6;i++) {
tone_energy[i] = goertzel_result(&s->tone_out[i]); tone_energy[i] = goertzel_result(&s->tone_out[i]);
} }
/* Find highest */ /* Find highest */
best1 = 0; best1 = 0;
max = tone_energy[0]; max = tone_energy[0];
@@ -687,11 +804,14 @@ static int mf_detect (mf_detect_state_t *s,
} }
/* Find 2nd highest */ /* Find 2nd highest */
if (best1) if (best1) {
max = tone_energy[0]; max = tone_energy[0];
else
max = tone_energy[1];
best2 = 0; best2 = 0;
} else {
max = tone_energy[1];
best2 = 1;
}
for (i=0;i<6;i++) { for (i=0;i<6;i++) {
if (i == best1) continue; if (i == best1) continue;
if (tone_energy[i] > max) { if (tone_energy[i] > max) {
@@ -757,6 +877,117 @@ static int mf_detect (mf_detect_state_t *s,
s->energy = 0.0; s->energy = 0.0;
s->current_sample = 0; s->current_sample = 0;
} }
#else
/* We're at the end of an MF detection block. */
/* Find the two highest energies. The spec says to look for
two tones and two tones only. Taking this literally -ie
only two tones pass the minimum threshold - doesn't work
well. The sinc function mess, due to rectangular windowing
ensure that! Find the two highest energies and ensure they
are considerably stronger than any of the others. */
energy[0] = goertzel_result(&s->tone_out[0]);
energy[1] = goertzel_result(&s->tone_out[1]);
if (energy[0] > energy[1])
{
best = 0;
second_best = 1;
}
else
{
best = 1;
second_best = 0;
}
/*endif*/
for (i = 2; i < 6; i++)
{
energy[i] = goertzel_result(&s->tone_out[i]);
if (energy[i] >= energy[best])
{
second_best = best;
best = i;
}
else if (energy[i] >= energy[second_best])
{
second_best = i;
}
}
/* Basic signal level and twist tests */
hit = 0;
if (energy[best] >= BELL_MF_THRESHOLD
&&
energy[second_best] >= BELL_MF_THRESHOLD
&&
energy[best] < energy[second_best]*BELL_MF_TWIST
&&
energy[best]*BELL_MF_TWIST > energy[second_best])
{
/* Relative peak test */
hit = -1;
for (i = 0; i < 6; i++)
{
if (i != best && i != second_best)
{
if (energy[i]*BELL_MF_RELATIVE_PEAK >= energy[second_best])
{
/* The best two are not clearly the best */
hit = 0;
break;
}
}
}
}
if (hit)
{
/* Get the values into ascending order */
if (second_best < best)
{
i = best;
best = second_best;
second_best = i;
}
best = best*5 + second_best - 1;
hit = bell_mf_positions[best];
/* Look for two successive similar results */
/* The logic in the next test is:
For KP we need 4 successive identical clean detects, with
two blocks of something different preceeding it. For anything
else we need two successive identical clean detects, with
two blocks of something different preceeding it. */
if (hit == s->hits[4]
&&
hit == s->hits[3]
&&
((hit != '*' && hit != s->hits[2] && hit != s->hits[1])
||
(hit == '*' && hit == s->hits[2] && hit != s->hits[1] && hit != s->hits[0])))
{
s->detected_digits++;
if (s->current_digits < MAX_DTMF_DIGITS)
{
s->digits[s->current_digits++] = hit;
s->digits[s->current_digits] = '\0';
}
else
{
s->lost_digits++;
}
}
}
else
{
hit = 0;
}
s->hits[0] = s->hits[1];
s->hits[1] = s->hits[2];
s->hits[2] = s->hits[3];
s->hits[3] = s->hits[4];
s->hits[4] = hit;
/* Reinitialise the detector for the next block */
for (i = 0; i < 6; i++)
goertzel_reset(&s->tone_out[i]);
s->current_sample = 0;
}
#endif
if ((!s->mhit) || (s->mhit != hit)) if ((!s->mhit) || (s->mhit != hit))
{ {
s->mhit = 0; s->mhit = 0;
@@ -1337,13 +1568,13 @@ struct ast_dsp *ast_dsp_new(void)
dsp->features = DSP_FEATURE_SILENCE_SUPPRESS; dsp->features = DSP_FEATURE_SILENCE_SUPPRESS;
dsp->busycount = DSP_HISTORY; dsp->busycount = DSP_HISTORY;
/* Initialize goertzels */ /* Initialize goertzels */
goertzel_init(&dsp->freqs[HZ_350], 350.0); goertzel_init(&dsp->freqs[HZ_350], 350.0, GSAMP_SIZE);
goertzel_init(&dsp->freqs[HZ_440], 440.0); goertzel_init(&dsp->freqs[HZ_440], 440.0, GSAMP_SIZE);
goertzel_init(&dsp->freqs[HZ_480], 480.0); goertzel_init(&dsp->freqs[HZ_480], 480.0, GSAMP_SIZE);
goertzel_init(&dsp->freqs[HZ_620], 620.0); goertzel_init(&dsp->freqs[HZ_620], 620.0, GSAMP_SIZE);
goertzel_init(&dsp->freqs[HZ_950], 950.0); goertzel_init(&dsp->freqs[HZ_950], 950.0, GSAMP_SIZE);
goertzel_init(&dsp->freqs[HZ_1400], 1400.0); goertzel_init(&dsp->freqs[HZ_1400], 1400.0, GSAMP_SIZE);
goertzel_init(&dsp->freqs[HZ_1800], 1800.0); goertzel_init(&dsp->freqs[HZ_1800], 1800.0, GSAMP_SIZE);
/* Initialize DTMF detector */ /* Initialize DTMF detector */
ast_dtmf_detect_init(&dsp->td.dtmf); ast_dtmf_detect_init(&dsp->td.dtmf);
} }
@@ -1384,11 +1615,17 @@ void ast_dsp_digitreset(struct ast_dsp *dsp)
/* Reinitialise the detector for the next block */ /* Reinitialise the detector for the next block */
for (i = 0; i < 6; i++) { for (i = 0; i < 6; i++) {
goertzel_reset(&dsp->td.mf.tone_out[i]); goertzel_reset(&dsp->td.mf.tone_out[i]);
#ifdef OLD_DSP_ROUTINES
goertzel_reset(&dsp->td.mf.tone_out2nd[i]); goertzel_reset(&dsp->td.mf.tone_out2nd[i]);
#endif
} }
#ifdef OLD_DSP_ROUTINES
dsp->td.mf.energy = 0.0; dsp->td.mf.energy = 0.0;
dsp->td.mf.current_sample = 0;
dsp->td.mf.hit1 = dsp->td.mf.hit2 = dsp->td.mf.hit3 = dsp->td.mf.hit4 = dsp->td.mf.mhit = 0; dsp->td.mf.hit1 = dsp->td.mf.hit2 = dsp->td.mf.hit3 = dsp->td.mf.hit4 = dsp->td.mf.mhit = 0;
#else
dsp->td.mf.hits[4] = dsp->td.mf.hits[3] = dsp->td.mf.hits[2] = dsp->td.mf.hits[1] = dsp->td.mf.hits[0] = dsp->td.mf.mhit = 0;
#endif
dsp->td.mf.current_sample = 0;
} else { } else {
memset(dsp->td.dtmf.digits, 0, sizeof(dsp->td.dtmf.digits)); memset(dsp->td.dtmf.digits, 0, sizeof(dsp->td.dtmf.digits));
dsp->td.dtmf.current_digits = 0; dsp->td.dtmf.current_digits = 0;
@@ -1396,14 +1633,20 @@ void ast_dsp_digitreset(struct ast_dsp *dsp)
for (i = 0; i < 4; i++) { for (i = 0; i < 4; i++) {
goertzel_reset(&dsp->td.dtmf.row_out[i]); goertzel_reset(&dsp->td.dtmf.row_out[i]);
goertzel_reset(&dsp->td.dtmf.col_out[i]); goertzel_reset(&dsp->td.dtmf.col_out[i]);
#ifdef OLD_DSP_ROUTINES
goertzel_reset(&dsp->td.dtmf.row_out2nd[i]); goertzel_reset(&dsp->td.dtmf.row_out2nd[i]);
goertzel_reset(&dsp->td.dtmf.col_out2nd[i]); goertzel_reset(&dsp->td.dtmf.col_out2nd[i]);
#endif
} }
goertzel_reset (&dsp->td.dtmf.fax_tone); goertzel_reset (&dsp->td.dtmf.fax_tone);
#ifdef OLD_DSP_ROUTINES
goertzel_reset (&dsp->td.dtmf.fax_tone2nd); goertzel_reset (&dsp->td.dtmf.fax_tone2nd);
dsp->td.dtmf.hit1 = dsp->td.dtmf.hit2 = dsp->td.dtmf.hit3 = dsp->td.dtmf.hit4 = dsp->td.dtmf.mhit = 0;
#else
dsp->td.dtmf.hits[2] = dsp->td.dtmf.hits[1] = dsp->td.dtmf.hits[0] = dsp->td.dtmf.mhit = 0;
#endif
dsp->td.dtmf.energy = 0.0; dsp->td.dtmf.energy = 0.0;
dsp->td.dtmf.current_sample = 0; dsp->td.dtmf.current_sample = 0;
dsp->td.dtmf.hit1 = dsp->td.dtmf.hit2 = dsp->td.dtmf.hit3 = dsp->td.dtmf.hit4 = dsp->td.dtmf.mhit = 0;
} }
} }