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