2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * Goertzel routines are borrowed from Steve Underwood's tremendous work on the
11 * See http://www.asterisk.org for more information about
12 * the Asterisk project. Please do not directly contact
13 * any of the maintainers of this project for assistance;
14 * the project provides a web site, mailing lists and IRC
15 * channels for your use.
17 * This program is free software, distributed under the terms of
18 * the GNU General Public License Version 2. See the LICENSE file
19 * at the top of the source tree.
24 * \brief Convenience Signal Processing routines
26 * \author Mark Spencer <markster@digium.com>
27 * \author Steve Underwood <steveu@coppice.org>
30 /* Some routines from tone_detect.c by Steven Underwood as published under the zapata library */
32 tone_detect.c - General telephony tone detection, and specific
35 Copyright (C) 2001 Steve Underwood <steveu@coppice.org>
37 Despite my general liking of the GPL, I place this code in the
38 public domain for the benefit of all mankind - even the slimy
39 ones who might try to proprietize my work and use it to my
45 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
49 #include "asterisk/frame.h"
50 #include "asterisk/channel.h"
51 #include "asterisk/dsp.h"
52 #include "asterisk/ulaw.h"
53 #include "asterisk/alaw.h"
54 #include "asterisk/utils.h"
55 #include "asterisk/options.h"
57 /*! Number of goertzels for progress detect */
59 GSAMP_SIZE_NA = 183, /*!< North America - 350, 440, 480, 620, 950, 1400, 1800 Hz */
60 GSAMP_SIZE_CR = 188, /*!< Costa Rica, Brazil - Only care about 425 Hz */
61 GSAMP_SIZE_UK = 160 /*!< UK disconnect goertzel feed - should trigger 400hz */
80 /*! For CR/BR modes */
87 static struct progalias {
91 { "us", PROG_MODE_NA },
92 { "ca", PROG_MODE_NA },
93 { "cr", PROG_MODE_CR },
94 { "br", PROG_MODE_CR },
95 { "uk", PROG_MODE_UK },
98 static struct progress {
102 { GSAMP_SIZE_NA, { 350, 440, 480, 620, 950, 1400, 1800 } }, /*!< North America */
103 { GSAMP_SIZE_CR, { 425 } }, /*!< Costa Rica, Brazil */
104 { GSAMP_SIZE_UK, { 400 } }, /*!< UK */
107 #define DEFAULT_THRESHOLD 512
110 BUSY_PERCENT = 10, /*!< The percentage difference between the two last silence periods */
111 BUSY_PAT_PERCENT = 7, /*!< The percentage difference between measured and actual pattern */
112 BUSY_THRESHOLD = 100, /*!< Max number of ms difference between max and min times in busy */
113 BUSY_MIN = 75, /*!< Busy must be at least 80 ms in half-cadence */
114 BUSY_MAX =3100 /*!< Busy can't be longer than 3100 ms in half-cadence */
117 /*! Remember last 15 units */
118 #define DSP_HISTORY 15
120 /*! Define if you want the fax detector -- NOT RECOMMENDED IN -STABLE */
123 #define TONE_THRESH 10.0 /*!< How much louder the tone should be than channel energy */
124 #define TONE_MIN_THRESH 1e8 /*!< How much tone there should be at least to attempt */
126 /*! All THRESH_XXX values are in GSAMP_SIZE chunks (us = 22ms) */
128 THRESH_RING = 8, /*!< Need at least 150ms ring to accept */
129 THRESH_TALK = 2, /*!< Talk detection does not work continuously */
130 THRESH_BUSY = 4, /*!< Need at least 80ms to accept */
131 THRESH_CONGESTION = 4, /*!< Need at least 80ms to accept */
132 THRESH_HANGUP = 60, /*!< Need at least 1300ms to accept hangup */
133 THRESH_RING2ANSWER = 300 /*!< Timeout from start of ring to answer (about 6600 ms) */
136 #define MAX_DTMF_DIGITS 128
140 * Minimum tone on = 40ms
141 * Minimum tone off = 50ms
142 * Maximum digit rate = 10 per second
143 * Normal twist <= 8dB accepted
144 * Reverse twist <= 4dB accepted
145 * S/N >= 15dB will detect OK
146 * Attenuation <= 26dB will detect OK
147 * Frequency tolerance +- 1.5% will detect, +-3.5% will reject
150 #define DTMF_THRESHOLD 8.0e7
151 #define FAX_THRESHOLD 8.0e7
152 #define FAX_2ND_HARMONIC 2.0 /* 4dB */
153 #define DTMF_NORMAL_TWIST 6.3 /* 8dB */
155 #define DTMF_REVERSE_TWIST ((digitmode & DSP_DIGITMODE_RELAXDTMF) ? 6.5 : 2.5) /* 4dB normal */
157 #define DTMF_REVERSE_TWIST ((digitmode & DSP_DIGITMODE_RELAXDTMF) ? 4.0 : 2.5) /* 4dB normal */
159 #define DTMF_RELATIVE_PEAK_ROW 6.3 /* 8dB */
160 #define DTMF_RELATIVE_PEAK_COL 6.3 /* 8dB */
161 #define DTMF_2ND_HARMONIC_ROW ((digitmode & DSP_DIGITMODE_RELAXDTMF) ? 1.7 : 2.5) /* 4dB normal */
162 #define DTMF_2ND_HARMONIC_COL 63.1 /* 18dB */
163 #define DTMF_TO_TOTAL_ENERGY 42.0
165 #ifdef OLD_DSP_ROUTINES
166 #define MF_THRESHOLD 8.0e7
167 #define MF_NORMAL_TWIST 5.3 /* 8dB */
168 #define MF_REVERSE_TWIST 4.0 /* was 2.5 */
169 #define MF_RELATIVE_PEAK 5.3 /* 8dB */
170 #define MF_2ND_HARMONIC 1.7 /* was 2.5 */
172 #define BELL_MF_THRESHOLD 1.6e9
173 #define BELL_MF_TWIST 4.0 /* 6dB */
174 #define BELL_MF_RELATIVE_PEAK 12.6 /* 11dB */
177 #if !defined(BUSYDETECT_MARTIN) && !defined(BUSYDETECT) && !defined(BUSYDETECT_TONEONLY) && !defined(BUSYDETECT_COMPARE_TONE_AND_SILENCE)
178 #define BUSYDETECT_MARTIN
186 #ifndef OLD_DSP_ROUTINES
198 goertzel_state_t row_out[4];
199 goertzel_state_t col_out[4];
201 goertzel_state_t fax_tone;
203 #ifdef OLD_DSP_ROUTINES
204 goertzel_state_t row_out2nd[4];
205 goertzel_state_t col_out2nd[4];
207 goertzel_state_t fax_tone2nd;
220 char digits[MAX_DTMF_DIGITS + 1];
229 } dtmf_detect_state_t;
233 goertzel_state_t tone_out[6];
235 #ifdef OLD_DSP_ROUTINES
240 goertzel_state_t tone_out2nd[6];
247 char digits[MAX_DTMF_DIGITS + 1];
257 static float dtmf_row[] =
259 697.0, 770.0, 852.0, 941.0
261 static float dtmf_col[] =
263 1209.0, 1336.0, 1477.0, 1633.0
266 static float mf_tones[] =
268 700.0, 900.0, 1100.0, 1300.0, 1500.0, 1700.0
272 static float fax_freq = 1100.0;
275 static char dtmf_positions[] = "123A" "456B" "789C" "*0#D";
277 #ifdef OLD_DSP_ROUTINES
278 static char mf_hit[6][6] = {
279 /* 700 + */ { 0, '1', '2', '4', '7', 'C' },
280 /* 900 + */ { '1', 0, '3', '5', '8', 'A' },
281 /* 1100 + */ { '2', '3', 0, '6', '9', '*' },
282 /* 1300 + */ { '4', '5', '6', 0, '0', 'B' },
283 /* 1500 + */ { '7', '8', '9', '0', 0, '#' },
284 /* 1700 + */ { 'C', 'A', '*', 'B', '#', 0 },
287 static char bell_mf_positions[] = "1247C-358A--69*---0B----#";
290 static inline void goertzel_sample(goertzel_state_t *s, short sample)
297 s->v3 = (s->fac * s->v2) >> 15;
298 s->v3 = s->v3 - v1 + (sample >> s->chunky);
299 if (abs(s->v3) > 32768) {
307 static inline void goertzel_update(goertzel_state_t *s, short *samps, int count)
311 for (i=0;i<count;i++)
312 goertzel_sample(s, samps[i]);
316 static inline float goertzel_result(goertzel_state_t *s)
319 r.value = (s->v3 * s->v3) + (s->v2 * s->v2);
320 r.value -= ((s->v2 * s->v3) >> 15) * s->fac;
321 r.power = s->chunky * 2;
322 return (float)r.value * (float)(1 << r.power);
325 static inline void goertzel_init(goertzel_state_t *s, float freq, int samples)
327 s->v2 = s->v3 = s->chunky = 0.0;
328 s->fac = (int)(32768.0 * 2.0 * cos(2.0 * M_PI * (freq / 8000.0)));
329 #ifndef OLD_DSP_ROUTINES
330 s->samples = samples;
334 static inline void goertzel_reset(goertzel_state_t *s)
336 s->v2 = s->v3 = s->chunky = 0.0;
349 int busy_quietlength;
350 int historicnoise[DSP_HISTORY];
351 int historicsilence[DSP_HISTORY];
352 goertzel_state_t freqs[7];
355 enum gsamp_size gsamp_size;
356 enum prog_mode progmode;
363 dtmf_detect_state_t dtmf;
364 mf_detect_state_t mf;
368 static void ast_dtmf_detect_init (dtmf_detect_state_t *s)
372 #ifdef OLD_DSP_ROUTINES
381 for (i = 0; i < 4; i++) {
382 goertzel_init (&s->row_out[i], dtmf_row[i], 102);
383 goertzel_init (&s->col_out[i], dtmf_col[i], 102);
384 #ifdef OLD_DSP_ROUTINES
385 goertzel_init (&s->row_out2nd[i], dtmf_row[i] * 2.0, 102);
386 goertzel_init (&s->col_out2nd[i], dtmf_col[i] * 2.0, 102);
391 /* Same for the fax dector */
392 goertzel_init (&s->fax_tone, fax_freq, 102);
394 #ifdef OLD_DSP_ROUTINES
395 /* Same for the fax dector 2nd harmonic */
396 goertzel_init (&s->fax_tone2nd, fax_freq * 2.0, 102);
398 #endif /* FAX_DETECT */
399 s->current_sample = 0;
400 s->detected_digits = 0;
401 s->current_digits = 0;
402 memset(&s->digits, 0, sizeof(s->digits));
407 static void ast_mf_detect_init (mf_detect_state_t *s)
410 #ifdef OLD_DSP_ROUTINES
414 s->hits[0] = s->hits[1] = s->hits[2] = s->hits[3] = s->hits[4] = 0;
416 for (i = 0; i < 6; i++) {
417 goertzel_init (&s->tone_out[i], mf_tones[i], 160);
418 #ifdef OLD_DSP_ROUTINES
419 goertzel_init (&s->tone_out2nd[i], mf_tones[i] * 2.0, 160);
423 s->current_digits = 0;
424 memset(&s->digits, 0, sizeof(s->digits));
425 s->current_sample = 0;
426 s->detected_digits = 0;
432 static int dtmf_detect (dtmf_detect_state_t *s, int16_t amp[], int samples,
433 int digitmode, int *writeback, int faxdetect)
439 #ifdef OLD_DSP_ROUTINES
440 float fax_energy_2nd;
442 #endif /* FAX_DETECT */
453 for (sample = 0; sample < samples; sample = limit) {
454 /* 102 is optimised to meet the DTMF specs. */
455 if ((samples - sample) >= (102 - s->current_sample))
456 limit = sample + (102 - s->current_sample);
459 #if defined(USE_3DNOW)
460 _dtmf_goertzel_update (s->row_out, amp + sample, limit - sample);
461 _dtmf_goertzel_update (s->col_out, amp + sample, limit - sample);
462 #ifdef OLD_DSP_ROUTINES
463 _dtmf_goertzel_update (s->row_out2nd, amp + sample, limit2 - sample);
464 _dtmf_goertzel_update (s->col_out2nd, amp + sample, limit2 - sample);
466 /* XXX Need to fax detect for 3dnow too XXX */
467 #warning "Fax Support Broken"
469 /* The following unrolled loop takes only 35% (rough estimate) of the
470 time of a rolled loop on the machine on which it was developed */
471 for (j = sample; j < limit; j++) {
473 s->energy += famp*famp;
474 /* With GCC 2.95, the following unrolled code seems to take about 35%
475 (rough estimate) as long as a neat little 0-3 loop */
476 goertzel_sample(s->row_out, amp[j]);
477 goertzel_sample(s->col_out, amp[j]);
478 goertzel_sample(s->row_out + 1, amp[j]);
479 goertzel_sample(s->col_out + 1, amp[j]);
480 goertzel_sample(s->row_out + 2, amp[j]);
481 goertzel_sample(s->col_out + 2, amp[j]);
482 goertzel_sample(s->row_out + 3, amp[j]);
483 goertzel_sample(s->col_out + 3, amp[j]);
485 /* Update fax tone */
486 goertzel_sample(&s->fax_tone, amp[j]);
487 #endif /* FAX_DETECT */
488 #ifdef OLD_DSP_ROUTINES
489 v1 = s->col_out2nd[0].v2;
490 s->col_out2nd[0].v2 = s->col_out2nd[0].v3;
491 s->col_out2nd[0].v3 = s->col_out2nd[0].fac*s->col_out2nd[0].v2 - v1 + famp;
492 v1 = s->row_out2nd[0].v2;
493 s->row_out2nd[0].v2 = s->row_out2nd[0].v3;
494 s->row_out2nd[0].v3 = s->row_out2nd[0].fac*s->row_out2nd[0].v2 - v1 + famp;
495 v1 = s->col_out2nd[1].v2;
496 s->col_out2nd[1].v2 = s->col_out2nd[1].v3;
497 s->col_out2nd[1].v3 = s->col_out2nd[1].fac*s->col_out2nd[1].v2 - v1 + famp;
498 v1 = s->row_out2nd[1].v2;
499 s->row_out2nd[1].v2 = s->row_out2nd[1].v3;
500 s->row_out2nd[1].v3 = s->row_out2nd[1].fac*s->row_out2nd[1].v2 - v1 + famp;
501 v1 = s->col_out2nd[2].v2;
502 s->col_out2nd[2].v2 = s->col_out2nd[2].v3;
503 s->col_out2nd[2].v3 = s->col_out2nd[2].fac*s->col_out2nd[2].v2 - v1 + famp;
504 v1 = s->row_out2nd[2].v2;
505 s->row_out2nd[2].v2 = s->row_out2nd[2].v3;
506 s->row_out2nd[2].v3 = s->row_out2nd[2].fac*s->row_out2nd[2].v2 - v1 + famp;
507 v1 = s->col_out2nd[3].v2;
508 s->col_out2nd[3].v2 = s->col_out2nd[3].v3;
509 s->col_out2nd[3].v3 = s->col_out2nd[3].fac*s->col_out2nd[3].v2 - v1 + famp;
510 v1 = s->row_out2nd[3].v2;
511 s->row_out2nd[3].v2 = s->row_out2nd[3].v3;
512 s->row_out2nd[3].v3 = s->row_out2nd[3].fac*s->row_out2nd[3].v2 - v1 + famp;
514 /* Update fax tone */
516 s->fax_tone2nd.v2 = s->fax_tone2nd.v3;
517 s->fax_tone2nd.v3 = s->fax_tone2nd.fac*s->fax_tone2nd.v2 - v1 + famp;
518 #endif /* FAX_DETECT */
522 s->current_sample += (limit - sample);
523 if (s->current_sample < 102) {
524 if (hit && !((digitmode & DSP_DIGITMODE_NOQUELCH))) {
525 /* If we had a hit last time, go ahead and clear this out since likely it
526 will be another hit */
527 for (i=sample;i<limit;i++)
534 /* Detect the fax energy, too */
535 fax_energy = goertzel_result(&s->fax_tone);
537 /* We are at the end of a DTMF detection block */
538 /* Find the peak row and the peak column */
539 row_energy[0] = goertzel_result (&s->row_out[0]);
540 col_energy[0] = goertzel_result (&s->col_out[0]);
542 for (best_row = best_col = 0, i = 1; i < 4; i++) {
543 row_energy[i] = goertzel_result (&s->row_out[i]);
544 if (row_energy[i] > row_energy[best_row])
546 col_energy[i] = goertzel_result (&s->col_out[i]);
547 if (col_energy[i] > col_energy[best_col])
551 /* Basic signal level test and the twist test */
552 if (row_energy[best_row] >= DTMF_THRESHOLD &&
553 col_energy[best_col] >= DTMF_THRESHOLD &&
554 col_energy[best_col] < row_energy[best_row]*DTMF_REVERSE_TWIST &&
555 col_energy[best_col]*DTMF_NORMAL_TWIST > row_energy[best_row]) {
556 /* Relative peak test */
557 for (i = 0; i < 4; i++) {
558 if ((i != best_col &&
559 col_energy[i]*DTMF_RELATIVE_PEAK_COL > col_energy[best_col]) ||
561 && row_energy[i]*DTMF_RELATIVE_PEAK_ROW > row_energy[best_row])) {
565 #ifdef OLD_DSP_ROUTINES
566 /* ... and second harmonic test */
568 (row_energy[best_row] + col_energy[best_col]) > 42.0*s->energy &&
569 goertzel_result(&s->col_out2nd[best_col])*DTMF_2ND_HARMONIC_COL < col_energy[best_col]
570 && goertzel_result(&s->row_out2nd[best_row])*DTMF_2ND_HARMONIC_ROW < row_energy[best_row]) {
572 /* ... and fraction of total energy test */
574 (row_energy[best_row] + col_energy[best_col]) > DTMF_TO_TOTAL_ENERGY*s->energy) {
577 hit = dtmf_positions[(best_row << 2) + best_col];
578 if (!(digitmode & DSP_DIGITMODE_NOQUELCH)) {
579 /* Zero out frame data if this is part DTMF */
580 for (i=sample;i<limit;i++)
584 #ifdef OLD_DSP_ROUTINES
585 /* Look for two successive similar results */
586 /* The logic in the next test is:
587 We need two successive identical clean detects, with
588 something different preceeding it. This can work with
589 back to back differing digits. More importantly, it
590 can work with nasty phones that give a very wobbly start
592 if (hit == s->hit3 && s->hit3 != s->hit2) {
594 s->digit_hits[(best_row << 2) + best_col]++;
595 s->detected_digits++;
596 if (s->current_digits < MAX_DTMF_DIGITS) {
597 s->digits[s->current_digits++] = hit;
598 s->digits[s->current_digits] = '\0';
607 #ifndef OLD_DSP_ROUTINES
608 /* Look for two successive similar results */
609 /* The logic in the next test is:
610 We need two successive identical clean detects, with
611 something different preceeding it. This can work with
612 back to back differing digits. More importantly, it
613 can work with nasty phones that give a very wobbly start
615 if (hit == s->lasthit && hit != s->mhit) {
617 s->digit_hits[(best_row << 2) + best_col]++;
618 s->detected_digits++;
619 if (s->current_digits < MAX_DTMF_DIGITS) {
620 s->digits[s->current_digits++] = hit;
621 s->digits[s->current_digits] = '\0';
631 if (!hit && (fax_energy >= FAX_THRESHOLD) &&
632 (fax_energy >= DTMF_TO_TOTAL_ENERGY*s->energy) &&
635 printf("Fax energy/Second Harmonic: %f\n", fax_energy);
637 /* XXX Probably need better checking than just this the energy XXX */
641 if (s->fax_hits > 5) {
644 s->detected_digits++;
645 if (s->current_digits < MAX_DTMF_DIGITS) {
646 s->digits[s->current_digits++] = hit;
647 s->digits[s->current_digits] = '\0';
654 #endif /* FAX_DETECT */
655 #ifdef OLD_DSP_ROUTINES
662 /* Reinitialise the detector for the next block */
663 for (i = 0; i < 4; i++) {
664 goertzel_reset(&s->row_out[i]);
665 goertzel_reset(&s->col_out[i]);
666 #ifdef OLD_DSP_ROUTINES
667 goertzel_reset(&s->row_out2nd[i]);
668 goertzel_reset(&s->col_out2nd[i]);
672 goertzel_reset (&s->fax_tone);
673 #ifdef OLD_DSP_ROUTINES
674 goertzel_reset (&s->fax_tone2nd);
678 s->current_sample = 0;
680 #ifdef OLD_DSP_ROUTINES
681 if ((!s->mhit) || (s->mhit != hit)) {
687 return (s->mhit); /* return the debounced hit */
691 /* MF goertzel size */
692 #ifdef OLD_DSP_ROUTINES
698 static int mf_detect (mf_detect_state_t *s, int16_t amp[],
699 int samples, int digitmode, int *writeback)
701 #ifdef OLD_DSP_ROUTINES
702 float tone_energy[6];
720 for (sample = 0; sample < samples; sample = limit) {
721 /* 80 is optimised to meet the MF specs. */
722 if ((samples - sample) >= (MF_GSIZE - s->current_sample))
723 limit = sample + (MF_GSIZE - s->current_sample);
726 #if defined(USE_3DNOW)
727 _dtmf_goertzel_update (s->row_out, amp + sample, limit - sample);
728 _dtmf_goertzel_update (s->col_out, amp + sample, limit - sample);
729 #ifdef OLD_DSP_ROUTINES
730 _dtmf_goertzel_update (s->row_out2nd, amp + sample, limit2 - sample);
731 _dtmf_goertzel_update (s->col_out2nd, amp + sample, limit2 - sample);
733 /* XXX Need to fax detect for 3dnow too XXX */
734 #warning "Fax Support Broken"
736 /* The following unrolled loop takes only 35% (rough estimate) of the
737 time of a rolled loop on the machine on which it was developed */
738 for (j = sample; j < limit; j++) {
740 #ifdef OLD_DSP_ROUTINES
741 s->energy += famp*famp;
743 /* With GCC 2.95, the following unrolled code seems to take about 35%
744 (rough estimate) as long as a neat little 0-3 loop */
745 goertzel_sample(s->tone_out, amp[j]);
746 goertzel_sample(s->tone_out + 1, amp[j]);
747 goertzel_sample(s->tone_out + 2, amp[j]);
748 goertzel_sample(s->tone_out + 3, amp[j]);
749 goertzel_sample(s->tone_out + 4, amp[j]);
750 goertzel_sample(s->tone_out + 5, amp[j]);
751 #ifdef OLD_DSP_ROUTINES
752 v1 = s->tone_out2nd[0].v2;
753 s->tone_out2nd[0].v2 = s->tone_out2nd[0].v3;
754 s->tone_out2nd[0].v3 = s->tone_out2nd[0].fac*s->tone_out2nd[0].v2 - v1 + famp;
755 v1 = s->tone_out2nd[1].v2;
756 s->tone_out2nd[1].v2 = s->tone_out2nd[1].v3;
757 s->tone_out2nd[1].v3 = s->tone_out2nd[1].fac*s->tone_out2nd[1].v2 - v1 + famp;
758 v1 = s->tone_out2nd[2].v2;
759 s->tone_out2nd[2].v2 = s->tone_out2nd[2].v3;
760 s->tone_out2nd[2].v3 = s->tone_out2nd[2].fac*s->tone_out2nd[2].v2 - v1 + famp;
761 v1 = s->tone_out2nd[3].v2;
762 s->tone_out2nd[3].v2 = s->tone_out2nd[3].v3;
763 s->tone_out2nd[3].v3 = s->tone_out2nd[3].fac*s->tone_out2nd[3].v2 - v1 + famp;
764 v1 = s->tone_out2nd[4].v2;
765 s->tone_out2nd[4].v2 = s->tone_out2nd[4].v3;
766 s->tone_out2nd[4].v3 = s->tone_out2nd[4].fac*s->tone_out2nd[2].v2 - v1 + famp;
767 v1 = s->tone_out2nd[3].v2;
768 s->tone_out2nd[5].v2 = s->tone_out2nd[6].v3;
769 s->tone_out2nd[5].v3 = s->tone_out2nd[6].fac*s->tone_out2nd[3].v2 - v1 + famp;
773 s->current_sample += (limit - sample);
774 if (s->current_sample < MF_GSIZE) {
775 if (hit && !((digitmode & DSP_DIGITMODE_NOQUELCH))) {
776 /* If we had a hit last time, go ahead and clear this out since likely it
777 will be another hit */
778 for (i=sample;i<limit;i++)
784 #ifdef OLD_DSP_ROUTINES
785 /* We're at the end of an MF detection block. Go ahead and calculate
788 tone_energy[i] = goertzel_result(&s->tone_out[i]);
792 max = tone_energy[0];
794 if (tone_energy[i] > max) {
795 max = tone_energy[i];
800 /* Find 2nd highest */
802 max = tone_energy[0];
805 max = tone_energy[1];
810 if (i == best1) continue;
811 if (tone_energy[i] > max) {
812 max = tone_energy[i];
821 /* Check for relative energies */
827 if (tone_energy[best1] < tone_energy[i] * MF_RELATIVE_PEAK) {
831 if (tone_energy[best2] < tone_energy[i] * MF_RELATIVE_PEAK) {
838 /* Check for 2nd harmonic */
839 if (goertzel_result(&s->tone_out2nd[best1]) * MF_2ND_HARMONIC > tone_energy[best1])
841 else if (goertzel_result(&s->tone_out2nd[best2]) * MF_2ND_HARMONIC > tone_energy[best2])
845 hit = mf_hit[best1][best2];
846 if (!(digitmode & DSP_DIGITMODE_NOQUELCH)) {
847 /* Zero out frame data if this is part DTMF */
848 for (i=sample;i<limit;i++)
852 /* Look for two consecutive clean hits */
853 if ((hit == s->hit3) && (s->hit3 != s->hit2)) {
855 s->detected_digits++;
856 if (s->current_digits < MAX_DTMF_DIGITS - 2) {
857 s->digits[s->current_digits++] = hit;
858 s->digits[s->current_digits] = '\0';
868 /* Reinitialise the detector for the next block */
869 for (i = 0; i < 6; i++) {
870 goertzel_reset(&s->tone_out[i]);
871 goertzel_reset(&s->tone_out2nd[i]);
874 s->current_sample = 0;
877 /* We're at the end of an MF detection block. */
878 /* Find the two highest energies. The spec says to look for
879 two tones and two tones only. Taking this literally -ie
880 only two tones pass the minimum threshold - doesn't work
881 well. The sinc function mess, due to rectangular windowing
882 ensure that! Find the two highest energies and ensure they
883 are considerably stronger than any of the others. */
884 energy[0] = goertzel_result(&s->tone_out[0]);
885 energy[1] = goertzel_result(&s->tone_out[1]);
886 if (energy[0] > energy[1]) {
895 energy[i] = goertzel_result(&s->tone_out[i]);
896 if (energy[i] >= energy[best]) {
899 } else if (energy[i] >= energy[second_best]) {
903 /* Basic signal level and twist tests */
905 if (energy[best] >= BELL_MF_THRESHOLD && energy[second_best] >= BELL_MF_THRESHOLD
906 && energy[best] < energy[second_best]*BELL_MF_TWIST
907 && energy[best]*BELL_MF_TWIST > energy[second_best]) {
908 /* Relative peak test */
911 if (i != best && i != second_best) {
912 if (energy[i]*BELL_MF_RELATIVE_PEAK >= energy[second_best]) {
913 /* The best two are not clearly the best */
921 /* Get the values into ascending order */
922 if (second_best < best) {
927 best = best*5 + second_best - 1;
928 hit = bell_mf_positions[best];
929 /* Look for two successive similar results */
930 /* The logic in the next test is:
931 For KP we need 4 successive identical clean detects, with
932 two blocks of something different preceeding it. For anything
933 else we need two successive identical clean detects, with
934 two blocks of something different preceeding it. */
935 if (hit == s->hits[4] && hit == s->hits[3] &&
936 ((hit != '*' && hit != s->hits[2] && hit != s->hits[1])||
937 (hit == '*' && hit == s->hits[2] && hit != s->hits[1] &&
938 hit != s->hits[0]))) {
939 s->detected_digits++;
940 if (s->current_digits < MAX_DTMF_DIGITS) {
941 s->digits[s->current_digits++] = hit;
942 s->digits[s->current_digits] = '\0';
950 s->hits[0] = s->hits[1];
951 s->hits[1] = s->hits[2];
952 s->hits[2] = s->hits[3];
953 s->hits[3] = s->hits[4];
955 /* Reinitialise the detector for the next block */
956 for (i = 0; i < 6; i++)
957 goertzel_reset(&s->tone_out[i]);
958 s->current_sample = 0;
961 if ((!s->mhit) || (s->mhit != hit)) {
968 static int __ast_dsp_digitdetect(struct ast_dsp *dsp, short *s, int len, int *writeback)
972 if (dsp->digitmode & DSP_DIGITMODE_MF)
973 res = mf_detect(&dsp->td.mf, s, len, dsp->digitmode & DSP_DIGITMODE_RELAXDTMF, writeback);
975 res = dtmf_detect(&dsp->td.dtmf, s, len, dsp->digitmode & DSP_DIGITMODE_RELAXDTMF, writeback, dsp->features & DSP_FEATURE_FAX_DETECT);
979 int ast_dsp_digitdetect(struct ast_dsp *dsp, struct ast_frame *inf)
985 if (inf->frametype != AST_FRAME_VOICE) {
986 ast_log(LOG_WARNING, "Can't check call progress of non-voice frames\n");
989 if (inf->subclass != AST_FORMAT_SLINEAR) {
990 ast_log(LOG_WARNING, "Can only check call progress in signed-linear frames\n");
994 len = inf->datalen / 2;
995 return __ast_dsp_digitdetect(dsp, s, len, &ign);
998 static inline int pair_there(float p1, float p2, float i1, float i2, float e)
1000 /* See if p1 and p2 are there, relative to i1 and i2 and total energy */
1001 /* Make sure absolute levels are high enough */
1002 if ((p1 < TONE_MIN_THRESH) || (p2 < TONE_MIN_THRESH))
1004 /* Amplify ignored stuff */
1008 /* Check first tone */
1009 if ((p1 < i1) || (p1 < i2) || (p1 < e))
1012 if ((p2 < i1) || (p2 < i2) || (p2 < e))
1014 /* Guess it's there... */
1018 int ast_dsp_getdigits (struct ast_dsp *dsp, char *buf, int max)
1020 if (dsp->digitmode & DSP_DIGITMODE_MF) {
1021 if (max > dsp->td.mf.current_digits)
1022 max = dsp->td.mf.current_digits;
1024 memcpy(buf, dsp->td.mf.digits, max);
1025 memmove(dsp->td.mf.digits, dsp->td.mf.digits + max, dsp->td.mf.current_digits - max);
1026 dsp->td.mf.current_digits -= max;
1031 if (max > dsp->td.dtmf.current_digits)
1032 max = dsp->td.dtmf.current_digits;
1034 memcpy (buf, dsp->td.dtmf.digits, max);
1035 memmove (dsp->td.dtmf.digits, dsp->td.dtmf.digits + max, dsp->td.dtmf.current_digits - max);
1036 dsp->td.dtmf.current_digits -= max;
1043 static int __ast_dsp_call_progress(struct ast_dsp *dsp, short *s, int len)
1048 int newstate = DSP_TONE_STATE_SILENCE;
1051 /* Take the lesser of the number of samples we need and what we have */
1053 if (pass > dsp->gsamp_size - dsp->gsamps)
1054 pass = dsp->gsamp_size - dsp->gsamps;
1055 for (x=0;x<pass;x++) {
1056 for (y=0;y<dsp->freqcount;y++)
1057 goertzel_sample(&dsp->freqs[y], s[x]);
1058 dsp->genergy += s[x] * s[x];
1061 dsp->gsamps += pass;
1063 if (dsp->gsamps == dsp->gsamp_size) {
1066 hz[y] = goertzel_result(&dsp->freqs[y]);
1068 printf("\n350: 425: 440: 480: 620: 950: 1400: 1800: Energy: \n");
1069 printf("%.2e %.2e %.2e %.2e %.2e %.2e %.2e %.2e %.2e\n",
1070 hz[HZ_350], hz[HZ_425], hz[HZ_440], hz[HZ_480], hz[HZ_620], hz[HZ_950], hz[HZ_1400], hz[HZ_1800], dsp->genergy);
1072 switch (dsp->progmode) {
1074 if (pair_there(hz[HZ_480], hz[HZ_620], hz[HZ_350], hz[HZ_440], dsp->genergy)) {
1075 newstate = DSP_TONE_STATE_BUSY;
1076 } else if (pair_there(hz[HZ_440], hz[HZ_480], hz[HZ_350], hz[HZ_620], dsp->genergy)) {
1077 newstate = DSP_TONE_STATE_RINGING;
1078 } else if (pair_there(hz[HZ_350], hz[HZ_440], hz[HZ_480], hz[HZ_620], dsp->genergy)) {
1079 newstate = DSP_TONE_STATE_DIALTONE;
1080 } else if (hz[HZ_950] > TONE_MIN_THRESH * TONE_THRESH) {
1081 newstate = DSP_TONE_STATE_SPECIAL1;
1082 } else if (hz[HZ_1400] > TONE_MIN_THRESH * TONE_THRESH) {
1083 if (dsp->tstate == DSP_TONE_STATE_SPECIAL1)
1084 newstate = DSP_TONE_STATE_SPECIAL2;
1085 } else if (hz[HZ_1800] > TONE_MIN_THRESH * TONE_THRESH) {
1086 if (dsp->tstate == DSP_TONE_STATE_SPECIAL2)
1087 newstate = DSP_TONE_STATE_SPECIAL3;
1088 } else if (dsp->genergy > TONE_MIN_THRESH * TONE_THRESH) {
1089 newstate = DSP_TONE_STATE_TALKING;
1091 newstate = DSP_TONE_STATE_SILENCE;
1094 if (hz[HZ_425] > TONE_MIN_THRESH * TONE_THRESH) {
1095 newstate = DSP_TONE_STATE_RINGING;
1096 } else if (dsp->genergy > TONE_MIN_THRESH * TONE_THRESH) {
1097 newstate = DSP_TONE_STATE_TALKING;
1099 newstate = DSP_TONE_STATE_SILENCE;
1102 if (hz[HZ_400] > TONE_MIN_THRESH * TONE_THRESH) {
1103 newstate = DSP_TONE_STATE_HUNGUP;
1107 ast_log(LOG_WARNING, "Can't process in unknown prog mode '%d'\n", dsp->progmode);
1109 if (newstate == dsp->tstate) {
1111 if (dsp->ringtimeout)
1113 switch (dsp->tstate) {
1114 case DSP_TONE_STATE_RINGING:
1115 if ((dsp->features & DSP_PROGRESS_RINGING) &&
1116 (dsp->tcount==THRESH_RING)) {
1117 res = AST_CONTROL_RINGING;
1118 dsp->ringtimeout= 1;
1121 case DSP_TONE_STATE_BUSY:
1122 if ((dsp->features & DSP_PROGRESS_BUSY) &&
1123 (dsp->tcount==THRESH_BUSY)) {
1124 res = AST_CONTROL_BUSY;
1125 dsp->features &= ~DSP_FEATURE_CALL_PROGRESS;
1128 case DSP_TONE_STATE_TALKING:
1129 if ((dsp->features & DSP_PROGRESS_TALK) &&
1130 (dsp->tcount==THRESH_TALK)) {
1131 res = AST_CONTROL_ANSWER;
1132 dsp->features &= ~DSP_FEATURE_CALL_PROGRESS;
1135 case DSP_TONE_STATE_SPECIAL3:
1136 if ((dsp->features & DSP_PROGRESS_CONGESTION) &&
1137 (dsp->tcount==THRESH_CONGESTION)) {
1138 res = AST_CONTROL_CONGESTION;
1139 dsp->features &= ~DSP_FEATURE_CALL_PROGRESS;
1142 case DSP_TONE_STATE_HUNGUP:
1143 if ((dsp->features & DSP_FEATURE_CALL_PROGRESS) &&
1144 (dsp->tcount==THRESH_HANGUP)) {
1145 res = AST_CONTROL_HANGUP;
1146 dsp->features &= ~DSP_FEATURE_CALL_PROGRESS;
1150 if (dsp->ringtimeout==THRESH_RING2ANSWER) {
1152 ast_log(LOG_NOTICE, "Consider call as answered because of timeout after last ring\n");
1154 res = AST_CONTROL_ANSWER;
1155 dsp->features &= ~DSP_FEATURE_CALL_PROGRESS;
1159 ast_log(LOG_NOTICE, "Stop state %d with duration %d\n", dsp->tstate, dsp->tcount);
1160 ast_log(LOG_NOTICE, "Start state %d\n", newstate);
1162 dsp->tstate = newstate;
1166 /* Reset goertzel */
1168 dsp->freqs[x].v2 = dsp->freqs[x].v3 = 0.0;
1175 printf("Returning %d\n", res);
1180 int ast_dsp_call_progress(struct ast_dsp *dsp, struct ast_frame *inf)
1182 if (inf->frametype != AST_FRAME_VOICE) {
1183 ast_log(LOG_WARNING, "Can't check call progress of non-voice frames\n");
1186 if (inf->subclass != AST_FORMAT_SLINEAR) {
1187 ast_log(LOG_WARNING, "Can only check call progress in signed-linear frames\n");
1190 return __ast_dsp_call_progress(dsp, inf->data, inf->datalen / 2);
1193 static int __ast_dsp_silence(struct ast_dsp *dsp, short *s, int len, int *totalsilence)
1202 for (x=0;x<len; x++)
1205 if (accum < dsp->threshold) {
1207 dsp->totalsilence += len/8;
1208 if (dsp->totalnoise) {
1209 /* Move and save history */
1210 memmove(dsp->historicnoise + DSP_HISTORY - dsp->busycount, dsp->historicnoise + DSP_HISTORY - dsp->busycount +1, dsp->busycount*sizeof(dsp->historicnoise[0]));
1211 dsp->historicnoise[DSP_HISTORY - 1] = dsp->totalnoise;
1212 /* we don't want to check for busydetect that frequently */
1217 dsp->totalnoise = 0;
1221 dsp->totalnoise += len/8;
1222 if (dsp->totalsilence) {
1223 int silence1 = dsp->historicsilence[DSP_HISTORY - 1];
1224 int silence2 = dsp->historicsilence[DSP_HISTORY - 2];
1225 /* Move and save history */
1226 memmove(dsp->historicsilence + DSP_HISTORY - dsp->busycount, dsp->historicsilence + DSP_HISTORY - dsp->busycount + 1, dsp->busycount*sizeof(dsp->historicsilence[0]));
1227 dsp->historicsilence[DSP_HISTORY - 1] = dsp->totalsilence;
1228 /* check if the previous sample differs only by BUSY_PERCENT from the one before it */
1229 if (silence1 < silence2) {
1230 if (silence1 + silence1*BUSY_PERCENT/100 >= silence2)
1235 if (silence1 - silence1*BUSY_PERCENT/100 <= silence2)
1241 dsp->totalsilence = 0;
1244 *totalsilence = dsp->totalsilence;
1248 #ifdef BUSYDETECT_MARTIN
1249 int ast_dsp_busydetect(struct ast_dsp *dsp)
1252 #ifndef BUSYDETECT_TONEONLY
1253 int avgsilence = 0, hitsilence = 0;
1255 int avgtone = 0, hittone = 0;
1256 if (!dsp->busymaybe)
1258 for (x=DSP_HISTORY - dsp->busycount;x<DSP_HISTORY;x++) {
1259 #ifndef BUSYDETECT_TONEONLY
1260 avgsilence += dsp->historicsilence[x];
1262 avgtone += dsp->historicnoise[x];
1264 #ifndef BUSYDETECT_TONEONLY
1265 avgsilence /= dsp->busycount;
1267 avgtone /= dsp->busycount;
1268 for (x=DSP_HISTORY - dsp->busycount;x<DSP_HISTORY;x++) {
1269 #ifndef BUSYDETECT_TONEONLY
1270 if (avgsilence > dsp->historicsilence[x]) {
1271 if (avgsilence - (avgsilence*BUSY_PERCENT/100) <= dsp->historicsilence[x])
1274 if (avgsilence + (avgsilence*BUSY_PERCENT/100) >= dsp->historicsilence[x])
1278 if (avgtone > dsp->historicnoise[x]) {
1279 if (avgtone - (avgtone*BUSY_PERCENT/100) <= dsp->historicnoise[x])
1282 if (avgtone + (avgtone*BUSY_PERCENT/100) >= dsp->historicnoise[x])
1286 #ifndef BUSYDETECT_TONEONLY
1287 if ((hittone >= dsp->busycount - 1) && (hitsilence >= dsp->busycount - 1) &&
1288 (avgtone >= BUSY_MIN && avgtone <= BUSY_MAX) &&
1289 (avgsilence >= BUSY_MIN && avgsilence <= BUSY_MAX)) {
1291 if ((hittone >= dsp->busycount - 1) && (avgtone >= BUSY_MIN && avgtone <= BUSY_MAX)) {
1293 #ifdef BUSYDETECT_COMPARE_TONE_AND_SILENCE
1294 #ifdef BUSYDETECT_TONEONLY
1295 #error You cant use BUSYDETECT_TONEONLY together with BUSYDETECT_COMPARE_TONE_AND_SILENCE
1297 if (avgtone > avgsilence) {
1298 if (avgtone - avgtone*BUSY_PERCENT/100 <= avgsilence)
1301 if (avgtone + avgtone*BUSY_PERCENT/100 >= avgsilence)
1308 /* If we know the expected busy tone length, check we are in the range */
1309 if (res && (dsp->busy_tonelength > 0)) {
1310 if (abs(avgtone - dsp->busy_tonelength) > (dsp->busy_tonelength*BUSY_PAT_PERCENT/100)) {
1312 ast_log(LOG_NOTICE, "busy detector: avgtone of %d not close enough to desired %d\n",
1313 avgtone, dsp->busy_tonelength);
1318 #ifndef BUSYDETECT_TONEONLY
1319 /* If we know the expected busy tone silent-period length, check we are in the range */
1320 if (res && (dsp->busy_quietlength > 0)) {
1321 if (abs(avgsilence - dsp->busy_quietlength) > (dsp->busy_quietlength*BUSY_PAT_PERCENT/100)) {
1323 ast_log(LOG_NOTICE, "busy detector: avgsilence of %d not close enough to desired %d\n",
1324 avgsilence, dsp->busy_quietlength);
1330 #ifndef BUSYDETECT_TONEONLY
1333 ast_debug(1, "ast_dsp_busydetect detected busy, avgtone: %d, avgsilence %d\n", avgtone, avgsilence);
1342 int ast_dsp_busydetect(struct ast_dsp *dsp)
1349 if (dsp->busy_hits > 5);
1352 if (dsp->busymaybe) {
1354 printf("Maybe busy!\n");
1359 for (x = DSP_HISTORY - dsp->busycount; x < DSP_HISTORY; x++) {
1361 printf("Silence: %d, Noise: %d\n", dsp->historicsilence[x], dsp->historicnoise[x]);
1363 if (dsp->historicsilence[x] < min)
1364 min = dsp->historicsilence[x];
1365 if (dsp->historicnoise[x] < min)
1366 min = dsp->historicnoise[x];
1367 if (dsp->historicsilence[x] > max)
1368 max = dsp->historicsilence[x];
1369 if (dsp->historicnoise[x] > max)
1370 max = dsp->historicnoise[x];
1372 if ((max - min < BUSY_THRESHOLD) && (max < BUSY_MAX) && (min > BUSY_MIN)) {
1379 printf("Min: %d, max: %d\n", min, max);
1386 int ast_dsp_silence(struct ast_dsp *dsp, struct ast_frame *f, int *totalsilence)
1391 if (f->frametype != AST_FRAME_VOICE) {
1392 ast_log(LOG_WARNING, "Can't calculate silence on a non-voice frame\n");
1395 if (f->subclass != AST_FORMAT_SLINEAR) {
1396 ast_log(LOG_WARNING, "Can only calculate silence on signed-linear frames :(\n");
1401 return __ast_dsp_silence(dsp, s, len, totalsilence);
1404 struct ast_frame *ast_dsp_process(struct ast_channel *chan, struct ast_dsp *dsp, struct ast_frame *af)
1411 unsigned char *odata;
1415 #define FIX_INF(inf) do { \
1417 switch (inf->subclass) { \
1418 case AST_FORMAT_SLINEAR: \
1420 case AST_FORMAT_ULAW: \
1421 for (x=0;x<len;x++) \
1422 odata[x] = AST_LIN2MU((unsigned short)shortdata[x]); \
1424 case AST_FORMAT_ALAW: \
1425 for (x=0;x<len;x++) \
1426 odata[x] = AST_LIN2A((unsigned short)shortdata[x]); \
1434 if (af->frametype != AST_FRAME_VOICE)
1438 /* Make sure we have short data */
1439 switch (af->subclass) {
1440 case AST_FORMAT_SLINEAR:
1441 shortdata = af->data;
1442 len = af->datalen / 2;
1444 case AST_FORMAT_ULAW:
1445 shortdata = alloca(af->datalen * 2);
1446 for (x = 0;x < len; x++)
1447 shortdata[x] = AST_MULAW(odata[x]);
1449 case AST_FORMAT_ALAW:
1450 shortdata = alloca(af->datalen * 2);
1451 for (x = 0; x < len; x++)
1452 shortdata[x] = AST_ALAW(odata[x]);
1455 ast_log(LOG_WARNING, "Inband DTMF is not supported on codec %s. Use RFC2833\n", ast_getformatname(af->subclass));
1458 silence = __ast_dsp_silence(dsp, shortdata, len, NULL);
1459 if ((dsp->features & DSP_FEATURE_SILENCE_SUPPRESS) && silence) {
1460 memset(&dsp->f, 0, sizeof(dsp->f));
1461 dsp->f.frametype = AST_FRAME_NULL;
1464 if ((dsp->features & DSP_FEATURE_BUSY_DETECT) && ast_dsp_busydetect(dsp)) {
1465 chan->_softhangup |= AST_SOFTHANGUP_DEV;
1466 memset(&dsp->f, 0, sizeof(dsp->f));
1467 dsp->f.frametype = AST_FRAME_CONTROL;
1468 dsp->f.subclass = AST_CONTROL_BUSY;
1469 ast_debug(1, "Requesting Hangup because the busy tone was detected on channel %s\n", chan->name);
1472 if ((dsp->features & DSP_FEATURE_DTMF_DETECT)) {
1473 digit = __ast_dsp_digitdetect(dsp, shortdata, len, &writeback);
1476 printf("Performing digit detection returned %d, digitmode is %d\n", digit, dsp->digitmode);
1478 if (dsp->digitmode & (DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX)) {
1479 if (!dsp->thinkdigit) {
1481 /* Looks like we might have something.
1482 * Request a conference mute for the moment */
1483 memset(&dsp->f, 0, sizeof(dsp->f));
1484 dsp->f.frametype = AST_FRAME_DTMF;
1485 dsp->f.subclass = 'm';
1486 dsp->thinkdigit = 'x';
1489 ast_queue_frame(chan, af);
1495 /* Thought we saw one last time. Pretty sure we really have now */
1496 if ((dsp->thinkdigit != 'x') && (dsp->thinkdigit != digit)) {
1497 /* If we found a digit, and we're changing digits, go
1498 ahead and send this one, but DON'T stop confmute because
1499 we're detecting something else, too... */
1500 memset(&dsp->f, 0, sizeof(dsp->f));
1501 dsp->f.frametype = AST_FRAME_DTMF_END;
1502 dsp->f.subclass = dsp->thinkdigit;
1505 ast_queue_frame(chan, af);
1508 dsp->thinkdigit = digit;
1509 memset(&dsp->f, 0, sizeof(dsp->f));
1510 dsp->f.frametype = AST_FRAME_DTMF_BEGIN;
1511 dsp->f.subclass = dsp->thinkdigit;
1514 ast_queue_frame(chan, af);
1519 memset(&dsp->f, 0, sizeof(dsp->f));
1520 if (dsp->thinkdigit != 'x') {
1521 /* If we found a digit, send it now */
1522 dsp->f.frametype = AST_FRAME_DTMF_END;
1523 dsp->f.subclass = dsp->thinkdigit;
1524 dsp->thinkdigit = 0;
1526 dsp->f.frametype = AST_FRAME_DTMF;
1527 dsp->f.subclass = 'u';
1528 dsp->thinkdigit = 0;
1532 ast_queue_frame(chan, af);
1537 } else if (!digit) {
1538 /* Only check when there is *not* a hit... */
1539 if (dsp->digitmode & DSP_DIGITMODE_MF) {
1540 if (dsp->td.mf.current_digits) {
1541 memset(&dsp->f, 0, sizeof(dsp->f));
1542 dsp->f.frametype = AST_FRAME_DTMF;
1543 dsp->f.subclass = dsp->td.mf.digits[0];
1544 memmove(dsp->td.mf.digits, dsp->td.mf.digits + 1, dsp->td.mf.current_digits);
1545 dsp->td.mf.current_digits--;
1548 ast_queue_frame(chan, af);
1553 if (dsp->td.dtmf.current_digits) {
1554 memset(&dsp->f, 0, sizeof(dsp->f));
1555 dsp->f.frametype = AST_FRAME_DTMF_END;
1556 dsp->f.subclass = dsp->td.dtmf.digits[0];
1557 memmove(dsp->td.dtmf.digits, dsp->td.dtmf.digits + 1, dsp->td.dtmf.current_digits);
1558 dsp->td.dtmf.current_digits--;
1561 ast_queue_frame(chan, af);
1568 if ((dsp->features & DSP_FEATURE_CALL_PROGRESS)) {
1569 res = __ast_dsp_call_progress(dsp, shortdata, len);
1572 case AST_CONTROL_ANSWER:
1573 case AST_CONTROL_BUSY:
1574 case AST_CONTROL_RINGING:
1575 case AST_CONTROL_CONGESTION:
1576 case AST_CONTROL_HANGUP:
1577 memset(&dsp->f, 0, sizeof(dsp->f));
1578 dsp->f.frametype = AST_FRAME_CONTROL;
1579 dsp->f.subclass = res;
1580 dsp->f.src = "dsp_progress";
1582 ast_queue_frame(chan, &dsp->f);
1585 ast_log(LOG_WARNING, "Don't know how to represent call progress message %d\n", res);
1593 static void ast_dsp_prog_reset(struct ast_dsp *dsp)
1598 dsp->gsamp_size = modes[dsp->progmode].size;
1600 for (x = 0; x < sizeof(modes[dsp->progmode].freqs) / sizeof(modes[dsp->progmode].freqs[0]); x++) {
1601 if (modes[dsp->progmode].freqs[x]) {
1602 goertzel_init(&dsp->freqs[x], (float)modes[dsp->progmode].freqs[x], dsp->gsamp_size);
1606 dsp->freqcount = max;
1607 dsp->ringtimeout= 0;
1610 struct ast_dsp *ast_dsp_new(void)
1612 struct ast_dsp *dsp;
1614 if ((dsp = ast_calloc(1, sizeof(*dsp)))) {
1615 dsp->threshold = DEFAULT_THRESHOLD;
1616 dsp->features = DSP_FEATURE_SILENCE_SUPPRESS;
1617 dsp->busycount = DSP_HISTORY;
1618 /* Initialize DTMF detector */
1619 ast_dtmf_detect_init(&dsp->td.dtmf);
1620 /* Initialize initial DSP progress detect parameters */
1621 ast_dsp_prog_reset(dsp);
1626 void ast_dsp_set_features(struct ast_dsp *dsp, int features)
1628 dsp->features = features;
1631 void ast_dsp_free(struct ast_dsp *dsp)
1636 void ast_dsp_set_threshold(struct ast_dsp *dsp, int threshold)
1638 dsp->threshold = threshold;
1641 void ast_dsp_set_busy_count(struct ast_dsp *dsp, int cadences)
1645 if (cadences > DSP_HISTORY)
1646 cadences = DSP_HISTORY;
1647 dsp->busycount = cadences;
1650 void ast_dsp_set_busy_pattern(struct ast_dsp *dsp, int tonelength, int quietlength)
1652 dsp->busy_tonelength = tonelength;
1653 dsp->busy_quietlength = quietlength;
1654 ast_debug(1, "dsp busy pattern set to %d,%d\n", tonelength, quietlength);
1657 void ast_dsp_digitreset(struct ast_dsp *dsp)
1661 dsp->thinkdigit = 0;
1662 if (dsp->digitmode & DSP_DIGITMODE_MF) {
1663 memset(dsp->td.mf.digits, 0, sizeof(dsp->td.mf.digits));
1664 dsp->td.mf.current_digits = 0;
1665 /* Reinitialise the detector for the next block */
1666 for (i = 0; i < 6; i++) {
1667 goertzel_reset(&dsp->td.mf.tone_out[i]);
1668 #ifdef OLD_DSP_ROUTINES
1669 goertzel_reset(&dsp->td.mf.tone_out2nd[i]);
1672 #ifdef OLD_DSP_ROUTINES
1673 dsp->td.mf.energy = 0.0;
1674 dsp->td.mf.hit1 = dsp->td.mf.hit2 = dsp->td.mf.hit3 = dsp->td.mf.hit4 = dsp->td.mf.mhit = 0;
1676 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;
1678 dsp->td.mf.current_sample = 0;
1680 memset(dsp->td.dtmf.digits, 0, sizeof(dsp->td.dtmf.digits));
1681 dsp->td.dtmf.current_digits = 0;
1682 /* Reinitialise the detector for the next block */
1683 for (i = 0; i < 4; i++) {
1684 goertzel_reset(&dsp->td.dtmf.row_out[i]);
1685 goertzel_reset(&dsp->td.dtmf.col_out[i]);
1686 #ifdef OLD_DSP_ROUTINES
1687 goertzel_reset(&dsp->td.dtmf.row_out2nd[i]);
1688 goertzel_reset(&dsp->td.dtmf.col_out2nd[i]);
1692 goertzel_reset (&dsp->td.dtmf.fax_tone);
1694 #ifdef OLD_DSP_ROUTINES
1696 goertzel_reset (&dsp->td.dtmf.fax_tone2nd);
1698 dsp->td.dtmf.hit1 = dsp->td.dtmf.hit2 = dsp->td.dtmf.hit3 = dsp->td.dtmf.hit4 = dsp->td.dtmf.mhit = 0;
1700 dsp->td.dtmf.lasthit = dsp->td.dtmf.mhit = 0;
1702 dsp->td.dtmf.energy = 0.0;
1703 dsp->td.dtmf.current_sample = 0;
1707 void ast_dsp_reset(struct ast_dsp *dsp)
1711 dsp->totalsilence = 0;
1714 dsp->freqs[x].v2 = dsp->freqs[x].v3 = 0.0;
1715 memset(dsp->historicsilence, 0, sizeof(dsp->historicsilence));
1716 memset(dsp->historicnoise, 0, sizeof(dsp->historicnoise));
1717 dsp->ringtimeout= 0;
1720 int ast_dsp_digitmode(struct ast_dsp *dsp, int digitmode)
1725 old = dsp->digitmode & (DSP_DIGITMODE_DTMF | DSP_DIGITMODE_MF | DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX);
1726 new = digitmode & (DSP_DIGITMODE_DTMF | DSP_DIGITMODE_MF | DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX);
1728 /* Must initialize structures if switching from MF to DTMF or vice-versa */
1729 if (new & DSP_DIGITMODE_MF)
1730 ast_mf_detect_init(&dsp->td.mf);
1732 ast_dtmf_detect_init(&dsp->td.dtmf);
1734 dsp->digitmode = digitmode;
1738 int ast_dsp_set_call_progress_zone(struct ast_dsp *dsp, char *zone)
1742 for (x = 0; x < ARRAY_LEN(aliases); x++) {
1743 if (!strcasecmp(aliases[x].name, zone)) {
1744 dsp->progmode = aliases[x].mode;
1745 ast_dsp_prog_reset(dsp);
1752 int ast_dsp_get_tstate(struct ast_dsp *dsp)
1757 int ast_dsp_get_tcount(struct ast_dsp *dsp)