Asterisk - The Open Source Telephony Project  GIT-master-16dfe8f
dsp.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * Goertzel routines are borrowed from Steve Underwood's tremendous work on the
9  * DTMF detector.
10  *
11  * See http://www.asterisk.org for more information about
12  * the Asterisk project. Please do not directly contact
13  * any of the maintainers of this project for assistance;
14  * the project provides a web site, mailing lists and IRC
15  * channels for your use.
16  *
17  * This program is free software, distributed under the terms of
18  * the GNU General Public License Version 2. See the LICENSE file
19  * at the top of the source tree.
20  */
21 
22 /*! \file
23  *
24  * \brief Convenience Signal Processing routines
25  *
26  * \author Mark Spencer <markster@digium.com>
27  * \author Steve Underwood <steveu@coppice.org>
28  */
29 
30 /*! \li \ref dsp.c uses the configuration file \ref dsp.conf
31  * \addtogroup configuration_file Configuration Files
32  */
33 
34 /*!
35  * \page dsp.conf dsp.conf
36  * \verbinclude dsp.conf.sample
37  */
38 
39 /* Some routines from tone_detect.c by Steven Underwood as published under the zapata library */
40 /*
41  tone_detect.c - General telephony tone detection, and specific
42  detection of DTMF.
43 
44  Copyright (C) 2001 Steve Underwood <steveu@coppice.org>
45 
46  Despite my general liking of the GPL, I place this code in the
47  public domain for the benefit of all mankind - even the slimy
48  ones who might try to proprietize my work and use it to my
49  detriment.
50 */
51 
52 /*** MODULEINFO
53  <support_level>core</support_level>
54  ***/
55 
56 #include "asterisk.h"
57 
58 #include <math.h>
59 
60 #include "asterisk/module.h"
61 #include "asterisk/frame.h"
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;
430  float genergy;
432  unsigned int sample_rate;
433  fragment_t mute_data[5];
437 };
438 
439 static void mute_fragment(struct ast_dsp *dsp, fragment_t *fragment)
440 {
441  if (dsp->mute_fragments >= ARRAY_LEN(dsp->mute_data)) {
442  ast_log(LOG_ERROR, "Too many fragments to mute. Ignoring\n");
443  return;
444  }
445 
446  dsp->mute_data[dsp->mute_fragments++] = *fragment;
447 }
448 
449 static void ast_tone_detect_init(tone_detect_state_t *s, int freq, int duration, int amp, unsigned int sample_rate)
450 {
451  int duration_samples;
452  float x;
453  int periods_in_block;
454 
455  s->freq = freq;
456 
457  /* Desired tone duration in samples */
458  duration_samples = duration * sample_rate / 1000;
459  /* We want to allow 10% deviation of tone duration */
460  duration_samples = duration_samples * 9 / 10;
461 
462  /* If we want to remove tone, it is important to have block size not
463  to exceed frame size. Otherwise by the moment tone is detected it is too late
464  to squelch it from previous frames. Block size is 20ms at the given sample rate.*/
465  s->block_size = (20 * sample_rate) / 1000;
466 
467  periods_in_block = s->block_size * freq / sample_rate;
468 
469  /* Make sure we will have at least 5 periods at target frequency for analisys.
470  This may make block larger than expected packet and will make squelching impossible
471  but at least we will be detecting the tone */
472  if (periods_in_block < 5) {
473  periods_in_block = 5;
474  }
475 
476  /* Now calculate final block size. It will contain integer number of periods */
477  s->block_size = periods_in_block * sample_rate / freq;
478 
479  /* tone_detect is currently only used to detect fax tones and we
480  do not need squelching the fax tones */
481  s->squelch = 0;
482 
483  /* Account for the first and the last block to be incomplete
484  and thus no tone will be detected in them */
485  s->hits_required = (duration_samples - (s->block_size - 1)) / s->block_size;
486 
487  goertzel_init(&s->tone, freq, sample_rate);
488 
489  s->samples_pending = s->block_size;
490  s->hit_count = 0;
491  s->last_hit = 0;
492  s->energy = 0.0;
493 
494  /* We want tone energy to be amp decibels above the rest of the signal (the noise).
495  According to Parseval's theorem the energy computed in time domain equals to energy
496  computed in frequency domain. So subtracting energy in the frequency domain (Goertzel result)
497  from the energy in the time domain we will get energy of the remaining signal (without the tone
498  we are detecting). We will be checking that
499  10*log(Ew / (Et - Ew)) > amp
500  Calculate threshold so that we will be actually checking
501  Ew > Et * threshold
502  */
503 
504  x = pow(10.0, amp / 10.0);
505  s->threshold = x / (x + 1);
506 
507  ast_debug(1, "Setup tone %d Hz, %d ms, block_size=%d, hits_required=%d\n", freq, duration, s->block_size, s->hits_required);
508 }
509 
510 static void ast_fax_detect_init(struct ast_dsp *s)
511 {
515  s->cng_tone_state.squelch = 1;
516  s->ced_tone_state.squelch = 1;
517  }
518 
519 }
520 
521 static void ast_dtmf_detect_init(dtmf_detect_state_t *s, unsigned int sample_rate)
522 {
523  int i;
524 
525  for (i = 0; i < 4; i++) {
526  goertzel_init(&s->row_out[i], dtmf_row[i], sample_rate);
527  goertzel_init(&s->col_out[i], dtmf_col[i], sample_rate);
528  }
529  s->lasthit = 0;
530  s->current_hit = 0;
531  s->energy = 0.0;
532  s->current_sample = 0;
533  s->hits = 0;
534  s->misses = 0;
535 }
536 
537 static void ast_mf_detect_init(mf_detect_state_t *s, unsigned int sample_rate)
538 {
539  int i;
540 
541  for (i = 0; i < 6; i++) {
542  goertzel_init(&s->tone_out[i], mf_tones[i], sample_rate);
543  }
544  s->hits[0] = s->hits[1] = s->hits[2] = s->hits[3] = s->hits[4] = 0;
545  s->current_sample = 0;
546  s->current_hit = 0;
547 }
548 
549 static void ast_digit_detect_init(digit_detect_state_t *s, int mf, unsigned int sample_rate)
550 {
551  s->current_digits = 0;
552  s->detected_digits = 0;
553  s->lost_digits = 0;
554  s->digits[0] = '\0';
555 
556  if (mf) {
557  ast_mf_detect_init(&s->td.mf, sample_rate);
558  } else {
559  ast_dtmf_detect_init(&s->td.dtmf, sample_rate);
560  }
561 }
562 
563 static int tone_detect(struct ast_dsp *dsp, tone_detect_state_t *s, int16_t *amp, int samples)
564 {
565  float tone_energy;
566  int i;
567  int hit = 0;
568  int limit;
569  int res = 0;
570  int16_t *ptr;
571  short samp;
572  int start, end;
573  fragment_t mute = {0, 0};
574 
575  if (s->squelch && s->mute_samples > 0) {
576  mute.end = (s->mute_samples < samples) ? s->mute_samples : samples;
577  s->mute_samples -= mute.end;
578  }
579 
580  for (start = 0; start < samples; start = end) {
581  /* Process in blocks. */
582  limit = samples - start;
583  if (limit > s->samples_pending) {
584  limit = s->samples_pending;
585  }
586  end = start + limit;
587 
588  for (i = limit, ptr = amp ; i > 0; i--, ptr++) {
589  samp = *ptr;
590  /* signed 32 bit int should be enough to square any possible signed 16 bit value */
591  s->energy += (int32_t) samp * (int32_t) samp;
592 
593  goertzel_sample(&s->tone, samp);
594  }
595 
596  s->samples_pending -= limit;
597 
598  if (s->samples_pending) {
599  /* Finished incomplete (last) block */
600  break;
601  }
602 
603  tone_energy = goertzel_result(&s->tone);
604 
605  /* Scale to make comparable */
606  tone_energy *= 2.0;
607  s->energy *= s->block_size;
608 
609  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));
610  hit = 0;
611  if (TONE_THRESHOLD <= tone_energy
612  && tone_energy > s->energy * s->threshold) {
613  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));
614  hit = 1;
615  }
616 
617  if (s->hit_count) {
618  s->hit_count++;
619  }
620 
621  if (hit == s->last_hit) {
622  if (!hit) {
623  /* Two successive misses. Tone ended */
624  s->hit_count = 0;
625  } else if (!s->hit_count) {
626  s->hit_count++;
627  }
628 
629  }
630 
631  if (s->hit_count == s->hits_required) {
632  ast_debug(1, "%d Hz tone detected\n", s->freq);
633  res = 1;
634  }
635 
636  s->last_hit = hit;
637 
638  /* If we had a hit in this block, include it into mute fragment */
639  if (s->squelch && hit) {
640  if (mute.end < start - s->block_size) {
641  /* There is a gap between fragments */
642  mute_fragment(dsp, &mute);
643  mute.start = (start > s->block_size) ? (start - s->block_size) : 0;
644  }
645  mute.end = end + s->block_size;
646  }
647 
648  /* Reinitialise the detector for the next block */
649  /* Reset for the next block */
650  goertzel_reset(&s->tone);
651 
652  /* Advance to the next block */
653  s->energy = 0.0;
654  s->samples_pending = s->block_size;
655 
656  amp += limit;
657  }
658 
659  if (s->squelch && mute.end) {
660  if (mute.end > samples) {
661  s->mute_samples = mute.end - samples;
662  mute.end = samples;
663  }
664  mute_fragment(dsp, &mute);
665  }
666 
667  return res;
668 }
669 
671 {
672  s->detected_digits++;
673  if (s->current_digits < MAX_DTMF_DIGITS) {
674  s->digitlen[s->current_digits] = 0;
675  s->digits[s->current_digits++] = digit;
676  s->digits[s->current_digits] = '\0';
677  } else {
678  ast_log(LOG_WARNING, "Digit lost due to full buffer\n");
679  s->lost_digits++;
680  }
681 }
682 
683 static int dtmf_detect(struct ast_dsp *dsp, digit_detect_state_t *s, int16_t amp[], int samples, int squelch, int relax)
684 {
685  float row_energy[4];
686  float col_energy[4];
687  int i;
688  int j;
689  int sample;
690  short samp;
691  int best_row;
692  int best_col;
693  int hit;
694  int limit;
695  fragment_t mute = {0, 0};
696 
697  if (squelch && s->td.dtmf.mute_samples > 0) {
698  mute.end = (s->td.dtmf.mute_samples < samples) ? s->td.dtmf.mute_samples : samples;
699  s->td.dtmf.mute_samples -= mute.end;
700  }
701 
702  hit = 0;
703  for (sample = 0; sample < samples; sample = limit) {
704  /* DTMF_GSIZE is optimised to meet the DTMF specs. */
705  if ((samples - sample) >= (DTMF_GSIZE - s->td.dtmf.current_sample)) {
706  limit = sample + (DTMF_GSIZE - s->td.dtmf.current_sample);
707  } else {
708  limit = samples;
709  }
710  /* The following unrolled loop takes only 35% (rough estimate) of the
711  time of a rolled loop on the machine on which it was developed */
712  for (j = sample; j < limit; j++) {
713  samp = amp[j];
714  s->td.dtmf.energy += (int32_t) samp * (int32_t) samp;
715  /* With GCC 2.95, the following unrolled code seems to take about 35%
716  (rough estimate) as long as a neat little 0-3 loop */
717  goertzel_sample(s->td.dtmf.row_out, samp);
718  goertzel_sample(s->td.dtmf.col_out, samp);
719  goertzel_sample(s->td.dtmf.row_out + 1, samp);
720  goertzel_sample(s->td.dtmf.col_out + 1, samp);
721  goertzel_sample(s->td.dtmf.row_out + 2, samp);
722  goertzel_sample(s->td.dtmf.col_out + 2, samp);
723  goertzel_sample(s->td.dtmf.row_out + 3, samp);
724  goertzel_sample(s->td.dtmf.col_out + 3, samp);
725  }
726  s->td.dtmf.current_sample += (limit - sample);
727  if (s->td.dtmf.current_sample < DTMF_GSIZE) {
728  continue;
729  }
730  /* We are at the end of a DTMF detection block */
731  /* Find the peak row and the peak column */
732  row_energy[0] = goertzel_result(&s->td.dtmf.row_out[0]);
733  col_energy[0] = goertzel_result(&s->td.dtmf.col_out[0]);
734 
735  for (best_row = best_col = 0, i = 1; i < 4; i++) {
736  row_energy[i] = goertzel_result(&s->td.dtmf.row_out[i]);
737  if (row_energy[i] > row_energy[best_row]) {
738  best_row = i;
739  }
740  col_energy[i] = goertzel_result(&s->td.dtmf.col_out[i]);
741  if (col_energy[i] > col_energy[best_col]) {
742  best_col = i;
743  }
744  }
745  ast_debug(10, "DTMF best '%c' Erow=%.4E Ecol=%.4E Erc=%.4E Et=%.4E\n",
746  dtmf_positions[(best_row << 2) + best_col],
747  row_energy[best_row], col_energy[best_col],
748  row_energy[best_row] + col_energy[best_col], s->td.dtmf.energy);
749  hit = 0;
750  /* Basic signal level test and the twist test */
751  if (row_energy[best_row] >= DTMF_THRESHOLD &&
752  col_energy[best_col] >= DTMF_THRESHOLD &&
753  col_energy[best_col] < row_energy[best_row] * (relax ? relax_dtmf_reverse_twist : dtmf_reverse_twist) &&
754  row_energy[best_row] < col_energy[best_col] * (relax ? relax_dtmf_normal_twist : dtmf_normal_twist)) {
755  /* Relative peak test */
756  for (i = 0; i < 4; i++) {
757  if ((i != best_col &&
758  col_energy[i] * DTMF_RELATIVE_PEAK_COL > col_energy[best_col]) ||
759  (i != best_row
760  && row_energy[i] * DTMF_RELATIVE_PEAK_ROW > row_energy[best_row])) {
761  break;
762  }
763  }
764  /* ... and fraction of total energy test */
765  if (i >= 4 &&
766  (row_energy[best_row] + col_energy[best_col]) > DTMF_TO_TOTAL_ENERGY * s->td.dtmf.energy) {
767  /* Got a hit */
768  hit = dtmf_positions[(best_row << 2) + best_col];
769  ast_debug(10, "DTMF hit '%c'\n", hit);
770  }
771  }
772 
773 /*
774  * Adapted from ETSI ES 201 235-3 V1.3.1 (2006-03)
775  * (40ms reference is tunable with hits_to_begin and misses_to_end)
776  * each hit/miss is 12.75ms with DTMF_GSIZE at 102
777  *
778  * Character recognition: When not DRC *(1) and then
779  * Shall exist VSC > 40 ms (hits_to_begin)
780  * May exist 20 ms <= VSC <= 40 ms
781  * Shall not exist VSC < 20 ms
782  *
783  * Character recognition: When DRC and then
784  * Shall cease Not VSC > 40 ms (misses_to_end)
785  * May cease 20 ms >= Not VSC >= 40 ms
786  * Shall not cease Not VSC < 20 ms
787  *
788  * *(1) or optionally a different digit recognition condition
789  *
790  * Legend: VSC The continuous existence of a valid signal condition.
791  * Not VSC The continuous non-existence of valid signal condition.
792  * DRC The existence of digit recognition condition.
793  * Not DRC The non-existence of digit recognition condition.
794  */
795 
796 /*
797  * Example: hits_to_begin=2 misses_to_end=3
798  * -------A last_hit=A hits=0&1
799  * ------AA hits=2 current_hit=A misses=0 BEGIN A
800  * -----AA- misses=1 last_hit=' ' hits=0
801  * ----AA-- misses=2
802  * ---AA--- misses=3 current_hit=' ' END A
803  * --AA---B last_hit=B hits=0&1
804  * -AA---BC last_hit=C hits=0&1
805  * AA---BCC hits=2 current_hit=C misses=0 BEGIN C
806  * A---BCC- misses=1 last_hit=' ' hits=0
807  * ---BCC-C misses=0 last_hit=C hits=0&1
808  * --BCC-CC misses=0
809  *
810  * Example: hits_to_begin=3 misses_to_end=2
811  * -------A last_hit=A hits=0&1
812  * ------AA hits=2
813  * -----AAA hits=3 current_hit=A misses=0 BEGIN A
814  * ----AAAB misses=1 last_hit=B hits=0&1
815  * ---AAABB misses=2 current_hit=' ' hits=2 END A
816  * --AAABBB hits=3 current_hit=B misses=0 BEGIN B
817  * -AAABBBB misses=0
818  *
819  * Example: hits_to_begin=2 misses_to_end=2
820  * -------A last_hit=A hits=0&1
821  * ------AA hits=2 current_hit=A misses=0 BEGIN A
822  * -----AAB misses=1 hits=0&1
823  * ----AABB misses=2 current_hit=' ' hits=2 current_hit=B misses=0 BEGIN B
824  * ---AABBB misses=0
825  */
826 
827  if (s->td.dtmf.current_hit) {
828  /* We are in the middle of a digit already */
829  if (hit != s->td.dtmf.current_hit) {
830  s->td.dtmf.misses++;
831  if (s->td.dtmf.misses == dtmf_misses_to_end) {
832  /* There were enough misses to consider digit ended */
833  s->td.dtmf.current_hit = 0;
834  }
835  } else {
836  s->td.dtmf.misses = 0;
837  /* Current hit was same as last, so increment digit duration (of last digit) */
838  s->digitlen[s->current_digits - 1] += DTMF_GSIZE;
839  }
840  }
841 
842  /* Look for a start of a new digit no matter if we are already in the middle of some
843  digit or not. This is because hits_to_begin may be smaller than misses_to_end
844  and we may find begin of new digit before we consider last one ended. */
845 
846  if (hit != s->td.dtmf.lasthit) {
847  s->td.dtmf.lasthit = hit;
848  s->td.dtmf.hits = 0;
849  }
850  if (hit && hit != s->td.dtmf.current_hit) {
851  s->td.dtmf.hits++;
852  if (s->td.dtmf.hits == dtmf_hits_to_begin) {
853  store_digit(s, hit);
855  s->td.dtmf.current_hit = hit;
856  s->td.dtmf.misses = 0;
857  }
858  }
859 
860  /* If we had a hit in this block, include it into mute fragment */
861  if (squelch && hit) {
862  if (mute.end < sample - DTMF_GSIZE) {
863  /* There is a gap between fragments */
864  mute_fragment(dsp, &mute);
865  mute.start = (sample > DTMF_GSIZE) ? (sample - DTMF_GSIZE) : 0;
866  }
867  mute.end = limit + DTMF_GSIZE;
868  }
869 
870  /* Reinitialise the detector for the next block */
871  for (i = 0; i < 4; i++) {
872  goertzel_reset(&s->td.dtmf.row_out[i]);
873  goertzel_reset(&s->td.dtmf.col_out[i]);
874  }
875  s->td.dtmf.energy = 0.0;
876  s->td.dtmf.current_sample = 0;
877  }
878 
879  if (squelch && mute.end) {
880  if (mute.end > samples) {
881  s->td.dtmf.mute_samples = mute.end - samples;
882  mute.end = samples;
883  }
884  mute_fragment(dsp, &mute);
885  }
886 
887  return (s->td.dtmf.current_hit); /* return the debounced hit */
888 }
889 
890 static int mf_detect(struct ast_dsp *dsp, digit_detect_state_t *s, int16_t amp[],
891  int samples, int squelch, int relax)
892 {
893  float energy[6];
894  int best;
895  int second_best;
896  int i;
897  int j;
898  int sample;
899  short samp;
900  int hit;
901  int limit;
902  fragment_t mute = {0, 0};
903 
904  if (squelch && s->td.mf.mute_samples > 0) {
905  mute.end = (s->td.mf.mute_samples < samples) ? s->td.mf.mute_samples : samples;
906  s->td.mf.mute_samples -= mute.end;
907  }
908 
909  hit = 0;
910  for (sample = 0; sample < samples; sample = limit) {
911  /* 80 is optimised to meet the MF specs. */
912  /* XXX So then why is MF_GSIZE defined as 120? */
913  if ((samples - sample) >= (MF_GSIZE - s->td.mf.current_sample)) {
914  limit = sample + (MF_GSIZE - s->td.mf.current_sample);
915  } else {
916  limit = samples;
917  }
918  /* The following unrolled loop takes only 35% (rough estimate) of the
919  time of a rolled loop on the machine on which it was developed */
920  for (j = sample; j < limit; j++) {
921  /* With GCC 2.95, the following unrolled code seems to take about 35%
922  (rough estimate) as long as a neat little 0-3 loop */
923  samp = amp[j];
924  goertzel_sample(s->td.mf.tone_out, samp);
925  goertzel_sample(s->td.mf.tone_out + 1, samp);
926  goertzel_sample(s->td.mf.tone_out + 2, samp);
927  goertzel_sample(s->td.mf.tone_out + 3, samp);
928  goertzel_sample(s->td.mf.tone_out + 4, samp);
929  goertzel_sample(s->td.mf.tone_out + 5, samp);
930  }
931  s->td.mf.current_sample += (limit - sample);
932  if (s->td.mf.current_sample < MF_GSIZE) {
933  continue;
934  }
935  /* We're at the end of an MF detection block. */
936  /* Find the two highest energies. The spec says to look for
937  two tones and two tones only. Taking this literally -ie
938  only two tones pass the minimum threshold - doesn't work
939  well. The sinc function mess, due to rectangular windowing
940  ensure that! Find the two highest energies and ensure they
941  are considerably stronger than any of the others. */
942  energy[0] = goertzel_result(&s->td.mf.tone_out[0]);
943  energy[1] = goertzel_result(&s->td.mf.tone_out[1]);
944  if (energy[0] > energy[1]) {
945  best = 0;
946  second_best = 1;
947  } else {
948  best = 1;
949  second_best = 0;
950  }
951  /*endif*/
952  for (i = 2; i < 6; i++) {
953  energy[i] = goertzel_result(&s->td.mf.tone_out[i]);
954  if (energy[i] >= energy[best]) {
955  second_best = best;
956  best = i;
957  } else if (energy[i] >= energy[second_best]) {
958  second_best = i;
959  }
960  }
961  /* Basic signal level and twist tests */
962  hit = 0;
963  if (energy[best] >= BELL_MF_THRESHOLD && energy[second_best] >= BELL_MF_THRESHOLD
964  && energy[best] < energy[second_best]*BELL_MF_TWIST
965  && energy[best] * BELL_MF_TWIST > energy[second_best]) {
966  /* Relative peak test */
967  hit = -1;
968  for (i = 0; i < 6; i++) {
969  if (i != best && i != second_best) {
970  if (energy[i]*BELL_MF_RELATIVE_PEAK >= energy[second_best]) {
971  /* The best two are not clearly the best */
972  hit = 0;
973  break;
974  }
975  }
976  }
977  }
978  if (hit) {
979  /* Get the values into ascending order */
980  if (second_best < best) {
981  i = best;
982  best = second_best;
983  second_best = i;
984  }
985  best = best * 5 + second_best - 1;
986  hit = bell_mf_positions[best];
987  /* Look for two successive similar results */
988  /* The logic in the next test is:
989  For KP we need 4 successive identical clean detects, with
990  two blocks of something different preceeding it. For anything
991  else we need two successive identical clean detects, with
992  two blocks of something different preceeding it. */
993  if (hit == s->td.mf.hits[4] && hit == s->td.mf.hits[3] &&
994  ((hit != '*' && hit != s->td.mf.hits[2] && hit != s->td.mf.hits[1])||
995  (hit == '*' && hit == s->td.mf.hits[2] && hit != s->td.mf.hits[1] &&
996  hit != s->td.mf.hits[0]))) {
997  store_digit(s, hit);
998  }
999  }
1000 
1001 
1002  if (hit != s->td.mf.hits[4] && hit != s->td.mf.hits[3]) {
1003  /* Two successive block without a hit terminate current digit */
1004  s->td.mf.current_hit = 0;
1005  }
1006 
1007  s->td.mf.hits[0] = s->td.mf.hits[1];
1008  s->td.mf.hits[1] = s->td.mf.hits[2];
1009  s->td.mf.hits[2] = s->td.mf.hits[3];
1010  s->td.mf.hits[3] = s->td.mf.hits[4];
1011  s->td.mf.hits[4] = hit;
1012 
1013  /* If we had a hit in this block, include it into mute fragment */
1014  if (squelch && hit) {
1015  if (mute.end < sample - MF_GSIZE) {
1016  /* There is a gap between fragments */
1017  mute_fragment(dsp, &mute);
1018  mute.start = (sample > MF_GSIZE) ? (sample - MF_GSIZE) : 0;
1019  }
1020  mute.end = limit + MF_GSIZE;
1021  }
1022 
1023  /* Reinitialise the detector for the next block */
1024  for (i = 0; i < 6; i++) {
1025  goertzel_reset(&s->td.mf.tone_out[i]);
1026  }
1027  s->td.mf.current_sample = 0;
1028  }
1029 
1030  if (squelch && mute.end) {
1031  if (mute.end > samples) {
1032  s->td.mf.mute_samples = mute.end - samples;
1033  mute.end = samples;
1034  }
1035  mute_fragment(dsp, &mute);
1036  }
1037 
1038  return (s->td.mf.current_hit); /* return the debounced hit */
1039 }
1040 
1041 static inline int pair_there(float p1, float p2, float i1, float i2, float e)
1042 {
1043  /* See if p1 and p2 are there, relative to i1 and i2 and total energy */
1044  /* Make sure absolute levels are high enough */
1045  if ((p1 < TONE_MIN_THRESH) || (p2 < TONE_MIN_THRESH)) {
1046  return 0;
1047  }
1048  /* Amplify ignored stuff */
1049  i2 *= TONE_THRESH;
1050  i1 *= TONE_THRESH;
1051  e *= TONE_THRESH;
1052  /* Check first tone */
1053  if ((p1 < i1) || (p1 < i2) || (p1 < e)) {
1054  return 0;
1055  }
1056  /* And second */
1057  if ((p2 < i1) || (p2 < i2) || (p2 < e)) {
1058  return 0;
1059  }
1060  /* Guess it's there... */
1061  return 1;
1062 }
1063 
1064 static int __ast_dsp_call_progress(struct ast_dsp *dsp, short *s, int len)
1065 {
1066  short samp;
1067  int x;
1068  int y;
1069  int pass;
1070  int newstate = DSP_TONE_STATE_SILENCE;
1071  int res = 0;
1072  int freqcount = dsp->freqcount > FREQ_ARRAY_SIZE ? FREQ_ARRAY_SIZE : dsp->freqcount;
1073 
1074  while (len) {
1075  /* Take the lesser of the number of samples we need and what we have */
1076  pass = len;
1077  if (pass > dsp->gsamp_size - dsp->gsamps) {
1078  pass = dsp->gsamp_size - dsp->gsamps;
1079  }
1080  for (x = 0; x < pass; x++) {
1081  samp = s[x];
1082  dsp->genergy += (int32_t) samp * (int32_t) samp;
1083  for (y = 0; y < freqcount; y++) {
1084  goertzel_sample(&dsp->freqs[y], samp);
1085  }
1086  }
1087  s += pass;
1088  dsp->gsamps += pass;
1089  len -= pass;
1090  if (dsp->gsamps == dsp->gsamp_size) {
1091  float hz[FREQ_ARRAY_SIZE];
1092  for (y = 0; y < FREQ_ARRAY_SIZE; y++) {
1093  hz[y] = goertzel_result(&dsp->freqs[y]);
1094  }
1095  switch (dsp->progmode) {
1096  case PROG_MODE_NA:
1097  if (pair_there(hz[HZ_480], hz[HZ_620], hz[HZ_350], hz[HZ_440], dsp->genergy)) {
1098  newstate = DSP_TONE_STATE_BUSY;
1099  } else if (pair_there(hz[HZ_440], hz[HZ_480], hz[HZ_350], hz[HZ_620], dsp->genergy)) {
1100  newstate = DSP_TONE_STATE_RINGING;
1101  } else if (pair_there(hz[HZ_350], hz[HZ_440], hz[HZ_480], hz[HZ_620], dsp->genergy)) {
1102  newstate = DSP_TONE_STATE_DIALTONE;
1103  } else if (hz[HZ_950] > TONE_MIN_THRESH * TONE_THRESH) {
1104  newstate = DSP_TONE_STATE_SPECIAL1;
1105  } else if (hz[HZ_1400] > TONE_MIN_THRESH * TONE_THRESH) {
1106  /* End of SPECIAL1 or middle of SPECIAL2 */
1108  newstate = DSP_TONE_STATE_SPECIAL2;
1109  }
1110  } else if (hz[HZ_1800] > TONE_MIN_THRESH * TONE_THRESH) {
1111  /* End of SPECIAL2 or middle of SPECIAL3 */
1113  newstate = DSP_TONE_STATE_SPECIAL3;
1114  }
1115  } else if (dsp->genergy > TONE_MIN_THRESH * TONE_THRESH) {
1116  newstate = DSP_TONE_STATE_TALKING;
1117  } else {
1118  newstate = DSP_TONE_STATE_SILENCE;
1119  }
1120  break;
1121  case PROG_MODE_CR:
1122  if (hz[HZ_425] > TONE_MIN_THRESH * TONE_THRESH) {
1123  newstate = DSP_TONE_STATE_RINGING;
1124  } else if (dsp->genergy > TONE_MIN_THRESH * TONE_THRESH) {
1125  newstate = DSP_TONE_STATE_TALKING;
1126  } else {
1127  newstate = DSP_TONE_STATE_SILENCE;
1128  }
1129  break;
1130  case PROG_MODE_UK:
1131  if (hz[HZ_400UK] > TONE_MIN_THRESH * TONE_THRESH) {
1132  newstate = DSP_TONE_STATE_HUNGUP;
1133  } else if (pair_there(hz[HZ_350UK], hz[HZ_440UK], hz[HZ_400UK], hz[HZ_400UK], dsp->genergy)) {
1134  newstate = DSP_TONE_STATE_DIALTONE;
1135  }
1136  break;
1137  default:
1138  ast_log(LOG_WARNING, "Can't process in unknown prog mode '%u'\n", dsp->progmode);
1139  }
1140  if (newstate == dsp->tstate) {
1141  dsp->tcount++;
1142  if (dsp->ringtimeout) {
1143  dsp->ringtimeout++;
1144  }
1145  switch (dsp->tstate) {
1147  if ((dsp->features & DSP_PROGRESS_RINGING) &&
1148  (dsp->tcount == THRESH_RING)) {
1149  res = AST_CONTROL_RINGING;
1150  dsp->ringtimeout = 1;
1151  }
1152  break;
1153  case DSP_TONE_STATE_BUSY:
1154  if ((dsp->features & DSP_PROGRESS_BUSY) &&
1155  (dsp->tcount == THRESH_BUSY)) {
1156  res = AST_CONTROL_BUSY;
1158  }
1159  break;
1161  if ((dsp->features & DSP_PROGRESS_TALK) &&
1162  (dsp->tcount == THRESH_TALK)) {
1163  res = AST_CONTROL_ANSWER;
1165  }
1166  break;
1168  if ((dsp->features & DSP_PROGRESS_CONGESTION) &&
1169  (dsp->tcount == THRESH_CONGESTION)) {
1170  res = AST_CONTROL_CONGESTION;
1172  }
1173  break;
1174  case DSP_TONE_STATE_HUNGUP:
1175  if ((dsp->features & DSP_FEATURE_CALL_PROGRESS) &&
1176  (dsp->tcount == THRESH_HANGUP)) {
1177  res = AST_CONTROL_HANGUP;
1179  }
1180  break;
1181  }
1182  if (dsp->ringtimeout == THRESH_RING2ANSWER) {
1183  ast_debug(1, "Consider call as answered because of timeout after last ring\n");
1184  res = AST_CONTROL_ANSWER;
1186  }
1187  } else {
1188  ast_debug(5, "Stop state %d with duration %d\n", dsp->tstate, dsp->tcount);
1189  ast_debug(5, "Start state %d\n", newstate);
1190  dsp->tstate = newstate;
1191  dsp->tcount = 1;
1192  }
1193 
1194  /* Reset goertzel */
1195  for (x = 0; x < 7; x++) {
1196  dsp->freqs[x].v2 = dsp->freqs[x].v3 = 0.0;
1197  }
1198  dsp->gsamps = 0;
1199  dsp->genergy = 0.0;
1200  }
1201  }
1202 
1203  return res;
1204 }
1205 
1206 int ast_dsp_call_progress(struct ast_dsp *dsp, struct ast_frame *inf)
1207 {
1208  if (inf->frametype != AST_FRAME_VOICE) {
1209  ast_log(LOG_WARNING, "Can't check call progress of non-voice frames\n");
1210  return 0;
1211  }
1213  ast_log(LOG_WARNING, "Can only check call progress in signed-linear frames\n");
1214  return 0;
1215  }
1216  return __ast_dsp_call_progress(dsp, inf->data.ptr, inf->datalen / 2);
1217 }
1218 
1219 static int __ast_dsp_silence_noise(struct ast_dsp *dsp, short *s, int len, int *totalsilence, int *totalnoise, int *frames_energy)
1220 {
1221  int accum;
1222  int x;
1223  int res = 0;
1224 
1225  if (!len) {
1226  return 0;
1227  }
1228  accum = 0;
1229  for (x = 0; x < len; x++) {
1230  accum += abs(s[x]);
1231  }
1232  accum /= len;
1233  if (accum < dsp->threshold) {
1234  /* Silent */
1235  dsp->totalsilence += len / (dsp->sample_rate / 1000);
1236  if (dsp->totalnoise) {
1237  /* Move and save history */
1238  memmove(dsp->historicnoise + DSP_HISTORY - dsp->busycount, dsp->historicnoise + DSP_HISTORY - dsp->busycount + 1, dsp->busycount * sizeof(dsp->historicnoise[0]));
1239  dsp->historicnoise[DSP_HISTORY - 1] = dsp->totalnoise;
1240 /* we don't want to check for busydetect that frequently */
1241 #if 0
1242  dsp->busymaybe = 1;
1243 #endif
1244  }
1245  dsp->totalnoise = 0;
1246  res = 1;
1247  } else {
1248  /* Not silent */
1249  dsp->totalnoise += len / (dsp->sample_rate / 1000);
1250  if (dsp->totalsilence) {
1251  int silence1 = dsp->historicsilence[DSP_HISTORY - 1];
1252  int silence2 = dsp->historicsilence[DSP_HISTORY - 2];
1253  /* Move and save history */
1254  memmove(dsp->historicsilence + DSP_HISTORY - dsp->busycount, dsp->historicsilence + DSP_HISTORY - dsp->busycount + 1, dsp->busycount * sizeof(dsp->historicsilence[0]));
1255  dsp->historicsilence[DSP_HISTORY - 1] = dsp->totalsilence;
1256  /* check if the previous sample differs only by BUSY_PERCENT from the one before it */
1257  if (silence1 < silence2) {
1258  if (silence1 + silence1 * BUSY_PERCENT / 100 >= silence2) {
1259  dsp->busymaybe = 1;
1260  } else {
1261  dsp->busymaybe = 0;
1262  }
1263  } else {
1264  if (silence1 - silence1 * BUSY_PERCENT / 100 <= silence2) {
1265  dsp->busymaybe = 1;
1266  } else {
1267  dsp->busymaybe = 0;
1268  }
1269  }
1270  }
1271  dsp->totalsilence = 0;
1272  }
1273  if (totalsilence) {
1274  *totalsilence = dsp->totalsilence;
1275  }
1276  if (totalnoise) {
1277  *totalnoise = dsp->totalnoise;
1278  }
1279  if (frames_energy) {
1280  *frames_energy = accum;
1281  }
1282  return res;
1283 }
1284 
1285 int ast_dsp_busydetect(struct ast_dsp *dsp)
1286 {
1287  int res = 0, x;
1288 #ifndef BUSYDETECT_TONEONLY
1289  int avgsilence = 0, hitsilence = 0;
1290 #endif
1291  int avgtone = 0, hittone = 0;
1292 
1293  /* if we have a 4 length pattern, the way busymaybe is set doesn't help us. */
1294  if (dsp->busy_cadence.length != 4) {
1295  if (!dsp->busymaybe) {
1296  return res;
1297  }
1298  }
1299 
1300  for (x = DSP_HISTORY - dsp->busycount; x < DSP_HISTORY; x++) {
1301 #ifndef BUSYDETECT_TONEONLY
1302  avgsilence += dsp->historicsilence[x];
1303 #endif
1304  avgtone += dsp->historicnoise[x];
1305  }
1306 #ifndef BUSYDETECT_TONEONLY
1307  avgsilence /= dsp->busycount;
1308 #endif
1309  avgtone /= dsp->busycount;
1310  for (x = DSP_HISTORY - dsp->busycount; x < DSP_HISTORY; x++) {
1311 #ifndef BUSYDETECT_TONEONLY
1312  if (avgsilence > dsp->historicsilence[x]) {
1313  if (avgsilence - (avgsilence * BUSY_PERCENT / 100) <= dsp->historicsilence[x]) {
1314  hitsilence++;
1315  }
1316  } else {
1317  if (avgsilence + (avgsilence * BUSY_PERCENT / 100) >= dsp->historicsilence[x]) {
1318  hitsilence++;
1319  }
1320  }
1321 #endif
1322  if (avgtone > dsp->historicnoise[x]) {
1323  if (avgtone - (avgtone * BUSY_PERCENT / 100) <= dsp->historicnoise[x]) {
1324  hittone++;
1325  }
1326  } else {
1327  if (avgtone + (avgtone * BUSY_PERCENT / 100) >= dsp->historicnoise[x]) {
1328  hittone++;
1329  }
1330  }
1331  }
1332 #ifndef BUSYDETECT_TONEONLY
1333  if ((hittone >= dsp->busycount - 1) && (hitsilence >= dsp->busycount - 1) &&
1334  (avgtone >= BUSY_MIN && avgtone <= BUSY_MAX) &&
1335  (avgsilence >= BUSY_MIN && avgsilence <= BUSY_MAX))
1336 #else
1337  if ((hittone >= dsp->busycount - 1) && (avgtone >= BUSY_MIN && avgtone <= BUSY_MAX))
1338 #endif
1339  {
1340 #ifdef BUSYDETECT_COMPARE_TONE_AND_SILENCE
1341  if (avgtone > avgsilence) {
1342  if (avgtone - avgtone*BUSY_PERCENT/100 <= avgsilence) {
1343  res = 1;
1344  }
1345  } else {
1346  if (avgtone + avgtone*BUSY_PERCENT/100 >= avgsilence) {
1347  res = 1;
1348  }
1349  }
1350 #else
1351  res = 1;
1352 #endif
1353  }
1354 
1355  /* If we have a 4-length pattern, we can go ahead and just check it in a different way. */
1356  if (dsp->busy_cadence.length == 4) {
1357  int x;
1358  int errors = 0;
1359  int errors_max = ((4 * dsp->busycount) / 100.0) * BUSY_PAT_PERCENT;
1360 
1361  for (x = DSP_HISTORY - (dsp->busycount); x < DSP_HISTORY; x += 2) {
1362  int temp_error;
1363  temp_error = abs(dsp->historicnoise[x] - dsp->busy_cadence.pattern[0]);
1364  if ((temp_error * 100) / dsp->busy_cadence.pattern[0] > BUSY_PERCENT) {
1365  errors++;
1366  }
1367 
1368  temp_error = abs(dsp->historicnoise[x + 1] - dsp->busy_cadence.pattern[2]);
1369  if ((temp_error * 100) / dsp->busy_cadence.pattern[2] > BUSY_PERCENT) {
1370  errors++;
1371  }
1372 
1373  temp_error = abs(dsp->historicsilence[x] - dsp->busy_cadence.pattern[1]);
1374  if ((temp_error * 100) / dsp->busy_cadence.pattern[1] > BUSY_PERCENT) {
1375  errors++;
1376  }
1377 
1378  temp_error = abs(dsp->historicsilence[x + 1] - dsp->busy_cadence.pattern[3]);
1379  if ((temp_error * 100) / dsp->busy_cadence.pattern[3] > BUSY_PERCENT) {
1380  errors++;
1381  }
1382  }
1383 
1384  ast_debug(5, "errors = %d max = %d\n", errors, errors_max);
1385 
1386  if (errors <= errors_max) {
1387  return 1;
1388  }
1389  }
1390 
1391  /* If we know the expected busy tone length, check we are in the range */
1392  if (res && (dsp->busy_cadence.pattern[0] > 0)) {
1393  if (abs(avgtone - dsp->busy_cadence.pattern[0]) > MAX(dsp->busy_cadence.pattern[0]*BUSY_PAT_PERCENT/100, 20)) {
1394 #ifdef BUSYDETECT_DEBUG
1395  ast_debug(5, "busy detector: avgtone of %d not close enough to desired %d\n",
1396  avgtone, dsp->busy_cadence.pattern[0]);
1397 #endif
1398  res = 0;
1399  }
1400  }
1401 #ifndef BUSYDETECT_TONEONLY
1402  /* If we know the expected busy tone silent-period length, check we are in the range */
1403  if (res && (dsp->busy_cadence.pattern[1] > 0)) {
1404  if (abs(avgsilence - dsp->busy_cadence.pattern[1]) > MAX(dsp->busy_cadence.pattern[1]*BUSY_PAT_PERCENT/100, 20)) {
1405 #ifdef BUSYDETECT_DEBUG
1406  ast_debug(5, "busy detector: avgsilence of %d not close enough to desired %d\n",
1407  avgsilence, dsp->busy_cadence.pattern[1]);
1408 #endif
1409  res = 0;
1410  }
1411  }
1412 #endif
1413 #if !defined(BUSYDETECT_TONEONLY) && defined(BUSYDETECT_DEBUG)
1414  if (res) {
1415  ast_debug(5, "ast_dsp_busydetect detected busy, avgtone: %d, avgsilence %d\n", avgtone, avgsilence);
1416  } else {
1417  ast_debug(5, "busy detector: FAILED with avgtone: %d, avgsilence %d\n", avgtone, avgsilence);
1418  }
1419 #endif
1420  return res;
1421 }
1422 
1423 static int ast_dsp_silence_noise_with_energy(struct ast_dsp *dsp, struct ast_frame *f, int *total, int *frames_energy, int noise)
1424 {
1425  short *s;
1426  int len;
1427  int x;
1428  unsigned char *odata;
1429 
1430  if (!f) {
1431  return 0;
1432  }
1433 
1434  if (f->frametype != AST_FRAME_VOICE) {
1435  ast_log(LOG_WARNING, "Can't calculate silence on a non-voice frame\n");
1436  return 0;
1437  }
1438 
1440  s = f->data.ptr;
1441  len = f->datalen/2;
1442  } else {
1443  odata = f->data.ptr;
1444  len = f->datalen;
1446  s = ast_alloca(len * 2);
1447  for (x = 0; x < len; x++) {
1448  s[x] = AST_MULAW(odata[x]);
1449  }
1451  s = ast_alloca(len * 2);
1452  for (x = 0; x < len; x++) {
1453  s[x] = AST_ALAW(odata[x]);
1454  }
1455  } else {
1456  ast_log(LOG_WARNING, "Can only calculate silence on signed-linear, alaw or ulaw frames :(\n");
1457  return 0;
1458  }
1459  }
1460 
1461  if (noise) {
1462  return __ast_dsp_silence_noise(dsp, s, len, NULL, total, frames_energy);
1463  } else {
1464  return __ast_dsp_silence_noise(dsp, s, len, total, NULL, frames_energy);
1465  }
1466 }
1467 
1468 int ast_dsp_silence_with_energy(struct ast_dsp *dsp, struct ast_frame *f, int *totalsilence, int *frames_energy)
1469 {
1470  return ast_dsp_silence_noise_with_energy(dsp, f, totalsilence, frames_energy, 0);
1471 }
1472 
1473 int ast_dsp_silence(struct ast_dsp *dsp, struct ast_frame *f, int *totalsilence)
1474 {
1475  return ast_dsp_silence_noise_with_energy(dsp, f, totalsilence, NULL, 0);
1476 }
1477 
1478 int ast_dsp_noise(struct ast_dsp *dsp, struct ast_frame *f, int *totalnoise)
1479 {
1480  return ast_dsp_silence_noise_with_energy(dsp, f, totalnoise, NULL, 1);
1481 }
1482 
1483 
1484 struct ast_frame *ast_dsp_process(struct ast_channel *chan, struct ast_dsp *dsp, struct ast_frame *af)
1485 {
1486  int silence;
1487  int res;
1488  int digit = 0, fax_digit = 0;
1489  int x;
1490  short *shortdata;
1491  unsigned char *odata;
1492  int len;
1493  struct ast_frame *outf = NULL;
1494 
1495  if (!af) {
1496  return NULL;
1497  }
1498  if (af->frametype != AST_FRAME_VOICE) {
1499  return af;
1500  }
1501 
1502  odata = af->data.ptr;
1503  len = af->datalen;
1504  /* Make sure we have short data */
1506  shortdata = af->data.ptr;
1507  len = af->datalen / 2;
1509  shortdata = ast_alloca(af->datalen * 2);
1510  for (x = 0; x < len; x++) {
1511  shortdata[x] = AST_MULAW(odata[x]);
1512  }
1514  shortdata = ast_alloca(af->datalen * 2);
1515  for (x = 0; x < len; x++) {
1516  shortdata[x] = AST_ALAW(odata[x]);
1517  }
1518  } else {
1519  /*Display warning only once. Otherwise you would get hundreds of warnings every second */
1520  if (dsp->display_inband_dtmf_warning) {
1521  ast_log(LOG_WARNING, "Inband DTMF is not supported on codec %s. Use RFC2833\n", ast_format_get_name(af->subclass.format));
1522  }
1523  dsp->display_inband_dtmf_warning = 0;
1524  return af;
1525  }
1526 
1527  /* Initially we do not want to mute anything */
1528  dsp->mute_fragments = 0;
1529 
1530  /* Need to run the silence detection stuff for silence suppression and busy detection */
1532  res = __ast_dsp_silence_noise(dsp, shortdata, len, &silence, NULL, NULL);
1533  }
1534 
1535  if ((dsp->features & DSP_FEATURE_SILENCE_SUPPRESS) && silence) {
1536  memset(&dsp->f, 0, sizeof(dsp->f));
1537  dsp->f.frametype = AST_FRAME_NULL;
1538  ast_frfree(af);
1539  return ast_frisolate(&dsp->f);
1540  }
1541  if ((dsp->features & DSP_FEATURE_BUSY_DETECT) && ast_dsp_busydetect(dsp)) {
1543  memset(&dsp->f, 0, sizeof(dsp->f));
1544  dsp->f.frametype = AST_FRAME_CONTROL;
1546  ast_frfree(af);
1547  ast_debug(1, "Requesting Hangup because the busy tone was detected on channel %s\n", ast_channel_name(chan));
1548  return ast_frisolate(&dsp->f);
1549  }
1550 
1551  if ((dsp->features & DSP_FEATURE_FAX_DETECT)) {
1552  if ((dsp->faxmode & DSP_FAXMODE_DETECT_CNG) && tone_detect(dsp, &dsp->cng_tone_state, shortdata, len)) {
1553  fax_digit = 'f';
1554  }
1555 
1556  if ((dsp->faxmode & DSP_FAXMODE_DETECT_CED) && tone_detect(dsp, &dsp->ced_tone_state, shortdata, len)) {
1557  fax_digit = 'e';
1558  }
1559  }
1560 
1562  if (dsp->digitmode & DSP_DIGITMODE_MF) {
1563  digit = mf_detect(dsp, &dsp->digit_state, shortdata, len, (dsp->digitmode & DSP_DIGITMODE_NOQUELCH) == 0, (dsp->digitmode & DSP_DIGITMODE_RELAXDTMF));
1564  } else {
1565  digit = dtmf_detect(dsp, &dsp->digit_state, shortdata, len, (dsp->digitmode & DSP_DIGITMODE_NOQUELCH) == 0, (dsp->digitmode & DSP_DIGITMODE_RELAXDTMF));
1566  }
1567 
1568  if (dsp->digit_state.current_digits) {
1569  int event = 0, event_len = 0;
1570  char event_digit = 0;
1571 
1572  if (!dsp->dtmf_began) {
1573  /* We have not reported DTMF_BEGIN for anything yet */
1574 
1575  if (dsp->features & DSP_FEATURE_DIGIT_DETECT) {
1576  event = AST_FRAME_DTMF_BEGIN;
1577  event_digit = dsp->digit_state.digits[0];
1578  }
1579  dsp->dtmf_began = 1;
1580 
1581  } else if (dsp->digit_state.current_digits > 1 || digit != dsp->digit_state.digits[0]) {
1582  /* Digit changed. This means digit we have reported with DTMF_BEGIN ended */
1583  if (dsp->features & DSP_FEATURE_DIGIT_DETECT) {
1584  event = AST_FRAME_DTMF_END;
1585  event_digit = dsp->digit_state.digits[0];
1586  event_len = dsp->digit_state.digitlen[0] * 1000 / dsp->sample_rate;
1587  }
1588  memmove(&dsp->digit_state.digits[0], &dsp->digit_state.digits[1], dsp->digit_state.current_digits);
1589  memmove(&dsp->digit_state.digitlen[0], &dsp->digit_state.digitlen[1], dsp->digit_state.current_digits * sizeof(dsp->digit_state.digitlen[0]));
1590  dsp->digit_state.current_digits--;
1591  dsp->dtmf_began = 0;
1592 
1593  if (dsp->features & DSP_FEATURE_BUSY_DETECT) {
1594  /* Reset Busy Detector as we have some confirmed activity */
1595  memset(dsp->historicsilence, 0, sizeof(dsp->historicsilence));
1596  memset(dsp->historicnoise, 0, sizeof(dsp->historicnoise));
1597  ast_debug(1, "DTMF Detected - Reset busydetector\n");
1598  }
1599  }
1600 
1601  if (event) {
1602  memset(&dsp->f, 0, sizeof(dsp->f));
1603  dsp->f.frametype = event;
1604  dsp->f.subclass.integer = event_digit;
1605  dsp->f.len = event_len;
1606  outf = &dsp->f;
1607  goto done;
1608  }
1609  }
1610  }
1611 
1612  if (fax_digit) {
1613  /* Fax was detected - digit is either 'f' or 'e' */
1614 
1615  memset(&dsp->f, 0, sizeof(dsp->f));
1616  dsp->f.frametype = AST_FRAME_DTMF;
1617  dsp->f.subclass.integer = fax_digit;
1618  outf = &dsp->f;
1619  goto done;
1620  }
1621 
1622  if ((dsp->features & DSP_FEATURE_CALL_PROGRESS)) {
1623  res = __ast_dsp_call_progress(dsp, shortdata, len);
1624  if (res) {
1625  switch (res) {
1626  case AST_CONTROL_ANSWER:
1627  case AST_CONTROL_BUSY:
1628  case AST_CONTROL_RINGING:
1630  case AST_CONTROL_HANGUP:
1631  memset(&dsp->f, 0, sizeof(dsp->f));
1632  dsp->f.frametype = AST_FRAME_CONTROL;
1633  dsp->f.subclass.integer = res;
1634  dsp->f.src = "dsp_progress";
1635  if (chan) {
1636  ast_queue_frame(chan, &dsp->f);
1637  }
1638  break;
1639  default:
1640  ast_log(LOG_WARNING, "Don't know how to represent call progress message %d\n", res);
1641  }
1642  }
1643  } else if ((dsp->features & DSP_FEATURE_WAITDIALTONE)) {
1644  res = __ast_dsp_call_progress(dsp, shortdata, len);
1645  }
1646 
1647 done:
1648  /* Mute fragment of the frame */
1649  for (x = 0; x < dsp->mute_fragments; x++) {
1650  memset(shortdata + dsp->mute_data[x].start, 0, sizeof(int16_t) * (dsp->mute_data[x].end - dsp->mute_data[x].start));
1651  }
1652 
1654  for (x = 0; x < len; x++) {
1655  odata[x] = AST_LIN2MU((unsigned short) shortdata[x]);
1656  }
1658  for (x = 0; x < len; x++) {
1659  odata[x] = AST_LIN2A((unsigned short) shortdata[x]);
1660  }
1661  }
1662 
1663  if (outf) {
1664  if (chan) {
1665  ast_queue_frame(chan, af);
1666  }
1667  ast_frfree(af);
1668  return ast_frisolate(outf);
1669  } else {
1670  return af;
1671  }
1672 }
1673 
1674 static void ast_dsp_prog_reset(struct ast_dsp *dsp)
1675 {
1676  int max = 0;
1677  int x;
1678 
1679  dsp->gsamp_size = modes[dsp->progmode].size;
1680  dsp->gsamps = 0;
1681  for (x = 0; x < FREQ_ARRAY_SIZE; x++) {
1682  if (modes[dsp->progmode].freqs[x]) {
1683  goertzel_init(&dsp->freqs[x], (float)modes[dsp->progmode].freqs[x], dsp->sample_rate);
1684  max = x + 1;
1685  }
1686  }
1687  dsp->freqcount = max;
1688  dsp->ringtimeout = 0;
1689 }
1690 
1691 unsigned int ast_dsp_get_sample_rate(const struct ast_dsp *dsp)
1692 {
1693  return dsp->sample_rate;
1694 }
1695 
1696 static struct ast_dsp *__ast_dsp_new(unsigned int sample_rate)
1697 {
1698  struct ast_dsp *dsp;
1699 
1700  if ((dsp = ast_calloc(1, sizeof(*dsp)))) {
1703  dsp->busycount = DSP_HISTORY;
1706  dsp->sample_rate = sample_rate;
1707  dsp->freqcount = 0;
1708  /* Initialize digit detector */
1710  dsp->display_inband_dtmf_warning = 1;
1711  /* Initialize initial DSP progress detect parameters */
1712  ast_dsp_prog_reset(dsp);
1713  /* Initialize fax detector */
1714  ast_fax_detect_init(dsp);
1715  }
1716  return dsp;
1717 }
1718 
1719 struct ast_dsp *ast_dsp_new(void)
1720 {
1722 }
1723 
1725 {
1726  return __ast_dsp_new(sample_rate);
1727 }
1728 
1729 void ast_dsp_set_features(struct ast_dsp *dsp, int features)
1730 {
1731  dsp->features = features;
1732  if (!(features & DSP_FEATURE_DIGIT_DETECT)) {
1733  dsp->display_inband_dtmf_warning = 0;
1734  }
1735 }
1736 
1737 
1739 {
1740  return (dsp->features);
1741 }
1742 
1743 
1744 void ast_dsp_free(struct ast_dsp *dsp)
1745 {
1746  ast_free(dsp);
1747 }
1748 
1750 {
1751  dsp->threshold = threshold;
1752 }
1753 
1755 {
1756  if (cadences < 4) {
1757  cadences = 4;
1758  }
1759  if (cadences > DSP_HISTORY) {
1760  cadences = DSP_HISTORY;
1761  }
1762  dsp->busycount = cadences;
1763 }
1764 
1765 void ast_dsp_set_busy_pattern(struct ast_dsp *dsp, const struct ast_dsp_busy_pattern *cadence)
1766 {
1767  dsp->busy_cadence = *cadence;
1768  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);
1769 }
1770 
1771 void ast_dsp_digitreset(struct ast_dsp *dsp)
1772 {
1773  int i;
1774 
1775  dsp->dtmf_began = 0;
1776  if (dsp->digitmode & DSP_DIGITMODE_MF) {
1777  mf_detect_state_t *s = &dsp->digit_state.td.mf;
1778  /* Reinitialise the detector for the next block */
1779  for (i = 0; i < 6; i++) {
1780  goertzel_reset(&s->tone_out[i]);
1781  }
1782  s->hits[4] = s->hits[3] = s->hits[2] = s->hits[1] = s->hits[0] = 0;
1783  s->current_hit = 0;
1784  s->current_sample = 0;
1785  } else {
1787  /* Reinitialise the detector for the next block */
1788  for (i = 0; i < 4; i++) {
1789  goertzel_reset(&s->row_out[i]);
1790  goertzel_reset(&s->col_out[i]);
1791  }
1792  s->lasthit = 0;
1793  s->current_hit = 0;
1794  s->energy = 0.0;
1795  s->current_sample = 0;
1796  s->hits = 0;
1797  s->misses = 0;
1798  }
1799 
1800  dsp->digit_state.digits[0] = '\0';
1801  dsp->digit_state.current_digits = 0;
1802 }
1803 
1804 void ast_dsp_reset(struct ast_dsp *dsp)
1805 {
1806  int x;
1807 
1808  dsp->totalsilence = 0;
1809  dsp->gsamps = 0;
1810  for (x = 0; x < 4; x++) {
1811  dsp->freqs[x].v2 = dsp->freqs[x].v3 = 0.0;
1812  }
1813  memset(dsp->historicsilence, 0, sizeof(dsp->historicsilence));
1814  memset(dsp->historicnoise, 0, sizeof(dsp->historicnoise));
1815  dsp->ringtimeout = 0;
1816 }
1817 
1819 {
1820  int new;
1821  int old;
1822 
1825  if (old != new) {
1826  /* Must initialize structures if switching from MF to DTMF or vice-versa */
1828  }
1829  dsp->digitmode = digitmode;
1830  return 0;
1831 }
1832 
1833 int ast_dsp_set_faxmode(struct ast_dsp *dsp, int faxmode)
1834 {
1835  if (dsp->faxmode != faxmode) {
1836  dsp->faxmode = faxmode;
1837  ast_fax_detect_init(dsp);
1838  }
1839  return 0;
1840 }
1841 
1842 int ast_dsp_set_call_progress_zone(struct ast_dsp *dsp, char *zone)
1843 {
1844  int x;
1845 
1846  for (x = 0; x < ARRAY_LEN(aliases); x++) {
1847  if (!strcasecmp(aliases[x].name, zone)) {
1848  dsp->progmode = aliases[x].mode;
1849  ast_dsp_prog_reset(dsp);
1850  return 0;
1851  }
1852  }
1853  return -1;
1854 }
1855 
1856 int ast_dsp_was_muted(struct ast_dsp *dsp)
1857 {
1858  return (dsp->mute_fragments > 0);
1859 }
1860 
1861 int ast_dsp_get_tstate(struct ast_dsp *dsp)
1862 {
1863  return dsp->tstate;
1864 }
1865 
1866 int ast_dsp_get_tcount(struct ast_dsp *dsp)
1867 {
1868  return dsp->tcount;
1869 }
1870 
1871 static int _dsp_init(int reload)
1872 {
1873  struct ast_config *cfg;
1874  struct ast_variable *v;
1875  struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
1876  int cfg_threshold;
1877  float cfg_twist;
1878 
1879  if ((cfg = ast_config_load2(CONFIG_FILE_NAME, "dsp", config_flags)) == CONFIG_STATUS_FILEUNCHANGED) {
1880  return 0;
1881  }
1882 
1890 
1892  return 0;
1893  }
1894 
1895  for (v = ast_variable_browse(cfg, "default"); v; v = v->next) {
1896  if (!strcasecmp(v->name, "silencethreshold")) {
1897  if (sscanf(v->value, "%30d", &cfg_threshold) < 1) {
1898  ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", v->value);
1899  } else if (cfg_threshold < 0) {
1900  ast_log(LOG_WARNING, "Invalid silence threshold '%d' specified, using default\n", cfg_threshold);
1901  } else {
1902  thresholds[THRESHOLD_SILENCE] = cfg_threshold;
1903  }
1904  } else if (!strcasecmp(v->name, "dtmf_normal_twist")) {
1905  if (sscanf(v->value, "%30f", &cfg_twist) < 1) {
1906  ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", v->value);
1907  } else if ((cfg_twist < 2.0) || (cfg_twist > 100.0)) { /* < 3.0dB or > 20dB */
1908  ast_log(LOG_WARNING, "Invalid dtmf_normal_twist value '%.2f' specified, using default of %.2f\n", cfg_twist, dtmf_normal_twist);
1909  } else {
1910  dtmf_normal_twist = cfg_twist;
1911  }
1912  } else if (!strcasecmp(v->name, "dtmf_reverse_twist")) {
1913  if (sscanf(v->value, "%30f", &cfg_twist) < 1) {
1914  ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", v->value);
1915  } else if ((cfg_twist < 2.0) || (cfg_twist > 100.0)) { /* < 3.0dB or > 20dB */
1916  ast_log(LOG_WARNING, "Invalid dtmf_reverse_twist value '%.2f' specified, using default of %.2f\n", cfg_twist, dtmf_reverse_twist);
1917  } else {
1918  dtmf_reverse_twist = cfg_twist;
1919  }
1920  } else if (!strcasecmp(v->name, "relax_dtmf_normal_twist")) {
1921  if (sscanf(v->value, "%30f", &cfg_twist) < 1) {
1922  ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", v->value);
1923  } else if ((cfg_twist < 2.0) || (cfg_twist > 100.0)) { /* < 3.0dB or > 20dB */
1924  ast_log(LOG_WARNING, "Invalid relax_dtmf_normal_twist value '%.2f' specified, using default of %.2f\n", cfg_twist, relax_dtmf_normal_twist);
1925  } else {
1926  relax_dtmf_normal_twist = cfg_twist;
1927  }
1928  } else if (!strcasecmp(v->name, "relax_dtmf_reverse_twist")) {
1929  if (sscanf(v->value, "%30f", &cfg_twist) < 1) {
1930  ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", v->value);
1931  } else if ((cfg_twist < 2.0) || (cfg_twist > 100.0)) { /* < 3.0dB or > 20dB */
1932  ast_log(LOG_WARNING, "Invalid relax_dtmf_reverse_twist value '%.2f' specified, using default of %.2f\n", cfg_twist, relax_dtmf_reverse_twist);
1933  } else {
1934  relax_dtmf_reverse_twist = cfg_twist;
1935  }
1936  } else if (!strcasecmp(v->name, "dtmf_hits_to_begin")) {
1937  if (sscanf(v->value, "%30d", &cfg_threshold) < 1) {
1938  ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", v->value);
1939  } else if (cfg_threshold < 1) { /* must be 1 or greater */
1940  ast_log(LOG_WARNING, "Invalid dtmf_hits_to_begin value '%d' specified, using default of %d\n", cfg_threshold, dtmf_hits_to_begin);
1941  } else {
1942  dtmf_hits_to_begin = cfg_threshold;
1943  }
1944  } else if (!strcasecmp(v->name, "dtmf_misses_to_end")) {
1945  if (sscanf(v->value, "%30d", &cfg_threshold) < 1) {
1946  ast_log(LOG_WARNING, "Unable to convert '%s' to a numeric value.\n", v->value);
1947  } else if (cfg_threshold < 1) { /* must be 1 or greater */
1948  ast_log(LOG_WARNING, "Invalid dtmf_misses_to_end value '%d' specified, using default of %d\n", cfg_threshold, dtmf_misses_to_end);
1949  } else {
1950  dtmf_misses_to_end = cfg_threshold;
1951  }
1952  }
1953  }
1954  ast_config_destroy(cfg);
1955 
1956  return 0;
1957 }
1958 
1960 {
1961  return thresholds[which];
1962 }
1963 
1964 #ifdef TEST_FRAMEWORK
1965 static void test_tone_sample_gen(short *slin_buf, int samples, int rate, int freq, short amplitude)
1966 {
1967  int idx;
1968  double sample_step = 2.0 * M_PI * freq / rate;/* radians per step */
1969 
1970  for (idx = 0; idx < samples; ++idx) {
1971  slin_buf[idx] = amplitude * sin(sample_step * idx);
1972  }
1973 }
1974 #endif
1975 
1976 #ifdef TEST_FRAMEWORK
1977 static void test_tone_sample_gen_add(short *slin_buf, int samples, int rate, int freq, short amplitude)
1978 {
1979  int idx;
1980  double sample_step = 2.0 * M_PI * freq / rate;/* radians per step */
1981 
1982  for (idx = 0; idx < samples; ++idx) {
1983  slin_buf[idx] += amplitude * sin(sample_step * idx);
1984  }
1985 }
1986 #endif
1987 
1988 #ifdef TEST_FRAMEWORK
1989 static void test_dual_sample_gen(short *slin_buf, int samples, int rate, int f1, short a1, int f2, short a2)
1990 {
1991  test_tone_sample_gen(slin_buf, samples, rate, f1, a1);
1992  test_tone_sample_gen_add(slin_buf, samples, rate, f2, a2);
1993 }
1994 #endif
1995 
1996 #ifdef TEST_FRAMEWORK
1997 #define TONE_AMPLITUDE_MAX 0x7fff /* Max signed linear amplitude */
1998 #define TONE_AMPLITUDE_MIN 80 /* Min signed linear amplitude detectable */
1999 
2000 static int test_tone_amplitude_sweep(struct ast_test *test, struct ast_dsp *dsp, tone_detect_state_t *tone_state)
2001 {
2002  short slin_buf[tone_state->block_size];
2003  int result;
2004  int idx;
2005  struct {
2006  short amp_val;
2007  int detect;
2008  } amp_tests[] = {
2009  { .amp_val = TONE_AMPLITUDE_MAX, .detect = 1, },
2010  { .amp_val = 10000, .detect = 1, },
2011  { .amp_val = 1000, .detect = 1, },
2012  { .amp_val = 100, .detect = 1, },
2013  { .amp_val = TONE_AMPLITUDE_MIN, .detect = 1, },
2014  { .amp_val = 75, .detect = 0, },
2015  { .amp_val = 10, .detect = 0, },
2016  { .amp_val = 1, .detect = 0, },
2017  };
2018 
2019  result = 0;
2020 
2021  for (idx = 0; idx < ARRAY_LEN(amp_tests); ++idx) {
2022  int detected;
2023  int duration;
2024 
2025  ast_debug(1, "Test %d Hz at amplitude %d\n",
2026  tone_state->freq, amp_tests[idx].amp_val);
2027  test_tone_sample_gen(slin_buf, tone_state->block_size, DEFAULT_SAMPLE_RATE,
2028  tone_state->freq, amp_tests[idx].amp_val);
2029 
2030  detected = 0;
2031  for (duration = 0; !detected && duration < tone_state->hits_required + 3; ++duration) {
2032  detected = tone_detect(dsp, tone_state, slin_buf, tone_state->block_size) ? 1 : 0;
2033  }
2034  if (amp_tests[idx].detect != detected) {
2035  /*
2036  * Both messages are needed. ast_debug for when figuring out
2037  * what went wrong and the test update for normal output before
2038  * you start debugging. The different logging methods are not
2039  * synchronized.
2040  */
2041  ast_debug(1,
2042  "Test %d Hz at amplitude %d failed. Detected: %s\n",
2043  tone_state->freq, amp_tests[idx].amp_val,
2044  detected ? "yes" : "no");
2046  "Test %d Hz at amplitude %d failed. Detected: %s\n",
2047  tone_state->freq, amp_tests[idx].amp_val,
2048  detected ? "yes" : "no");
2049  result = -1;
2050  }
2051  tone_state->hit_count = 0;
2052  }
2053 
2054  return result;
2055 }
2056 #endif
2057 
2058 #ifdef TEST_FRAMEWORK
2059 static int test_dtmf_amplitude_sweep(struct ast_test *test, struct ast_dsp *dsp, int digit_index)
2060 {
2061  short slin_buf[DTMF_GSIZE];
2062  int result;
2063  int row;
2064  int column;
2065  int idx;
2066  struct {
2067  short amp_val;
2068  int digit;
2069  } amp_tests[] = {
2070  /*
2071  * XXX Since there is no current DTMF level detection issue. This test
2072  * just checks the current detection levels.
2073  */
2074  { .amp_val = TONE_AMPLITUDE_MAX/2, .digit = dtmf_positions[digit_index], },
2075  { .amp_val = 10000, .digit = dtmf_positions[digit_index], },
2076  { .amp_val = 1000, .digit = dtmf_positions[digit_index], },
2077  { .amp_val = 500, .digit = dtmf_positions[digit_index], },
2078  { .amp_val = 250, .digit = dtmf_positions[digit_index], },
2079  { .amp_val = 200, .digit = dtmf_positions[digit_index], },
2080  { .amp_val = 180, .digit = dtmf_positions[digit_index], },
2081  /* Various digits detect and not detect in this range */
2082  { .amp_val = 170, .digit = 0, },
2083  { .amp_val = 100, .digit = 0, },
2084  /*
2085  * Amplitudes below TONE_AMPLITUDE_MIN start having questionable detection
2086  * over quantization and background noise.
2087  */
2088  { .amp_val = TONE_AMPLITUDE_MIN, .digit = 0, },
2089  { .amp_val = 75, .digit = 0, },
2090  { .amp_val = 10, .digit = 0, },
2091  { .amp_val = 1, .digit = 0, },
2092  };
2093 
2094  row = (digit_index >> 2) & 0x03;
2095  column = digit_index & 0x03;
2096 
2097  result = 0;
2098 
2099  for (idx = 0; idx < ARRAY_LEN(amp_tests); ++idx) {
2100  int digit;
2101  int duration;
2102 
2103  ast_debug(1, "Test '%c' at amplitude %d\n",
2104  dtmf_positions[digit_index], amp_tests[idx].amp_val);
2105  test_dual_sample_gen(slin_buf, ARRAY_LEN(slin_buf), DEFAULT_SAMPLE_RATE,
2106  (int) dtmf_row[row], amp_tests[idx].amp_val,
2107  (int) dtmf_col[column], amp_tests[idx].amp_val);
2108 
2109  digit = 0;
2110  for (duration = 0; !digit && duration < 3; ++duration) {
2111  digit = dtmf_detect(dsp, &dsp->digit_state, slin_buf, ARRAY_LEN(slin_buf),
2112  0, 0);
2113  }
2114  if (amp_tests[idx].digit != digit) {
2115  /*
2116  * Both messages are needed. ast_debug for when figuring out
2117  * what went wrong and the test update for normal output before
2118  * you start debugging. The different logging methods are not
2119  * synchronized.
2120  */
2121  ast_debug(1,
2122  "Test '%c' at amplitude %d failed. Detected Digit: '%c'\n",
2123  dtmf_positions[digit_index], amp_tests[idx].amp_val,
2124  digit ?: ' ');
2126  "Test '%c' at amplitude %d failed. Detected Digit: '%c'\n",
2127  dtmf_positions[digit_index], amp_tests[idx].amp_val,
2128  digit ?: ' ');
2129  result = -1;
2130  }
2131  ast_dsp_digitreset(dsp);
2132  }
2133 
2134  return result;
2135 }
2136 #endif
2137 
2138 #ifdef TEST_FRAMEWORK
2139 static int test_dtmf_twist_sweep(struct ast_test *test, struct ast_dsp *dsp, int digit_index)
2140 {
2141  short slin_buf[DTMF_GSIZE];
2142  int result;
2143  int row;
2144  int column;
2145  int idx;
2146  struct {
2147  short amp_row;
2148  short amp_col;
2149  int digit;
2150  } twist_tests[] = {
2151  /*
2152  * XXX Since there is no current DTMF twist detection issue. This test
2153  * just checks the current detection levels.
2154  *
2155  * Normal twist has the column higher than the row amplitude.
2156  * Reverse twist is the other way.
2157  */
2158  { .amp_row = 1000 + 1800, .amp_col = 1000 + 0, .digit = 0, },
2159  { .amp_row = 1000 + 1700, .amp_col = 1000 + 0, .digit = 0, },
2160  /* Various digits detect and not detect in this range */
2161  { .amp_row = 1000 + 1400, .amp_col = 1000 + 0, .digit = dtmf_positions[digit_index], },
2162  { .amp_row = 1000 + 1300, .amp_col = 1000 + 0, .digit = dtmf_positions[digit_index], },
2163  { .amp_row = 1000 + 1200, .amp_col = 1000 + 0, .digit = dtmf_positions[digit_index], },
2164  { .amp_row = 1000 + 1100, .amp_col = 1000 + 0, .digit = dtmf_positions[digit_index], },
2165  { .amp_row = 1000 + 1000, .amp_col = 1000 + 0, .digit = dtmf_positions[digit_index], },
2166  { .amp_row = 1000 + 100, .amp_col = 1000 + 0, .digit = dtmf_positions[digit_index], },
2167  { .amp_row = 1000 + 0, .amp_col = 1000 + 100, .digit = dtmf_positions[digit_index], },
2168  { .amp_row = 1000 + 0, .amp_col = 1000 + 200, .digit = dtmf_positions[digit_index], },
2169  { .amp_row = 1000 + 0, .amp_col = 1000 + 300, .digit = dtmf_positions[digit_index], },
2170  { .amp_row = 1000 + 0, .amp_col = 1000 + 400, .digit = dtmf_positions[digit_index], },
2171  { .amp_row = 1000 + 0, .amp_col = 1000 + 500, .digit = dtmf_positions[digit_index], },
2172  { .amp_row = 1000 + 0, .amp_col = 1000 + 550, .digit = dtmf_positions[digit_index], },
2173  /* Various digits detect and not detect in this range */
2174  { .amp_row = 1000 + 0, .amp_col = 1000 + 650, .digit = 0, },
2175  { .amp_row = 1000 + 0, .amp_col = 1000 + 700, .digit = 0, },
2176  { .amp_row = 1000 + 0, .amp_col = 1000 + 800, .digit = 0, },
2177  };
2178  float save_normal_twist;
2179  float save_reverse_twist;
2180 
2181  save_normal_twist = dtmf_normal_twist;
2182  save_reverse_twist = dtmf_reverse_twist;
2185 
2186  row = (digit_index >> 2) & 0x03;
2187  column = digit_index & 0x03;
2188 
2189  result = 0;
2190 
2191  for (idx = 0; idx < ARRAY_LEN(twist_tests); ++idx) {
2192  int digit;
2193  int duration;
2194 
2195  ast_debug(1, "Test '%c' twist row %d col %d amplitudes\n",
2196  dtmf_positions[digit_index],
2197  twist_tests[idx].amp_row, twist_tests[idx].amp_col);
2198  test_dual_sample_gen(slin_buf, ARRAY_LEN(slin_buf), DEFAULT_SAMPLE_RATE,
2199  (int) dtmf_row[row], twist_tests[idx].amp_row,
2200  (int) dtmf_col[column], twist_tests[idx].amp_col);
2201 
2202  digit = 0;
2203  for (duration = 0; !digit && duration < 3; ++duration) {
2204  digit = dtmf_detect(dsp, &dsp->digit_state, slin_buf, ARRAY_LEN(slin_buf),
2205  0, 0);
2206  }
2207  if (twist_tests[idx].digit != digit) {
2208  /*
2209  * Both messages are needed. ast_debug for when figuring out
2210  * what went wrong and the test update for normal output before
2211  * you start debugging. The different logging methods are not
2212  * synchronized.
2213  */
2214  ast_debug(1,
2215  "Test '%c' twist row %d col %d amplitudes failed. Detected Digit: '%c'\n",
2216  dtmf_positions[digit_index],
2217  twist_tests[idx].amp_row, twist_tests[idx].amp_col,
2218  digit ?: ' ');
2220  "Test '%c' twist row %d col %d amplitudes failed. Detected Digit: '%c'\n",
2221  dtmf_positions[digit_index],
2222  twist_tests[idx].amp_row, twist_tests[idx].amp_col,
2223  digit ?: ' ');
2224  result = -1;
2225  }
2226  ast_dsp_digitreset(dsp);
2227  }
2228 
2229  dtmf_normal_twist = save_normal_twist;
2230  dtmf_reverse_twist = save_reverse_twist;
2231 
2232  return result;
2233 }
2234 #endif
2235 
2236 #ifdef TEST_FRAMEWORK
2237 static int test_tone_freq_sweep(struct ast_test *test, struct ast_dsp *dsp, tone_detect_state_t *tone_state, short amplitude)
2238 {
2239  short slin_buf[tone_state->block_size];
2240  int result;
2241  int freq;
2242  int lower_freq;
2243  int upper_freq;
2244 
2245  /* Calculate detection frequency range */
2246  lower_freq = tone_state->freq - 4;
2247  upper_freq = tone_state->freq + 4;
2248 
2249  result = 0;
2250 
2251  /* Sweep frequencies loop. */
2252  for (freq = 100; freq <= 3500; freq += 1) {
2253  int detected;
2254  int duration;
2255  int expect_detection;
2256 
2257  if (freq == tone_state->freq) {
2258  /* This case is done by the amplitude sweep. */
2259  continue;
2260  }
2261 
2262  expect_detection = (lower_freq <= freq && freq <= upper_freq) ? 1 : 0;
2263 
2264  ast_debug(1, "Test %d Hz detection given %d Hz tone at amplitude %d. Range:%d-%d Expect detect: %s\n",
2265  tone_state->freq, freq, amplitude, lower_freq, upper_freq,
2266  expect_detection ? "yes" : "no");
2267  test_tone_sample_gen(slin_buf, tone_state->block_size, DEFAULT_SAMPLE_RATE, freq,
2268  amplitude);
2269 
2270  detected = 0;
2271  for (duration = 0; !detected && duration < tone_state->hits_required + 3; ++duration) {
2272  detected = tone_detect(dsp, tone_state, slin_buf, tone_state->block_size) ? 1 : 0;
2273  }
2274  if (expect_detection != detected) {
2275  /*
2276  * Both messages are needed. ast_debug for when figuring out
2277  * what went wrong and the test update for normal output before
2278  * you start debugging. The different logging methods are not
2279  * synchronized.
2280  */
2281  ast_debug(1,
2282  "Test %d Hz detection given %d Hz tone at amplitude %d failed. Range:%d-%d Detected: %s\n",
2283  tone_state->freq, freq, amplitude, lower_freq, upper_freq,
2284  detected ? "yes" : "no");
2286  "Test %d Hz detection given %d Hz tone at amplitude %d failed. Range:%d-%d Detected: %s\n",
2287  tone_state->freq, freq, amplitude, lower_freq, upper_freq,
2288  detected ? "yes" : "no");
2289  result = -1;
2290  }
2291  tone_state->hit_count = 0;
2292  }
2293 
2294  return result;
2295 }
2296 #endif
2297 
2298 #ifdef TEST_FRAMEWORK
2299 AST_TEST_DEFINE(test_dsp_fax_detect)
2300 {
2301  struct ast_dsp *dsp;
2303 
2304  switch (cmd) {
2305  case TEST_INIT:
2306  info->name = "fax";
2307  info->category = "/main/dsp/";
2308  info->summary = "DSP fax tone detect unit test";
2309  info->description =
2310  "Tests fax tone detection code.";
2311  return AST_TEST_NOT_RUN;
2312  case TEST_EXECUTE:
2313  break;
2314  }
2315 
2316  dsp = ast_dsp_new();
2317  if (!dsp) {
2318  return AST_TEST_FAIL;
2319  }
2320 
2321  result = AST_TEST_PASS;
2322 
2323  /* Test CNG tone amplitude detection */
2324  if (test_tone_amplitude_sweep(test, dsp, &dsp->cng_tone_state)) {
2325  result = AST_TEST_FAIL;
2326  }
2327 
2328  /* Test CED tone amplitude detection */
2329  if (test_tone_amplitude_sweep(test, dsp, &dsp->ced_tone_state)) {
2330  result = AST_TEST_FAIL;
2331  }
2332 
2333  /* Test CNG tone frequency detection */
2334  if (test_tone_freq_sweep(test, dsp, &dsp->cng_tone_state, TONE_AMPLITUDE_MAX)) {
2335  result = AST_TEST_FAIL;
2336  }
2337  if (test_tone_freq_sweep(test, dsp, &dsp->cng_tone_state, TONE_AMPLITUDE_MIN)) {
2338  result = AST_TEST_FAIL;
2339  }
2340 
2341  /* Test CED tone frequency detection */
2342  if (test_tone_freq_sweep(test, dsp, &dsp->ced_tone_state, TONE_AMPLITUDE_MAX)) {
2343  result = AST_TEST_FAIL;
2344  }
2345  if (test_tone_freq_sweep(test, dsp, &dsp->ced_tone_state, TONE_AMPLITUDE_MIN)) {
2346  result = AST_TEST_FAIL;
2347  }
2348 
2349  ast_dsp_free(dsp);
2350  return result;
2351 }
2352 #endif
2353 
2354 #ifdef TEST_FRAMEWORK
2355 AST_TEST_DEFINE(test_dsp_dtmf_detect)
2356 {
2357  int idx;
2358  struct ast_dsp *dsp;
2360 
2361  switch (cmd) {
2362  case TEST_INIT:
2363  info->name = "dtmf";
2364  info->category = "/main/dsp/";
2365  info->summary = "DSP DTMF detect unit test";
2366  info->description =
2367  "Tests DTMF detection code.";
2368  return AST_TEST_NOT_RUN;
2369  case TEST_EXECUTE:
2370  break;
2371  }
2372 
2373  dsp = ast_dsp_new();
2374  if (!dsp) {
2375  return AST_TEST_FAIL;
2376  }
2377 
2378  result = AST_TEST_PASS;
2379 
2380  for (idx = 0; dtmf_positions[idx]; ++idx) {
2381  if (test_dtmf_amplitude_sweep(test, dsp, idx)) {
2382  result = AST_TEST_FAIL;
2383  }
2384  }
2385 
2386  for (idx = 0; dtmf_positions[idx]; ++idx) {
2387  if (test_dtmf_twist_sweep(test, dsp, idx)) {
2388  result = AST_TEST_FAIL;
2389  }
2390  }
2391 
2392  ast_dsp_free(dsp);
2393  return result;
2394 }
2395 #endif
2396 
2397 static int unload_module(void)
2398 {
2399  AST_TEST_UNREGISTER(test_dsp_fax_detect);
2400  AST_TEST_UNREGISTER(test_dsp_dtmf_detect);
2401 
2402  return 0;
2403 }
2404 
2405 static int load_module(void)
2406 {
2407  if (_dsp_init(0)) {
2408  return AST_MODULE_LOAD_FAILURE;
2409  }
2410 
2411  AST_TEST_REGISTER(test_dsp_fax_detect);
2412  AST_TEST_REGISTER(test_dsp_dtmf_detect);
2413 
2414  return AST_MODULE_LOAD_SUCCESS;
2415 }
2416 
2417 static int reload_module(void)
2418 {
2419  return _dsp_init(1);
2420 }
2421 
2423  .support_level = AST_MODULE_SUPPORT_CORE,
2424  .load = load_module,
2425  .unload = unload_module,
2426  .reload = reload_module,
2427  .load_pri = AST_MODPRI_CORE,
2428  .requires = "extconfig",
2429 );
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:1856
static void ast_digit_detect_init(digit_detect_state_t *s, int mf, unsigned int sample_rate)
Definition: dsp.c:549
static void mute_fragment(struct ast_dsp *dsp, fragment_t *fragment)
Definition: dsp.c:439
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.
static float relax_dtmf_reverse_twist
Definition: dsp.c:332
int start
Definition: dsp.c:388
unsigned int cos
Definition: chan_iax2.c:353
static int unload_module(void)
Definition: dsp.c:2397
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:1423
static int load_module(void)
Definition: dsp.c:2405
freq_index
Definition: dsp.c:85
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
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:1484
float genergy
Definition: dsp.c:430
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:2417
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
#define DSP_TONE_STATE_HUNGUP
Definition: dsp.h:59
union digit_detect_state_t::@377 td
#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:1749
#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:67
#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 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:1842
#define AST_ALAW(a)
Definition: alaw.h:84
Definition: dsp.c:101
union ast_frame::@257 data
#define DSP_PROGRESS_TALK
Definition: dsp.h:39
#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:1738
int ast_dsp_set_digitmode(struct ast_dsp *dsp, int digitmode)
Set digit mode.
Definition: dsp.c:1818
#define DSP_TONE_STATE_DIALTONE
Definition: dsp.h:53
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:1064
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:48
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:683
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
#define NULL
Definition: resample.c:96
#define FAX_TONE_CNG_DURATION
Definition: dsp.c:210
#define DSP_TONE_STATE_TALKING
Definition: dsp.h:54
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:1285
static void ast_dsp_prog_reset(struct ast_dsp *dsp)
Definition: dsp.c:1674
#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
void ast_dsp_reset(struct ast_dsp *dsp)
Reset total silence count.
Definition: dsp.c:1804
digit_detect_state_t digit_state
Definition: dsp.c:434
int historicnoise[DSP_HISTORY]
Definition: dsp.c:417
#define BELL_MF_TWIST
Definition: dsp.c:199
struct ast_frame_subclass subclass
static int mute
Definition: chan_alsa.c:144
int totalsilence
Definition: dsp.c:409
Utility functions.
threshold
Definition: dsp.h:70
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:1473
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:1478
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:444
#define ast_log
Definition: astobj2.c:42
#define DEF_DTMF_REVERSE_TWIST
Definition: dsp.c:190
#define DSP_TONE_STATE_SPECIAL2
Definition: dsp.h:57
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:1729
#define DSP_TONE_STATE_RINGING
Definition: dsp.h:52
#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:55
float threshold
Definition: dsp.c:274
void ast_dsp_digitreset(struct ast_dsp *dsp)
Reset DTMF detector.
Definition: dsp.c:1771
#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:521
int freqcount
Definition: dsp.c:420
#define DSP_TONE_STATE_SPECIAL3
Definition: dsp.h:58
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:1959
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:432
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:1135
#define CONFIG_STATUS_FILEUNCHANGED
static void store_digit(digit_detect_state_t *s, char digit)
Definition: dsp.c:670
void ast_dsp_free(struct ast_dsp *dsp)
Definition: dsp.c:1744
#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:449
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:890
int dtmf_began
Definition: dsp.c:428
#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:56
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:1206
#define DSP_FAXMODE_DETECT_CED
Definition: dsp.h:47
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:1724
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:1468
static void ast_fax_detect_init(struct ast_dsp *s)
Definition: dsp.c:510
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:1691
#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:1219
int mute_fragments
Definition: dsp.c:431
int tcount
Definition: dsp.c:425
int ast_dsp_set_faxmode(struct ast_dsp *dsp, int faxmode)
Set fax mode.
Definition: dsp.c:1833
#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:1866
tone_detect_state_t ced_tone_state
Definition: dsp.c:436
Structure used to handle boolean flags.
Definition: utils.h:199
static int _dsp_init(int reload)
Definition: dsp.c:1871
int ast_dsp_get_tstate(struct ast_dsp *dsp)
Get tstate (Tone State)
Definition: dsp.c:1861
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:563
static void goertzel_sample(goertzel_state_t *s, short sample)
Definition: dsp.c:336
#define abs(x)
Definition: f2c.h:195
Definition: dsp.c:89
#define BELL_MF_RELATIVE_PEAK
Definition: dsp.c:200
#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:429
#define ast_frfree(fr)
static PGresult * result
Definition: cel_pgsql.c:88
tone_detect_state_t cng_tone_state
Definition: dsp.c:435
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)
#define AST_TEST_DEFINE(hdr)
Definition: test.h:126
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:1765
#define DSP_FAXMODE_DETECT_CNG
Definition: dsp.h:46
int tstate
Definition: dsp.c:424
fragment_t mute_data[5]
Definition: dsp.c:433
goertzel_state_t tone
Definition: dsp.c:268
enum ast_frame_type frametype
#define DSP_TONE_STATE_SILENCE
Definition: dsp.h:51
#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:537
int int32_t
Definition: db.h:60
int current_hit
Definition: dsp.c:297
struct ast_format * format
#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:1041
#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:1719
void ast_dsp_set_busy_count(struct ast_dsp *dsp, int cadences)
Set number of required cadences for busy.
Definition: dsp.c:1754
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:1696
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