dsp.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. * \brief Convenient Signal Processing routines
  20. */
  21. #ifndef _ASTERISK_DSP_H
  22. #define _ASTERISK_DSP_H
  23. #define DSP_FEATURE_SILENCE_SUPPRESS (1 << 0)
  24. #define DSP_FEATURE_BUSY_DETECT (1 << 1)
  25. #define DSP_FEATURE_DIGIT_DETECT (1 << 3)
  26. #define DSP_FEATURE_FAX_DETECT (1 << 4)
  27. #define DSP_DIGITMODE_DTMF 0 /*!< Detect DTMF digits */
  28. #define DSP_DIGITMODE_MF 1 /*!< Detect MF digits */
  29. #define DSP_DIGITMODE_NOQUELCH (1 << 8) /*!< Do not quelch DTMF from in-band */
  30. #define DSP_DIGITMODE_MUTECONF (1 << 9) /*!< Mute conference */
  31. #define DSP_DIGITMODE_MUTEMAX (1 << 10) /*!< Delay audio by a frame to try to extra quelch */
  32. #define DSP_DIGITMODE_RELAXDTMF (1 << 11) /*!< "Radio" mode (relaxed DTMF) */
  33. #define DSP_PROGRESS_TALK (1 << 16) /*!< Enable talk detection */
  34. #define DSP_PROGRESS_RINGING (1 << 17) /*!< Enable calling tone detection */
  35. #define DSP_PROGRESS_BUSY (1 << 18) /*!< Enable busy tone detection */
  36. #define DSP_PROGRESS_CONGESTION (1 << 19) /*!< Enable congestion tone detection */
  37. #define DSP_FEATURE_CALL_PROGRESS (DSP_PROGRESS_TALK | DSP_PROGRESS_RINGING | DSP_PROGRESS_BUSY | DSP_PROGRESS_CONGESTION)
  38. #define DSP_FEATURE_WAITDIALTONE (1 << 20) /*!< Enable dial tone detection */
  39. #define DSP_FAXMODE_DETECT_CNG (1 << 0)
  40. #define DSP_FAXMODE_DETECT_CED (1 << 1)
  41. #define DSP_FAXMODE_DETECT_SQUELCH (1 << 2)
  42. #define DSP_FAXMODE_DETECT_ALL (DSP_FAXMODE_DETECT_CNG | DSP_FAXMODE_DETECT_CED)
  43. #define DSP_TONE_STATE_SILENCE 0
  44. #define DSP_TONE_STATE_RINGING 1
  45. #define DSP_TONE_STATE_DIALTONE 2
  46. #define DSP_TONE_STATE_TALKING 3
  47. #define DSP_TONE_STATE_BUSY 4
  48. #define DSP_TONE_STATE_SPECIAL1 5
  49. #define DSP_TONE_STATE_SPECIAL2 6
  50. #define DSP_TONE_STATE_SPECIAL3 7
  51. #define DSP_TONE_STATE_HUNGUP 8
  52. struct ast_dsp;
  53. struct ast_dsp_busy_pattern {
  54. /*! Number of elements. */
  55. int length;
  56. /*! Pattern elements in on/off time durations. */
  57. int pattern[4];
  58. };
  59. enum threshold {
  60. /* Array offsets */
  61. THRESHOLD_SILENCE = 0,
  62. /* Always the last */
  63. THRESHOLD_MAX = 1,
  64. };
  65. /*! \brief Allocates a new dsp with a specific internal sample rate used
  66. * during processing. */
  67. struct ast_dsp *ast_dsp_new_with_rate(unsigned int sample_rate);
  68. /*! \brief Allocates a new dsp, assumes 8khz for internal sample rate */
  69. struct ast_dsp *ast_dsp_new(void);
  70. void ast_dsp_free(struct ast_dsp *dsp);
  71. /*! \brief Retrieve the sample rate this DSP structure was
  72. * created with */
  73. unsigned int ast_dsp_get_sample_rate(const struct ast_dsp *dsp);
  74. /*! \brief Set the minimum average magnitude threshold to determine talking by the DSP. */
  75. void ast_dsp_set_threshold(struct ast_dsp *dsp, int threshold);
  76. /*! \brief Set number of required cadences for busy */
  77. void ast_dsp_set_busy_count(struct ast_dsp *dsp, int cadences);
  78. /*! \brief Set expected lengths of the busy tone */
  79. void ast_dsp_set_busy_pattern(struct ast_dsp *dsp, const struct ast_dsp_busy_pattern *cadence);
  80. /*! \brief Scans for progress indication in audio */
  81. int ast_dsp_call_progress(struct ast_dsp *dsp, struct ast_frame *inf);
  82. /*! \brief Set zone for doing progress detection */
  83. int ast_dsp_set_call_progress_zone(struct ast_dsp *dsp, char *zone);
  84. /*! \brief Return AST_FRAME_NULL frames when there is silence, AST_FRAME_BUSY on
  85. busies, and call progress, all dependent upon which features are enabled */
  86. struct ast_frame *ast_dsp_process(struct ast_channel *chan, struct ast_dsp *dsp, struct ast_frame *inf);
  87. /*!
  88. * \brief Process the audio frame for silence.
  89. *
  90. * \param dsp DSP processing audio media.
  91. * \param f Audio frame to process.
  92. * \param totalsilence Variable to set to the total accumulated silence in ms
  93. * seen by the DSP since the last noise.
  94. *
  95. * \return Non-zero if the frame is silence.
  96. */
  97. int ast_dsp_silence(struct ast_dsp *dsp, struct ast_frame *f, int *totalsilence);
  98. /*!
  99. * \brief Process the audio frame for silence.
  100. *
  101. * \param dsp DSP processing audio media.
  102. * \param f Audio frame to process.
  103. * \param totalsilence Variable to set to the total accumulated silence in ms
  104. * seen by the DSP since the last noise.
  105. * \param frames_energy Variable to set to the average energy of the samples in the frame.
  106. *
  107. * \return Non-zero if the frame is silence.
  108. */
  109. int ast_dsp_silence_with_energy(struct ast_dsp *dsp, struct ast_frame *f, int *totalsilence, int *frames_energy);
  110. /*!
  111. * \brief Process the audio frame for noise.
  112. * \since 1.6.1
  113. *
  114. * \param dsp DSP processing audio media.
  115. * \param f Audio frame to process.
  116. * \param totalnoise Variable to set to the total accumulated noise in ms
  117. * seen by the DSP since the last silence.
  118. *
  119. * \return Non-zero if the frame is silence.
  120. */
  121. int ast_dsp_noise(struct ast_dsp *dsp, struct ast_frame *f, int *totalnoise);
  122. /*! \brief Return non-zero if historically this should be a busy, request that
  123. ast_dsp_silence has already been called */
  124. int ast_dsp_busydetect(struct ast_dsp *dsp);
  125. /*! \brief Return non-zero if DTMF hit was found */
  126. int ast_dsp_digitdetect(struct ast_dsp *dsp, struct ast_frame *f);
  127. /*! \brief Reset total silence count */
  128. void ast_dsp_reset(struct ast_dsp *dsp);
  129. /*! \brief Reset DTMF detector */
  130. void ast_dsp_digitreset(struct ast_dsp *dsp);
  131. /*! \brief Select feature set */
  132. void ast_dsp_set_features(struct ast_dsp *dsp, int features);
  133. /*! \brief Get features */
  134. int ast_dsp_get_features(struct ast_dsp *dsp);
  135. /*! \brief Get pending DTMF/MF digits */
  136. int ast_dsp_getdigits(struct ast_dsp *dsp, char *buf, int max);
  137. /*! \brief Set digit mode
  138. * \version 1.6.1 renamed from ast_dsp_digitmode to ast_dsp_set_digitmode
  139. */
  140. int ast_dsp_set_digitmode(struct ast_dsp *dsp, int digitmode);
  141. /*! \brief Set fax mode */
  142. int ast_dsp_set_faxmode(struct ast_dsp *dsp, int faxmode);
  143. /*!
  144. * \brief Returns true if DSP code was muting any fragment of the last processed frame.
  145. * Muting (squelching) happens when DSP code removes DTMF/MF/generic tones from the audio
  146. * \since 1.6.1
  147. */
  148. int ast_dsp_was_muted(struct ast_dsp *dsp);
  149. /*! \brief Get tstate (Tone State) */
  150. int ast_dsp_get_tstate(struct ast_dsp *dsp);
  151. /*! \brief Get tcount (Threshold counter) */
  152. int ast_dsp_get_tcount(struct ast_dsp *dsp);
  153. /*!
  154. * \brief Get silence threshold from dsp.conf
  155. * \since 1.6.1
  156. */
  157. int ast_dsp_get_threshold_from_settings(enum threshold which);
  158. /*!
  159. * \brief Reloads dsp settings from dsp.conf
  160. * \since 1.6.1
  161. */
  162. int ast_dsp_reload(void);
  163. /*!
  164. * \brief Load dsp settings from dsp.conf
  165. * \since 1.6.1
  166. */
  167. int ast_dsp_init(void);
  168. #endif /* _ASTERISK_DSP_H */