Asterisk - The Open Source Telephony Project GIT-master-2de1a68
dsp.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 1999 - 2005, Digium, Inc.
5 *
6 * Mark Spencer <markster@digium.com>
7 *
8 * Goertzel routines are borrowed from Steve Underwood's tremendous work on the
9 * DTMF detector.
10 *
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.
16 *
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.
20 */
21
22/*! \file
23 *
24 * \brief Convenience Signal Processing routines
25 *
26 * \author Mark Spencer <markster@digium.com>
27 * \author Steve Underwood <steveu@coppice.org>
28 */
29
30/*! \li \ref dsp.c uses the configuration file \ref dsp.conf
31 * \addtogroup configuration_file Configuration Files
32 */
33
34/*!
35 * \page dsp.conf dsp.conf
36 * \verbinclude dsp.conf.sample
37 */
38
39/* Some routines from tone_detect.c by Steven Underwood as published under the zapata library */
40/*
41 tone_detect.c - General telephony tone detection, and specific
42 detection of DTMF.
43
44 Copyright (C) 2001 Steve Underwood <steveu@coppice.org>
45
46 Despite my general liking of the GPL, I place this code in the
47 public domain for the benefit of all mankind - even the slimy
48 ones who might try to proprietize my work and use it to my
49 detriment.
50*/
51
52/*** MODULEINFO
53 <support_level>core</support_level>
54 ***/
55
56#include "asterisk.h"
57
58#include <math.h>
59
60#include "asterisk/module.h"
61#include "asterisk/frame.h"
63#include "asterisk/channel.h"
64#include "asterisk/dsp.h"
65#include "asterisk/ulaw.h"
66#include "asterisk/alaw.h"
67#include "asterisk/utils.h"
68#include "asterisk/options.h"
69#include "asterisk/config.h"
70#include "asterisk/test.h"
71
72/*! Number of goertzels for progress detect */
74 GSAMP_SIZE_NA = 183, /*!< North America - 350, 440, 480, 620, 950, 1400, 1800 Hz */
75 GSAMP_SIZE_CR = 188, /*!< Costa Rica, Brazil - Only care about 425 Hz */
76 GSAMP_SIZE_UK = 160 /*!< UK disconnect goertzel feed - should trigger 400hz */
77};
78
83};
84
86 /*! For US modes { */
87 HZ_350 = 0,
93 HZ_1800, /*!< } */
94
95 /*! For CR/BR modes */
96 HZ_425 = 0,
97
98 /*! For UK mode */
103
104static struct progalias {
105 char *name;
107} aliases[] = {
108 { "us", PROG_MODE_NA },
109 { "ca", PROG_MODE_NA },
110 { "cr", PROG_MODE_CR },
111 { "br", PROG_MODE_CR },
112 { "uk", PROG_MODE_UK },
114
115#define FREQ_ARRAY_SIZE 7
116
117static struct progress {
120} modes[] = {
121 { GSAMP_SIZE_NA, { 350, 440, 480, 620, 950, 1400, 1800 } }, /*!< North America */
122 { GSAMP_SIZE_CR, { 425 } }, /*!< Costa Rica, Brazil */
123 { GSAMP_SIZE_UK, { 350, 400, 440 } }, /*!< UK */
125
126/*!
127 * \brief Default minimum average magnitude threshold to determine talking/noise by the DSP.
128 *
129 * \details
130 * The magnitude calculated for this threshold is determined by
131 * averaging the absolute value of all samples within a frame.
132 *
133 * This value is the threshold for which a frame's average magnitude
134 * is determined to either be silence (below the threshold) or
135 * noise/talking (at or above the threshold). Please note that while
136 * the default threshold is an even exponent of 2, there is no
137 * requirement that it be so. The threshold will work for any value
138 * between 1 and 2^15.
139 */
140#define DEFAULT_THRESHOLD 512
141
143 BUSY_PERCENT = 10, /*!< The percentage difference between the two last silence periods */
144 BUSY_PAT_PERCENT = 7, /*!< The percentage difference between measured and actual pattern */
145 BUSY_THRESHOLD = 100, /*!< Max number of ms difference between max and min times in busy */
146 BUSY_MIN = 75, /*!< Busy must be at least 80 ms in half-cadence */
147 BUSY_MAX = 3100 /*!< Busy can't be longer than 3100 ms in half-cadence */
149
150/*! Remember last 15 units */
151#define DSP_HISTORY 15
152
153#define TONE_THRESH 10.0 /*!< How much louder the tone should be than channel energy */
154#define TONE_MIN_THRESH 1e8 /*!< How much tone there should be at least to attempt */
155
156/*! All THRESH_XXX values are in GSAMP_SIZE chunks (us = 22ms) */
158 THRESH_RING = 8, /*!< Need at least 150ms ring to accept */
159 THRESH_TALK = 2, /*!< Talk detection does not work continuously */
160 THRESH_BUSY = 4, /*!< Need at least 80ms to accept */
161 THRESH_CONGESTION = 4, /*!< Need at least 80ms to accept */
162 THRESH_HANGUP = 60, /*!< Need at least 1300ms to accept hangup */
163 THRESH_RING2ANSWER = 300 /*!< Timeout from start of ring to answer (about 6600 ms) */
165
166#define MAX_DTMF_DIGITS 128
167
168#define DTMF_MATRIX_SIZE 4
169
170/* Basic DTMF (AT&T) specs:
171 *
172 * Minimum tone on = 40ms
173 * Minimum tone off = 50ms
174 * Maximum digit rate = 10 per second
175 * Normal twist <= 8dB accepted
176 * Reverse twist <= 4dB accepted
177 * S/N >= 15dB will detect OK
178 * Attenuation <= 26dB will detect OK
179 * Frequency tolerance +- 1.5% will detect, +-3.5% will reject
180 */
181
182#define DTMF_THRESHOLD 8.0e7
183#define TONE_THRESHOLD 7.8e7
184
185#define DEF_DTMF_NORMAL_TWIST 6.31 /* 8.0dB */
186#define DEF_RELAX_DTMF_NORMAL_TWIST 6.31 /* 8.0dB */
187
188#ifdef RADIO_RELAX
189#define DEF_DTMF_REVERSE_TWIST 2.51 /* 4.01dB */
190#define DEF_RELAX_DTMF_REVERSE_TWIST 6.61 /* 8.2dB */
191#else
192#define DEF_DTMF_REVERSE_TWIST 2.51 /* 4.01dB */
193#define DEF_RELAX_DTMF_REVERSE_TWIST 3.98 /* 6.0dB */
194#endif
195
196#define DTMF_RELATIVE_PEAK_ROW 6.3 /* 8dB */
197#define DTMF_RELATIVE_PEAK_COL 6.3 /* 8dB */
198#define DTMF_TO_TOTAL_ENERGY 42.0
199
200#define BELL_MF_THRESHOLD 1.6e9
201#define BELL_MF_TWIST 4.0 /* 6dB */
202#define BELL_MF_RELATIVE_PEAK 12.6 /* 11dB */
203
204#if defined(BUSYDETECT_TONEONLY) && defined(BUSYDETECT_COMPARE_TONE_AND_SILENCE)
205#error You cant use BUSYDETECT_TONEONLY together with BUSYDETECT_COMPARE_TONE_AND_SILENCE
206#endif
207
208/* The CNG signal consists of the transmission of 1100 Hz for 1/2 second,
209 * followed by a 3 second silent (2100 Hz OFF) period.
210 */
211#define FAX_TONE_CNG_FREQ 1100
212#define FAX_TONE_CNG_DURATION 500 /* ms */
213#define FAX_TONE_CNG_DB 16
214
215/* This signal may be sent by the Terminating FAX machine anywhere between
216 * 1.8 to 2.5 seconds AFTER answering the call. The CED signal consists
217 * of a 2100 Hz tone that is from 2.6 to 4 seconds in duration.
218*/
219#define FAX_TONE_CED_FREQ 2100
220#define FAX_TONE_CED_DURATION 2600 /* ms */
221#define FAX_TONE_CED_DB 16
222
223#define DEFAULT_SAMPLE_RATE 8000
224
225/* MF goertzel size */
226#define MF_GSIZE 120
227
228/* DTMF goertzel size */
229#define DTMF_GSIZE 102
230
231/* How many successive hits needed to consider begin of a digit
232 * IE. Override with dtmf_hits_to_begin=4 in dsp.conf
233 */
234#define DEF_DTMF_HITS_TO_BEGIN 2
235
236/* How many successive misses needed to consider end of a digit
237 * IE. Override with dtmf_misses_to_end=4 in dsp.conf
238 */
239#define DEF_DTMF_MISSES_TO_END 3
240
241/*!
242 * \brief The default silence threshold we will use if an alternate
243 * configured value is not present or is invalid.
244 */
245static const int DEFAULT_SILENCE_THRESHOLD = 256;
246
247#define CONFIG_FILE_NAME "dsp.conf"
248
249typedef struct {
250 /*! The previous previous sample calculation (No binary point just plain int) */
251 int v2;
252 /*! The previous sample calculation (No binary point just plain int) */
253 int v3;
254 /*! v2 and v3 power of two exponent to keep value in int range */
256 /*! 15 bit fixed point goertzel coefficient = 2 * cos(2 * pi * freq / sample_rate) */
257 int fac;
259
260typedef struct {
261 int value;
262 int power;
264
265typedef struct
266{
267 int freq;
269 int squelch; /* Remove (squelch) tone */
271 float energy; /* Accumulated energy of the current block */
272 int samples_pending; /* Samples remain to complete the current block */
273 int mute_samples; /* How many additional samples needs to be muted to suppress already detected tone */
274
275 int hits_required; /* How many successive blocks with tone we are looking for */
276 float threshold; /* Energy of the tone relative to energy from all other signals to consider a hit */
277
278 int hit_count; /* How many successive blocks we consider tone present */
279 int last_hit; /* Indicates if the last processed block was a hit */
280
282
283typedef struct
284{
287 int hits; /* How many successive hits we have seen already */
288 int misses; /* How many successive misses we have seen already */
291 float energy;
295
296typedef struct
297{
298 goertzel_state_t tone_out[6];
300 int hits[5];
304
305typedef struct
306{
307 char digits[MAX_DTMF_DIGITS + 1];
308 int digitlen[MAX_DTMF_DIGITS + 1];
312
313 union {
316 } td;
318
319static const float dtmf_row[] = {
320 697.0, 770.0, 852.0, 941.0
321};
322static const float dtmf_col[] = {
323 1209.0, 1336.0, 1477.0, 1633.0
324};
325static const float mf_tones[] = {
326 700.0, 900.0, 1100.0, 1300.0, 1500.0, 1700.0
327};
328static const char dtmf_positions[] = "123A" "456B" "789C" "*0#D";
329static const char bell_mf_positions[] = "1247C-358A--69*---0B----#";
331static float dtmf_normal_twist; /* AT&T = 8dB */
332static float dtmf_reverse_twist; /* AT&T = 4dB */
333static float relax_dtmf_normal_twist; /* AT&T = 8dB */
334static float relax_dtmf_reverse_twist; /* AT&T = 6dB */
335static int dtmf_hits_to_begin; /* How many successive hits needed to consider begin of a digit */
336static int dtmf_misses_to_end; /* How many successive misses needed to consider end of a digit */
337
338static inline void goertzel_sample(goertzel_state_t *s, short sample)
339{
340 int v1;
341
342 /*
343 * Shift previous values so
344 * v1 is previous previous value
345 * v2 is previous value
346 * until the new v3 is calculated.
347 */
348 v1 = s->v2;
349 s->v2 = s->v3;
350
351 /* Discard the binary fraction introduced by s->fac */
352 s->v3 = (s->fac * s->v2) >> 15;
353 /* Scale sample to match previous values */
354 s->v3 = s->v3 - v1 + (sample >> s->chunky);
355
356 if (abs(s->v3) > (1 << 15)) {
357 /* The result is now too large so increase the chunky power. */
358 s->chunky++;
359 s->v3 = s->v3 >> 1;
360 s->v2 = s->v2 >> 1;
361 }
362}
363
364static inline float goertzel_result(goertzel_state_t *s)
365{
367
368 r.value = (s->v3 * s->v3) + (s->v2 * s->v2);
369 r.value -= ((s->v2 * s->v3) >> 15) * s->fac;
370 /*
371 * We have to double the exponent because we multiplied the
372 * previous sample calculation values together.
373 */
374 r.power = s->chunky * 2;
375 return (float)r.value * (float)(1 << r.power);
376}
377
378static inline void goertzel_init(goertzel_state_t *s, float freq, unsigned int sample_rate)
379{
380 s->v2 = s->v3 = s->chunky = 0;
381 s->fac = (int)(32768.0 * 2.0 * cos(2.0 * M_PI * freq / sample_rate));
382}
383
384static inline void goertzel_reset(goertzel_state_t *s)
385{
386 s->v2 = s->v3 = s->chunky = 0;
387}
388
389typedef struct {
390 int start;
391 int end;
392} fragment_t;
393
394/* Note on tone suppression (squelching). Individual detectors (DTMF/MF/generic tone)
395 * report fragments of the frame in which detected tone resides and which needs
396 * to be "muted" in order to suppress the tone. To mark fragment for muting,
397 * detectors call mute_fragment passing fragment_t there. Multiple fragments
398 * can be marked and ast_dsp_process later will mute all of them.
399 *
400 * Note: When tone starts in the middle of a Goertzel block, it won't be properly
401 * detected in that block, only in the next. If we only mute the next block
402 * where tone is actually detected, the user will still hear beginning
403 * of the tone in preceeding block. This is why we usually want to mute some amount
404 * of samples preceeding and following the block where tone was detected.
405*/
406
407struct ast_dsp {
408 struct ast_frame f;
410 /*! Accumulated total silence in ms since last talking/noise. */
412 /*! Accumulated total talking/noise in ms since last silence. */
416 int busymaybe; /* Boolean, could be a bitfield */
431 int dtmf_began; /* Boolean, could be a bitfield */
432 int display_inband_dtmf_warning; /* Boolean, could be a bitfield */
433 float genergy;
435 unsigned int sample_rate;
440};
441
442static void mute_fragment(struct ast_dsp *dsp, fragment_t *fragment)
443{
444 if (dsp->mute_fragments >= ARRAY_LEN(dsp->mute_data)) {
445 ast_log(LOG_ERROR, "Too many fragments to mute. Ignoring\n");
446 return;
447 }
448
449 dsp->mute_data[dsp->mute_fragments++] = *fragment;
450}
451
452static void ast_tone_detect_init(tone_detect_state_t *s, int freq, int duration, int amp, unsigned int sample_rate)
453{
454 int duration_samples;
455 float x;
456 int periods_in_block;
457
458 s->freq = freq;
459
460 /* Desired tone duration in samples */
461 duration_samples = duration * sample_rate / 1000;
462 /* We want to allow 10% deviation of tone duration */
463 duration_samples = duration_samples * 9 / 10;
464
465 /* If we want to remove tone, it is important to have block size not
466 to exceed frame size. Otherwise by the moment tone is detected it is too late
467 to squelch it from previous frames. Block size is 20ms at the given sample rate.*/
468 s->block_size = (20 * sample_rate) / 1000;
469
470 periods_in_block = s->block_size * freq / sample_rate;
471
472 /* Make sure we will have at least 5 periods at target frequency for analysis.
473 This may make block larger than expected packet and will make squelching impossible
474 but at least we will be detecting the tone */
475 if (periods_in_block < 5) {
476 periods_in_block = 5;
477 }
478
479 /* Now calculate final block size. It will contain integer number of periods */
480 s->block_size = periods_in_block * sample_rate / freq;
481
482 /* tone_detect is generally only used to detect fax tones and we
483 do not need squelching the fax tones */
484 s->squelch = 0;
485
486 /* Account for the first and the last block to be incomplete
487 and thus no tone will be detected in them */
488 s->hits_required = (duration_samples - (s->block_size - 1)) / s->block_size;
489
490 goertzel_init(&s->tone, freq, sample_rate);
491
493 s->hit_count = 0;
494 s->last_hit = 0;
495 s->energy = 0.0;
496
497 /* We want tone energy to be amp decibels above the rest of the signal (the noise).
498 According to Parseval's theorem the energy computed in time domain equals to energy
499 computed in frequency domain. So subtracting energy in the frequency domain (Goertzel result)
500 from the energy in the time domain we will get energy of the remaining signal (without the tone
501 we are detecting). We will be checking that
502 10*log(Ew / (Et - Ew)) > amp
503 Calculate threshold so that we will be actually checking
504 Ew > Et * threshold
505 */
506
507 x = pow(10.0, amp / 10.0);
508 s->threshold = x / (x + 1);
509
510 ast_debug(1, "Setup tone %d Hz, %d ms, block_size=%d, hits_required=%d\n", freq, duration, s->block_size, s->hits_required);
511}
512
513static void ast_fax_detect_init(struct ast_dsp *s)
514{
518 s->cng_tone_state.squelch = 1;
519 s->ced_tone_state.squelch = 1;
520 }
521
522}
523
524static void ast_freq_detect_init(struct ast_dsp *s, int freq, int dur, int db, int squelch)
525{
526 /* we can conveniently just use one of the two fax tone states */
528 if (s->freqmode & squelch) {
529 s->cng_tone_state.squelch = 1;
530 }
531}
532
533static void ast_dtmf_detect_init(dtmf_detect_state_t *s, unsigned int sample_rate)
534{
535 int i;
536
537 for (i = 0; i < DTMF_MATRIX_SIZE; i++) {
538 goertzel_init(&s->row_out[i], dtmf_row[i], sample_rate);
539 goertzel_init(&s->col_out[i], dtmf_col[i], sample_rate);
540 }
541 s->lasthit = 0;
542 s->current_hit = 0;
543 s->energy = 0.0;
544 s->current_sample = 0;
545 s->hits = 0;
546 s->misses = 0;
547}
548
549static void ast_mf_detect_init(mf_detect_state_t *s, unsigned int sample_rate)
550{
551 int i;
552
553 for (i = 0; i < 6; i++) {
554 goertzel_init(&s->tone_out[i], mf_tones[i], sample_rate);
555 }
556 s->hits[0] = s->hits[1] = s->hits[2] = s->hits[3] = s->hits[4] = 0;
557 s->current_sample = 0;
558 s->current_hit = 0;
559}
560
561static void ast_digit_detect_init(digit_detect_state_t *s, int mf, unsigned int sample_rate)
562{
563 s->current_digits = 0;
564 s->detected_digits = 0;
565 s->lost_digits = 0;
566 s->digits[0] = '\0';
567
568 if (mf) {
569 ast_mf_detect_init(&s->td.mf, sample_rate);
570 } else {
571 ast_dtmf_detect_init(&s->td.dtmf, sample_rate);
572 }
573}
574
575static int tone_detect(struct ast_dsp *dsp, tone_detect_state_t *s, int16_t *amp, int samples)
576{
577 float tone_energy;
578 int i;
579 int hit = 0;
580 int limit;
581 int res = 0;
582 int16_t *ptr;
583 short samp;
584 int start, end;
585 fragment_t mute = {0, 0};
586
587 if (s->squelch && s->mute_samples > 0) {
588 mute.end = (s->mute_samples < samples) ? s->mute_samples : samples;
589 s->mute_samples -= mute.end;
590 }
591
592 for (start = 0; start < samples; start = end) {
593 /* Process in blocks. */
594 limit = samples - start;
595 if (limit > s->samples_pending) {
596 limit = s->samples_pending;
597 }
598 end = start + limit;
599
600 for (i = limit, ptr = amp ; i > 0; i--, ptr++) {
601 samp = *ptr;
602 /* signed 32 bit int should be enough to square any possible signed 16 bit value */
603 s->energy += (int32_t) samp * (int32_t) samp;
604
605 goertzel_sample(&s->tone, samp);
606 }
607
608 s->samples_pending -= limit;
609
610 if (s->samples_pending) {
611 /* Finished incomplete (last) block */
612 break;
613 }
614
615 tone_energy = goertzel_result(&s->tone);
616
617 /* Scale to make comparable */
618 tone_energy *= 2.0;
619 s->energy *= s->block_size;
620
621 ast_debug(10, "%d Hz tone %2d Ew=%.4E, Et=%.4E, s/n=%10.2f\n", s->freq, s->hit_count, tone_energy, s->energy, tone_energy / (s->energy - tone_energy));
622 hit = 0;
623 if (TONE_THRESHOLD <= tone_energy
624 && tone_energy > s->energy * s->threshold) {
625 ast_debug(10, "%d Hz tone Hit! %2d Ew=%.4E, Et=%.4E, s/n=%10.2f\n", s->freq, s->hit_count, tone_energy, s->energy, tone_energy / (s->energy - tone_energy));
626 hit = 1;
627 }
628
629 if (s->hit_count) {
630 s->hit_count++;
631 }
632
633 if (hit == s->last_hit) {
634 if (!hit) {
635 /* Two successive misses. Tone ended */
636 s->hit_count = 0;
637 } else if (!s->hit_count) {
638 s->hit_count++;
639 }
640
641 }
642
643 if (s->hit_count == s->hits_required) {
644 ast_debug(1, "%d Hz tone detected\n", s->freq);
645 res = 1;
646 }
647
648 s->last_hit = hit;
649
650 /* If we had a hit in this block, include it into mute fragment */
651 if (s->squelch && hit) {
652 if (mute.end < start - s->block_size) {
653 /* There is a gap between fragments */
654 mute_fragment(dsp, &mute);
655 mute.start = (start > s->block_size) ? (start - s->block_size) : 0;
656 }
657 mute.end = end + s->block_size;
658 }
659
660 /* Reinitialise the detector for the next block */
661 /* Reset for the next block */
662 goertzel_reset(&s->tone);
663
664 /* Advance to the next block */
665 s->energy = 0.0;
667
668 amp += limit;
669 }
670
671 if (s->squelch && mute.end) {
672 if (mute.end > samples) {
673 s->mute_samples = mute.end - samples;
674 mute.end = samples;
675 }
676 mute_fragment(dsp, &mute);
677 }
678
679 return res;
680}
681
683{
684 s->detected_digits++;
686 s->digitlen[s->current_digits] = 0;
687 s->digits[s->current_digits++] = digit;
688 s->digits[s->current_digits] = '\0';
689 } else {
690 ast_log(LOG_WARNING, "Digit lost due to full buffer\n");
691 s->lost_digits++;
692 }
693}
694
695static int dtmf_detect(struct ast_dsp *dsp, digit_detect_state_t *s, int16_t amp[], int samples, int squelch, int relax)
696{
697 float row_energy[DTMF_MATRIX_SIZE];
698 float col_energy[DTMF_MATRIX_SIZE];
699 int i;
700 int j;
701 int sample;
702 short samp;
703 int best_row;
704 int best_col;
705 int hit;
706 int limit;
707 fragment_t mute = {0, 0};
708
709 if (squelch && s->td.dtmf.mute_samples > 0) {
710 mute.end = (s->td.dtmf.mute_samples < samples) ? s->td.dtmf.mute_samples : samples;
711 s->td.dtmf.mute_samples -= mute.end;
712 }
713
714 hit = 0;
715 for (sample = 0; sample < samples; sample = limit) {
716 /* DTMF_GSIZE is optimised to meet the DTMF specs. */
717 if ((samples - sample) >= (DTMF_GSIZE - s->td.dtmf.current_sample)) {
718 limit = sample + (DTMF_GSIZE - s->td.dtmf.current_sample);
719 } else {
720 limit = samples;
721 }
722 /* The following unrolled loop takes only 35% (rough estimate) of the
723 time of a rolled loop on the machine on which it was developed */
724 for (j = sample; j < limit; j++) {
725 samp = amp[j];
726 s->td.dtmf.energy += (int32_t) samp * (int32_t) samp;
727 /* With GCC 2.95, the following unrolled code seems to take about 35%
728 (rough estimate) as long as a neat little 0-3 loop */
729 goertzel_sample(s->td.dtmf.row_out, samp);
730 goertzel_sample(s->td.dtmf.col_out, samp);
731 goertzel_sample(s->td.dtmf.row_out + 1, samp);
732 goertzel_sample(s->td.dtmf.col_out + 1, samp);
733 goertzel_sample(s->td.dtmf.row_out + 2, samp);
734 goertzel_sample(s->td.dtmf.col_out + 2, samp);
735 goertzel_sample(s->td.dtmf.row_out + 3, samp);
736 goertzel_sample(s->td.dtmf.col_out + 3, samp);
737 /* go up to DTMF_MATRIX_SIZE - 1 */
738 }
739 s->td.dtmf.current_sample += (limit - sample);
740 if (s->td.dtmf.current_sample < DTMF_GSIZE) {
741 continue;
742 }
743 /* We are at the end of a DTMF detection block */
744 /* Find the peak row and the peak column */
745 row_energy[0] = goertzel_result(&s->td.dtmf.row_out[0]);
746 col_energy[0] = goertzel_result(&s->td.dtmf.col_out[0]);
747
748 for (best_row = best_col = 0, i = 1; i < DTMF_MATRIX_SIZE; i++) {
749 row_energy[i] = goertzel_result(&s->td.dtmf.row_out[i]);
750 if (row_energy[i] > row_energy[best_row]) {
751 best_row = i;
752 }
753 col_energy[i] = goertzel_result(&s->td.dtmf.col_out[i]);
754 if (col_energy[i] > col_energy[best_col]) {
755 best_col = i;
756 }
757 }
758 ast_debug(10, "DTMF best '%c' Erow=%.4E Ecol=%.4E Erc=%.4E Et=%.4E\n",
759 dtmf_positions[(best_row << 2) + best_col],
760 row_energy[best_row], col_energy[best_col],
761 row_energy[best_row] + col_energy[best_col], s->td.dtmf.energy);
762 hit = 0;
763 /* Basic signal level test and the twist test */
764 if (row_energy[best_row] >= DTMF_THRESHOLD &&
765 col_energy[best_col] >= DTMF_THRESHOLD &&
766 col_energy[best_col] < row_energy[best_row] * (relax ? relax_dtmf_reverse_twist : dtmf_reverse_twist) &&
767 row_energy[best_row] < col_energy[best_col] * (relax ? relax_dtmf_normal_twist : dtmf_normal_twist)) {
768 /* Relative peak test */
769 for (i = 0; i < DTMF_MATRIX_SIZE; i++) {
770 if ((i != best_col &&
771 col_energy[i] * DTMF_RELATIVE_PEAK_COL > col_energy[best_col]) ||
772 (i != best_row
773 && row_energy[i] * DTMF_RELATIVE_PEAK_ROW > row_energy[best_row])) {
774 break;
775 }
776 }
777 /* ... and fraction of total energy test */
778 if (i >= DTMF_MATRIX_SIZE &&
779 (row_energy[best_row] + col_energy[best_col]) > DTMF_TO_TOTAL_ENERGY * s->td.dtmf.energy) {
780 /* Got a hit */
781 hit = dtmf_positions[(best_row << 2) + best_col];
782 ast_debug(10, "DTMF hit '%c'\n", hit);
783 }
784 }
785
786/*
787 * Adapted from ETSI ES 201 235-3 V1.3.1 (2006-03)
788 * (40ms reference is tunable with hits_to_begin and misses_to_end)
789 * each hit/miss is 12.75ms with DTMF_GSIZE at 102
790 *
791 * Character recognition: When not DRC *(1) and then
792 * Shall exist VSC > 40 ms (hits_to_begin)
793 * May exist 20 ms <= VSC <= 40 ms
794 * Shall not exist VSC < 20 ms
795 *
796 * Character recognition: When DRC and then
797 * Shall cease Not VSC > 40 ms (misses_to_end)
798 * May cease 20 ms >= Not VSC >= 40 ms
799 * Shall not cease Not VSC < 20 ms
800 *
801 * *(1) or optionally a different digit recognition condition
802 *
803 * Legend: VSC The continuous existence of a valid signal condition.
804 * Not VSC The continuous non-existence of valid signal condition.
805 * DRC The existence of digit recognition condition.
806 * Not DRC The non-existence of digit recognition condition.
807 */
808
809/*
810 * Example: hits_to_begin=2 misses_to_end=3
811 * -------A last_hit=A hits=0&1
812 * ------AA hits=2 current_hit=A misses=0 BEGIN A
813 * -----AA- misses=1 last_hit=' ' hits=0
814 * ----AA-- misses=2
815 * ---AA--- misses=3 current_hit=' ' END A
816 * --AA---B last_hit=B hits=0&1
817 * -AA---BC last_hit=C hits=0&1
818 * AA---BCC hits=2 current_hit=C misses=0 BEGIN C
819 * A---BCC- misses=1 last_hit=' ' hits=0
820 * ---BCC-C misses=0 last_hit=C hits=0&1
821 * --BCC-CC misses=0
822 *
823 * Example: hits_to_begin=3 misses_to_end=2
824 * -------A last_hit=A hits=0&1
825 * ------AA hits=2
826 * -----AAA hits=3 current_hit=A misses=0 BEGIN A
827 * ----AAAB misses=1 last_hit=B hits=0&1
828 * ---AAABB misses=2 current_hit=' ' hits=2 END A
829 * --AAABBB hits=3 current_hit=B misses=0 BEGIN B
830 * -AAABBBB misses=0
831 *
832 * Example: hits_to_begin=2 misses_to_end=2
833 * -------A last_hit=A hits=0&1
834 * ------AA hits=2 current_hit=A misses=0 BEGIN A
835 * -----AAB misses=1 hits=0&1
836 * ----AABB misses=2 current_hit=' ' hits=2 current_hit=B misses=0 BEGIN B
837 * ---AABBB misses=0
838 */
839
840 if (s->td.dtmf.current_hit) {
841 /* We are in the middle of a digit already */
842 if (hit != s->td.dtmf.current_hit) {
843 s->td.dtmf.misses++;
844 if (s->td.dtmf.misses == dtmf_misses_to_end) {
845 /* There were enough misses to consider digit ended */
846 s->td.dtmf.current_hit = 0;
847 }
848 } else {
849 s->td.dtmf.misses = 0;
850 /* Current hit was same as last, so increment digit duration (of last digit) */
851 s->digitlen[s->current_digits - 1] += DTMF_GSIZE;
852 }
853 }
854
855 /* Look for a start of a new digit no matter if we are already in the middle of some
856 digit or not. This is because hits_to_begin may be smaller than misses_to_end
857 and we may find begin of new digit before we consider last one ended. */
858
859 if (hit != s->td.dtmf.lasthit) {
860 s->td.dtmf.lasthit = hit;
861 s->td.dtmf.hits = 0;
862 }
863 if (hit && hit != s->td.dtmf.current_hit) {
864 s->td.dtmf.hits++;
865 if (s->td.dtmf.hits == dtmf_hits_to_begin) {
866 store_digit(s, hit);
868 s->td.dtmf.current_hit = hit;
869 s->td.dtmf.misses = 0;
870 }
871 }
872
873 /* If we had a hit in this block, include it into mute fragment */
874 if (squelch && hit) {
875 if (mute.end < sample - DTMF_GSIZE) {
876 /* There is a gap between fragments */
877 mute_fragment(dsp, &mute);
878 mute.start = (sample > DTMF_GSIZE) ? (sample - DTMF_GSIZE) : 0;
879 }
880 mute.end = limit + DTMF_GSIZE;
881 }
882
883 /* Reinitialise the detector for the next block */
884 for (i = 0; i < DTMF_MATRIX_SIZE; i++) {
887 }
888 s->td.dtmf.energy = 0.0;
889 s->td.dtmf.current_sample = 0;
890 }
891
892 if (squelch && mute.end) {
893 if (mute.end > samples) {
894 s->td.dtmf.mute_samples = mute.end - samples;
895 mute.end = samples;
896 }
897 mute_fragment(dsp, &mute);
898 }
899
900 return (s->td.dtmf.current_hit); /* return the debounced hit */
901}
902
903static int mf_detect(struct ast_dsp *dsp, digit_detect_state_t *s, int16_t amp[],
904 int samples, int squelch, int relax)
905{
906 float energy[6];
907 int best;
908 int second_best;
909 int i;
910 int j;
911 int sample;
912 short samp;
913 int hit;
914 int limit;
915 fragment_t mute = {0, 0};
916
917 if (squelch && s->td.mf.mute_samples > 0) {
918 mute.end = (s->td.mf.mute_samples < samples) ? s->td.mf.mute_samples : samples;
919 s->td.mf.mute_samples -= mute.end;
920 }
921
922 hit = 0;
923 for (sample = 0; sample < samples; sample = limit) {
924 /* 80 is optimised to meet the MF specs. */
925 /* XXX So then why is MF_GSIZE defined as 120? */
926 if ((samples - sample) >= (MF_GSIZE - s->td.mf.current_sample)) {
927 limit = sample + (MF_GSIZE - s->td.mf.current_sample);
928 } else {
929 limit = samples;
930 }
931 /* The following unrolled loop takes only 35% (rough estimate) of the
932 time of a rolled loop on the machine on which it was developed */
933 for (j = sample; j < limit; j++) {
934 /* With GCC 2.95, the following unrolled code seems to take about 35%
935 (rough estimate) as long as a neat little 0-3 loop */
936 samp = amp[j];
937 goertzel_sample(s->td.mf.tone_out, samp);
938 goertzel_sample(s->td.mf.tone_out + 1, samp);
939 goertzel_sample(s->td.mf.tone_out + 2, samp);
940 goertzel_sample(s->td.mf.tone_out + 3, samp);
941 goertzel_sample(s->td.mf.tone_out + 4, samp);
942 goertzel_sample(s->td.mf.tone_out + 5, samp);
943 }
944 s->td.mf.current_sample += (limit - sample);
945 if (s->td.mf.current_sample < MF_GSIZE) {
946 continue;
947 }
948 /* We're at the end of an MF detection block. */
949 /* Find the two highest energies. The spec says to look for
950 two tones and two tones only. Taking this literally -ie
951 only two tones pass the minimum threshold - doesn't work
952 well. The sinc function mess, due to rectangular windowing
953 ensure that! Find the two highest energies and ensure they
954 are considerably stronger than any of the others. */
955 energy[0] = goertzel_result(&s->td.mf.tone_out[0]);
956 energy[1] = goertzel_result(&s->td.mf.tone_out[1]);
957 if (energy[0] > energy[1]) {
958 best = 0;
959 second_best = 1;
960 } else {
961 best = 1;
962 second_best = 0;
963 }
964 /*endif*/
965 for (i = 2; i < 6; i++) {
966 energy[i] = goertzel_result(&s->td.mf.tone_out[i]);
967 if (energy[i] >= energy[best]) {
968 second_best = best;
969 best = i;
970 } else if (energy[i] >= energy[second_best]) {
971 second_best = i;
972 }
973 }
974 /* Basic signal level and twist tests */
975 hit = 0;
976 if (energy[best] >= BELL_MF_THRESHOLD && energy[second_best] >= BELL_MF_THRESHOLD
977 && energy[best] < energy[second_best]*BELL_MF_TWIST
978 && energy[best] * BELL_MF_TWIST > energy[second_best]) {
979 /* Relative peak test */
980 hit = -1;
981 for (i = 0; i < 6; i++) {
982 if (i != best && i != second_best) {
983 if (energy[i]*BELL_MF_RELATIVE_PEAK >= energy[second_best]) {
984 /* The best two are not clearly the best */
985 hit = 0;
986 break;
987 }
988 }
989 }
990 }
991 if (hit) {
992 /* Get the values into ascending order */
993 if (second_best < best) {
994 i = best;
995 best = second_best;
996 second_best = i;
997 }
998 best = best * 5 + second_best - 1;
999 hit = bell_mf_positions[best];
1000 /* Look for two successive similar results */
1001 /* The logic in the next test is:
1002 For KP we need 4 successive identical clean detects, with
1003 two blocks of something different preceeding it. For anything
1004 else we need two successive identical clean detects, with
1005 two blocks of something different preceeding it. */
1006 if (hit == s->td.mf.hits[4] && hit == s->td.mf.hits[3] &&
1007 ((hit != '*' && hit != s->td.mf.hits[2] && hit != s->td.mf.hits[1])||
1008 (hit == '*' && hit == s->td.mf.hits[2] && hit != s->td.mf.hits[1] &&
1009 hit != s->td.mf.hits[0]))) {
1010 store_digit(s, hit);
1011 }
1012 }
1013
1014
1015 if (hit != s->td.mf.hits[4] && hit != s->td.mf.hits[3]) {
1016 /* Two successive block without a hit terminate current digit */
1017 s->td.mf.current_hit = 0;
1018 }
1019
1020 s->td.mf.hits[0] = s->td.mf.hits[1];
1021 s->td.mf.hits[1] = s->td.mf.hits[2];
1022 s->td.mf.hits[2] = s->td.mf.hits[3];
1023 s->td.mf.hits[3] = s->td.mf.hits[4];
1024 s->td.mf.hits[4] = hit;
1025
1026 /* If we had a hit in this block, include it into mute fragment */
1027 if (squelch && hit) {
1028 if (mute.end < sample - MF_GSIZE) {
1029 /* There is a gap between fragments */
1030 mute_fragment(dsp, &mute);
1031 mute.start = (sample > MF_GSIZE) ? (sample - MF_GSIZE) : 0;
1032 }
1033 mute.end = limit + MF_GSIZE;
1034 }
1035
1036 /* Reinitialise the detector for the next block */
1037 for (i = 0; i < 6; i++) {
1038 goertzel_reset(&s->td.mf.tone_out[i]);
1039 }
1040 s->td.mf.current_sample = 0;
1041 }
1042
1043 if (squelch && mute.end) {
1044 if (mute.end > samples) {
1045 s->td.mf.mute_samples = mute.end - samples;
1046 mute.end = samples;
1047 }
1048 mute_fragment(dsp, &mute);
1049 }
1050
1051 return (s->td.mf.current_hit); /* return the debounced hit */
1052}
1053
1054static inline int pair_there(float p1, float p2, float i1, float i2, float e)
1055{
1056 /* See if p1 and p2 are there, relative to i1 and i2 and total energy */
1057 /* Make sure absolute levels are high enough */
1058 if ((p1 < TONE_MIN_THRESH) || (p2 < TONE_MIN_THRESH)) {
1059 return 0;
1060 }
1061 /* Amplify ignored stuff */
1062 i2 *= TONE_THRESH;
1063 i1 *= TONE_THRESH;
1064 e *= TONE_THRESH;
1065 /* Check first tone */
1066 if ((p1 < i1) || (p1 < i2) || (p1 < e)) {
1067 return 0;
1068 }
1069 /* And second */
1070 if ((p2 < i1) || (p2 < i2) || (p2 < e)) {
1071 return 0;
1072 }
1073 /* Guess it's there... */
1074 return 1;
1075}
1076
1077static int __ast_dsp_call_progress(struct ast_dsp *dsp, short *s, int len)
1078{
1079 short samp;
1080 int x;
1081 int y;
1082 int pass;
1083 int newstate = DSP_TONE_STATE_SILENCE;
1084 int res = 0;
1085 int freqcount = dsp->freqcount > FREQ_ARRAY_SIZE ? FREQ_ARRAY_SIZE : dsp->freqcount;
1086
1087 while (len) {
1088 /* Take the lesser of the number of samples we need and what we have */
1089 pass = len;
1090 if (pass > dsp->gsamp_size - dsp->gsamps) {
1091 pass = dsp->gsamp_size - dsp->gsamps;
1092 }
1093 for (x = 0; x < pass; x++) {
1094 samp = s[x];
1095 dsp->genergy += (int32_t) samp * (int32_t) samp;
1096 for (y = 0; y < freqcount; y++) {
1097 goertzel_sample(&dsp->freqs[y], samp);
1098 }
1099 }
1100 s += pass;
1101 dsp->gsamps += pass;
1102 len -= pass;
1103 if (dsp->gsamps == dsp->gsamp_size) {
1104 float hz[FREQ_ARRAY_SIZE];
1105 for (y = 0; y < FREQ_ARRAY_SIZE; y++) {
1106 hz[y] = goertzel_result(&dsp->freqs[y]);
1107 }
1108 switch (dsp->progmode) {
1109 case PROG_MODE_NA:
1110 if (pair_there(hz[HZ_480], hz[HZ_620], hz[HZ_350], hz[HZ_440], dsp->genergy)) {
1111 newstate = DSP_TONE_STATE_BUSY;
1112 } else if (pair_there(hz[HZ_440], hz[HZ_480], hz[HZ_350], hz[HZ_620], dsp->genergy)) {
1113 newstate = DSP_TONE_STATE_RINGING;
1114 } else if (pair_there(hz[HZ_350], hz[HZ_440], hz[HZ_480], hz[HZ_620], dsp->genergy)) {
1115 newstate = DSP_TONE_STATE_DIALTONE;
1116 } else if (hz[HZ_950] > TONE_MIN_THRESH * TONE_THRESH) {
1117 newstate = DSP_TONE_STATE_SPECIAL1;
1118 } else if (hz[HZ_1400] > TONE_MIN_THRESH * TONE_THRESH) {
1119 /* End of SPECIAL1 or middle of SPECIAL2 */
1121 newstate = DSP_TONE_STATE_SPECIAL2;
1122 }
1123 } else if (hz[HZ_1800] > TONE_MIN_THRESH * TONE_THRESH) {
1124 /* End of SPECIAL2 or middle of SPECIAL3 */
1126 newstate = DSP_TONE_STATE_SPECIAL3;
1127 }
1128 } else if (dsp->genergy > TONE_MIN_THRESH * TONE_THRESH) {
1129 newstate = DSP_TONE_STATE_TALKING;
1130 } else {
1131 newstate = DSP_TONE_STATE_SILENCE;
1132 }
1133 break;
1134 case PROG_MODE_CR:
1135 if (hz[HZ_425] > TONE_MIN_THRESH * TONE_THRESH) {
1136 newstate = DSP_TONE_STATE_RINGING;
1137 } else if (dsp->genergy > TONE_MIN_THRESH * TONE_THRESH) {
1138 newstate = DSP_TONE_STATE_TALKING;
1139 } else {
1140 newstate = DSP_TONE_STATE_SILENCE;
1141 }
1142 break;
1143 case PROG_MODE_UK:
1144 if (hz[HZ_400UK] > TONE_MIN_THRESH * TONE_THRESH) {
1145 newstate = DSP_TONE_STATE_HUNGUP;
1146 } else if (pair_there(hz[HZ_350UK], hz[HZ_440UK], hz[HZ_400UK], hz[HZ_400UK], dsp->genergy)) {
1147 newstate = DSP_TONE_STATE_DIALTONE;
1148 }
1149 break;
1150 default:
1151 ast_log(LOG_WARNING, "Can't process in unknown prog mode '%u'\n", dsp->progmode);
1152 }
1153 if (newstate == dsp->tstate) {
1154 dsp->tcount++;
1155 if (dsp->ringtimeout) {
1156 dsp->ringtimeout++;
1157 }
1158 switch (dsp->tstate) {
1160 if ((dsp->features & DSP_PROGRESS_RINGING) &&
1161 (dsp->tcount == THRESH_RING)) {
1162 res = AST_CONTROL_RINGING;
1163 dsp->ringtimeout = 1;
1164 }
1165 break;
1167 if ((dsp->features & DSP_PROGRESS_BUSY) &&
1168 (dsp->tcount == THRESH_BUSY)) {
1169 res = AST_CONTROL_BUSY;
1170 dsp->features &= ~DSP_FEATURE_CALL_PROGRESS;
1171 }
1172 break;
1174 if ((dsp->features & DSP_PROGRESS_TALK) &&
1175 (dsp->tcount == THRESH_TALK)) {
1176 res = AST_CONTROL_ANSWER;
1177 dsp->features &= ~DSP_FEATURE_CALL_PROGRESS;
1178 }
1179 break;
1181 if ((dsp->features & DSP_PROGRESS_CONGESTION) &&
1182 (dsp->tcount == THRESH_CONGESTION)) {
1184 dsp->features &= ~DSP_FEATURE_CALL_PROGRESS;
1185 }
1186 break;
1188 if ((dsp->features & DSP_FEATURE_CALL_PROGRESS) &&
1189 (dsp->tcount == THRESH_HANGUP)) {
1190 res = AST_CONTROL_HANGUP;
1191 dsp->features &= ~DSP_FEATURE_CALL_PROGRESS;
1192 }
1193 break;
1194 }
1195 if (dsp->ringtimeout == THRESH_RING2ANSWER) {
1196 ast_debug(1, "Consider call as answered because of timeout after last ring\n");
1197 res = AST_CONTROL_ANSWER;
1198 dsp->features &= ~DSP_FEATURE_CALL_PROGRESS;
1199 }
1200 } else {
1201 ast_debug(5, "Stop state %d with duration %d\n", dsp->tstate, dsp->tcount);
1202 ast_debug(5, "Start state %d\n", newstate);
1203 dsp->tstate = newstate;
1204 dsp->tcount = 1;
1205 }
1206
1207 /* Reset goertzel */
1208 for (x = 0; x < 7; x++) {
1209 dsp->freqs[x].v2 = dsp->freqs[x].v3 = 0.0;
1210 }
1211 dsp->gsamps = 0;
1212 dsp->genergy = 0.0;
1213 }
1214 }
1215
1216 return res;
1217}
1218
1219int ast_dsp_call_progress(struct ast_dsp *dsp, struct ast_frame *inf)
1220{
1221 if (inf->frametype != AST_FRAME_VOICE) {
1222 ast_log(LOG_WARNING, "Can't check call progress of non-voice frames\n");
1223 return 0;
1224 }
1226 ast_log(LOG_WARNING, "Can only check call progress in signed-linear frames, %s not supported\n",
1228 return 0;
1229 }
1230 return __ast_dsp_call_progress(dsp, inf->data.ptr, inf->datalen / 2);
1231}
1232
1233static int __ast_dsp_silence_noise(struct ast_dsp *dsp, short *s, int len, int *totalsilence, int *totalnoise, int *frames_energy)
1234{
1235 int accum;
1236 int x;
1237 int res = 0;
1238
1239 if (!len) {
1240 return 0;
1241 }
1242 accum = 0;
1243 for (x = 0; x < len; x++) {
1244 accum += abs(s[x]);
1245 }
1246 accum /= len;
1247 if (accum < dsp->threshold) {
1248 /* Silent */
1249 dsp->totalsilence += len / (dsp->sample_rate / 1000);
1250 if (dsp->totalnoise) {
1251 /* Move and save history */
1252 memmove(dsp->historicnoise + DSP_HISTORY - dsp->busycount, dsp->historicnoise + DSP_HISTORY - dsp->busycount + 1, dsp->busycount * sizeof(dsp->historicnoise[0]));
1253 dsp->historicnoise[DSP_HISTORY - 1] = dsp->totalnoise;
1254/* we don't want to check for busydetect that frequently */
1255#if 0
1256 dsp->busymaybe = 1;
1257#endif
1258 }
1259 dsp->totalnoise = 0;
1260 res = 1;
1261 } else {
1262 /* Not silent */
1263 dsp->totalnoise += len / (dsp->sample_rate / 1000);
1264 if (dsp->totalsilence) {
1265 int silence1 = dsp->historicsilence[DSP_HISTORY - 1];
1266 int silence2 = dsp->historicsilence[DSP_HISTORY - 2];
1267 /* Move and save history */
1268 memmove(dsp->historicsilence + DSP_HISTORY - dsp->busycount, dsp->historicsilence + DSP_HISTORY - dsp->busycount + 1, dsp->busycount * sizeof(dsp->historicsilence[0]));
1269 dsp->historicsilence[DSP_HISTORY - 1] = dsp->totalsilence;
1270 /* check if the previous sample differs only by BUSY_PERCENT from the one before it */
1271 if (silence1 < silence2) {
1272 if (silence1 + silence1 * BUSY_PERCENT / 100 >= silence2) {
1273 dsp->busymaybe = 1;
1274 } else {
1275 dsp->busymaybe = 0;
1276 }
1277 } else {
1278 if (silence1 - silence1 * BUSY_PERCENT / 100 <= silence2) {
1279 dsp->busymaybe = 1;
1280 } else {
1281 dsp->busymaybe = 0;
1282 }
1283 }
1284 }
1285 dsp->totalsilence = 0;
1286 }
1287 if (totalsilence) {
1288 *totalsilence = dsp->totalsilence;
1289 }
1290 if (totalnoise) {
1291 *totalnoise = dsp->totalnoise;
1292 }
1293 if (frames_energy) {
1294 *frames_energy = accum;
1295 }
1296 return res;
1297}
1298
1300{
1301 int res = 0, x;
1302#ifndef BUSYDETECT_TONEONLY
1303 int avgsilence = 0, hitsilence = 0;
1304#endif
1305 int avgtone = 0, hittone = 0;
1306
1307 /* if we have a 4 length pattern, the way busymaybe is set doesn't help us. */
1308 if (dsp->busy_cadence.length != 4) {
1309 if (!dsp->busymaybe) {
1310 return res;
1311 }
1312 }
1313
1314 for (x = DSP_HISTORY - dsp->busycount; x < DSP_HISTORY; x++) {
1315#ifndef BUSYDETECT_TONEONLY
1316 avgsilence += dsp->historicsilence[x];
1317#endif
1318 avgtone += dsp->historicnoise[x];
1319 }
1320#ifndef BUSYDETECT_TONEONLY
1321 avgsilence /= dsp->busycount;
1322#endif
1323 avgtone /= dsp->busycount;
1324 for (x = DSP_HISTORY - dsp->busycount; x < DSP_HISTORY; x++) {
1325#ifndef BUSYDETECT_TONEONLY
1326 if (avgsilence > dsp->historicsilence[x]) {
1327 if (avgsilence - (avgsilence * BUSY_PERCENT / 100) <= dsp->historicsilence[x]) {
1328 hitsilence++;
1329 }
1330 } else {
1331 if (avgsilence + (avgsilence * BUSY_PERCENT / 100) >= dsp->historicsilence[x]) {
1332 hitsilence++;
1333 }
1334 }
1335#endif
1336 if (avgtone > dsp->historicnoise[x]) {
1337 if (avgtone - (avgtone * BUSY_PERCENT / 100) <= dsp->historicnoise[x]) {
1338 hittone++;
1339 }
1340 } else {
1341 if (avgtone + (avgtone * BUSY_PERCENT / 100) >= dsp->historicnoise[x]) {
1342 hittone++;
1343 }
1344 }
1345 }
1346#ifndef BUSYDETECT_TONEONLY
1347 if ((hittone >= dsp->busycount - 1) && (hitsilence >= dsp->busycount - 1) &&
1348 (avgtone >= BUSY_MIN && avgtone <= BUSY_MAX) &&
1349 (avgsilence >= BUSY_MIN && avgsilence <= BUSY_MAX))
1350#else
1351 if ((hittone >= dsp->busycount - 1) && (avgtone >= BUSY_MIN && avgtone <= BUSY_MAX))
1352#endif
1353 {
1354#ifdef BUSYDETECT_COMPARE_TONE_AND_SILENCE
1355 if (avgtone > avgsilence) {
1356 if (avgtone - avgtone*BUSY_PERCENT/100 <= avgsilence) {
1357 res = 1;
1358 }
1359 } else {
1360 if (avgtone + avgtone*BUSY_PERCENT/100 >= avgsilence) {
1361 res = 1;
1362 }
1363 }
1364#else
1365 res = 1;
1366#endif
1367 }
1368
1369 /* If we have a 4-length pattern, we can go ahead and just check it in a different way. */
1370 if (dsp->busy_cadence.length == 4) {
1371 int x;
1372 int errors = 0;
1373 int errors_max = ((4 * dsp->busycount) / 100.0) * BUSY_PAT_PERCENT;
1374
1375 for (x = DSP_HISTORY - (dsp->busycount); x < DSP_HISTORY; x += 2) {
1376 int temp_error;
1377 temp_error = abs(dsp->historicnoise[x] - dsp->busy_cadence.pattern[0]);
1378 if ((temp_error * 100) / dsp->busy_cadence.pattern[0] > BUSY_PERCENT) {
1379 errors++;
1380 }
1381
1382 temp_error = abs(dsp->historicnoise[x + 1] - dsp->busy_cadence.pattern[2]);
1383 if ((temp_error * 100) / dsp->busy_cadence.pattern[2] > BUSY_PERCENT) {
1384 errors++;
1385 }
1386
1387 temp_error = abs(dsp->historicsilence[x] - dsp->busy_cadence.pattern[1]);
1388 if ((temp_error * 100) / dsp->busy_cadence.pattern[1] > BUSY_PERCENT) {
1389 errors++;
1390 }
1391
1392 temp_error = abs(dsp->historicsilence[x + 1] - dsp->busy_cadence.pattern[3]);
1393 if ((temp_error * 100) / dsp->busy_cadence.pattern[3] > BUSY_PERCENT) {
1394 errors++;
1395 }
1396 }
1397
1398 ast_debug(5, "errors = %d max = %d\n", errors, errors_max);
1399
1400 if (errors <= errors_max) {
1401 return 1;
1402 }
1403 }
1404
1405 /* If we know the expected busy tone length, check we are in the range */
1406 if (res && (dsp->busy_cadence.pattern[0] > 0)) {
1407 if (abs(avgtone - dsp->busy_cadence.pattern[0]) > MAX(dsp->busy_cadence.pattern[0]*BUSY_PAT_PERCENT/100, 20)) {
1408#ifdef BUSYDETECT_DEBUG
1409 ast_debug(5, "busy detector: avgtone of %d not close enough to desired %d\n",
1410 avgtone, dsp->busy_cadence.pattern[0]);
1411#endif
1412 res = 0;
1413 }
1414 }
1415#ifndef BUSYDETECT_TONEONLY
1416 /* If we know the expected busy tone silent-period length, check we are in the range */
1417 if (res && (dsp->busy_cadence.pattern[1] > 0)) {
1418 if (abs(avgsilence - dsp->busy_cadence.pattern[1]) > MAX(dsp->busy_cadence.pattern[1]*BUSY_PAT_PERCENT/100, 20)) {
1419#ifdef BUSYDETECT_DEBUG
1420 ast_debug(5, "busy detector: avgsilence of %d not close enough to desired %d\n",
1421 avgsilence, dsp->busy_cadence.pattern[1]);
1422#endif
1423 res = 0;
1424 }
1425 }
1426#endif
1427#if !defined(BUSYDETECT_TONEONLY) && defined(BUSYDETECT_DEBUG)
1428 if (res) {
1429 ast_debug(5, "ast_dsp_busydetect detected busy, avgtone: %d, avgsilence %d\n", avgtone, avgsilence);
1430 } else {
1431 ast_debug(5, "busy detector: FAILED with avgtone: %d, avgsilence %d\n", avgtone, avgsilence);
1432 }
1433#endif
1434 return res;
1435}
1436
1437static int ast_dsp_silence_noise_with_energy(struct ast_dsp *dsp, struct ast_frame *f, int *total, int *frames_energy, int noise)
1438{
1439 short *s;
1440 int len;
1441 int x;
1442 unsigned char *odata;
1443
1444 if (!f) {
1445 return 0;
1446 }
1447
1448 if (f->frametype != AST_FRAME_VOICE) {
1449 ast_log(LOG_WARNING, "Can't calculate silence on a non-voice frame\n");
1450 return 0;
1451 }
1452
1454 s = f->data.ptr;
1455 len = f->datalen/2;
1456 } else {
1457 odata = f->data.ptr;
1458 len = f->datalen;
1460 s = ast_alloca(len * 2);
1461 for (x = 0; x < len; x++) {
1462 s[x] = AST_MULAW(odata[x]);
1463 }
1465 s = ast_alloca(len * 2);
1466 for (x = 0; x < len; x++) {
1467 s[x] = AST_ALAW(odata[x]);
1468 }
1469 } else {
1470 ast_log(LOG_WARNING, "Can only calculate silence on signed-linear, alaw or ulaw frames, %s not supported\n",
1472 return 0;
1473 }
1474 }
1475
1476 if (noise) {
1477 return __ast_dsp_silence_noise(dsp, s, len, NULL, total, frames_energy);
1478 } else {
1479 return __ast_dsp_silence_noise(dsp, s, len, total, NULL, frames_energy);
1480 }
1481}
1482
1483int ast_dsp_silence_with_energy(struct ast_dsp *dsp, struct ast_frame *f, int *totalsilence, int *frames_energy)
1484{
1485 return ast_dsp_silence_noise_with_energy(dsp, f, totalsilence, frames_energy, 0);
1486}
1487
1488int ast_dsp_silence(struct ast_dsp *dsp, struct ast_frame *f, int *totalsilence)
1489{
1490 return ast_dsp_silence_noise_with_energy(dsp, f, totalsilence, NULL, 0);
1491}
1492
1493int ast_dsp_noise(struct ast_dsp *dsp, struct ast_frame *f, int *totalnoise)
1494{
1495 return ast_dsp_silence_noise_with_energy(dsp, f, totalnoise, NULL, 1);
1496}
1497
1498
1499struct ast_frame *ast_dsp_process(struct ast_channel *chan, struct ast_dsp *dsp, struct ast_frame *af)
1500{
1501 int silence;
1502 int res;
1503 int digit = 0, fax_digit = 0, custom_freq_digit = 0;
1504 int x;
1505 short *shortdata;
1506 unsigned char *odata;
1507 int len;
1508 struct ast_frame *outf = NULL;
1509
1510 if (!af) {
1511 return NULL;
1512 }
1513 if (af->frametype != AST_FRAME_VOICE) {
1514 return af;
1515 }
1516
1517 odata = af->data.ptr;
1518 len = af->datalen;
1519 /* Make sure we have short data */
1521 shortdata = af->data.ptr;
1522 len = af->datalen / 2;
1524 shortdata = ast_alloca(af->datalen * 2);
1525 for (x = 0; x < len; x++) {
1526 shortdata[x] = AST_MULAW(odata[x]);
1527 }
1529 shortdata = ast_alloca(af->datalen * 2);
1530 for (x = 0; x < len; x++) {
1531 shortdata[x] = AST_ALAW(odata[x]);
1532 }
1533 } else {
1534 /* Display warning only once. Otherwise you would get hundreds of warnings every second */
1535 if (dsp->display_inband_dtmf_warning) {
1536 /* If DTMF is enabled for the DSP, try to be helpful and warn about that specifically,
1537 * otherwise emit a more generic message that covers all other cases. */
1539 ast_log(LOG_WARNING, "Inband DTMF is not supported on codec %s. Use RFC2833\n",
1541 } else {
1542 ast_log(LOG_WARNING, "Can only do DSP on signed-linear, alaw or ulaw frames (%s not supported)\n",
1544 }
1545 }
1547 return af;
1548 }
1549
1550 /* Initially we do not want to mute anything */
1551 dsp->mute_fragments = 0;
1552
1553 /* Need to run the silence detection stuff for silence suppression and busy detection */
1555 res = __ast_dsp_silence_noise(dsp, shortdata, len, &silence, NULL, NULL);
1556 }
1557
1558 if ((dsp->features & DSP_FEATURE_SILENCE_SUPPRESS) && silence) {
1559 memset(&dsp->f, 0, sizeof(dsp->f));
1560 dsp->f.frametype = AST_FRAME_NULL;
1561 ast_frfree(af);
1562 return ast_frisolate(&dsp->f);
1563 }
1566 memset(&dsp->f, 0, sizeof(dsp->f));
1569 ast_frfree(af);
1570 ast_debug(1, "Requesting Hangup because the busy tone was detected on channel %s\n", ast_channel_name(chan));
1571 return ast_frisolate(&dsp->f);
1572 }
1573
1574 if ((dsp->features & DSP_FEATURE_FAX_DETECT)) {
1575 if ((dsp->faxmode & DSP_FAXMODE_DETECT_CNG) && tone_detect(dsp, &dsp->cng_tone_state, shortdata, len)) {
1576 fax_digit = 'f';
1577 }
1578
1579 if ((dsp->faxmode & DSP_FAXMODE_DETECT_CED) && tone_detect(dsp, &dsp->ced_tone_state, shortdata, len)) {
1580 fax_digit = 'e';
1581 }
1582 }
1583
1584 if ((dsp->features & DSP_FEATURE_FREQ_DETECT)) {
1585 if ((dsp->freqmode) && tone_detect(dsp, &dsp->cng_tone_state, shortdata, len)) {
1586 custom_freq_digit = 'q';
1587 }
1588 }
1589
1591 if (dsp->digitmode & DSP_DIGITMODE_MF) {
1592 digit = mf_detect(dsp, &dsp->digit_state, shortdata, len, (dsp->digitmode & DSP_DIGITMODE_NOQUELCH) == 0, (dsp->digitmode & DSP_DIGITMODE_RELAXDTMF));
1593 } else {
1594 digit = dtmf_detect(dsp, &dsp->digit_state, shortdata, len, (dsp->digitmode & DSP_DIGITMODE_NOQUELCH) == 0, (dsp->digitmode & DSP_DIGITMODE_RELAXDTMF));
1595 }
1596
1597 if (dsp->digit_state.current_digits) {
1598 int event = 0, event_len = 0;
1599 char event_digit = 0;
1600
1601 if (!dsp->dtmf_began) {
1602 /* We have not reported DTMF_BEGIN for anything yet */
1603
1605 event = AST_FRAME_DTMF_BEGIN;
1606 event_digit = dsp->digit_state.digits[0];
1607 }
1608 dsp->dtmf_began = 1;
1609
1610 } else if (dsp->digit_state.current_digits > 1 || digit != dsp->digit_state.digits[0]) {
1611 /* Digit changed. This means digit we have reported with DTMF_BEGIN ended */
1613 event = AST_FRAME_DTMF_END;
1614 event_digit = dsp->digit_state.digits[0];
1615 event_len = dsp->digit_state.digitlen[0] * 1000 / dsp->sample_rate;
1616 }
1617 memmove(&dsp->digit_state.digits[0], &dsp->digit_state.digits[1], dsp->digit_state.current_digits);
1618 memmove(&dsp->digit_state.digitlen[0], &dsp->digit_state.digitlen[1], dsp->digit_state.current_digits * sizeof(dsp->digit_state.digitlen[0]));
1620 dsp->dtmf_began = 0;
1621
1622 if (dsp->features & DSP_FEATURE_BUSY_DETECT) {
1623 /* Reset Busy Detector as we have some confirmed activity */
1624 memset(dsp->historicsilence, 0, sizeof(dsp->historicsilence));
1625 memset(dsp->historicnoise, 0, sizeof(dsp->historicnoise));
1626 ast_debug(1, "DTMF Detected - Reset busydetector\n");
1627 }
1628 }
1629
1630 if (event) {
1631 memset(&dsp->f, 0, sizeof(dsp->f));
1632 dsp->f.frametype = event;
1633 dsp->f.subclass.integer = event_digit;
1634 dsp->f.len = event_len;
1635 outf = &dsp->f;
1636 goto done;
1637 }
1638 }
1639 }
1640
1641 if (fax_digit) {
1642 /* Fax was detected - digit is either 'f' or 'e' */
1643
1644 memset(&dsp->f, 0, sizeof(dsp->f));
1645 dsp->f.frametype = AST_FRAME_DTMF;
1646 dsp->f.subclass.integer = fax_digit;
1647 outf = &dsp->f;
1648 goto done;
1649 }
1650
1651 if (custom_freq_digit) {
1652 /* Custom frequency was detected - digit is 'q' */
1653
1654 memset(&dsp->f, 0, sizeof(dsp->f));
1655 dsp->f.frametype = AST_FRAME_DTMF;
1656 dsp->f.subclass.integer = custom_freq_digit;
1657 outf = &dsp->f;
1658 goto done;
1659 }
1660
1661 if ((dsp->features & DSP_FEATURE_CALL_PROGRESS)) {
1662 res = __ast_dsp_call_progress(dsp, shortdata, len);
1663 if (res) {
1664 switch (res) {
1665 case AST_CONTROL_ANSWER:
1666 case AST_CONTROL_BUSY:
1669 case AST_CONTROL_HANGUP:
1670 memset(&dsp->f, 0, sizeof(dsp->f));
1672 dsp->f.subclass.integer = res;
1673 dsp->f.src = "dsp_progress";
1674 if (chan) {
1675 ast_queue_frame(chan, &dsp->f);
1676 }
1677 break;
1678 default:
1679 ast_log(LOG_WARNING, "Don't know how to represent call progress message %d\n", res);
1680 }
1681 }
1682 } else if ((dsp->features & DSP_FEATURE_WAITDIALTONE)) {
1683 res = __ast_dsp_call_progress(dsp, shortdata, len);
1684 }
1685
1686done:
1687 /* Mute fragment of the frame */
1688 for (x = 0; x < dsp->mute_fragments; x++) {
1689 memset(shortdata + dsp->mute_data[x].start, 0, sizeof(int16_t) * (dsp->mute_data[x].end - dsp->mute_data[x].start));
1690 }
1691
1693 for (x = 0; x < len; x++) {
1694 odata[x] = AST_LIN2MU((unsigned short) shortdata[x]);
1695 }
1697 for (x = 0; x < len; x++) {
1698 odata[x] = AST_LIN2A((unsigned short) shortdata[x]);
1699 }
1700 }
1701
1702 if (outf) {
1703 if (chan) {
1704 ast_queue_frame(chan, af);
1705 }
1706 ast_frfree(af);
1707 return ast_frisolate(outf);
1708 } else {
1709 return af;
1710 }
1711}
1712
1713static void ast_dsp_prog_reset(struct ast_dsp *dsp)
1714{
1715 int max = 0;
1716 int x;
1717
1718 dsp->gsamp_size = modes[dsp->progmode].size;
1719 dsp->gsamps = 0;
1720 for (x = 0; x < FREQ_ARRAY_SIZE; x++) {
1721 if (modes[dsp->progmode].freqs[x]) {
1722 goertzel_init(&dsp->freqs[x], (float)modes[dsp->progmode].freqs[x], dsp->sample_rate);
1723 max = x + 1;
1724 }
1725 }
1726 dsp->freqcount = max;
1727 dsp->ringtimeout = 0;
1728}
1729
1730unsigned int ast_dsp_get_sample_rate(const struct ast_dsp *dsp)
1731{
1732 return dsp->sample_rate;
1733}
1734
1735static struct ast_dsp *__ast_dsp_new(unsigned int sample_rate)
1736{
1737 struct ast_dsp *dsp;
1738
1739 if ((dsp = ast_calloc(1, sizeof(*dsp)))) {
1742 dsp->busycount = DSP_HISTORY;
1745 dsp->sample_rate = sample_rate;
1746 dsp->freqcount = 0;
1747 /* Initialize digit detector */
1750 /* Initialize initial DSP progress detect parameters */
1751 ast_dsp_prog_reset(dsp);
1752 /* Initialize fax detector */
1754 }
1755 return dsp;
1756}
1757
1759{
1761}
1762
1764{
1765 return __ast_dsp_new(sample_rate);
1766}
1767
1769{
1770 dsp->features = features;
1773 }
1774}
1775
1776
1778{
1779 return (dsp->features);
1780}
1781
1782
1783void ast_dsp_free(struct ast_dsp *dsp)
1784{
1785 ast_free(dsp);
1786}
1787
1789{
1790 dsp->threshold = threshold;
1791}
1792
1794{
1795 if (cadences < 4) {
1796 cadences = 4;
1797 }
1798 if (cadences > DSP_HISTORY) {
1800 }
1801 dsp->busycount = cadences;
1802}
1803
1804void ast_dsp_set_busy_pattern(struct ast_dsp *dsp, const struct ast_dsp_busy_pattern *cadence)
1805{
1806 dsp->busy_cadence = *cadence;
1807 ast_debug(1, "dsp busy pattern set to %d,%d,%d,%d\n", cadence->pattern[0], cadence->pattern[1], (cadence->length == 4) ? cadence->pattern[2] : 0, (cadence->length == 4) ? cadence->pattern[3] : 0);
1808}
1809
1811{
1812 int i;
1813
1814 dsp->dtmf_began = 0;
1815 if (dsp->digitmode & DSP_DIGITMODE_MF) {
1817 /* Reinitialise the detector for the next block */
1818 for (i = 0; i < 6; i++) {
1819 goertzel_reset(&s->tone_out[i]);
1820 }
1821 s->hits[4] = s->hits[3] = s->hits[2] = s->hits[1] = s->hits[0] = 0;
1822 s->current_hit = 0;
1823 s->current_sample = 0;
1824 } else {
1826 /* Reinitialise the detector for the next block */
1827 for (i = 0; i < 4; i++) {
1828 goertzel_reset(&s->row_out[i]);
1829 goertzel_reset(&s->col_out[i]);
1830 }
1831 s->lasthit = 0;
1832 s->current_hit = 0;
1833 s->energy = 0.0;
1834 s->current_sample = 0;
1835 s->hits = 0;
1836 s->misses = 0;
1837 }
1838
1839 dsp->digit_state.digits[0] = '\0';
1840 dsp->digit_state.current_digits = 0;
1841}
1842
1843void ast_dsp_reset(struct ast_dsp *dsp)
1844{
1845 int x;
1846
1847 dsp->totalsilence = 0;
1848 dsp->gsamps = 0;
1849 for (x = 0; x < 4; x++) {
1850 dsp->freqs[x].v2 = dsp->freqs[x].v3 = 0.0;
1851 }
1852 memset(dsp->historicsilence, 0, sizeof(dsp->historicsilence));
1853 memset(dsp->historicnoise, 0, sizeof(dsp->historicnoise));
1854 dsp->ringtimeout = 0;
1855}
1856
1858{
1859 int new;
1860 int old;
1861
1864 if (old != new) {
1865 /* Must initialize structures if switching from MF to DTMF or vice-versa */
1867 }
1868 dsp->digitmode = digitmode;
1869 return 0;
1870}
1871
1872int ast_dsp_set_freqmode(struct ast_dsp *dsp, int freq, int dur, int db, int squelch)
1873{
1874 if (freq > 0) {
1875 dsp->freqmode = 1;
1876 ast_freq_detect_init(dsp, freq, dur, db, squelch);
1877 } else {
1878 dsp->freqmode = 0;
1879 }
1880 return 0;
1881}
1882
1884{
1885 if (dsp->faxmode != faxmode) {
1886 dsp->faxmode = faxmode;
1888 }
1889 return 0;
1890}
1891
1892int ast_dsp_set_call_progress_zone(struct ast_dsp *dsp, char *zone)
1893{
1894 int x;
1895
1896 for (x = 0; x < ARRAY_LEN(aliases); x++) {
1897 if (!strcasecmp(aliases[x].name, zone)) {
1898 dsp->progmode = aliases[x].mode;
1899 ast_dsp_prog_reset(dsp);
1900 return 0;
1901 }
1902 }
1903 return -1;
1904}
1905
1907{
1908 return (dsp->mute_fragments > 0);
1909}
1910
1912{
1913 return dsp->tstate;
1914}
1915
1917{
1918 return dsp->tcount;
1919}
1920
1921static int _dsp_init(int reload)
1922{
1923 struct ast_config *cfg;
1924 struct ast_variable *v;
1925 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
1926 int cfg_threshold;
1927 float cfg_twist;
1928
1929 if ((cfg = ast_config_load2(CONFIG_FILE_NAME, "dsp", config_flags)) == CONFIG_STATUS_FILEUNCHANGED) {
1930 return 0;
1931 }
1932
1940
1942 return 0;
1943 }
1944
1945 for (v = ast_variable_browse(cfg, "default"); v; v = v->next) {
1946 if (!strcasecmp(v->name, "silencethreshold")) {
1947 if (sscanf(v->value, "%30d", &cfg_threshold) < 1) {
1948 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", v->value);
1949 } else if (cfg_threshold < 0) {
1950 ast_log(LOG_WARNING, "Invalid silence threshold '%d' specified, using default\n", cfg_threshold);
1951 } else {
1952 thresholds[THRESHOLD_SILENCE] = cfg_threshold;
1953 }
1954 } else if (!strcasecmp(v->name, "dtmf_normal_twist")) {
1955 if (sscanf(v->value, "%30f", &cfg_twist) < 1) {
1956 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", v->value);
1957 } else if ((cfg_twist < 2.0) || (cfg_twist > 100.0)) { /* < 3.0dB or > 20dB */
1958 ast_log(LOG_WARNING, "Invalid dtmf_normal_twist value '%.2f' specified, using default of %.2f\n", cfg_twist, dtmf_normal_twist);
1959 } else {
1960 dtmf_normal_twist = cfg_twist;
1961 }
1962 } else if (!strcasecmp(v->name, "dtmf_reverse_twist")) {
1963 if (sscanf(v->value, "%30f", &cfg_twist) < 1) {
1964 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", v->value);
1965 } else if ((cfg_twist < 2.0) || (cfg_twist > 100.0)) { /* < 3.0dB or > 20dB */
1966 ast_log(LOG_WARNING, "Invalid dtmf_reverse_twist value '%.2f' specified, using default of %.2f\n", cfg_twist, dtmf_reverse_twist);
1967 } else {
1968 dtmf_reverse_twist = cfg_twist;
1969 }
1970 } else if (!strcasecmp(v->name, "relax_dtmf_normal_twist")) {
1971 if (sscanf(v->value, "%30f", &cfg_twist) < 1) {
1972 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", v->value);
1973 } else if ((cfg_twist < 2.0) || (cfg_twist > 100.0)) { /* < 3.0dB or > 20dB */
1974 ast_log(LOG_WARNING, "Invalid relax_dtmf_normal_twist value '%.2f' specified, using default of %.2f\n", cfg_twist, relax_dtmf_normal_twist);
1975 } else {
1976 relax_dtmf_normal_twist = cfg_twist;
1977 }
1978 } else if (!strcasecmp(v->name, "relax_dtmf_reverse_twist")) {
1979 if (sscanf(v->value, "%30f", &cfg_twist) < 1) {
1980 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", v->value);
1981 } else if ((cfg_twist < 2.0) || (cfg_twist > 100.0)) { /* < 3.0dB or > 20dB */
1982 ast_log(LOG_WARNING, "Invalid relax_dtmf_reverse_twist value '%.2f' specified, using default of %.2f\n", cfg_twist, relax_dtmf_reverse_twist);
1983 } else {
1984 relax_dtmf_reverse_twist = cfg_twist;
1985 }
1986 } else if (!strcasecmp(v->name, "dtmf_hits_to_begin")) {
1987 if (sscanf(v->value, "%30d", &cfg_threshold) < 1) {
1988 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", v->value);
1989 } else if (cfg_threshold < 1) { /* must be 1 or greater */
1990 ast_log(LOG_WARNING, "Invalid dtmf_hits_to_begin value '%d' specified, using default of %d\n", cfg_threshold, dtmf_hits_to_begin);
1991 } else {
1992 dtmf_hits_to_begin = cfg_threshold;
1993 }
1994 } else if (!strcasecmp(v->name, "dtmf_misses_to_end")) {
1995 if (sscanf(v->value, "%30d", &cfg_threshold) < 1) {
1996 ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", v->value);
1997 } else if (cfg_threshold < 1) { /* must be 1 or greater */
1998 ast_log(LOG_WARNING, "Invalid dtmf_misses_to_end value '%d' specified, using default of %d\n", cfg_threshold, dtmf_misses_to_end);
1999 } else {
2000 dtmf_misses_to_end = cfg_threshold;
2001 }
2002 }
2003 }
2004 ast_config_destroy(cfg);
2005
2006 return 0;
2007}
2008
2010{
2011 return thresholds[which];
2012}
2013
2014#ifdef TEST_FRAMEWORK
2015static void test_tone_sample_gen(short *slin_buf, int samples, int rate, int freq, short amplitude)
2016{
2017 int idx;
2018 double sample_step = 2.0 * M_PI * freq / rate;/* radians per step */
2019
2020 for (idx = 0; idx < samples; ++idx) {
2021 slin_buf[idx] = amplitude * sin(sample_step * idx);
2022 }
2023}
2024#endif
2025
2026#ifdef TEST_FRAMEWORK
2027static void test_tone_sample_gen_add(short *slin_buf, int samples, int rate, int freq, short amplitude)
2028{
2029 int idx;
2030 double sample_step = 2.0 * M_PI * freq / rate;/* radians per step */
2031
2032 for (idx = 0; idx < samples; ++idx) {
2033 slin_buf[idx] += amplitude * sin(sample_step * idx);
2034 }
2035}
2036#endif
2037
2038#ifdef TEST_FRAMEWORK
2039static void test_dual_sample_gen(short *slin_buf, int samples, int rate, int f1, short a1, int f2, short a2)
2040{
2041 test_tone_sample_gen(slin_buf, samples, rate, f1, a1);
2042 test_tone_sample_gen_add(slin_buf, samples, rate, f2, a2);
2043}
2044#endif
2045
2046#ifdef TEST_FRAMEWORK
2047#define TONE_AMPLITUDE_MAX 0x7fff /* Max signed linear amplitude */
2048#define TONE_AMPLITUDE_MIN 80 /* Min signed linear amplitude detectable */
2049
2050static int test_tone_amplitude_sweep(struct ast_test *test, struct ast_dsp *dsp, tone_detect_state_t *tone_state)
2051{
2052 short slin_buf[tone_state->block_size];
2053 int result;
2054 int idx;
2055 struct {
2056 short amp_val;
2057 int detect;
2058 } amp_tests[] = {
2059 { .amp_val = TONE_AMPLITUDE_MAX, .detect = 1, },
2060 { .amp_val = 10000, .detect = 1, },
2061 { .amp_val = 1000, .detect = 1, },
2062 { .amp_val = 100, .detect = 1, },
2063 { .amp_val = TONE_AMPLITUDE_MIN, .detect = 1, },
2064 { .amp_val = 75, .detect = 0, },
2065 { .amp_val = 10, .detect = 0, },
2066 { .amp_val = 1, .detect = 0, },
2067 };
2068
2069 result = 0;
2070
2071 for (idx = 0; idx < ARRAY_LEN(amp_tests); ++idx) {
2072 int detected;
2073 int duration;
2074
2075 ast_debug(1, "Test %d Hz at amplitude %d\n",
2076 tone_state->freq, amp_tests[idx].amp_val);
2077 test_tone_sample_gen(slin_buf, tone_state->block_size, DEFAULT_SAMPLE_RATE,
2078 tone_state->freq, amp_tests[idx].amp_val);
2079
2080 detected = 0;
2081 for (duration = 0; !detected && duration < tone_state->hits_required + 3; ++duration) {
2082 detected = tone_detect(dsp, tone_state, slin_buf, tone_state->block_size) ? 1 : 0;
2083 }
2084 if (amp_tests[idx].detect != detected) {
2085 /*
2086 * Both messages are needed. ast_debug for when figuring out
2087 * what went wrong and the test update for normal output before
2088 * you start debugging. The different logging methods are not
2089 * synchronized.
2090 */
2091 ast_debug(1,
2092 "Test %d Hz at amplitude %d failed. Detected: %s\n",
2093 tone_state->freq, amp_tests[idx].amp_val,
2094 detected ? "yes" : "no");
2096 "Test %d Hz at amplitude %d failed. Detected: %s\n",
2097 tone_state->freq, amp_tests[idx].amp_val,
2098 detected ? "yes" : "no");
2099 result = -1;
2100 }
2101 tone_state->hit_count = 0;
2102 }
2103
2104 return result;
2105}
2106#endif
2107
2108#ifdef TEST_FRAMEWORK
2109static int test_dtmf_amplitude_sweep(struct ast_test *test, struct ast_dsp *dsp, int digit_index)
2110{
2111 short slin_buf[DTMF_GSIZE];
2112 int result;
2113 int row;
2114 int column;
2115 int idx;
2116 struct {
2117 short amp_val;
2118 int digit;
2119 } amp_tests[] = {
2120 /*
2121 * XXX Since there is no current DTMF level detection issue. This test
2122 * just checks the current detection levels.
2123 */
2124 { .amp_val = TONE_AMPLITUDE_MAX/2, .digit = dtmf_positions[digit_index], },
2125 { .amp_val = 10000, .digit = dtmf_positions[digit_index], },
2126 { .amp_val = 1000, .digit = dtmf_positions[digit_index], },
2127 { .amp_val = 500, .digit = dtmf_positions[digit_index], },
2128 { .amp_val = 250, .digit = dtmf_positions[digit_index], },
2129 { .amp_val = 200, .digit = dtmf_positions[digit_index], },
2130 { .amp_val = 180, .digit = dtmf_positions[digit_index], },
2131 /* Various digits detect and not detect in this range */
2132 { .amp_val = 170, .digit = 0, },
2133 { .amp_val = 100, .digit = 0, },
2134 /*
2135 * Amplitudes below TONE_AMPLITUDE_MIN start having questionable detection
2136 * over quantization and background noise.
2137 */
2138 { .amp_val = TONE_AMPLITUDE_MIN, .digit = 0, },
2139 { .amp_val = 75, .digit = 0, },
2140 { .amp_val = 10, .digit = 0, },
2141 { .amp_val = 1, .digit = 0, },
2142 };
2143
2144 row = (digit_index >> 2) & 0x03;
2145 column = digit_index & 0x03;
2146
2147 result = 0;
2148
2149 for (idx = 0; idx < ARRAY_LEN(amp_tests); ++idx) {
2150 int digit;
2151 int duration;
2152
2153 ast_debug(1, "Test '%c' at amplitude %d\n",
2154 dtmf_positions[digit_index], amp_tests[idx].amp_val);
2155 test_dual_sample_gen(slin_buf, ARRAY_LEN(slin_buf), DEFAULT_SAMPLE_RATE,
2156 (int) dtmf_row[row], amp_tests[idx].amp_val,
2157 (int) dtmf_col[column], amp_tests[idx].amp_val);
2158
2159 digit = 0;
2160 for (duration = 0; !digit && duration < 3; ++duration) {
2161 digit = dtmf_detect(dsp, &dsp->digit_state, slin_buf, ARRAY_LEN(slin_buf),
2162 0, 0);
2163 }
2164 if (amp_tests[idx].digit != digit) {
2165 /*
2166 * Both messages are needed. ast_debug for when figuring out
2167 * what went wrong and the test update for normal output before
2168 * you start debugging. The different logging methods are not
2169 * synchronized.
2170 */
2171 ast_debug(1,
2172 "Test '%c' at amplitude %d failed. Detected Digit: '%c'\n",
2173 dtmf_positions[digit_index], amp_tests[idx].amp_val,
2174 digit ?: ' ');
2176 "Test '%c' at amplitude %d failed. Detected Digit: '%c'\n",
2177 dtmf_positions[digit_index], amp_tests[idx].amp_val,
2178 digit ?: ' ');
2179 result = -1;
2180 }
2181 ast_dsp_digitreset(dsp);
2182 }
2183
2184 return result;
2185}
2186#endif
2187
2188#ifdef TEST_FRAMEWORK
2189static int test_dtmf_twist_sweep(struct ast_test *test, struct ast_dsp *dsp, int digit_index)
2190{
2191 short slin_buf[DTMF_GSIZE];
2192 int result;
2193 int row;
2194 int column;
2195 int idx;
2196 struct {
2197 short amp_row;
2198 short amp_col;
2199 int digit;
2200 } twist_tests[] = {
2201 /*
2202 * XXX Since there is no current DTMF twist detection issue. This test
2203 * just checks the current detection levels.
2204 *
2205 * Normal twist has the column higher than the row amplitude.
2206 * Reverse twist is the other way.
2207 */
2208 { .amp_row = 1000 + 1800, .amp_col = 1000 + 0, .digit = 0, },
2209 { .amp_row = 1000 + 1700, .amp_col = 1000 + 0, .digit = 0, },
2210 /* Various digits detect and not detect in this range */
2211 { .amp_row = 1000 + 1400, .amp_col = 1000 + 0, .digit = dtmf_positions[digit_index], },
2212 { .amp_row = 1000 + 1300, .amp_col = 1000 + 0, .digit = dtmf_positions[digit_index], },
2213 { .amp_row = 1000 + 1200, .amp_col = 1000 + 0, .digit = dtmf_positions[digit_index], },
2214 { .amp_row = 1000 + 1100, .amp_col = 1000 + 0, .digit = dtmf_positions[digit_index], },
2215 { .amp_row = 1000 + 1000, .amp_col = 1000 + 0, .digit = dtmf_positions[digit_index], },
2216 { .amp_row = 1000 + 100, .amp_col = 1000 + 0, .digit = dtmf_positions[digit_index], },
2217 { .amp_row = 1000 + 0, .amp_col = 1000 + 100, .digit = dtmf_positions[digit_index], },
2218 { .amp_row = 1000 + 0, .amp_col = 1000 + 200, .digit = dtmf_positions[digit_index], },
2219 { .amp_row = 1000 + 0, .amp_col = 1000 + 300, .digit = dtmf_positions[digit_index], },
2220 { .amp_row = 1000 + 0, .amp_col = 1000 + 400, .digit = dtmf_positions[digit_index], },
2221 { .amp_row = 1000 + 0, .amp_col = 1000 + 500, .digit = dtmf_positions[digit_index], },
2222 { .amp_row = 1000 + 0, .amp_col = 1000 + 550, .digit = dtmf_positions[digit_index], },
2223 /* Various digits detect and not detect in this range */
2224 { .amp_row = 1000 + 0, .amp_col = 1000 + 650, .digit = 0, },
2225 { .amp_row = 1000 + 0, .amp_col = 1000 + 700, .digit = 0, },
2226 { .amp_row = 1000 + 0, .amp_col = 1000 + 800, .digit = 0, },
2227 };
2228 float save_normal_twist;
2229 float save_reverse_twist;
2230
2231 save_normal_twist = dtmf_normal_twist;
2232 save_reverse_twist = dtmf_reverse_twist;
2235
2236 row = (digit_index >> 2) & 0x03;
2237 column = digit_index & 0x03;
2238
2239 result = 0;
2240
2241 for (idx = 0; idx < ARRAY_LEN(twist_tests); ++idx) {
2242 int digit;
2243 int duration;
2244
2245 ast_debug(1, "Test '%c' twist row %d col %d amplitudes\n",
2246 dtmf_positions[digit_index],
2247 twist_tests[idx].amp_row, twist_tests[idx].amp_col);
2248 test_dual_sample_gen(slin_buf, ARRAY_LEN(slin_buf), DEFAULT_SAMPLE_RATE,
2249 (int) dtmf_row[row], twist_tests[idx].amp_row,
2250 (int) dtmf_col[column], twist_tests[idx].amp_col);
2251
2252 digit = 0;
2253 for (duration = 0; !digit && duration < 3; ++duration) {
2254 digit = dtmf_detect(dsp, &dsp->digit_state, slin_buf, ARRAY_LEN(slin_buf),
2255 0, 0);
2256 }
2257 if (twist_tests[idx].digit != digit) {
2258 /*
2259 * Both messages are needed. ast_debug for when figuring out
2260 * what went wrong and the test update for normal output before
2261 * you start debugging. The different logging methods are not
2262 * synchronized.
2263 */
2264 ast_debug(1,
2265 "Test '%c' twist row %d col %d amplitudes failed. Detected Digit: '%c'\n",
2266 dtmf_positions[digit_index],
2267 twist_tests[idx].amp_row, twist_tests[idx].amp_col,
2268 digit ?: ' ');
2270 "Test '%c' twist row %d col %d amplitudes failed. Detected Digit: '%c'\n",
2271 dtmf_positions[digit_index],
2272 twist_tests[idx].amp_row, twist_tests[idx].amp_col,
2273 digit ?: ' ');
2274 result = -1;
2275 }
2276 ast_dsp_digitreset(dsp);
2277 }
2278
2279 dtmf_normal_twist = save_normal_twist;
2280 dtmf_reverse_twist = save_reverse_twist;
2281
2282 return result;
2283}
2284#endif
2285
2286#ifdef TEST_FRAMEWORK
2287static int test_tone_freq_sweep(struct ast_test *test, struct ast_dsp *dsp, tone_detect_state_t *tone_state, short amplitude)
2288{
2289 short slin_buf[tone_state->block_size];
2290 int result;
2291 int freq;
2292 int lower_freq;
2293 int upper_freq;
2294
2295 /* Calculate detection frequency range */
2296 lower_freq = tone_state->freq - 4;
2297 upper_freq = tone_state->freq + 4;
2298
2299 result = 0;
2300
2301 /* Sweep frequencies loop. */
2302 for (freq = 100; freq <= 3500; freq += 1) {
2303 int detected;
2304 int duration;
2305 int expect_detection;
2306
2307 if (freq == tone_state->freq) {
2308 /* This case is done by the amplitude sweep. */
2309 continue;
2310 }
2311
2312 expect_detection = (lower_freq <= freq && freq <= upper_freq) ? 1 : 0;
2313
2314 ast_debug(1, "Test %d Hz detection given %d Hz tone at amplitude %d. Range:%d-%d Expect detect: %s\n",
2315 tone_state->freq, freq, amplitude, lower_freq, upper_freq,
2316 expect_detection ? "yes" : "no");
2317 test_tone_sample_gen(slin_buf, tone_state->block_size, DEFAULT_SAMPLE_RATE, freq,
2318 amplitude);
2319
2320 detected = 0;
2321 for (duration = 0; !detected && duration < tone_state->hits_required + 3; ++duration) {
2322 detected = tone_detect(dsp, tone_state, slin_buf, tone_state->block_size) ? 1 : 0;
2323 }
2324 if (expect_detection != detected) {
2325 /*
2326 * Both messages are needed. ast_debug for when figuring out
2327 * what went wrong and the test update for normal output before
2328 * you start debugging. The different logging methods are not
2329 * synchronized.
2330 */
2331 ast_debug(1,
2332 "Test %d Hz detection given %d Hz tone at amplitude %d failed. Range:%d-%d Detected: %s\n",
2333 tone_state->freq, freq, amplitude, lower_freq, upper_freq,
2334 detected ? "yes" : "no");
2336 "Test %d Hz detection given %d Hz tone at amplitude %d failed. Range:%d-%d Detected: %s\n",
2337 tone_state->freq, freq, amplitude, lower_freq, upper_freq,
2338 detected ? "yes" : "no");
2339 result = -1;
2340 }
2341 tone_state->hit_count = 0;
2342 }
2343
2344 return result;
2345}
2346#endif
2347
2348#ifdef TEST_FRAMEWORK
2349AST_TEST_DEFINE(test_dsp_fax_detect)
2350{
2351 struct ast_dsp *dsp;
2353
2354 switch (cmd) {
2355 case TEST_INIT:
2356 info->name = "fax";
2357 info->category = "/main/dsp/";
2358 info->summary = "DSP fax tone detect unit test";
2359 info->description =
2360 "Tests fax tone detection code.";
2361 return AST_TEST_NOT_RUN;
2362 case TEST_EXECUTE:
2363 break;
2364 }
2365
2366 dsp = ast_dsp_new();
2367 if (!dsp) {
2368 return AST_TEST_FAIL;
2369 }
2370
2372
2373 /* Test CNG tone amplitude detection */
2374 if (test_tone_amplitude_sweep(test, dsp, &dsp->cng_tone_state)) {
2376 }
2377
2378 /* Test CED tone amplitude detection */
2379 if (test_tone_amplitude_sweep(test, dsp, &dsp->ced_tone_state)) {
2381 }
2382
2383 /* Test CNG tone frequency detection */
2384 if (test_tone_freq_sweep(test, dsp, &dsp->cng_tone_state, TONE_AMPLITUDE_MAX)) {
2386 }
2387 if (test_tone_freq_sweep(test, dsp, &dsp->cng_tone_state, TONE_AMPLITUDE_MIN)) {
2389 }
2390
2391 /* Test CED tone frequency detection */
2392 if (test_tone_freq_sweep(test, dsp, &dsp->ced_tone_state, TONE_AMPLITUDE_MAX)) {
2394 }
2395 if (test_tone_freq_sweep(test, dsp, &dsp->ced_tone_state, TONE_AMPLITUDE_MIN)) {
2397 }
2398
2399 ast_dsp_free(dsp);
2400 return result;
2401}
2402#endif
2403
2404#ifdef TEST_FRAMEWORK
2405AST_TEST_DEFINE(test_dsp_dtmf_detect)
2406{
2407 int idx;
2408 struct ast_dsp *dsp;
2410
2411 switch (cmd) {
2412 case TEST_INIT:
2413 info->name = "dtmf";
2414 info->category = "/main/dsp/";
2415 info->summary = "DSP DTMF detect unit test";
2416 info->description =
2417 "Tests DTMF detection code.";
2418 return AST_TEST_NOT_RUN;
2419 case TEST_EXECUTE:
2420 break;
2421 }
2422
2423 dsp = ast_dsp_new();
2424 if (!dsp) {
2425 return AST_TEST_FAIL;
2426 }
2427
2429
2430 for (idx = 0; dtmf_positions[idx]; ++idx) {
2431 if (test_dtmf_amplitude_sweep(test, dsp, idx)) {
2433 }
2434 }
2435
2436 for (idx = 0; dtmf_positions[idx]; ++idx) {
2437 if (test_dtmf_twist_sweep(test, dsp, idx)) {
2439 }
2440 }
2441
2442 ast_dsp_free(dsp);
2443 return result;
2444}
2445#endif
2446
2447static int unload_module(void)
2448{
2449 AST_TEST_UNREGISTER(test_dsp_fax_detect);
2450 AST_TEST_UNREGISTER(test_dsp_dtmf_detect);
2451
2452 return 0;
2453}
2454
2455static int load_module(void)
2456{
2457 if (_dsp_init(0)) {
2459 }
2460
2461 AST_TEST_REGISTER(test_dsp_fax_detect);
2462 AST_TEST_REGISTER(test_dsp_dtmf_detect);
2463
2465}
2466
2467static int reload_module(void)
2468{
2469 return _dsp_init(1);
2470}
2471
2473 .support_level = AST_MODULE_SUPPORT_CORE,
2474 .load = load_module,
2475 .unload = unload_module,
2477 .load_pri = AST_MODPRI_CORE,
2478 .requires = "extconfig",
A-Law to Signed linear conversion.
#define AST_LIN2A(a)
Definition: alaw.h:50
#define AST_ALAW(a)
Definition: alaw.h:84
char digit
Asterisk main include file. File version handling, generic pbx functions.
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:288
#define ast_free(a)
Definition: astmm.h:180
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
#define ast_log
Definition: astobj2.c:42
static sqlite3 * db
static PGresult * result
Definition: cel_pgsql.c:84
static struct dahdi_ring_cadence cadences[NUM_CADENCE_MAX]
Definition: chan_dahdi.c:681
unsigned int cos
Definition: chan_iax2.c:359
General Asterisk PBX channel definitions.
const char * ast_channel_name(const struct ast_channel *chan)
int ast_queue_frame(struct ast_channel *chan, struct ast_frame *f)
Queue one or more frames to a channel's frame queue.
Definition: channel.c:1139
void ast_channel_softhangup_internal_flag_add(struct ast_channel *chan, int value)
@ AST_SOFTHANGUP_DEV
Definition: channel.h:1121
int int32_t
Definition: db.h:60
short int16_t
Definition: db.h:59
static void ast_mf_detect_init(mf_detect_state_t *s, unsigned int sample_rate)
Definition: dsp.c:549
static int thresholds[THRESHOLD_MAX]
Definition: dsp.c:330
#define DEF_DTMF_HITS_TO_BEGIN
Definition: dsp.c:234
void ast_dsp_set_threshold(struct ast_dsp *dsp, int threshold)
Set the minimum average magnitude threshold to determine talking by the DSP.
Definition: dsp.c:1788
#define BELL_MF_THRESHOLD
Definition: dsp.c:200
void ast_dsp_free(struct ast_dsp *dsp)
Definition: dsp.c:1783
int ast_dsp_get_tcount(struct ast_dsp *dsp)
Get tcount (Threshold counter)
Definition: dsp.c:1916
#define DEFAULT_THRESHOLD
Default minimum average magnitude threshold to determine talking/noise by the DSP.
Definition: dsp.c:140
#define DEF_DTMF_REVERSE_TWIST
Definition: dsp.c:192
static void ast_tone_detect_init(tone_detect_state_t *s, int freq, int duration, int amp, unsigned int sample_rate)
Definition: dsp.c:452
gsamp_thresh
Definition: dsp.c:157
@ THRESH_RING
Definition: dsp.c:158
@ THRESH_CONGESTION
Definition: dsp.c:161
@ THRESH_RING2ANSWER
Definition: dsp.c:163
@ THRESH_TALK
Definition: dsp.c:159
@ THRESH_HANGUP
Definition: dsp.c:162
@ THRESH_BUSY
Definition: dsp.c:160
#define FAX_TONE_CED_DB
Definition: dsp.c:221
struct ast_frame * ast_dsp_process(struct ast_channel *chan, struct ast_dsp *dsp, struct ast_frame *af)
Return AST_FRAME_NULL frames when there is silence, AST_FRAME_BUSY on busies, and call progress,...
Definition: dsp.c:1499
#define DTMF_TO_TOTAL_ENERGY
Definition: dsp.c:198
#define BELL_MF_RELATIVE_PEAK
Definition: dsp.c:202
#define DEF_DTMF_MISSES_TO_END
Definition: dsp.c:239
static const int DEFAULT_SILENCE_THRESHOLD
The default silence threshold we will use if an alternate configured value is not present or is inval...
Definition: dsp.c:245
static const char dtmf_positions[]
Definition: dsp.c:328
static int __ast_dsp_call_progress(struct ast_dsp *dsp, short *s, int len)
Definition: dsp.c:1077
#define DTMF_MATRIX_SIZE
Definition: dsp.c:168
static void goertzel_init(goertzel_state_t *s, float freq, unsigned int sample_rate)
Definition: dsp.c:378
static struct progalias aliases[]
static int _dsp_init(int reload)
Definition: dsp.c:1921
static int tone_detect(struct ast_dsp *dsp, tone_detect_state_t *s, int16_t *amp, int samples)
Definition: dsp.c:575
int ast_dsp_silence(struct ast_dsp *dsp, struct ast_frame *f, int *totalsilence)
Process the audio frame for silence.
Definition: dsp.c:1488
#define FAX_TONE_CNG_FREQ
Definition: dsp.c:211
void ast_dsp_digitreset(struct ast_dsp *dsp)
Reset DTMF detector.
Definition: dsp.c:1810
#define DEF_RELAX_DTMF_NORMAL_TWIST
Definition: dsp.c:186
static int mf_detect(struct ast_dsp *dsp, digit_detect_state_t *s, int16_t amp[], int samples, int squelch, int relax)
Definition: dsp.c:903
struct ast_dsp * ast_dsp_new_with_rate(unsigned int sample_rate)
Allocates a new dsp with a specific internal sample rate used during processing.
Definition: dsp.c:1763
#define FAX_TONE_CED_DURATION
Definition: dsp.c:220
#define DTMF_THRESHOLD
Definition: dsp.c:182
#define TONE_MIN_THRESH
Definition: dsp.c:154
static const char bell_mf_positions[]
Definition: dsp.c:329
static void ast_fax_detect_init(struct ast_dsp *s)
Definition: dsp.c:513
#define MAX_DTMF_DIGITS
Definition: dsp.c:166
int ast_dsp_silence_with_energy(struct ast_dsp *dsp, struct ast_frame *f, int *totalsilence, int *frames_energy)
Process the audio frame for silence.
Definition: dsp.c:1483
#define DEF_DTMF_NORMAL_TWIST
Definition: dsp.c:185
#define CONFIG_FILE_NAME
Definition: dsp.c:247
static float goertzel_result(goertzel_state_t *s)
Definition: dsp.c:364
#define FAX_TONE_CNG_DB
Definition: dsp.c:213
int ast_dsp_call_progress(struct ast_dsp *dsp, struct ast_frame *inf)
Scans for progress indication in audio.
Definition: dsp.c:1219
void ast_dsp_reset(struct ast_dsp *dsp)
Reset total silence count.
Definition: dsp.c:1843
static void ast_freq_detect_init(struct ast_dsp *s, int freq, int dur, int db, int squelch)
Definition: dsp.c:524
void ast_dsp_set_busy_pattern(struct ast_dsp *dsp, const struct ast_dsp_busy_pattern *cadence)
Set expected lengths of the busy tone.
Definition: dsp.c:1804
unsigned int ast_dsp_get_sample_rate(const struct ast_dsp *dsp)
Retrieve the sample rate this DSP structure was created with.
Definition: dsp.c:1730
#define FAX_TONE_CED_FREQ
Definition: dsp.c:219
static int reload_module(void)
Definition: dsp.c:2467
#define DTMF_RELATIVE_PEAK_COL
Definition: dsp.c:197
static struct progress modes[]
#define DTMF_GSIZE
Definition: dsp.c:229
static const float dtmf_row[]
Definition: dsp.c:319
static void goertzel_sample(goertzel_state_t *s, short sample)
Definition: dsp.c:338
#define DEF_RELAX_DTMF_REVERSE_TWIST
Definition: dsp.c:193
static int pair_there(float p1, float p2, float i1, float i2, float e)
Definition: dsp.c:1054
static const float mf_tones[]
Definition: dsp.c:325
static float dtmf_reverse_twist
Definition: dsp.c:332
int ast_dsp_get_features(struct ast_dsp *dsp)
Get features.
Definition: dsp.c:1777
#define TONE_THRESHOLD
Definition: dsp.c:183
busy_detect
Definition: dsp.c:142
@ BUSY_MAX
Definition: dsp.c:147
@ BUSY_PAT_PERCENT
Definition: dsp.c:144
@ BUSY_MIN
Definition: dsp.c:146
@ BUSY_PERCENT
Definition: dsp.c:143
@ BUSY_THRESHOLD
Definition: dsp.c:145
prog_mode
Definition: dsp.c:79
@ PROG_MODE_CR
Definition: dsp.c:81
@ PROG_MODE_UK
Definition: dsp.c:82
@ PROG_MODE_NA
Definition: dsp.c:80
static int dtmf_detect(struct ast_dsp *dsp, digit_detect_state_t *s, int16_t amp[], int samples, int squelch, int relax)
Definition: dsp.c:695
int ast_dsp_set_digitmode(struct ast_dsp *dsp, int digitmode)
Set digit mode.
Definition: dsp.c:1857
freq_index
Definition: dsp.c:85
@ HZ_425
Definition: dsp.c:96
@ HZ_950
Definition: dsp.c:91
@ HZ_350UK
Definition: dsp.c:99
@ HZ_620
Definition: dsp.c:90
@ HZ_440
Definition: dsp.c:88
@ HZ_440UK
Definition: dsp.c:101
@ HZ_480
Definition: dsp.c:89
@ HZ_350
Definition: dsp.c:87
@ HZ_1400
Definition: dsp.c:92
@ HZ_400UK
Definition: dsp.c:100
@ HZ_1800
Definition: dsp.c:93
static void goertzel_reset(goertzel_state_t *s)
Definition: dsp.c:384
static int ast_dsp_silence_noise_with_energy(struct ast_dsp *dsp, struct ast_frame *f, int *total, int *frames_energy, int noise)
Definition: dsp.c:1437
gsamp_size
Definition: dsp.c:73
@ GSAMP_SIZE_NA
Definition: dsp.c:74
@ GSAMP_SIZE_CR
Definition: dsp.c:75
@ GSAMP_SIZE_UK
Definition: dsp.c:76
int ast_dsp_get_threshold_from_settings(enum threshold which)
Get silence threshold from dsp.conf.
Definition: dsp.c:2009
static void ast_dtmf_detect_init(dtmf_detect_state_t *s, unsigned int sample_rate)
Definition: dsp.c:533
#define FREQ_ARRAY_SIZE
Definition: dsp.c:115
static void mute_fragment(struct ast_dsp *dsp, fragment_t *fragment)
Definition: dsp.c:442
#define DTMF_RELATIVE_PEAK_ROW
Definition: dsp.c:196
#define DSP_HISTORY
Definition: dsp.c:151
static int dtmf_misses_to_end
Definition: dsp.c:336
static int dtmf_hits_to_begin
Definition: dsp.c:335
#define DEFAULT_SAMPLE_RATE
Definition: dsp.c:223
static int load_module(void)
Definition: dsp.c:2455
int ast_dsp_noise(struct ast_dsp *dsp, struct ast_frame *f, int *totalnoise)
Process the audio frame for noise.
Definition: dsp.c:1493
int ast_dsp_get_tstate(struct ast_dsp *dsp)
Get tstate (Tone State)
Definition: dsp.c:1911
int ast_dsp_was_muted(struct ast_dsp *dsp)
Returns true if DSP code was muting any fragment of the last processed frame. Muting (squelching) hap...
Definition: dsp.c:1906
static int unload_module(void)
Definition: dsp.c:2447
static float relax_dtmf_normal_twist
Definition: dsp.c:333
int ast_dsp_busydetect(struct ast_dsp *dsp)
Return non-zero if historically this should be a busy, request that ast_dsp_silence has already been ...
Definition: dsp.c:1299
static void store_digit(digit_detect_state_t *s, char digit)
Definition: dsp.c:682
int ast_dsp_set_faxmode(struct ast_dsp *dsp, int faxmode)
Set fax mode.
Definition: dsp.c:1883
static void ast_digit_detect_init(digit_detect_state_t *s, int mf, unsigned int sample_rate)
Definition: dsp.c:561
#define BELL_MF_TWIST
Definition: dsp.c:201
static const float dtmf_col[]
Definition: dsp.c:322
#define MF_GSIZE
Definition: dsp.c:226
#define FAX_TONE_CNG_DURATION
Definition: dsp.c:212
int ast_dsp_set_freqmode(struct ast_dsp *dsp, int freq, int dur, int db, int squelch)
Set arbitrary frequency detection mode.
Definition: dsp.c:1872
static struct ast_dsp * __ast_dsp_new(unsigned int sample_rate)
Definition: dsp.c:1735
void ast_dsp_set_busy_count(struct ast_dsp *dsp, int cadences)
Set number of required cadences for busy.
Definition: dsp.c:1793
static float dtmf_normal_twist
Definition: dsp.c:331
void ast_dsp_set_features(struct ast_dsp *dsp, int features)
Select feature set.
Definition: dsp.c:1768
static float relax_dtmf_reverse_twist
Definition: dsp.c:334
struct ast_dsp * ast_dsp_new(void)
Allocates a new dsp, assumes 8khz for internal sample rate.
Definition: dsp.c:1758
static int __ast_dsp_silence_noise(struct ast_dsp *dsp, short *s, int len, int *totalsilence, int *totalnoise, int *frames_energy)
Definition: dsp.c:1233
int ast_dsp_set_call_progress_zone(struct ast_dsp *dsp, char *zone)
Set zone for doing progress detection.
Definition: dsp.c:1892
#define TONE_THRESH
Definition: dsp.c:153
static void ast_dsp_prog_reset(struct ast_dsp *dsp)
Definition: dsp.c:1713
Convenient Signal Processing routines.
threshold
Definition: dsp.h:71
@ THRESHOLD_SILENCE
Definition: dsp.h:73
@ THRESHOLD_MAX
Definition: dsp.h:75
#define DSP_PROGRESS_RINGING
Definition: dsp.h:40
#define DSP_TONE_STATE_SPECIAL3
Definition: dsp.h:59
#define DSP_FEATURE_WAITDIALTONE
Definition: dsp.h:44
#define DSP_TONE_STATE_SPECIAL2
Definition: dsp.h:58
#define DSP_FEATURE_BUSY_DETECT
Definition: dsp.h:27
#define DSP_TONE_STATE_DIALTONE
Definition: dsp.h:54
#define DSP_PROGRESS_TALK
Definition: dsp.h:39
#define DSP_TONE_STATE_SILENCE
Definition: dsp.h:52
#define DSP_DIGITMODE_NOQUELCH
Definition: dsp.h:34
#define DSP_DIGITMODE_MF
Definition: dsp.h:32
#define DSP_TONE_STATE_BUSY
Definition: dsp.h:56
#define DSP_DIGITMODE_MUTEMAX
Definition: dsp.h:36
#define DSP_FEATURE_DIGIT_DETECT
Definition: dsp.h:28
#define DSP_PROGRESS_BUSY
Definition: dsp.h:41
#define DSP_FEATURE_FAX_DETECT
Definition: dsp.h:29
#define DSP_TONE_STATE_SPECIAL1
Definition: dsp.h:57
#define DSP_FAXMODE_DETECT_SQUELCH
Definition: dsp.h:49
#define DSP_FAXMODE_DETECT_CED
Definition: dsp.h:48
#define DSP_PROGRESS_CONGESTION
Definition: dsp.h:42
#define DSP_FEATURE_CALL_PROGRESS
Definition: dsp.h:43
#define DSP_DIGITMODE_MUTECONF
Definition: dsp.h:35
#define DSP_FAXMODE_DETECT_CNG
Definition: dsp.h:47
#define DSP_FEATURE_SILENCE_SUPPRESS
Definition: dsp.h:26
#define DSP_TONE_STATE_HUNGUP
Definition: dsp.h:60
#define DSP_FEATURE_FREQ_DETECT
Definition: dsp.h:45
#define DSP_TONE_STATE_TALKING
Definition: dsp.h:55
#define DSP_TONE_STATE_RINGING
Definition: dsp.h:53
#define DSP_DIGITMODE_DTMF
Definition: dsp.h:31
#define DSP_DIGITMODE_RELAXDTMF
Definition: dsp.h:37
char * end
Definition: eagi_proxy.c:73
#define abs(x)
Definition: f2c.h:195
#define max(a, b)
Definition: f2c.h:198
enum ast_format_cmp_res ast_format_cmp(const struct ast_format *format1, const struct ast_format *format2)
Compare two formats.
Definition: format.c:201
@ AST_FORMAT_CMP_EQUAL
Definition: format.h:36
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition: format.c:334
Media Format Cache API.
int ast_format_cache_is_slinear(struct ast_format *format)
Determines if a format is one of the cached slin formats.
Definition: format_cache.c:534
struct ast_format * ast_format_ulaw
Built-in cached ulaw format.
Definition: format_cache.c:86
struct ast_format * ast_format_alaw
Built-in cached alaw format.
Definition: format_cache.c:91
static const char name[]
Definition: format_mp3.c:68
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
Configuration File Parser.
struct ast_config * ast_config_load2(const char *filename, const char *who_asked, struct ast_flags flags)
Load a config file.
Definition: main/config.c:3321
#define CONFIG_STATUS_FILEMISSING
#define CONFIG_STATUS_FILEUNCHANGED
#define CONFIG_STATUS_FILEINVALID
void ast_config_destroy(struct ast_config *cfg)
Destroys a config.
Definition: extconf.c:1289
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category_name)
Definition: extconf.c:1215
@ CONFIG_FLAG_FILEUNCHANGED
Asterisk internal frame definitions.
#define AST_FRAME_DTMF
#define ast_frisolate(fr)
Makes a frame independent of any static storage.
#define ast_frfree(fr)
@ AST_FRAME_NULL
@ AST_FRAME_DTMF_END
@ AST_FRAME_DTMF_BEGIN
@ AST_FRAME_VOICE
@ AST_FRAME_CONTROL
@ AST_CONTROL_BUSY
@ AST_CONTROL_CONGESTION
@ AST_CONTROL_ANSWER
@ AST_CONTROL_RINGING
@ AST_CONTROL_HANGUP
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_ERROR
#define LOG_WARNING
Asterisk module definitions.
@ AST_MODFLAG_LOAD_ORDER
Definition: module.h:317
@ AST_MODFLAG_GLOBAL_SYMBOLS
Definition: module.h:316
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:543
@ AST_MODPRI_CORE
Definition: module.h:324
@ AST_MODULE_SUPPORT_CORE
Definition: module.h:121
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
@ AST_MODULE_LOAD_FAILURE
Module could not be loaded properly.
Definition: module.h:102
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
def info(msg)
Options provided by main asterisk program.
static int total
Definition: res_adsi.c:970
static char pass[512]
static int reload(void)
#define NULL
Definition: resample.c:96
#define M_PI
Definition: resample.c:83
Main Channel structure associated with a channel.
int pattern[4]
Definition: dsp.h:68
Definition: dsp.c:407
fragment_t mute_data[5]
Definition: dsp.c:436
int historicnoise[DSP_HISTORY]
Definition: dsp.c:419
int tcount
Definition: dsp.c:427
int faxmode
Definition: dsp.c:429
unsigned int sample_rate
Definition: dsp.c:435
int busymaybe
Definition: dsp.c:416
int mute_fragments
Definition: dsp.c:434
int busycount
Definition: dsp.c:417
int totalsilence
Definition: dsp.c:411
goertzel_state_t freqs[FREQ_ARRAY_SIZE]
Definition: dsp.c:421
int tstate
Definition: dsp.c:426
int threshold
Definition: dsp.c:409
struct ast_frame f
Definition: dsp.c:408
digit_detect_state_t digit_state
Definition: dsp.c:437
int totalnoise
Definition: dsp.c:413
tone_detect_state_t ced_tone_state
Definition: dsp.c:439
int historicsilence[DSP_HISTORY]
Definition: dsp.c:420
enum prog_mode progmode
Definition: dsp.c:425
int freqcount
Definition: dsp.c:422
int features
Definition: dsp.c:414
int digitmode
Definition: dsp.c:428
enum gsamp_size gsamp_size
Definition: dsp.c:424
int display_inband_dtmf_warning
Definition: dsp.c:432
int dtmf_began
Definition: dsp.c:431
float genergy
Definition: dsp.c:433
int freqmode
Definition: dsp.c:430
int gsamps
Definition: dsp.c:423
int ringtimeout
Definition: dsp.c:415
struct ast_dsp_busy_pattern busy_cadence
Definition: dsp.c:418
tone_detect_state_t cng_tone_state
Definition: dsp.c:438
Structure used to handle boolean flags.
Definition: utils.h:199
struct ast_format * format
Data structure associated with a single frame of data.
struct ast_frame_subclass subclass
union ast_frame::@226 data
enum ast_frame_type frametype
const char * src
Structure for variables, used for configurations and for channel variables.
struct ast_variable * next
int detected_digits
Definition: dsp.c:310
mf_detect_state_t mf
Definition: dsp.c:315
dtmf_detect_state_t dtmf
Definition: dsp.c:314
int digitlen[MAX_DTMF_DIGITS+1]
Definition: dsp.c:308
char digits[MAX_DTMF_DIGITS+1]
Definition: dsp.c:307
int current_digits
Definition: dsp.c:309
union digit_detect_state_t::@345 td
float energy
Definition: dsp.c:291
goertzel_state_t row_out[DTMF_MATRIX_SIZE]
Definition: dsp.c:285
goertzel_state_t col_out[DTMF_MATRIX_SIZE]
Definition: dsp.c:286
int mute_samples
Definition: dsp.c:293
int current_sample
Definition: dsp.c:292
Definition: astman.c:222
int start
Definition: dsp.c:390
int end
Definition: dsp.c:391
int chunky
Definition: dsp.c:255
int mute_samples
Definition: dsp.c:302
int hits[5]
Definition: dsp.c:300
goertzel_state_t tone_out[6]
Definition: dsp.c:298
int current_hit
Definition: dsp.c:299
int current_sample
Definition: dsp.c:301
Definition: dsp.c:104
char * name
Definition: dsp.c:105
enum prog_mode mode
Definition: dsp.c:106
Definition: dsp.c:117
enum gsamp_size size
Definition: dsp.c:118
int freqs[FREQ_ARRAY_SIZE]
Definition: dsp.c:119
float energy
Definition: dsp.c:271
float threshold
Definition: dsp.c:276
int mute_samples
Definition: dsp.c:273
goertzel_state_t tone
Definition: dsp.c:270
int samples_pending
Definition: dsp.c:272
int hits_required
Definition: dsp.c:275
Test Framework API.
@ TEST_INIT
Definition: test.h:200
@ TEST_EXECUTE
Definition: test.h:201
#define AST_TEST_REGISTER(cb)
Definition: test.h:127
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
#define AST_TEST_UNREGISTER(cb)
Definition: test.h:128
#define AST_TEST_DEFINE(hdr)
Definition: test.h:126
ast_test_result_state
Definition: test.h:193
@ AST_TEST_PASS
Definition: test.h:195
@ AST_TEST_FAIL
Definition: test.h:196
@ AST_TEST_NOT_RUN
Definition: test.h:194
int done
Definition: test_amihooks.c:48
u-Law to Signed linear conversion
#define AST_MULAW(a)
Definition: ulaw.h:85
#define AST_LIN2MU(a)
Definition: ulaw.h:49
Utility functions.
#define ARRAY_LEN(a)
Definition: utils.h:666
#define MAX(a, b)
Definition: utils.h:233