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$")
47 #include <sys/types.h>
55 #include "asterisk/frame.h"
56 #include "asterisk/channel.h"
57 #include "asterisk/logger.h"
58 #include "asterisk/dsp.h"
59 #include "asterisk/ulaw.h"
60 #include "asterisk/alaw.h"
61 #include "asterisk/utils.h"
62 #include "asterisk/options.h"
64 /*! Number of goertzels for progress detect */
66 GSAMP_SIZE_NA = 183, /*!< North America - 350, 440, 480, 620, 950, 1400, 1800 Hz */
67 GSAMP_SIZE_CR = 188, /*!< Costa Rica, Brazil - Only care about 425 Hz */
68 GSAMP_SIZE_UK = 160 /*!< UK disconnect goertzel feed - should trigger 400hz */
87 /*! For CR/BR modes */
94 static struct progalias {
98 { "us", PROG_MODE_NA },
99 { "ca", PROG_MODE_NA },
100 { "cr", PROG_MODE_CR },
101 { "br", PROG_MODE_CR },
102 { "uk", PROG_MODE_UK },
105 static struct progress {
106 enum gsamp_size size;
109 { GSAMP_SIZE_NA, { 350, 440, 480, 620, 950, 1400, 1800 } }, /*!< North America */
110 { GSAMP_SIZE_CR, { 425 } }, /*!< Costa Rica, Brazil */
111 { GSAMP_SIZE_UK, { 400 } }, /*!< UK */
114 #define DEFAULT_THRESHOLD 512
117 BUSY_PERCENT = 10, /*!< The percentage difference between the two last silence periods */
118 BUSY_PAT_PERCENT = 7, /*!< The percentage difference between measured and actual pattern */
119 BUSY_THRESHOLD = 100, /*!< Max number of ms difference between max and min times in busy */
120 BUSY_MIN = 75, /*!< Busy must be at least 80 ms in half-cadence */
121 BUSY_MAX =3100 /*!< Busy can't be longer than 3100 ms in half-cadence */
124 /*! Remember last 15 units */
125 #define DSP_HISTORY 15
127 /*! Define if you want the fax detector -- NOT RECOMMENDED IN -STABLE */
130 #define TONE_THRESH 10.0 /*!< How much louder the tone should be than channel energy */
131 #define TONE_MIN_THRESH 1e8 /*!< How much tone there should be at least to attempt */
133 /*! All THRESH_XXX values are in GSAMP_SIZE chunks (us = 22ms) */
135 THRESH_RING = 8, /*!< Need at least 150ms ring to accept */
136 THRESH_TALK = 2, /*!< Talk detection does not work continuously */
137 THRESH_BUSY = 4, /*!< Need at least 80ms to accept */
138 THRESH_CONGESTION = 4, /*!< Need at least 80ms to accept */
139 THRESH_HANGUP = 60, /*!< Need at least 1300ms to accept hangup */
140 THRESH_RING2ANSWER = 300 /*!< Timeout from start of ring to answer (about 6600 ms) */
143 #define MAX_DTMF_DIGITS 128
147 * Minimum tone on = 40ms
148 * Minimum tone off = 50ms
149 * Maximum digit rate = 10 per second
150 * Normal twist <= 8dB accepted
151 * Reverse twist <= 4dB accepted
152 * S/N >= 15dB will detect OK
153 * Attenuation <= 26dB will detect OK
154 * Frequency tolerance +- 1.5% will detect, +-3.5% will reject
157 #define DTMF_THRESHOLD 8.0e7
158 #define FAX_THRESHOLD 8.0e7
159 #define FAX_2ND_HARMONIC 2.0 /* 4dB */
160 #define DTMF_NORMAL_TWIST 6.3 /* 8dB */
162 #define DTMF_REVERSE_TWIST ((digitmode & DSP_DIGITMODE_RELAXDTMF) ? 6.5 : 2.5) /* 4dB normal */
164 #define DTMF_REVERSE_TWIST ((digitmode & DSP_DIGITMODE_RELAXDTMF) ? 4.0 : 2.5) /* 4dB normal */
166 #define DTMF_RELATIVE_PEAK_ROW 6.3 /* 8dB */
167 #define DTMF_RELATIVE_PEAK_COL 6.3 /* 8dB */
168 #define DTMF_2ND_HARMONIC_ROW ((digitmode & DSP_DIGITMODE_RELAXDTMF) ? 1.7 : 2.5) /* 4dB normal */
169 #define DTMF_2ND_HARMONIC_COL 63.1 /* 18dB */
170 #define DTMF_TO_TOTAL_ENERGY 42.0
172 #ifdef OLD_DSP_ROUTINES
173 #define MF_THRESHOLD 8.0e7
174 #define MF_NORMAL_TWIST 5.3 /* 8dB */
175 #define MF_REVERSE_TWIST 4.0 /* was 2.5 */
176 #define MF_RELATIVE_PEAK 5.3 /* 8dB */
177 #define MF_2ND_HARMONIC 1.7 /* was 2.5 */
179 #define BELL_MF_THRESHOLD 1.6e9
180 #define BELL_MF_TWIST 4.0 /* 6dB */
181 #define BELL_MF_RELATIVE_PEAK 12.6 /* 11dB */
184 #if !defined(BUSYDETECT_MARTIN) && !defined(BUSYDETECT) && !defined(BUSYDETECT_TONEONLY) && !defined(BUSYDETECT_COMPARE_TONE_AND_SILENCE)
185 #define BUSYDETECT_MARTIN
193 #ifndef OLD_DSP_ROUTINES
205 goertzel_state_t row_out[4];
206 goertzel_state_t col_out[4];
208 goertzel_state_t fax_tone;
210 #ifdef OLD_DSP_ROUTINES
211 goertzel_state_t row_out2nd[4];
212 goertzel_state_t col_out2nd[4];
214 goertzel_state_t fax_tone2nd;
227 char digits[MAX_DTMF_DIGITS + 1];
236 } dtmf_detect_state_t;
240 goertzel_state_t tone_out[6];
242 #ifdef OLD_DSP_ROUTINES
247 goertzel_state_t tone_out2nd[6];
254 char digits[MAX_DTMF_DIGITS + 1];
264 static float dtmf_row[] =
266 697.0, 770.0, 852.0, 941.0
268 static float dtmf_col[] =
270 1209.0, 1336.0, 1477.0, 1633.0
273 static float mf_tones[] =
275 700.0, 900.0, 1100.0, 1300.0, 1500.0, 1700.0
279 static float fax_freq = 1100.0;
282 static char dtmf_positions[] = "123A" "456B" "789C" "*0#D";
284 #ifdef OLD_DSP_ROUTINES
285 static char mf_hit[6][6] = {
286 /* 700 + */ { 0, '1', '2', '4', '7', 'C' },
287 /* 900 + */ { '1', 0, '3', '5', '8', 'A' },
288 /* 1100 + */ { '2', '3', 0, '6', '9', '*' },
289 /* 1300 + */ { '4', '5', '6', 0, '0', 'B' },
290 /* 1500 + */ { '7', '8', '9', '0', 0, '#' },
291 /* 1700 + */ { 'C', 'A', '*', 'B', '#', 0 },
294 static char bell_mf_positions[] = "1247C-358A--69*---0B----#";
297 static inline void goertzel_sample(goertzel_state_t *s, short sample)
304 s->v3 = (s->fac * s->v2) >> 15;
305 s->v3 = s->v3 - v1 + (sample >> s->chunky);
306 if (abs(s->v3) > 32768) {
314 static inline void goertzel_update(goertzel_state_t *s, short *samps, int count)
318 for (i=0;i<count;i++)
319 goertzel_sample(s, samps[i]);
323 static inline float goertzel_result(goertzel_state_t *s)
326 r.value = (s->v3 * s->v3) + (s->v2 * s->v2);
327 r.value -= ((s->v2 * s->v3) >> 15) * s->fac;
328 r.power = s->chunky * 2;
329 return (float)r.value * (float)(1 << r.power);
332 static inline void goertzel_init(goertzel_state_t *s, float freq, int samples)
334 s->v2 = s->v3 = s->chunky = 0.0;
335 s->fac = (int)(32768.0 * 2.0 * cos(2.0 * M_PI * (freq / 8000.0)));
336 #ifndef OLD_DSP_ROUTINES
337 s->samples = samples;
341 static inline void goertzel_reset(goertzel_state_t *s)
343 s->v2 = s->v3 = s->chunky = 0.0;
356 int busy_quietlength;
357 int historicnoise[DSP_HISTORY];
358 int historicsilence[DSP_HISTORY];
359 goertzel_state_t freqs[7];
362 enum gsamp_size gsamp_size;
363 enum prog_mode progmode;
370 dtmf_detect_state_t dtmf;
371 mf_detect_state_t mf;
375 static void ast_dtmf_detect_init (dtmf_detect_state_t *s)
379 #ifdef OLD_DSP_ROUTINES
388 for (i = 0; i < 4; i++) {
389 goertzel_init (&s->row_out[i], dtmf_row[i], 102);
390 goertzel_init (&s->col_out[i], dtmf_col[i], 102);
391 #ifdef OLD_DSP_ROUTINES
392 goertzel_init (&s->row_out2nd[i], dtmf_row[i] * 2.0, 102);
393 goertzel_init (&s->col_out2nd[i], dtmf_col[i] * 2.0, 102);
398 /* Same for the fax dector */
399 goertzel_init (&s->fax_tone, fax_freq, 102);
401 #ifdef OLD_DSP_ROUTINES
402 /* Same for the fax dector 2nd harmonic */
403 goertzel_init (&s->fax_tone2nd, fax_freq * 2.0, 102);
405 #endif /* FAX_DETECT */
406 s->current_sample = 0;
407 s->detected_digits = 0;
408 s->current_digits = 0;
409 memset(&s->digits, 0, sizeof(s->digits));
414 static void ast_mf_detect_init (mf_detect_state_t *s)
417 #ifdef OLD_DSP_ROUTINES
421 s->hits[0] = s->hits[1] = s->hits[2] = s->hits[3] = s->hits[4] = 0;
423 for (i = 0; i < 6; i++) {
424 goertzel_init (&s->tone_out[i], mf_tones[i], 160);
425 #ifdef OLD_DSP_ROUTINES
426 goertzel_init (&s->tone_out2nd[i], mf_tones[i] * 2.0, 160);
430 s->current_digits = 0;
431 memset(&s->digits, 0, sizeof(s->digits));
432 s->current_sample = 0;
433 s->detected_digits = 0;
439 static int dtmf_detect (dtmf_detect_state_t *s, int16_t amp[], int samples,
440 int digitmode, int *writeback, int faxdetect)
446 #ifdef OLD_DSP_ROUTINES
447 float fax_energy_2nd;
449 #endif /* FAX_DETECT */
460 for (sample = 0; sample < samples; sample = limit) {
461 /* 102 is optimised to meet the DTMF specs. */
462 if ((samples - sample) >= (102 - s->current_sample))
463 limit = sample + (102 - s->current_sample);
466 #if defined(USE_3DNOW)
467 _dtmf_goertzel_update (s->row_out, amp + sample, limit - sample);
468 _dtmf_goertzel_update (s->col_out, amp + sample, limit - sample);
469 #ifdef OLD_DSP_ROUTINES
470 _dtmf_goertzel_update (s->row_out2nd, amp + sample, limit2 - sample);
471 _dtmf_goertzel_update (s->col_out2nd, amp + sample, limit2 - sample);
473 /* XXX Need to fax detect for 3dnow too XXX */
474 #warning "Fax Support Broken"
476 /* The following unrolled loop takes only 35% (rough estimate) of the
477 time of a rolled loop on the machine on which it was developed */
478 for (j = sample; j < limit; j++) {
480 s->energy += famp*famp;
481 /* With GCC 2.95, the following unrolled code seems to take about 35%
482 (rough estimate) as long as a neat little 0-3 loop */
483 goertzel_sample(s->row_out, amp[j]);
484 goertzel_sample(s->col_out, amp[j]);
485 goertzel_sample(s->row_out + 1, amp[j]);
486 goertzel_sample(s->col_out + 1, amp[j]);
487 goertzel_sample(s->row_out + 2, amp[j]);
488 goertzel_sample(s->col_out + 2, amp[j]);
489 goertzel_sample(s->row_out + 3, amp[j]);
490 goertzel_sample(s->col_out + 3, amp[j]);
492 /* Update fax tone */
493 goertzel_sample(&s->fax_tone, amp[j]);
494 #endif /* FAX_DETECT */
495 #ifdef OLD_DSP_ROUTINES
496 v1 = s->col_out2nd[0].v2;
497 s->col_out2nd[0].v2 = s->col_out2nd[0].v3;
498 s->col_out2nd[0].v3 = s->col_out2nd[0].fac*s->col_out2nd[0].v2 - v1 + famp;
499 v1 = s->row_out2nd[0].v2;
500 s->row_out2nd[0].v2 = s->row_out2nd[0].v3;
501 s->row_out2nd[0].v3 = s->row_out2nd[0].fac*s->row_out2nd[0].v2 - v1 + famp;
502 v1 = s->col_out2nd[1].v2;
503 s->col_out2nd[1].v2 = s->col_out2nd[1].v3;
504 s->col_out2nd[1].v3 = s->col_out2nd[1].fac*s->col_out2nd[1].v2 - v1 + famp;
505 v1 = s->row_out2nd[1].v2;
506 s->row_out2nd[1].v2 = s->row_out2nd[1].v3;
507 s->row_out2nd[1].v3 = s->row_out2nd[1].fac*s->row_out2nd[1].v2 - v1 + famp;
508 v1 = s->col_out2nd[2].v2;
509 s->col_out2nd[2].v2 = s->col_out2nd[2].v3;
510 s->col_out2nd[2].v3 = s->col_out2nd[2].fac*s->col_out2nd[2].v2 - v1 + famp;
511 v1 = s->row_out2nd[2].v2;
512 s->row_out2nd[2].v2 = s->row_out2nd[2].v3;
513 s->row_out2nd[2].v3 = s->row_out2nd[2].fac*s->row_out2nd[2].v2 - v1 + famp;
514 v1 = s->col_out2nd[3].v2;
515 s->col_out2nd[3].v2 = s->col_out2nd[3].v3;
516 s->col_out2nd[3].v3 = s->col_out2nd[3].fac*s->col_out2nd[3].v2 - v1 + famp;
517 v1 = s->row_out2nd[3].v2;
518 s->row_out2nd[3].v2 = s->row_out2nd[3].v3;
519 s->row_out2nd[3].v3 = s->row_out2nd[3].fac*s->row_out2nd[3].v2 - v1 + famp;
521 /* Update fax tone */
523 s->fax_tone2nd.v2 = s->fax_tone2nd.v3;
524 s->fax_tone2nd.v3 = s->fax_tone2nd.fac*s->fax_tone2nd.v2 - v1 + famp;
525 #endif /* FAX_DETECT */
529 s->current_sample += (limit - sample);
530 if (s->current_sample < 102) {
531 if (hit && !((digitmode & DSP_DIGITMODE_NOQUELCH))) {
532 /* If we had a hit last time, go ahead and clear this out since likely it
533 will be another hit */
534 for (i=sample;i<limit;i++)
541 /* Detect the fax energy, too */
542 fax_energy = goertzel_result(&s->fax_tone);
544 /* We are at the end of a DTMF detection block */
545 /* Find the peak row and the peak column */
546 row_energy[0] = goertzel_result (&s->row_out[0]);
547 col_energy[0] = goertzel_result (&s->col_out[0]);
549 for (best_row = best_col = 0, i = 1; i < 4; i++) {
550 row_energy[i] = goertzel_result (&s->row_out[i]);
551 if (row_energy[i] > row_energy[best_row])
553 col_energy[i] = goertzel_result (&s->col_out[i]);
554 if (col_energy[i] > col_energy[best_col])
558 /* Basic signal level test and the twist test */
559 if (row_energy[best_row] >= DTMF_THRESHOLD &&
560 col_energy[best_col] >= DTMF_THRESHOLD &&
561 col_energy[best_col] < row_energy[best_row]*DTMF_REVERSE_TWIST &&
562 col_energy[best_col]*DTMF_NORMAL_TWIST > row_energy[best_row]) {
563 /* Relative peak test */
564 for (i = 0; i < 4; i++) {
565 if ((i != best_col &&
566 col_energy[i]*DTMF_RELATIVE_PEAK_COL > col_energy[best_col]) ||
568 && row_energy[i]*DTMF_RELATIVE_PEAK_ROW > row_energy[best_row])) {
572 #ifdef OLD_DSP_ROUTINES
573 /* ... and second harmonic test */
575 (row_energy[best_row] + col_energy[best_col]) > 42.0*s->energy &&
576 goertzel_result(&s->col_out2nd[best_col])*DTMF_2ND_HARMONIC_COL < col_energy[best_col]
577 && goertzel_result(&s->row_out2nd[best_row])*DTMF_2ND_HARMONIC_ROW < row_energy[best_row]) {
579 /* ... and fraction of total energy test */
581 (row_energy[best_row] + col_energy[best_col]) > DTMF_TO_TOTAL_ENERGY*s->energy) {
584 hit = dtmf_positions[(best_row << 2) + best_col];
585 if (!(digitmode & DSP_DIGITMODE_NOQUELCH)) {
586 /* Zero out frame data if this is part DTMF */
587 for (i=sample;i<limit;i++)
591 #ifdef OLD_DSP_ROUTINES
592 /* Look for two successive similar results */
593 /* The logic in the next test is:
594 We need two successive identical clean detects, with
595 something different preceeding it. This can work with
596 back to back differing digits. More importantly, it
597 can work with nasty phones that give a very wobbly start
599 if (hit == s->hit3 && s->hit3 != s->hit2) {
601 s->digit_hits[(best_row << 2) + best_col]++;
602 s->detected_digits++;
603 if (s->current_digits < MAX_DTMF_DIGITS) {
604 s->digits[s->current_digits++] = hit;
605 s->digits[s->current_digits] = '\0';
614 #ifndef OLD_DSP_ROUTINES
615 /* Look for two successive similar results */
616 /* The logic in the next test is:
617 We need two successive identical clean detects, with
618 something different preceeding it. This can work with
619 back to back differing digits. More importantly, it
620 can work with nasty phones that give a very wobbly start
622 if (hit == s->lasthit && hit != s->mhit) {
624 s->digit_hits[(best_row << 2) + best_col]++;
625 s->detected_digits++;
626 if (s->current_digits < MAX_DTMF_DIGITS) {
627 s->digits[s->current_digits++] = hit;
628 s->digits[s->current_digits] = '\0';
638 if (!hit && (fax_energy >= FAX_THRESHOLD) &&
639 (fax_energy >= DTMF_TO_TOTAL_ENERGY*s->energy) &&
642 printf("Fax energy/Second Harmonic: %f\n", fax_energy);
644 /* XXX Probably need better checking than just this the energy XXX */
648 if (s->fax_hits > 5) {
651 s->detected_digits++;
652 if (s->current_digits < MAX_DTMF_DIGITS) {
653 s->digits[s->current_digits++] = hit;
654 s->digits[s->current_digits] = '\0';
661 #endif /* FAX_DETECT */
662 #ifdef OLD_DSP_ROUTINES
669 /* Reinitialise the detector for the next block */
670 for (i = 0; i < 4; i++) {
671 goertzel_reset(&s->row_out[i]);
672 goertzel_reset(&s->col_out[i]);
673 #ifdef OLD_DSP_ROUTINES
674 goertzel_reset(&s->row_out2nd[i]);
675 goertzel_reset(&s->col_out2nd[i]);
679 goertzel_reset (&s->fax_tone);
680 #ifdef OLD_DSP_ROUTINES
681 goertzel_reset (&s->fax_tone2nd);
685 s->current_sample = 0;
687 #ifdef OLD_DSP_ROUTINES
688 if ((!s->mhit) || (s->mhit != hit)) {
694 return (s->mhit); /* return the debounced hit */
698 /* MF goertzel size */
699 #ifdef OLD_DSP_ROUTINES
705 static int mf_detect (mf_detect_state_t *s, int16_t amp[],
706 int samples, int digitmode, int *writeback)
708 #ifdef OLD_DSP_ROUTINES
709 float tone_energy[6];
727 for (sample = 0; sample < samples; sample = limit) {
728 /* 80 is optimised to meet the MF specs. */
729 if ((samples - sample) >= (MF_GSIZE - s->current_sample))
730 limit = sample + (MF_GSIZE - s->current_sample);
733 #if defined(USE_3DNOW)
734 _dtmf_goertzel_update (s->row_out, amp + sample, limit - sample);
735 _dtmf_goertzel_update (s->col_out, amp + sample, limit - sample);
736 #ifdef OLD_DSP_ROUTINES
737 _dtmf_goertzel_update (s->row_out2nd, amp + sample, limit2 - sample);
738 _dtmf_goertzel_update (s->col_out2nd, amp + sample, limit2 - sample);
740 /* XXX Need to fax detect for 3dnow too XXX */
741 #warning "Fax Support Broken"
743 /* The following unrolled loop takes only 35% (rough estimate) of the
744 time of a rolled loop on the machine on which it was developed */
745 for (j = sample; j < limit; j++) {
747 #ifdef OLD_DSP_ROUTINES
748 s->energy += famp*famp;
750 /* With GCC 2.95, the following unrolled code seems to take about 35%
751 (rough estimate) as long as a neat little 0-3 loop */
752 goertzel_sample(s->tone_out, amp[j]);
753 goertzel_sample(s->tone_out + 1, amp[j]);
754 goertzel_sample(s->tone_out + 2, amp[j]);
755 goertzel_sample(s->tone_out + 3, amp[j]);
756 goertzel_sample(s->tone_out + 4, amp[j]);
757 goertzel_sample(s->tone_out + 5, amp[j]);
758 #ifdef OLD_DSP_ROUTINES
759 v1 = s->tone_out2nd[0].v2;
760 s->tone_out2nd[0].v2 = s->tone_out2nd[0].v3;
761 s->tone_out2nd[0].v3 = s->tone_out2nd[0].fac*s->tone_out2nd[0].v2 - v1 + famp;
762 v1 = s->tone_out2nd[1].v2;
763 s->tone_out2nd[1].v2 = s->tone_out2nd[1].v3;
764 s->tone_out2nd[1].v3 = s->tone_out2nd[1].fac*s->tone_out2nd[1].v2 - v1 + famp;
765 v1 = s->tone_out2nd[2].v2;
766 s->tone_out2nd[2].v2 = s->tone_out2nd[2].v3;
767 s->tone_out2nd[2].v3 = s->tone_out2nd[2].fac*s->tone_out2nd[2].v2 - v1 + famp;
768 v1 = s->tone_out2nd[3].v2;
769 s->tone_out2nd[3].v2 = s->tone_out2nd[3].v3;
770 s->tone_out2nd[3].v3 = s->tone_out2nd[3].fac*s->tone_out2nd[3].v2 - v1 + famp;
771 v1 = s->tone_out2nd[4].v2;
772 s->tone_out2nd[4].v2 = s->tone_out2nd[4].v3;
773 s->tone_out2nd[4].v3 = s->tone_out2nd[4].fac*s->tone_out2nd[2].v2 - v1 + famp;
774 v1 = s->tone_out2nd[3].v2;
775 s->tone_out2nd[5].v2 = s->tone_out2nd[6].v3;
776 s->tone_out2nd[5].v3 = s->tone_out2nd[6].fac*s->tone_out2nd[3].v2 - v1 + famp;
780 s->current_sample += (limit - sample);
781 if (s->current_sample < MF_GSIZE) {
782 if (hit && !((digitmode & DSP_DIGITMODE_NOQUELCH))) {
783 /* If we had a hit last time, go ahead and clear this out since likely it
784 will be another hit */
785 for (i=sample;i<limit;i++)
791 #ifdef OLD_DSP_ROUTINES
792 /* We're at the end of an MF detection block. Go ahead and calculate
795 tone_energy[i] = goertzel_result(&s->tone_out[i]);
799 max = tone_energy[0];
801 if (tone_energy[i] > max) {
802 max = tone_energy[i];
807 /* Find 2nd highest */
809 max = tone_energy[0];
812 max = tone_energy[1];
817 if (i == best1) continue;
818 if (tone_energy[i] > max) {
819 max = tone_energy[i];
828 /* Check for relative energies */
834 if (tone_energy[best1] < tone_energy[i] * MF_RELATIVE_PEAK) {
838 if (tone_energy[best2] < tone_energy[i] * MF_RELATIVE_PEAK) {
845 /* Check for 2nd harmonic */
846 if (goertzel_result(&s->tone_out2nd[best1]) * MF_2ND_HARMONIC > tone_energy[best1])
848 else if (goertzel_result(&s->tone_out2nd[best2]) * MF_2ND_HARMONIC > tone_energy[best2])
852 hit = mf_hit[best1][best2];
853 if (!(digitmode & DSP_DIGITMODE_NOQUELCH)) {
854 /* Zero out frame data if this is part DTMF */
855 for (i=sample;i<limit;i++)
859 /* Look for two consecutive clean hits */
860 if ((hit == s->hit3) && (s->hit3 != s->hit2)) {
862 s->detected_digits++;
863 if (s->current_digits < MAX_DTMF_DIGITS - 2) {
864 s->digits[s->current_digits++] = hit;
865 s->digits[s->current_digits] = '\0';
875 /* Reinitialise the detector for the next block */
876 for (i = 0; i < 6; i++) {
877 goertzel_reset(&s->tone_out[i]);
878 goertzel_reset(&s->tone_out2nd[i]);
881 s->current_sample = 0;
884 /* We're at the end of an MF detection block. */
885 /* Find the two highest energies. The spec says to look for
886 two tones and two tones only. Taking this literally -ie
887 only two tones pass the minimum threshold - doesn't work
888 well. The sinc function mess, due to rectangular windowing
889 ensure that! Find the two highest energies and ensure they
890 are considerably stronger than any of the others. */
891 energy[0] = goertzel_result(&s->tone_out[0]);
892 energy[1] = goertzel_result(&s->tone_out[1]);
893 if (energy[0] > energy[1]) {
902 energy[i] = goertzel_result(&s->tone_out[i]);
903 if (energy[i] >= energy[best]) {
906 } else if (energy[i] >= energy[second_best]) {
910 /* Basic signal level and twist tests */
912 if (energy[best] >= BELL_MF_THRESHOLD && energy[second_best] >= BELL_MF_THRESHOLD
913 && energy[best] < energy[second_best]*BELL_MF_TWIST
914 && energy[best]*BELL_MF_TWIST > energy[second_best]) {
915 /* Relative peak test */
918 if (i != best && i != second_best) {
919 if (energy[i]*BELL_MF_RELATIVE_PEAK >= energy[second_best]) {
920 /* The best two are not clearly the best */
928 /* Get the values into ascending order */
929 if (second_best < best) {
934 best = best*5 + second_best - 1;
935 hit = bell_mf_positions[best];
936 /* Look for two successive similar results */
937 /* The logic in the next test is:
938 For KP we need 4 successive identical clean detects, with
939 two blocks of something different preceeding it. For anything
940 else we need two successive identical clean detects, with
941 two blocks of something different preceeding it. */
942 if (hit == s->hits[4] && hit == s->hits[3] &&
943 ((hit != '*' && hit != s->hits[2] && hit != s->hits[1])||
944 (hit == '*' && hit == s->hits[2] && hit != s->hits[1] &&
945 hit != s->hits[0]))) {
946 s->detected_digits++;
947 if (s->current_digits < MAX_DTMF_DIGITS) {
948 s->digits[s->current_digits++] = hit;
949 s->digits[s->current_digits] = '\0';
957 s->hits[0] = s->hits[1];
958 s->hits[1] = s->hits[2];
959 s->hits[2] = s->hits[3];
960 s->hits[3] = s->hits[4];
962 /* Reinitialise the detector for the next block */
963 for (i = 0; i < 6; i++)
964 goertzel_reset(&s->tone_out[i]);
965 s->current_sample = 0;
968 if ((!s->mhit) || (s->mhit != hit)) {
975 static int __ast_dsp_digitdetect(struct ast_dsp *dsp, short *s, int len, int *writeback)
979 if (dsp->digitmode & DSP_DIGITMODE_MF)
980 res = mf_detect(&dsp->td.mf, s, len, dsp->digitmode & DSP_DIGITMODE_RELAXDTMF, writeback);
982 res = dtmf_detect(&dsp->td.dtmf, s, len, dsp->digitmode & DSP_DIGITMODE_RELAXDTMF, writeback, dsp->features & DSP_FEATURE_FAX_DETECT);
986 int ast_dsp_digitdetect(struct ast_dsp *dsp, struct ast_frame *inf)
992 if (inf->frametype != AST_FRAME_VOICE) {
993 ast_log(LOG_WARNING, "Can't check call progress of non-voice frames\n");
996 if (inf->subclass != AST_FORMAT_SLINEAR) {
997 ast_log(LOG_WARNING, "Can only check call progress in signed-linear frames\n");
1001 len = inf->datalen / 2;
1002 return __ast_dsp_digitdetect(dsp, s, len, &ign);
1005 static inline int pair_there(float p1, float p2, float i1, float i2, float e)
1007 /* See if p1 and p2 are there, relative to i1 and i2 and total energy */
1008 /* Make sure absolute levels are high enough */
1009 if ((p1 < TONE_MIN_THRESH) || (p2 < TONE_MIN_THRESH))
1011 /* Amplify ignored stuff */
1015 /* Check first tone */
1016 if ((p1 < i1) || (p1 < i2) || (p1 < e))
1019 if ((p2 < i1) || (p2 < i2) || (p2 < e))
1021 /* Guess it's there... */
1025 int ast_dsp_getdigits (struct ast_dsp *dsp, char *buf, int max)
1027 if (dsp->digitmode & DSP_DIGITMODE_MF) {
1028 if (max > dsp->td.mf.current_digits)
1029 max = dsp->td.mf.current_digits;
1031 memcpy(buf, dsp->td.mf.digits, max);
1032 memmove(dsp->td.mf.digits, dsp->td.mf.digits + max, dsp->td.mf.current_digits - max);
1033 dsp->td.mf.current_digits -= max;
1038 if (max > dsp->td.dtmf.current_digits)
1039 max = dsp->td.dtmf.current_digits;
1041 memcpy (buf, dsp->td.dtmf.digits, max);
1042 memmove (dsp->td.dtmf.digits, dsp->td.dtmf.digits + max, dsp->td.dtmf.current_digits - max);
1043 dsp->td.dtmf.current_digits -= max;
1050 static int __ast_dsp_call_progress(struct ast_dsp *dsp, short *s, int len)
1055 int newstate = DSP_TONE_STATE_SILENCE;
1058 /* Take the lesser of the number of samples we need and what we have */
1060 if (pass > dsp->gsamp_size - dsp->gsamps)
1061 pass = dsp->gsamp_size - dsp->gsamps;
1062 for (x=0;x<pass;x++) {
1063 for (y=0;y<dsp->freqcount;y++)
1064 goertzel_sample(&dsp->freqs[y], s[x]);
1065 dsp->genergy += s[x] * s[x];
1068 dsp->gsamps += pass;
1070 if (dsp->gsamps == dsp->gsamp_size) {
1073 hz[y] = goertzel_result(&dsp->freqs[y]);
1075 printf("\n350: 425: 440: 480: 620: 950: 1400: 1800: Energy: \n");
1076 printf("%.2e %.2e %.2e %.2e %.2e %.2e %.2e %.2e %.2e\n",
1077 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);
1079 switch (dsp->progmode) {
1081 if (pair_there(hz[HZ_480], hz[HZ_620], hz[HZ_350], hz[HZ_440], dsp->genergy)) {
1082 newstate = DSP_TONE_STATE_BUSY;
1083 } else if (pair_there(hz[HZ_440], hz[HZ_480], hz[HZ_350], hz[HZ_620], dsp->genergy)) {
1084 newstate = DSP_TONE_STATE_RINGING;
1085 } else if (pair_there(hz[HZ_350], hz[HZ_440], hz[HZ_480], hz[HZ_620], dsp->genergy)) {
1086 newstate = DSP_TONE_STATE_DIALTONE;
1087 } else if (hz[HZ_950] > TONE_MIN_THRESH * TONE_THRESH) {
1088 newstate = DSP_TONE_STATE_SPECIAL1;
1089 } else if (hz[HZ_1400] > TONE_MIN_THRESH * TONE_THRESH) {
1090 if (dsp->tstate == DSP_TONE_STATE_SPECIAL1)
1091 newstate = DSP_TONE_STATE_SPECIAL2;
1092 } else if (hz[HZ_1800] > TONE_MIN_THRESH * TONE_THRESH) {
1093 if (dsp->tstate == DSP_TONE_STATE_SPECIAL2)
1094 newstate = DSP_TONE_STATE_SPECIAL3;
1095 } else if (dsp->genergy > TONE_MIN_THRESH * TONE_THRESH) {
1096 newstate = DSP_TONE_STATE_TALKING;
1098 newstate = DSP_TONE_STATE_SILENCE;
1101 if (hz[HZ_425] > TONE_MIN_THRESH * TONE_THRESH) {
1102 newstate = DSP_TONE_STATE_RINGING;
1103 } else if (dsp->genergy > TONE_MIN_THRESH * TONE_THRESH) {
1104 newstate = DSP_TONE_STATE_TALKING;
1106 newstate = DSP_TONE_STATE_SILENCE;
1109 if (hz[HZ_400] > TONE_MIN_THRESH * TONE_THRESH) {
1110 newstate = DSP_TONE_STATE_HUNGUP;
1114 ast_log(LOG_WARNING, "Can't process in unknown prog mode '%d'\n", dsp->progmode);
1116 if (newstate == dsp->tstate) {
1118 if (dsp->ringtimeout)
1120 switch (dsp->tstate) {
1121 case DSP_TONE_STATE_RINGING:
1122 if ((dsp->features & DSP_PROGRESS_RINGING) &&
1123 (dsp->tcount==THRESH_RING)) {
1124 res = AST_CONTROL_RINGING;
1125 dsp->ringtimeout= 1;
1128 case DSP_TONE_STATE_BUSY:
1129 if ((dsp->features & DSP_PROGRESS_BUSY) &&
1130 (dsp->tcount==THRESH_BUSY)) {
1131 res = AST_CONTROL_BUSY;
1132 dsp->features &= ~DSP_FEATURE_CALL_PROGRESS;
1135 case DSP_TONE_STATE_TALKING:
1136 if ((dsp->features & DSP_PROGRESS_TALK) &&
1137 (dsp->tcount==THRESH_TALK)) {
1138 res = AST_CONTROL_ANSWER;
1139 dsp->features &= ~DSP_FEATURE_CALL_PROGRESS;
1142 case DSP_TONE_STATE_SPECIAL3:
1143 if ((dsp->features & DSP_PROGRESS_CONGESTION) &&
1144 (dsp->tcount==THRESH_CONGESTION)) {
1145 res = AST_CONTROL_CONGESTION;
1146 dsp->features &= ~DSP_FEATURE_CALL_PROGRESS;
1149 case DSP_TONE_STATE_HUNGUP:
1150 if ((dsp->features & DSP_FEATURE_CALL_PROGRESS) &&
1151 (dsp->tcount==THRESH_HANGUP)) {
1152 res = AST_CONTROL_HANGUP;
1153 dsp->features &= ~DSP_FEATURE_CALL_PROGRESS;
1157 if (dsp->ringtimeout==THRESH_RING2ANSWER) {
1159 ast_log(LOG_NOTICE, "Consider call as answered because of timeout after last ring\n");
1161 res = AST_CONTROL_ANSWER;
1162 dsp->features &= ~DSP_FEATURE_CALL_PROGRESS;
1166 ast_log(LOG_NOTICE, "Stop state %d with duration %d\n", dsp->tstate, dsp->tcount);
1167 ast_log(LOG_NOTICE, "Start state %d\n", newstate);
1169 dsp->tstate = newstate;
1173 /* Reset goertzel */
1175 dsp->freqs[x].v2 = dsp->freqs[x].v3 = 0.0;
1182 printf("Returning %d\n", res);
1187 int ast_dsp_call_progress(struct ast_dsp *dsp, struct ast_frame *inf)
1189 if (inf->frametype != AST_FRAME_VOICE) {
1190 ast_log(LOG_WARNING, "Can't check call progress of non-voice frames\n");
1193 if (inf->subclass != AST_FORMAT_SLINEAR) {
1194 ast_log(LOG_WARNING, "Can only check call progress in signed-linear frames\n");
1197 return __ast_dsp_call_progress(dsp, inf->data, inf->datalen / 2);
1200 static int __ast_dsp_silence(struct ast_dsp *dsp, short *s, int len, int *totalsilence)
1209 for (x=0;x<len; x++)
1212 if (accum < dsp->threshold) {
1214 dsp->totalsilence += len/8;
1215 if (dsp->totalnoise) {
1216 /* Move and save history */
1217 memmove(dsp->historicnoise + DSP_HISTORY - dsp->busycount, dsp->historicnoise + DSP_HISTORY - dsp->busycount +1, dsp->busycount*sizeof(dsp->historicnoise[0]));
1218 dsp->historicnoise[DSP_HISTORY - 1] = dsp->totalnoise;
1219 /* we don't want to check for busydetect that frequently */
1224 dsp->totalnoise = 0;
1228 dsp->totalnoise += len/8;
1229 if (dsp->totalsilence) {
1230 int silence1 = dsp->historicsilence[DSP_HISTORY - 1];
1231 int silence2 = dsp->historicsilence[DSP_HISTORY - 2];
1232 /* Move and save history */
1233 memmove(dsp->historicsilence + DSP_HISTORY - dsp->busycount, dsp->historicsilence + DSP_HISTORY - dsp->busycount + 1, dsp->busycount*sizeof(dsp->historicsilence[0]));
1234 dsp->historicsilence[DSP_HISTORY - 1] = dsp->totalsilence;
1235 /* check if the previous sample differs only by BUSY_PERCENT from the one before it */
1236 if (silence1 < silence2) {
1237 if (silence1 + silence1*BUSY_PERCENT/100 >= silence2)
1242 if (silence1 - silence1*BUSY_PERCENT/100 <= silence2)
1248 dsp->totalsilence = 0;
1251 *totalsilence = dsp->totalsilence;
1255 #ifdef BUSYDETECT_MARTIN
1256 int ast_dsp_busydetect(struct ast_dsp *dsp)
1259 #ifndef BUSYDETECT_TONEONLY
1260 int avgsilence = 0, hitsilence = 0;
1262 int avgtone = 0, hittone = 0;
1263 if (!dsp->busymaybe)
1265 for (x=DSP_HISTORY - dsp->busycount;x<DSP_HISTORY;x++) {
1266 #ifndef BUSYDETECT_TONEONLY
1267 avgsilence += dsp->historicsilence[x];
1269 avgtone += dsp->historicnoise[x];
1271 #ifndef BUSYDETECT_TONEONLY
1272 avgsilence /= dsp->busycount;
1274 avgtone /= dsp->busycount;
1275 for (x=DSP_HISTORY - dsp->busycount;x<DSP_HISTORY;x++) {
1276 #ifndef BUSYDETECT_TONEONLY
1277 if (avgsilence > dsp->historicsilence[x]) {
1278 if (avgsilence - (avgsilence*BUSY_PERCENT/100) <= dsp->historicsilence[x])
1281 if (avgsilence + (avgsilence*BUSY_PERCENT/100) >= dsp->historicsilence[x])
1285 if (avgtone > dsp->historicnoise[x]) {
1286 if (avgtone - (avgtone*BUSY_PERCENT/100) <= dsp->historicnoise[x])
1289 if (avgtone + (avgtone*BUSY_PERCENT/100) >= dsp->historicnoise[x])
1293 #ifndef BUSYDETECT_TONEONLY
1294 if ((hittone >= dsp->busycount - 1) && (hitsilence >= dsp->busycount - 1) &&
1295 (avgtone >= BUSY_MIN && avgtone <= BUSY_MAX) &&
1296 (avgsilence >= BUSY_MIN && avgsilence <= BUSY_MAX)) {
1298 if ((hittone >= dsp->busycount - 1) && (avgtone >= BUSY_MIN && avgtone <= BUSY_MAX)) {
1300 #ifdef BUSYDETECT_COMPARE_TONE_AND_SILENCE
1301 #ifdef BUSYDETECT_TONEONLY
1302 #error You cant use BUSYDETECT_TONEONLY together with BUSYDETECT_COMPARE_TONE_AND_SILENCE
1304 if (avgtone > avgsilence) {
1305 if (avgtone - avgtone*BUSY_PERCENT/100 <= avgsilence)
1308 if (avgtone + avgtone*BUSY_PERCENT/100 >= avgsilence)
1315 /* If we know the expected busy tone length, check we are in the range */
1316 if (res && (dsp->busy_tonelength > 0)) {
1317 if (abs(avgtone - dsp->busy_tonelength) > (dsp->busy_tonelength*BUSY_PAT_PERCENT/100)) {
1319 ast_log(LOG_NOTICE, "busy detector: avgtone of %d not close enough to desired %d\n",
1320 avgtone, dsp->busy_tonelength);
1325 #ifndef BUSYDETECT_TONEONLY
1326 /* If we know the expected busy tone silent-period length, check we are in the range */
1327 if (res && (dsp->busy_quietlength > 0)) {
1328 if (abs(avgsilence - dsp->busy_quietlength) > (dsp->busy_quietlength*BUSY_PAT_PERCENT/100)) {
1330 ast_log(LOG_NOTICE, "busy detector: avgsilence of %d not close enough to desired %d\n",
1331 avgsilence, dsp->busy_quietlength);
1337 #ifndef BUSYDETECT_TONEONLY
1340 ast_debug(1, "ast_dsp_busydetect detected busy, avgtone: %d, avgsilence %d\n", avgtone, avgsilence);
1349 int ast_dsp_busydetect(struct ast_dsp *dsp)
1356 if (dsp->busy_hits > 5);
1359 if (dsp->busymaybe) {
1361 printf("Maybe busy!\n");
1366 for (x = DSP_HISTORY - dsp->busycount; x < DSP_HISTORY; x++) {
1368 printf("Silence: %d, Noise: %d\n", dsp->historicsilence[x], dsp->historicnoise[x]);
1370 if (dsp->historicsilence[x] < min)
1371 min = dsp->historicsilence[x];
1372 if (dsp->historicnoise[x] < min)
1373 min = dsp->historicnoise[x];
1374 if (dsp->historicsilence[x] > max)
1375 max = dsp->historicsilence[x];
1376 if (dsp->historicnoise[x] > max)
1377 max = dsp->historicnoise[x];
1379 if ((max - min < BUSY_THRESHOLD) && (max < BUSY_MAX) && (min > BUSY_MIN)) {
1386 printf("Min: %d, max: %d\n", min, max);
1393 int ast_dsp_silence(struct ast_dsp *dsp, struct ast_frame *f, int *totalsilence)
1398 if (f->frametype != AST_FRAME_VOICE) {
1399 ast_log(LOG_WARNING, "Can't calculate silence on a non-voice frame\n");
1402 if (f->subclass != AST_FORMAT_SLINEAR) {
1403 ast_log(LOG_WARNING, "Can only calculate silence on signed-linear frames :(\n");
1408 return __ast_dsp_silence(dsp, s, len, totalsilence);
1411 struct ast_frame *ast_dsp_process(struct ast_channel *chan, struct ast_dsp *dsp, struct ast_frame *af)
1418 unsigned char *odata;
1422 #define FIX_INF(inf) do { \
1424 switch (inf->subclass) { \
1425 case AST_FORMAT_SLINEAR: \
1427 case AST_FORMAT_ULAW: \
1428 for (x=0;x<len;x++) \
1429 odata[x] = AST_LIN2MU((unsigned short)shortdata[x]); \
1431 case AST_FORMAT_ALAW: \
1432 for (x=0;x<len;x++) \
1433 odata[x] = AST_LIN2A((unsigned short)shortdata[x]); \
1441 if (af->frametype != AST_FRAME_VOICE)
1445 /* Make sure we have short data */
1446 switch (af->subclass) {
1447 case AST_FORMAT_SLINEAR:
1448 shortdata = af->data;
1449 len = af->datalen / 2;
1451 case AST_FORMAT_ULAW:
1452 shortdata = alloca(af->datalen * 2);
1453 for (x = 0;x < len; x++)
1454 shortdata[x] = AST_MULAW(odata[x]);
1456 case AST_FORMAT_ALAW:
1457 shortdata = alloca(af->datalen * 2);
1458 for (x = 0; x < len; x++)
1459 shortdata[x] = AST_ALAW(odata[x]);
1462 ast_log(LOG_WARNING, "Inband DTMF is not supported on codec %s. Use RFC2833\n", ast_getformatname(af->subclass));
1465 silence = __ast_dsp_silence(dsp, shortdata, len, NULL);
1466 if ((dsp->features & DSP_FEATURE_SILENCE_SUPPRESS) && silence) {
1467 memset(&dsp->f, 0, sizeof(dsp->f));
1468 dsp->f.frametype = AST_FRAME_NULL;
1471 if ((dsp->features & DSP_FEATURE_BUSY_DETECT) && ast_dsp_busydetect(dsp)) {
1472 chan->_softhangup |= AST_SOFTHANGUP_DEV;
1473 memset(&dsp->f, 0, sizeof(dsp->f));
1474 dsp->f.frametype = AST_FRAME_CONTROL;
1475 dsp->f.subclass = AST_CONTROL_BUSY;
1476 ast_debug(1, "Requesting Hangup because the busy tone was detected on channel %s\n", chan->name);
1479 if ((dsp->features & DSP_FEATURE_DTMF_DETECT)) {
1480 digit = __ast_dsp_digitdetect(dsp, shortdata, len, &writeback);
1483 printf("Performing digit detection returned %d, digitmode is %d\n", digit, dsp->digitmode);
1485 if (dsp->digitmode & (DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX)) {
1486 if (!dsp->thinkdigit) {
1488 /* Looks like we might have something.
1489 * Request a conference mute for the moment */
1490 memset(&dsp->f, 0, sizeof(dsp->f));
1491 dsp->f.frametype = AST_FRAME_DTMF;
1492 dsp->f.subclass = 'm';
1493 dsp->thinkdigit = 'x';
1496 ast_queue_frame(chan, af);
1502 /* Thought we saw one last time. Pretty sure we really have now */
1503 if ((dsp->thinkdigit != 'x') && (dsp->thinkdigit != digit)) {
1504 /* If we found a digit, and we're changing digits, go
1505 ahead and send this one, but DON'T stop confmute because
1506 we're detecting something else, too... */
1507 memset(&dsp->f, 0, sizeof(dsp->f));
1508 dsp->f.frametype = AST_FRAME_DTMF_END;
1509 dsp->f.subclass = dsp->thinkdigit;
1512 ast_queue_frame(chan, af);
1515 dsp->thinkdigit = digit;
1516 memset(&dsp->f, 0, sizeof(dsp->f));
1517 dsp->f.frametype = AST_FRAME_DTMF_BEGIN;
1518 dsp->f.subclass = dsp->thinkdigit;
1521 ast_queue_frame(chan, af);
1526 memset(&dsp->f, 0, sizeof(dsp->f));
1527 if (dsp->thinkdigit != 'x') {
1528 /* If we found a digit, send it now */
1529 dsp->f.frametype = AST_FRAME_DTMF_END;
1530 dsp->f.subclass = dsp->thinkdigit;
1531 dsp->thinkdigit = 0;
1533 dsp->f.frametype = AST_FRAME_DTMF;
1534 dsp->f.subclass = 'u';
1535 dsp->thinkdigit = 0;
1539 ast_queue_frame(chan, af);
1544 } else if (!digit) {
1545 /* Only check when there is *not* a hit... */
1546 if (dsp->digitmode & DSP_DIGITMODE_MF) {
1547 if (dsp->td.mf.current_digits) {
1548 memset(&dsp->f, 0, sizeof(dsp->f));
1549 dsp->f.frametype = AST_FRAME_DTMF;
1550 dsp->f.subclass = dsp->td.mf.digits[0];
1551 memmove(dsp->td.mf.digits, dsp->td.mf.digits + 1, dsp->td.mf.current_digits);
1552 dsp->td.mf.current_digits--;
1555 ast_queue_frame(chan, af);
1560 if (dsp->td.dtmf.current_digits) {
1561 memset(&dsp->f, 0, sizeof(dsp->f));
1562 dsp->f.frametype = AST_FRAME_DTMF_END;
1563 dsp->f.subclass = dsp->td.dtmf.digits[0];
1564 memmove(dsp->td.dtmf.digits, dsp->td.dtmf.digits + 1, dsp->td.dtmf.current_digits);
1565 dsp->td.dtmf.current_digits--;
1568 ast_queue_frame(chan, af);
1575 if ((dsp->features & DSP_FEATURE_CALL_PROGRESS)) {
1576 res = __ast_dsp_call_progress(dsp, shortdata, len);
1579 case AST_CONTROL_ANSWER:
1580 case AST_CONTROL_BUSY:
1581 case AST_CONTROL_RINGING:
1582 case AST_CONTROL_CONGESTION:
1583 case AST_CONTROL_HANGUP:
1584 memset(&dsp->f, 0, sizeof(dsp->f));
1585 dsp->f.frametype = AST_FRAME_CONTROL;
1586 dsp->f.subclass = res;
1587 dsp->f.src = "dsp_progress";
1589 ast_queue_frame(chan, &dsp->f);
1592 ast_log(LOG_WARNING, "Don't know how to represent call progress message %d\n", res);
1600 static void ast_dsp_prog_reset(struct ast_dsp *dsp)
1605 dsp->gsamp_size = modes[dsp->progmode].size;
1607 for (x = 0; x < sizeof(modes[dsp->progmode].freqs) / sizeof(modes[dsp->progmode].freqs[0]); x++) {
1608 if (modes[dsp->progmode].freqs[x]) {
1609 goertzel_init(&dsp->freqs[x], (float)modes[dsp->progmode].freqs[x], dsp->gsamp_size);
1613 dsp->freqcount = max;
1614 dsp->ringtimeout= 0;
1617 struct ast_dsp *ast_dsp_new(void)
1619 struct ast_dsp *dsp;
1621 if ((dsp = ast_calloc(1, sizeof(*dsp)))) {
1622 dsp->threshold = DEFAULT_THRESHOLD;
1623 dsp->features = DSP_FEATURE_SILENCE_SUPPRESS;
1624 dsp->busycount = DSP_HISTORY;
1625 /* Initialize DTMF detector */
1626 ast_dtmf_detect_init(&dsp->td.dtmf);
1627 /* Initialize initial DSP progress detect parameters */
1628 ast_dsp_prog_reset(dsp);
1633 void ast_dsp_set_features(struct ast_dsp *dsp, int features)
1635 dsp->features = features;
1638 void ast_dsp_free(struct ast_dsp *dsp)
1643 void ast_dsp_set_threshold(struct ast_dsp *dsp, int threshold)
1645 dsp->threshold = threshold;
1648 void ast_dsp_set_busy_count(struct ast_dsp *dsp, int cadences)
1652 if (cadences > DSP_HISTORY)
1653 cadences = DSP_HISTORY;
1654 dsp->busycount = cadences;
1657 void ast_dsp_set_busy_pattern(struct ast_dsp *dsp, int tonelength, int quietlength)
1659 dsp->busy_tonelength = tonelength;
1660 dsp->busy_quietlength = quietlength;
1661 ast_debug(1, "dsp busy pattern set to %d,%d\n", tonelength, quietlength);
1664 void ast_dsp_digitreset(struct ast_dsp *dsp)
1668 dsp->thinkdigit = 0;
1669 if (dsp->digitmode & DSP_DIGITMODE_MF) {
1670 memset(dsp->td.mf.digits, 0, sizeof(dsp->td.mf.digits));
1671 dsp->td.mf.current_digits = 0;
1672 /* Reinitialise the detector for the next block */
1673 for (i = 0; i < 6; i++) {
1674 goertzel_reset(&dsp->td.mf.tone_out[i]);
1675 #ifdef OLD_DSP_ROUTINES
1676 goertzel_reset(&dsp->td.mf.tone_out2nd[i]);
1679 #ifdef OLD_DSP_ROUTINES
1680 dsp->td.mf.energy = 0.0;
1681 dsp->td.mf.hit1 = dsp->td.mf.hit2 = dsp->td.mf.hit3 = dsp->td.mf.hit4 = dsp->td.mf.mhit = 0;
1683 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;
1685 dsp->td.mf.current_sample = 0;
1687 memset(dsp->td.dtmf.digits, 0, sizeof(dsp->td.dtmf.digits));
1688 dsp->td.dtmf.current_digits = 0;
1689 /* Reinitialise the detector for the next block */
1690 for (i = 0; i < 4; i++) {
1691 goertzel_reset(&dsp->td.dtmf.row_out[i]);
1692 goertzel_reset(&dsp->td.dtmf.col_out[i]);
1693 #ifdef OLD_DSP_ROUTINES
1694 goertzel_reset(&dsp->td.dtmf.row_out2nd[i]);
1695 goertzel_reset(&dsp->td.dtmf.col_out2nd[i]);
1699 goertzel_reset (&dsp->td.dtmf.fax_tone);
1701 #ifdef OLD_DSP_ROUTINES
1703 goertzel_reset (&dsp->td.dtmf.fax_tone2nd);
1705 dsp->td.dtmf.hit1 = dsp->td.dtmf.hit2 = dsp->td.dtmf.hit3 = dsp->td.dtmf.hit4 = dsp->td.dtmf.mhit = 0;
1707 dsp->td.dtmf.lasthit = dsp->td.dtmf.mhit = 0;
1709 dsp->td.dtmf.energy = 0.0;
1710 dsp->td.dtmf.current_sample = 0;
1714 void ast_dsp_reset(struct ast_dsp *dsp)
1718 dsp->totalsilence = 0;
1721 dsp->freqs[x].v2 = dsp->freqs[x].v3 = 0.0;
1722 memset(dsp->historicsilence, 0, sizeof(dsp->historicsilence));
1723 memset(dsp->historicnoise, 0, sizeof(dsp->historicnoise));
1724 dsp->ringtimeout= 0;
1727 int ast_dsp_digitmode(struct ast_dsp *dsp, int digitmode)
1732 old = dsp->digitmode & (DSP_DIGITMODE_DTMF | DSP_DIGITMODE_MF | DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX);
1733 new = digitmode & (DSP_DIGITMODE_DTMF | DSP_DIGITMODE_MF | DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX);
1735 /* Must initialize structures if switching from MF to DTMF or vice-versa */
1736 if (new & DSP_DIGITMODE_MF)
1737 ast_mf_detect_init(&dsp->td.mf);
1739 ast_dtmf_detect_init(&dsp->td.dtmf);
1741 dsp->digitmode = digitmode;
1745 int ast_dsp_set_call_progress_zone(struct ast_dsp *dsp, char *zone)
1749 for (x = 0; x < ARRAY_LEN(aliases); x++) {
1750 if (!strcasecmp(aliases[x].name, zone)) {
1751 dsp->progmode = aliases[x].mode;
1752 ast_dsp_prog_reset(dsp);
1759 int ast_dsp_get_tstate(struct ast_dsp *dsp)
1764 int ast_dsp_get_tcount(struct ast_dsp *dsp)