Skip to content

Commit 7df6963

Browse files
InterLinked1gtjoseph
authored andcommitted
res_tonedetect: Tone detection module
dsp.c contains arbitrary tone detection functionality which is currently only used for fax tone recognition. This change makes this functionality publicly accessible so that other modules can take advantage of this. Additionally, a WaitForTone and TONE_DETECT app and function are included to allow users to do their own tone detection operations in the dialplan. ASTERISK-29546 Change-Id: Ie38c395000f4fd4d04e942e8658e177f8f499b26
1 parent 448962d commit 7df6963

File tree

4 files changed

+719
-2
lines changed

4 files changed

+719
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Subject: res_tonedetect
2+
3+
Arbitrary tone detection is now available through a
4+
WaitForTone application (blocking) and a TONE_DETECT
5+
function (non-blocking).

include/asterisk/dsp.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#define DSP_PROGRESS_CONGESTION (1 << 19) /*!< Enable congestion tone detection */
4343
#define DSP_FEATURE_CALL_PROGRESS (DSP_PROGRESS_TALK | DSP_PROGRESS_RINGING | DSP_PROGRESS_BUSY | DSP_PROGRESS_CONGESTION)
4444
#define DSP_FEATURE_WAITDIALTONE (1 << 20) /*!< Enable dial tone detection */
45+
#define DSP_FEATURE_FREQ_DETECT (1 << 21) /*!< Enable arbitrary tone detection */
4546

4647
#define DSP_FAXMODE_DETECT_CNG (1 << 0)
4748
#define DSP_FAXMODE_DETECT_CED (1 << 1)
@@ -171,6 +172,9 @@ int ast_dsp_getdigits(struct ast_dsp *dsp, char *buf, int max);
171172
*/
172173
int ast_dsp_set_digitmode(struct ast_dsp *dsp, int digitmode);
173174

175+
/*! \brief Set arbitrary frequency detection mode */
176+
int ast_dsp_set_freqmode(struct ast_dsp *dsp, int freq, int dur, int db, int squelch);
177+
174178
/*! \brief Set fax mode */
175179
int ast_dsp_set_faxmode(struct ast_dsp *dsp, int faxmode);
176180

main/dsp.c

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ struct ast_dsp {
425425
int tcount;
426426
int digitmode;
427427
int faxmode;
428+
int freqmode;
428429
int dtmf_began;
429430
int display_inband_dtmf_warning;
430431
float genergy;
@@ -476,7 +477,7 @@ static void ast_tone_detect_init(tone_detect_state_t *s, int freq, int duration,
476477
/* Now calculate final block size. It will contain integer number of periods */
477478
s->block_size = periods_in_block * sample_rate / freq;
478479

479-
/* tone_detect is currently only used to detect fax tones and we
480+
/* tone_detect is generally only used to detect fax tones and we
480481
do not need squelching the fax tones */
481482
s->squelch = 0;
482483

@@ -518,6 +519,15 @@ static void ast_fax_detect_init(struct ast_dsp *s)
518519

519520
}
520521

522+
static void ast_freq_detect_init(struct ast_dsp *s, int freq, int dur, int db, int squelch)
523+
{
524+
/* we can conveniently just use one of the two fax tone states */
525+
ast_tone_detect_init(&s->cng_tone_state, freq, dur, db, s->sample_rate);
526+
if (s->freqmode & squelch) {
527+
s->cng_tone_state.squelch = 1;
528+
}
529+
}
530+
521531
static void ast_dtmf_detect_init(dtmf_detect_state_t *s, unsigned int sample_rate)
522532
{
523533
int i;
@@ -1485,7 +1495,7 @@ struct ast_frame *ast_dsp_process(struct ast_channel *chan, struct ast_dsp *dsp,
14851495
{
14861496
int silence;
14871497
int res;
1488-
int digit = 0, fax_digit = 0;
1498+
int digit = 0, fax_digit = 0, custom_freq_digit = 0;
14891499
int x;
14901500
short *shortdata;
14911501
unsigned char *odata;
@@ -1558,6 +1568,12 @@ struct ast_frame *ast_dsp_process(struct ast_channel *chan, struct ast_dsp *dsp,
15581568
}
15591569
}
15601570

1571+
if ((dsp->features & DSP_FEATURE_FREQ_DETECT)) {
1572+
if ((dsp->freqmode) && tone_detect(dsp, &dsp->cng_tone_state, shortdata, len)) {
1573+
custom_freq_digit = 'q';
1574+
}
1575+
}
1576+
15611577
if (dsp->features & (DSP_FEATURE_DIGIT_DETECT | DSP_FEATURE_BUSY_DETECT)) {
15621578
if (dsp->digitmode & DSP_DIGITMODE_MF) {
15631579
digit = mf_detect(dsp, &dsp->digit_state, shortdata, len, (dsp->digitmode & DSP_DIGITMODE_NOQUELCH) == 0, (dsp->digitmode & DSP_DIGITMODE_RELAXDTMF));
@@ -1619,6 +1635,16 @@ struct ast_frame *ast_dsp_process(struct ast_channel *chan, struct ast_dsp *dsp,
16191635
goto done;
16201636
}
16211637

1638+
if (custom_freq_digit) {
1639+
/* Custom frequency was detected - digit is 'q' */
1640+
1641+
memset(&dsp->f, 0, sizeof(dsp->f));
1642+
dsp->f.frametype = AST_FRAME_DTMF;
1643+
dsp->f.subclass.integer = custom_freq_digit;
1644+
outf = &dsp->f;
1645+
goto done;
1646+
}
1647+
16221648
if ((dsp->features & DSP_FEATURE_CALL_PROGRESS)) {
16231649
res = __ast_dsp_call_progress(dsp, shortdata, len);
16241650
if (res) {
@@ -1830,6 +1856,17 @@ int ast_dsp_set_digitmode(struct ast_dsp *dsp, int digitmode)
18301856
return 0;
18311857
}
18321858

1859+
int ast_dsp_set_freqmode(struct ast_dsp *dsp, int freq, int dur, int db, int squelch)
1860+
{
1861+
if (freq > 0) {
1862+
dsp->freqmode = 1;
1863+
ast_freq_detect_init(dsp, freq, dur, db, squelch);
1864+
} else {
1865+
dsp->freqmode = 0;
1866+
}
1867+
return 0;
1868+
}
1869+
18331870
int ast_dsp_set_faxmode(struct ast_dsp *dsp, int faxmode)
18341871
{
18351872
if (dsp->faxmode != faxmode) {

0 commit comments

Comments
 (0)