2 * Asterisk -- A telephony toolkit for Linux.
4 * Convenience Signal Processing routines
6 * Copyright (C) 2002, Digium
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License.
13 * Goertzel routines are borrowed from Steve Underwood's tremendous work on the
18 /* Some routines from tone_detect.c by Steven Underwood as published under the zapata library */
20 tone_detect.c - General telephony tone detection, and specific
23 Copyright (C) 2001 Steve Underwood <steveu@coppice.org>
25 Despite my general liking of the GPL, I place this code in the
26 public domain for the benefit of all mankind - even the slimy
27 ones who might try to proprietize my work and use it to my
31 #include <sys/types.h>
32 #include <asterisk/frame.h>
33 #include <asterisk/channel.h>
34 #include <asterisk/channel_pvt.h>
35 #include <asterisk/logger.h>
36 #include <asterisk/dsp.h>
37 #include <asterisk/ulaw.h>
38 #include <asterisk/alaw.h>
46 #define DEFAULT_THRESHOLD 1024
48 #define BUSY_THRESHOLD 100 /* Max number of ms difference between max and min times in busy */
49 #define BUSY_MIN 80 /* Busy must be at least 80 ms in half-cadence */
50 #define BUSY_MAX 1100 /* Busy can't be longer than 1100 ms in half-cadence */
52 /* Remember last 3 units */
55 /* Number of goertzels for progress detect */
56 #define GSAMP_SIZE 183
66 #define TONE_THRESH 10.0 /* How much louder the tone should be than channel energy */
67 #define TONE_MIN_THRESH 1e8 /* How much tone there should be at least to attempt */
68 #define COUNT_THRESH 3 /* Need at least 50ms of stuff to count it */
70 #define TONE_STATE_SILENCE 0
71 #define TONE_STATE_RINGING 1
72 #define TONE_STATE_DIALTONE 2
73 #define TONE_STATE_TALKING 3
74 #define TONE_STATE_BUSY 4
75 #define TONE_STATE_SPECIAL1 5
76 #define TONE_STATE_SPECIAL2 6
77 #define TONE_STATE_SPECIAL3 7
79 #define MAX_DTMF_DIGITS 128
83 * Minimum tone on = 40ms
84 * Minimum tone off = 50ms
85 * Maximum digit rate = 10 per second
86 * Normal twist <= 8dB accepted
87 * Reverse twist <= 4dB accepted
88 * S/N >= 15dB will detect OK
89 * Attenuation <= 26dB will detect OK
90 * Frequency tolerance +- 1.5% will detect, +-3.5% will reject
93 #define DTMF_THRESHOLD 8.0e7
94 #define FAX_THRESHOLD 8.0e7
95 #define FAX_2ND_HARMONIC 2.0 /* 4dB */
96 #define DTMF_NORMAL_TWIST 6.3 /* 8dB */
97 #define DTMF_REVERSE_TWIST ((digitmode & DSP_DIGITMODE_RELAXDTMF) ? 4.0 : 2.5) /* 4dB normal */
98 #define DTMF_RELATIVE_PEAK_ROW 6.3 /* 8dB */
99 #define DTMF_RELATIVE_PEAK_COL 6.3 /* 8dB */
100 #define DTMF_2ND_HARMONIC_ROW ((digitmode & DSP_DIGITMODE_RELAXDTMF) ? 1.7 : 2.5) /* 4dB normal */
101 #define DTMF_2ND_HARMONIC_COL 63.1 /* 18dB */
103 #define MF_THRESHOLD 8.0e7
104 #define MF_NORMAL_TWIST 5.3 /* 8dB */
105 #define MF_REVERSE_TWIST 4.0 /* was 2.5 */
106 #define MF_RELATIVE_PEAK 5.3 /* 8dB */
107 #define MF_2ND_HARMONIC 1.7 /* was 2.5 */
123 goertzel_state_t row_out[4];
124 goertzel_state_t col_out[4];
125 goertzel_state_t row_out2nd[4];
126 goertzel_state_t col_out2nd[4];
127 goertzel_state_t fax_tone;
128 goertzel_state_t fax_tone2nd;
132 char digits[MAX_DTMF_DIGITS + 1];
138 } dtmf_detect_state_t;
148 goertzel_state_t tone_out[6];
149 goertzel_state_t tone_out2nd[6];
153 char digits[MAX_DTMF_DIGITS + 1];
160 static float dtmf_row[] =
162 697.0, 770.0, 852.0, 941.0
164 static float dtmf_col[] =
166 1209.0, 1336.0, 1477.0, 1633.0
169 static float mf_tones[] =
171 700.0, 900.0, 1100.0, 1300.0, 1500.0, 1700.0
174 static float fax_freq = 1100.0;
176 static char dtmf_positions[] = "123A" "456B" "789C" "*0#D";
178 static char mf_hit[6][6] = {
179 /* 700 + */ { 0, '1', '2', '4', '7', 'C' },
180 /* 900 + */ { '1', 0, '3', '5', '8', 'A' },
181 /* 1100 + */ { '2', '3', 0, '6', '9', '*' },
182 /* 1300 + */ { '4', '5', '6', 0, '0', 'B' },
183 /* 1500 + */ { '7', '8', '9', '0', 0, '#' },
184 /* 1700 + */ { 'C', 'A', '*', 'B', '#', 0 },
187 static inline void goertzel_sample(goertzel_state_t *s, short sample)
190 float fsamp = sample;
193 s->v3 = s->fac * s->v2 - v1 + fsamp;
196 static inline void goertzel_update(goertzel_state_t *s, short *samps, int count)
199 for (i=0;i<count;i++)
200 goertzel_sample(s, samps[i]);
204 static inline float goertzel_result(goertzel_state_t *s)
206 return s->v3 * s->v3 + s->v2 * s->v2 - s->v2 * s->v3 * s->fac;
209 static inline void goertzel_init(goertzel_state_t *s, float freq)
212 s->fac = 2.0 * cos(2.0 * M_PI * (freq / 8000.0));
215 static inline void goertzel_reset(goertzel_state_t *s)
228 int historicnoise[DSP_HISTORY];
229 int historicsilence[DSP_HISTORY];
230 goertzel_state_t freqs[7];
238 dtmf_detect_state_t dtmf;
239 mf_detect_state_t mf;
243 static void ast_dtmf_detect_init (dtmf_detect_state_t *s)
250 for (i = 0; i < 4; i++)
253 goertzel_init (&s->row_out[i], dtmf_row[i]);
254 goertzel_init (&s->col_out[i], dtmf_col[i]);
255 goertzel_init (&s->row_out2nd[i], dtmf_row[i] * 2.0);
256 goertzel_init (&s->col_out2nd[i], dtmf_col[i] * 2.0);
261 /* Same for the fax dector */
262 goertzel_init (&s->fax_tone, fax_freq);
264 /* Same for the fax dector 2nd harmonic */
265 goertzel_init (&s->fax_tone2nd, fax_freq * 2.0);
267 s->current_sample = 0;
268 s->detected_digits = 0;
269 s->current_digits = 0;
270 memset(&s->digits, 0, sizeof(s->digits));
276 static void ast_mf_detect_init (mf_detect_state_t *s)
283 for (i = 0; i < 6; i++)
286 goertzel_init (&s->tone_out[i], mf_tones[i]);
287 goertzel_init (&s->tone_out2nd[i], mf_tones[i] * 2.0);
292 s->current_digits = 0;
293 memset(&s->digits, 0, sizeof(s->digits));
294 s->current_sample = 0;
295 s->detected_digits = 0;
301 static int dtmf_detect (dtmf_detect_state_t *s,
304 int digitmode, int *writeback)
310 float fax_energy_2nd;
322 for (sample = 0; sample < samples; sample = limit)
324 /* 102 is optimised to meet the DTMF specs. */
325 if ((samples - sample) >= (102 - s->current_sample))
326 limit = sample + (102 - s->current_sample);
329 #if defined(USE_3DNOW)
330 _dtmf_goertzel_update (s->row_out, amp + sample, limit - sample);
331 _dtmf_goertzel_update (s->col_out, amp + sample, limit - sample);
332 _dtmf_goertzel_update (s->row_out2nd, amp + sample, limit2 - sample);
333 _dtmf_goertzel_update (s->col_out2nd, amp + sample, limit2 - sample);
334 /* XXX Need to fax detect for 3dnow too XXX */
335 #warning "Fax Support Broken"
337 /* The following unrolled loop takes only 35% (rough estimate) of the
338 time of a rolled loop on the machine on which it was developed */
339 for (j = sample; j < limit; j++)
343 s->energy += famp*famp;
345 /* With GCC 2.95, the following unrolled code seems to take about 35%
346 (rough estimate) as long as a neat little 0-3 loop */
347 v1 = s->row_out[0].v2;
348 s->row_out[0].v2 = s->row_out[0].v3;
349 s->row_out[0].v3 = s->row_out[0].fac*s->row_out[0].v2 - v1 + famp;
351 v1 = s->col_out[0].v2;
352 s->col_out[0].v2 = s->col_out[0].v3;
353 s->col_out[0].v3 = s->col_out[0].fac*s->col_out[0].v2 - v1 + famp;
355 v1 = s->row_out[1].v2;
356 s->row_out[1].v2 = s->row_out[1].v3;
357 s->row_out[1].v3 = s->row_out[1].fac*s->row_out[1].v2 - v1 + famp;
359 v1 = s->col_out[1].v2;
360 s->col_out[1].v2 = s->col_out[1].v3;
361 s->col_out[1].v3 = s->col_out[1].fac*s->col_out[1].v2 - v1 + famp;
363 v1 = s->row_out[2].v2;
364 s->row_out[2].v2 = s->row_out[2].v3;
365 s->row_out[2].v3 = s->row_out[2].fac*s->row_out[2].v2 - v1 + famp;
367 v1 = s->col_out[2].v2;
368 s->col_out[2].v2 = s->col_out[2].v3;
369 s->col_out[2].v3 = s->col_out[2].fac*s->col_out[2].v2 - v1 + famp;
371 v1 = s->row_out[3].v2;
372 s->row_out[3].v2 = s->row_out[3].v3;
373 s->row_out[3].v3 = s->row_out[3].fac*s->row_out[3].v2 - v1 + famp;
375 v1 = s->col_out[3].v2;
376 s->col_out[3].v2 = s->col_out[3].v3;
377 s->col_out[3].v3 = s->col_out[3].fac*s->col_out[3].v2 - v1 + famp;
379 v1 = s->col_out2nd[0].v2;
380 s->col_out2nd[0].v2 = s->col_out2nd[0].v3;
381 s->col_out2nd[0].v3 = s->col_out2nd[0].fac*s->col_out2nd[0].v2 - v1 + famp;
383 v1 = s->row_out2nd[0].v2;
384 s->row_out2nd[0].v2 = s->row_out2nd[0].v3;
385 s->row_out2nd[0].v3 = s->row_out2nd[0].fac*s->row_out2nd[0].v2 - v1 + famp;
387 v1 = s->col_out2nd[1].v2;
388 s->col_out2nd[1].v2 = s->col_out2nd[1].v3;
389 s->col_out2nd[1].v3 = s->col_out2nd[1].fac*s->col_out2nd[1].v2 - v1 + famp;
391 v1 = s->row_out2nd[1].v2;
392 s->row_out2nd[1].v2 = s->row_out2nd[1].v3;
393 s->row_out2nd[1].v3 = s->row_out2nd[1].fac*s->row_out2nd[1].v2 - v1 + famp;
395 v1 = s->col_out2nd[2].v2;
396 s->col_out2nd[2].v2 = s->col_out2nd[2].v3;
397 s->col_out2nd[2].v3 = s->col_out2nd[2].fac*s->col_out2nd[2].v2 - v1 + famp;
399 v1 = s->row_out2nd[2].v2;
400 s->row_out2nd[2].v2 = s->row_out2nd[2].v3;
401 s->row_out2nd[2].v3 = s->row_out2nd[2].fac*s->row_out2nd[2].v2 - v1 + famp;
403 v1 = s->col_out2nd[3].v2;
404 s->col_out2nd[3].v2 = s->col_out2nd[3].v3;
405 s->col_out2nd[3].v3 = s->col_out2nd[3].fac*s->col_out2nd[3].v2 - v1 + famp;
407 v1 = s->row_out2nd[3].v2;
408 s->row_out2nd[3].v2 = s->row_out2nd[3].v3;
409 s->row_out2nd[3].v3 = s->row_out2nd[3].fac*s->row_out2nd[3].v2 - v1 + famp;
411 /* Update fax tone */
413 s->fax_tone.v2 = s->fax_tone.v3;
414 s->fax_tone.v3 = s->fax_tone.fac*s->fax_tone.v2 - v1 + famp;
417 s->fax_tone2nd.v2 = s->fax_tone2nd.v3;
418 s->fax_tone2nd.v3 = s->fax_tone2nd.fac*s->fax_tone2nd.v2 - v1 + famp;
421 s->current_sample += (limit - sample);
422 if (s->current_sample < 102) {
423 if (hit && !((digitmode & DSP_DIGITMODE_NOQUELCH))) {
424 /* If we had a hit last time, go ahead and clear this out since likely it
425 will be another hit */
426 for (i=sample;i<limit;i++)
433 /* Detect the fax energy, too */
434 fax_energy = goertzel_result(&s->fax_tone);
436 /* We are at the end of a DTMF detection block */
437 /* Find the peak row and the peak column */
438 row_energy[0] = goertzel_result (&s->row_out[0]);
439 col_energy[0] = goertzel_result (&s->col_out[0]);
441 for (best_row = best_col = 0, i = 1; i < 4; i++)
443 row_energy[i] = goertzel_result (&s->row_out[i]);
444 if (row_energy[i] > row_energy[best_row])
446 col_energy[i] = goertzel_result (&s->col_out[i]);
447 if (col_energy[i] > col_energy[best_col])
451 /* Basic signal level test and the twist test */
452 if (row_energy[best_row] >= DTMF_THRESHOLD
454 col_energy[best_col] >= DTMF_THRESHOLD
456 col_energy[best_col] < row_energy[best_row]*DTMF_REVERSE_TWIST
458 col_energy[best_col]*DTMF_NORMAL_TWIST > row_energy[best_row])
460 /* Relative peak test */
461 for (i = 0; i < 4; i++)
463 if ((i != best_col && col_energy[i]*DTMF_RELATIVE_PEAK_COL > col_energy[best_col])
465 (i != best_row && row_energy[i]*DTMF_RELATIVE_PEAK_ROW > row_energy[best_row]))
470 /* ... and second harmonic test */
473 (row_energy[best_row] + col_energy[best_col]) > 42.0*s->energy
475 goertzel_result (&s->col_out2nd[best_col])*DTMF_2ND_HARMONIC_COL < col_energy[best_col]
477 goertzel_result (&s->row_out2nd[best_row])*DTMF_2ND_HARMONIC_ROW < row_energy[best_row])
480 hit = dtmf_positions[(best_row << 2) + best_col];
481 if (!(digitmode & DSP_DIGITMODE_NOQUELCH)) {
482 /* Zero out frame data if this is part DTMF */
483 for (i=sample;i<limit;i++)
487 /* Look for two successive similar results */
488 /* The logic in the next test is:
489 We need two successive identical clean detects, with
490 something different preceeding it. This can work with
491 back to back differing digits. More importantly, it
492 can work with nasty phones that give a very wobbly start
494 if (hit == s->hit3 && s->hit3 != s->hit2)
497 s->digit_hits[(best_row << 2) + best_col]++;
498 s->detected_digits++;
499 if (s->current_digits < MAX_DTMF_DIGITS)
501 s->digits[s->current_digits++] = hit;
502 s->digits[s->current_digits] = '\0';
511 if (!hit && (fax_energy >= FAX_THRESHOLD) && (fax_energy > s->energy * 21.0)) {
512 fax_energy_2nd = goertzel_result(&s->fax_tone2nd);
513 if (fax_energy_2nd * FAX_2ND_HARMONIC < fax_energy) {
515 printf("Fax energy/Second Harmonic: %f/%f\n", fax_energy, fax_energy_2nd);
517 /* XXX Probably need better checking than just this the energy XXX */
520 } /* Don't reset fax hits counter */
522 if (s->fax_hits > 5) {
525 s->detected_digits++;
526 if (s->current_digits < MAX_DTMF_DIGITS)
528 s->digits[s->current_digits++] = hit;
529 s->digits[s->current_digits] = '\0';
541 /* Reinitialise the detector for the next block */
542 for (i = 0; i < 4; i++)
544 goertzel_reset(&s->row_out[i]);
545 goertzel_reset(&s->col_out[i]);
546 goertzel_reset(&s->row_out2nd[i]);
547 goertzel_reset(&s->col_out2nd[i]);
549 goertzel_reset (&s->fax_tone);
550 goertzel_reset (&s->fax_tone2nd);
552 s->current_sample = 0;
554 if ((!s->mhit) || (s->mhit != hit))
562 /* MF goertzel size */
565 static int mf_detect (mf_detect_state_t *s,
568 int digitmode, int *writeback)
571 float tone_energy[6];
585 for (sample = 0; sample < samples; sample = limit)
587 /* 80 is optimised to meet the MF specs. */
588 if ((samples - sample) >= (MF_GSIZE - s->current_sample))
589 limit = sample + (MF_GSIZE - s->current_sample);
592 #if defined(USE_3DNOW)
593 _dtmf_goertzel_update (s->row_out, amp + sample, limit - sample);
594 _dtmf_goertzel_update (s->col_out, amp + sample, limit - sample);
595 _dtmf_goertzel_update (s->row_out2nd, amp + sample, limit2 - sample);
596 _dtmf_goertzel_update (s->col_out2nd, amp + sample, limit2 - sample);
597 /* XXX Need to fax detect for 3dnow too XXX */
598 #warning "Fax Support Broken"
600 /* The following unrolled loop takes only 35% (rough estimate) of the
601 time of a rolled loop on the machine on which it was developed */
602 for (j = sample; j < limit; j++)
606 s->energy += famp*famp;
608 /* With GCC 2.95, the following unrolled code seems to take about 35%
609 (rough estimate) as long as a neat little 0-3 loop */
610 v1 = s->tone_out[0].v2;
611 s->tone_out[0].v2 = s->tone_out[0].v3;
612 s->tone_out[0].v3 = s->tone_out[0].fac*s->tone_out[0].v2 - v1 + famp;
614 v1 = s->tone_out[1].v2;
615 s->tone_out[1].v2 = s->tone_out[1].v3;
616 s->tone_out[1].v3 = s->tone_out[1].fac*s->tone_out[1].v2 - v1 + famp;
618 v1 = s->tone_out[2].v2;
619 s->tone_out[2].v2 = s->tone_out[2].v3;
620 s->tone_out[2].v3 = s->tone_out[2].fac*s->tone_out[2].v2 - v1 + famp;
622 v1 = s->tone_out[3].v2;
623 s->tone_out[3].v2 = s->tone_out[3].v3;
624 s->tone_out[3].v3 = s->tone_out[3].fac*s->tone_out[3].v2 - v1 + famp;
626 v1 = s->tone_out[4].v2;
627 s->tone_out[4].v2 = s->tone_out[4].v3;
628 s->tone_out[4].v3 = s->tone_out[4].fac*s->tone_out[4].v2 - v1 + famp;
630 v1 = s->tone_out[5].v2;
631 s->tone_out[5].v2 = s->tone_out[5].v3;
632 s->tone_out[5].v3 = s->tone_out[5].fac*s->tone_out[5].v2 - v1 + famp;
634 v1 = s->tone_out2nd[0].v2;
635 s->tone_out2nd[0].v2 = s->tone_out2nd[0].v3;
636 s->tone_out2nd[0].v3 = s->tone_out2nd[0].fac*s->tone_out2nd[0].v2 - v1 + famp;
638 v1 = s->tone_out2nd[1].v2;
639 s->tone_out2nd[1].v2 = s->tone_out2nd[1].v3;
640 s->tone_out2nd[1].v3 = s->tone_out2nd[1].fac*s->tone_out2nd[1].v2 - v1 + famp;
642 v1 = s->tone_out2nd[2].v2;
643 s->tone_out2nd[2].v2 = s->tone_out2nd[2].v3;
644 s->tone_out2nd[2].v3 = s->tone_out2nd[2].fac*s->tone_out2nd[2].v2 - v1 + famp;
646 v1 = s->tone_out2nd[3].v2;
647 s->tone_out2nd[3].v2 = s->tone_out2nd[3].v3;
648 s->tone_out2nd[3].v3 = s->tone_out2nd[3].fac*s->tone_out2nd[3].v2 - v1 + famp;
650 v1 = s->tone_out2nd[4].v2;
651 s->tone_out2nd[4].v2 = s->tone_out2nd[4].v3;
652 s->tone_out2nd[4].v3 = s->tone_out2nd[4].fac*s->tone_out2nd[2].v2 - v1 + famp;
654 v1 = s->tone_out2nd[3].v2;
655 s->tone_out2nd[5].v2 = s->tone_out2nd[6].v3;
656 s->tone_out2nd[5].v3 = s->tone_out2nd[6].fac*s->tone_out2nd[3].v2 - v1 + famp;
660 s->current_sample += (limit - sample);
661 if (s->current_sample < MF_GSIZE) {
662 if (hit && !((digitmode & DSP_DIGITMODE_NOQUELCH))) {
663 /* If we had a hit last time, go ahead and clear this out since likely it
664 will be another hit */
665 for (i=sample;i<limit;i++)
672 /* We're at the end of an MF detection block. Go ahead and calculate
675 tone_energy[i] = goertzel_result(&s->tone_out[i]);
680 max = tone_energy[0];
682 if (tone_energy[i] > max) {
683 max = tone_energy[i];
688 /* Find 2nd highest */
690 max = tone_energy[0];
692 max = tone_energy[1];
695 if (i == best1) continue;
696 if (tone_energy[i] > max) {
697 max = tone_energy[i];
704 /* Check for relative energies */
706 if (i == best1) continue;
707 if (i == best2) continue;
708 if (tone_energy[best1] < tone_energy[i] * MF_RELATIVE_PEAK) {
712 if (tone_energy[best2] < tone_energy[i] * MF_RELATIVE_PEAK) {
719 /* Check for 2nd harmonic */
720 if (goertzel_result(&s->tone_out2nd[best1]) * MF_2ND_HARMONIC > tone_energy[best1])
722 else if (goertzel_result(&s->tone_out2nd[best2]) * MF_2ND_HARMONIC > tone_energy[best2])
726 hit = mf_hit[best1][best2];
727 if (!(digitmode & DSP_DIGITMODE_NOQUELCH)) {
728 /* Zero out frame data if this is part DTMF */
729 for (i=sample;i<limit;i++)
733 /* Look for two consecutive clean hits */
734 if ((hit == s->hit3) && (s->hit3 != s->hit2)) {
736 s->detected_digits++;
737 if (s->current_digits < MAX_DTMF_DIGITS - 2) {
738 s->digits[s->current_digits++] = hit;
739 s->digits[s->current_digits] = '\0';
749 /* Reinitialise the detector for the next block */
750 for (i = 0; i < 6; i++)
752 goertzel_reset(&s->tone_out[i]);
753 goertzel_reset(&s->tone_out2nd[i]);
756 s->current_sample = 0;
758 if ((!s->mhit) || (s->mhit != hit))
766 static int __ast_dsp_digitdetect(struct ast_dsp *dsp, short *s, int len, int *writeback)
769 if (dsp->digitmode & DSP_DIGITMODE_MF)
770 res = mf_detect(&dsp->td.mf, s, len, dsp->digitmode & DSP_DIGITMODE_RELAXDTMF, writeback);
772 res = dtmf_detect(&dsp->td.dtmf, s, len, dsp->digitmode & DSP_DIGITMODE_RELAXDTMF, writeback);
776 int ast_dsp_digitdetect(struct ast_dsp *dsp, struct ast_frame *inf)
781 if (inf->frametype != AST_FRAME_VOICE) {
782 ast_log(LOG_WARNING, "Can't check call progress of non-voice frames\n");
785 if (inf->subclass != AST_FORMAT_SLINEAR) {
786 ast_log(LOG_WARNING, "Can only check call progress in signed-linear frames\n");
790 len = inf->datalen / 2;
791 return __ast_dsp_digitdetect(dsp, s, len, &ign);
794 static inline int pair_there(float p1, float p2, float i1, float i2, float e)
796 /* See if p1 and p2 are there, relative to i1 and i2 and total energy */
797 /* Make sure absolute levels are high enough */
798 if ((p1 < TONE_MIN_THRESH) || (p2 < TONE_MIN_THRESH))
800 /* Amplify ignored stuff */
804 /* Check first tone */
805 if ((p1 < i1) || (p1 < i2) || (p1 < e))
808 if ((p2 < i1) || (p2 < i2) || (p2 < e))
810 /* Guess it's there... */
814 int ast_dsp_getdigits (struct ast_dsp *dsp,
818 if (dsp->digitmode & DSP_DIGITMODE_MF) {
819 if (max > dsp->td.mf.current_digits)
820 max = dsp->td.mf.current_digits;
823 memcpy (buf, dsp->td.mf.digits, max);
824 memmove (dsp->td.mf.digits, dsp->td.mf.digits + max, dsp->td.mf.current_digits - max);
825 dsp->td.mf.current_digits -= max;
830 if (max > dsp->td.dtmf.current_digits)
831 max = dsp->td.dtmf.current_digits;
834 memcpy (buf, dsp->td.dtmf.digits, max);
835 memmove (dsp->td.dtmf.digits, dsp->td.dtmf.digits + max, dsp->td.dtmf.current_digits - max);
836 dsp->td.dtmf.current_digits -= max;
843 static int __ast_dsp_call_progress(struct ast_dsp *dsp, short *s, int len)
847 int newstate = TONE_STATE_SILENCE;
850 /* Take the lesser of the number of samples we need and what we have */
852 if (pass > GSAMP_SIZE - dsp->gsamps)
853 pass = GSAMP_SIZE - dsp->gsamps;
854 for (x=0;x<pass;x++) {
855 goertzel_sample(&dsp->freqs[HZ_350], s[x]);
856 goertzel_sample(&dsp->freqs[HZ_440], s[x]);
857 goertzel_sample(&dsp->freqs[HZ_480], s[x]);
858 goertzel_sample(&dsp->freqs[HZ_620], s[x]);
859 goertzel_sample(&dsp->freqs[HZ_950], s[x]);
860 goertzel_sample(&dsp->freqs[HZ_1400], s[x]);
861 goertzel_sample(&dsp->freqs[HZ_1800], s[x]);
862 dsp->genergy += s[x] * s[x];
867 if (dsp->gsamps == GSAMP_SIZE) {
875 hz_350 = goertzel_result(&dsp->freqs[HZ_350]);
876 hz_440 = goertzel_result(&dsp->freqs[HZ_440]);
877 hz_480 = goertzel_result(&dsp->freqs[HZ_480]);
878 hz_620 = goertzel_result(&dsp->freqs[HZ_620]);
879 hz_950 = goertzel_result(&dsp->freqs[HZ_950]);
880 hz_1400 = goertzel_result(&dsp->freqs[HZ_1400]);
881 hz_1800 = goertzel_result(&dsp->freqs[HZ_1800]);
883 printf("Got whole dsp state: 350: %e, 440: %e, 480: %e, 620: %e, 950: %e, 1400: %e, 1800: %e, Energy: %e\n",
884 hz_350, hz_440, hz_480, hz_620, hz_950, hz_1400, hz_1800, dsp->genergy);
886 if (pair_there(hz_480, hz_620, hz_350, hz_440, dsp->genergy)) {
887 newstate = TONE_STATE_BUSY;
888 } else if (pair_there(hz_440, hz_480, hz_350, hz_620, dsp->genergy)) {
889 newstate = TONE_STATE_RINGING;
890 } else if (pair_there(hz_350, hz_440, hz_480, hz_620, dsp->genergy)) {
891 newstate = TONE_STATE_DIALTONE;
892 } else if (hz_950 > TONE_MIN_THRESH * TONE_THRESH) {
893 newstate = TONE_STATE_SPECIAL1;
894 } else if (hz_1400 > TONE_MIN_THRESH * TONE_THRESH) {
895 if (dsp->tstate == TONE_STATE_SPECIAL1)
896 newstate = TONE_STATE_SPECIAL2;
897 } else if (hz_1800 > TONE_MIN_THRESH * TONE_THRESH) {
898 if (dsp->tstate == TONE_STATE_SPECIAL2)
899 newstate = TONE_STATE_SPECIAL3;
900 } else if (dsp->genergy > TONE_MIN_THRESH * TONE_THRESH) {
901 newstate = TONE_STATE_TALKING;
903 newstate = TONE_STATE_SILENCE;
905 if (newstate == dsp->tstate) {
907 if (dsp->tcount == COUNT_THRESH) {
908 if (dsp->tstate == TONE_STATE_BUSY) {
909 res = AST_CONTROL_BUSY;
910 dsp->features &= ~DSP_FEATURE_CALL_PROGRESS;
911 } else if (dsp->tstate == TONE_STATE_TALKING) {
912 res = AST_CONTROL_ANSWER;
913 dsp->features &= ~DSP_FEATURE_CALL_PROGRESS;
914 } else if (dsp->tstate == TONE_STATE_RINGING)
915 res = AST_CONTROL_RINGING;
916 else if (dsp->tstate == TONE_STATE_SPECIAL3) {
917 res = AST_CONTROL_CONGESTION;
918 dsp->features &= ~DSP_FEATURE_CALL_PROGRESS;
924 printf("Newstate: %d\n", newstate);
926 dsp->tstate = newstate;
932 dsp->freqs[x].v2 = dsp->freqs[x].v3 = 0.0;
939 printf("Returning %d\n", res);
944 int ast_dsp_call_progress(struct ast_dsp *dsp, struct ast_frame *inf)
946 if (inf->frametype != AST_FRAME_VOICE) {
947 ast_log(LOG_WARNING, "Can't check call progress of non-voice frames\n");
950 if (inf->subclass != AST_FORMAT_SLINEAR) {
951 ast_log(LOG_WARNING, "Can only check call progress in signed-linear frames\n");
954 return __ast_dsp_call_progress(dsp, inf->data, inf->datalen / 2);
957 static int __ast_dsp_silence(struct ast_dsp *dsp, short *s, int len, int *totalsilence)
967 if (accum < dsp->threshold) {
968 dsp->totalsilence += len/8;
969 if (dsp->totalnoise) {
970 /* Move and save history */
971 memmove(dsp->historicnoise, dsp->historicnoise + 1, sizeof(dsp->historicnoise) - sizeof(dsp->historicnoise[0]));
972 dsp->historicnoise[DSP_HISTORY - 1] = dsp->totalnoise;
978 dsp->totalnoise += len/8;
979 if (dsp->totalsilence) {
980 /* Move and save history */
981 memmove(dsp->historicsilence, dsp->historicsilence + 1, sizeof(dsp->historicsilence) - sizeof(dsp->historicsilence[0]));
982 dsp->historicsilence[DSP_HISTORY - 1] = dsp->totalsilence;
985 dsp->totalsilence = 0;
988 *totalsilence = dsp->totalsilence;
992 int ast_dsp_busydetect(struct ast_dsp *dsp)
997 if (dsp->busymaybe) {
999 printf("Maybe busy!\n");
1004 for (x=DSP_HISTORY - dsp->busycount;x<DSP_HISTORY;x++) {
1006 printf("Silence: %d, Noise: %d\n", dsp->historicsilence[x], dsp->historicnoise[x]);
1008 if (dsp->historicsilence[x] < min)
1009 min = dsp->historicsilence[x];
1010 if (dsp->historicnoise[x] < min)
1011 min = dsp->historicnoise[x];
1012 if (dsp->historicsilence[x] > max)
1013 max = dsp->historicsilence[x];
1014 if (dsp->historicnoise[x] > max)
1015 max = dsp->historicnoise[x];
1017 if ((max - min < BUSY_THRESHOLD) && (max < BUSY_MAX) && (min > BUSY_MIN)) {
1024 printf("Min: %d, max: %d\n", min, max);
1030 int ast_dsp_silence(struct ast_dsp *dsp, struct ast_frame *f, int *totalsilence)
1035 if (f->frametype != AST_FRAME_VOICE) {
1036 ast_log(LOG_WARNING, "Can't calculate silence on a non-voice frame\n");
1039 if (f->subclass != AST_FORMAT_SLINEAR) {
1040 ast_log(LOG_WARNING, "Can only calculate silence on signed-linear frames :(\n");
1045 return __ast_dsp_silence(dsp, s, len, totalsilence);
1048 struct ast_frame *ast_dsp_process(struct ast_channel *chan, struct ast_dsp *dsp, struct ast_frame *af, int needlock)
1054 unsigned short *shortdata;
1055 unsigned char *odata;
1059 #define FIX_INF(inf) do { \
1061 switch(inf->subclass) { \
1062 case AST_FORMAT_SLINEAR: \
1064 case AST_FORMAT_ULAW: \
1065 for (x=0;x<len;x++) \
1066 odata[x] = AST_LIN2MU(shortdata[x]); \
1068 case AST_FORMAT_ALAW: \
1069 for (x=0;x<len;x++) \
1070 odata[x] = AST_LIN2A(shortdata[x]); \
1078 if (af->frametype != AST_FRAME_VOICE)
1082 /* Make sure we have short data */
1083 switch(af->subclass) {
1084 case AST_FORMAT_SLINEAR:
1085 shortdata = af->data;
1086 len = af->datalen / 2;
1088 case AST_FORMAT_ULAW:
1089 shortdata = alloca(af->datalen * 2);
1091 ast_log(LOG_WARNING, "Unable to allocate stack space for data: %s\n", strerror(errno));
1095 shortdata[x] = AST_MULAW(odata[x]);
1097 case AST_FORMAT_ALAW:
1098 shortdata = alloca(af->datalen * 2);
1100 ast_log(LOG_WARNING, "Unable to allocate stack space for data: %s\n", strerror(errno));
1104 shortdata[x] = AST_ALAW(odata[x]);
1107 ast_log(LOG_WARNING, "Unable to detect process %d frames\n", af->subclass);
1110 silence = __ast_dsp_silence(dsp, shortdata, len, NULL);
1111 if ((dsp->features & DSP_FEATURE_SILENCE_SUPPRESS) && silence) {
1112 memset(&dsp->f, 0, sizeof(dsp->f));
1113 dsp->f.frametype = AST_FRAME_NULL;
1116 if ((dsp->features & DSP_FEATURE_BUSY_DETECT) && ast_dsp_busydetect(dsp)) {
1117 memset(&dsp->f, 0, sizeof(dsp->f));
1118 dsp->f.frametype = AST_FRAME_CONTROL;
1119 dsp->f.subclass = AST_CONTROL_BUSY;
1122 if ((dsp->features & DSP_FEATURE_DTMF_DETECT)) {
1123 digit = __ast_dsp_digitdetect(dsp, shortdata, len, &writeback);
1126 printf("Performing digit detection returned %d, digitmode is %d\n", digit, dsp->digitmode);
1128 if (dsp->digitmode & (DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX)) {
1129 if (!dsp->thinkdigit) {
1131 /* Looks like we might have something. Request a conference mute for the moment */
1132 memset(&dsp->f, 0, sizeof(dsp->f));
1133 dsp->f.frametype = AST_FRAME_DTMF;
1134 dsp->f.subclass = 'm';
1135 dsp->thinkdigit = 'x';
1138 ast_queue_frame(chan, af, needlock);
1144 /* Thought we saw one last time. Pretty sure we really have now */
1145 if (dsp->thinkdigit) {
1146 if ((dsp->thinkdigit != 'x') && (dsp->thinkdigit != digit)) {
1147 /* If we found a digit, and we're changing digits, go
1148 ahead and send this one, but DON'T stop confmute because
1149 we're detecting something else, too... */
1150 memset(&dsp->f, 0, sizeof(dsp->f));
1151 dsp->f.frametype = AST_FRAME_DTMF;
1152 dsp->f.subclass = dsp->thinkdigit;
1155 ast_queue_frame(chan, af, needlock);
1158 dsp->thinkdigit = digit;
1161 dsp->thinkdigit = digit;
1163 if (dsp->thinkdigit) {
1164 memset(&dsp->f, 0, sizeof(dsp->f));
1165 if (dsp->thinkdigit != 'x') {
1166 /* If we found a digit, send it now */
1167 dsp->f.frametype = AST_FRAME_DTMF;
1168 dsp->f.subclass = dsp->thinkdigit;
1170 ast_queue_frame(chan, &dsp->f, needlock);
1172 dsp->f.frametype = AST_FRAME_DTMF;
1173 dsp->f.subclass = 'u';
1174 dsp->thinkdigit = 0;
1177 ast_queue_frame(chan, af, needlock);
1183 } else if (!digit) {
1184 /* Only check when there is *not* a hit... */
1185 if (dsp->digitmode & DSP_DIGITMODE_MF) {
1186 if (dsp->td.mf.current_digits) {
1187 memset(&dsp->f, 0, sizeof(dsp->f));
1188 dsp->f.frametype = AST_FRAME_DTMF;
1189 dsp->f.subclass = dsp->td.mf.digits[0];
1190 memmove(dsp->td.mf.digits, dsp->td.mf.digits + 1, dsp->td.mf.current_digits);
1191 dsp->td.mf.current_digits--;
1194 ast_queue_frame(chan, af, needlock);
1199 if (dsp->td.dtmf.current_digits) {
1200 memset(&dsp->f, 0, sizeof(dsp->f));
1201 dsp->f.frametype = AST_FRAME_DTMF;
1202 dsp->f.subclass = dsp->td.dtmf.digits[0];
1203 memmove(dsp->td.dtmf.digits, dsp->td.dtmf.digits + 1, dsp->td.dtmf.current_digits);
1204 dsp->td.dtmf.current_digits--;
1207 ast_queue_frame(chan, af, needlock);
1214 if ((dsp->features & DSP_FEATURE_CALL_PROGRESS)) {
1215 res = __ast_dsp_call_progress(dsp, shortdata, len);
1216 memset(&dsp->f, 0, sizeof(dsp->f));
1217 dsp->f.frametype = AST_FRAME_CONTROL;
1220 case AST_CONTROL_ANSWER:
1221 case AST_CONTROL_BUSY:
1222 case AST_CONTROL_RINGING:
1223 case AST_CONTROL_CONGESTION:
1224 dsp->f.subclass = res;
1226 ast_queue_frame(chan, &dsp->f, needlock);
1229 ast_log(LOG_WARNING, "Don't know how to represent call progress message %d\n", res);
1237 struct ast_dsp *ast_dsp_new(void)
1239 struct ast_dsp *dsp;
1240 dsp = malloc(sizeof(struct ast_dsp));
1242 memset(dsp, 0, sizeof(struct ast_dsp));
1243 dsp->threshold = DEFAULT_THRESHOLD;
1244 dsp->features = DSP_FEATURE_SILENCE_SUPPRESS;
1246 /* Initialize goertzels */
1247 goertzel_init(&dsp->freqs[HZ_350], 350.0);
1248 goertzel_init(&dsp->freqs[HZ_440], 440.0);
1249 goertzel_init(&dsp->freqs[HZ_480], 480.0);
1250 goertzel_init(&dsp->freqs[HZ_620], 620.0);
1251 goertzel_init(&dsp->freqs[HZ_950], 950.0);
1252 goertzel_init(&dsp->freqs[HZ_1400], 1400.0);
1253 goertzel_init(&dsp->freqs[HZ_1800], 1800.0);
1254 /* Initialize DTMF detector */
1255 ast_dtmf_detect_init(&dsp->td.dtmf);
1260 void ast_dsp_set_features(struct ast_dsp *dsp, int features)
1262 dsp->features = features;
1265 void ast_dsp_free(struct ast_dsp *dsp)
1270 void ast_dsp_set_threshold(struct ast_dsp *dsp, int threshold)
1272 dsp->threshold = threshold;
1275 void ast_dsp_set_busy_count(struct ast_dsp *dsp, int cadences)
1279 if (cadences > DSP_HISTORY)
1280 cadences = DSP_HISTORY;
1281 dsp->busycount = cadences;
1284 void ast_dsp_digitreset(struct ast_dsp *dsp)
1287 dsp->thinkdigit = 0;
1288 if (dsp->digitmode & DSP_DIGITMODE_MF) {
1289 memset(dsp->td.mf.digits, 0, sizeof(dsp->td.mf.digits));
1290 dsp->td.mf.current_digits = 0;
1291 /* Reinitialise the detector for the next block */
1292 for (i = 0; i < 6; i++) {
1293 goertzel_reset(&dsp->td.mf.tone_out[i]);
1294 goertzel_reset(&dsp->td.mf.tone_out2nd[i]);
1296 dsp->td.mf.energy = 0.0;
1297 dsp->td.mf.current_sample = 0;
1298 dsp->td.mf.hit1 = dsp->td.mf.hit2 = dsp->td.mf.hit3 = dsp->td.mf.hit4 = dsp->td.mf.mhit = 0;
1300 memset(dsp->td.dtmf.digits, 0, sizeof(dsp->td.dtmf.digits));
1301 dsp->td.dtmf.current_digits = 0;
1302 /* Reinitialise the detector for the next block */
1303 for (i = 0; i < 4; i++) {
1304 goertzel_reset(&dsp->td.dtmf.row_out[i]);
1305 goertzel_reset(&dsp->td.dtmf.col_out[i]);
1306 goertzel_reset(&dsp->td.dtmf.row_out2nd[i]);
1307 goertzel_reset(&dsp->td.dtmf.col_out2nd[i]);
1309 goertzel_reset (&dsp->td.dtmf.fax_tone);
1310 goertzel_reset (&dsp->td.dtmf.fax_tone2nd);
1311 dsp->td.dtmf.energy = 0.0;
1312 dsp->td.dtmf.current_sample = 0;
1313 dsp->td.dtmf.hit1 = dsp->td.dtmf.hit2 = dsp->td.dtmf.hit3 = dsp->td.dtmf.hit4 = dsp->td.dtmf.mhit = 0;
1317 void ast_dsp_reset(struct ast_dsp *dsp)
1320 dsp->totalsilence = 0;
1323 dsp->freqs[x].v2 = dsp->freqs[x].v3 = 0.0;
1324 memset(dsp->historicsilence, 0, sizeof(dsp->historicsilence));
1325 memset(dsp->historicnoise, 0, sizeof(dsp->historicnoise));
1329 int ast_dsp_digitmode(struct ast_dsp *dsp, int digitmode)
1332 old = dsp->digitmode & (DSP_DIGITMODE_DTMF | DSP_DIGITMODE_MF | DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX);
1333 new = digitmode & (DSP_DIGITMODE_DTMF | DSP_DIGITMODE_MF | DSP_DIGITMODE_MUTECONF | DSP_DIGITMODE_MUTEMAX);
1335 /* Must initialize structures if switching from MF to DTMF or vice-versa */
1336 if (new & DSP_DIGITMODE_MF)
1337 ast_mf_detect_init(&dsp->td.mf);
1339 ast_dtmf_detect_init(&dsp->td.dtmf);
1341 dsp->digitmode = digitmode;