silencedet.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #ifndef __PJMEDIA_SILENCE_DET_H__
  20. #define __PJMEDIA_SILENCE_DET_H__
  21. /**
  22. * @file silencedet.h
  23. * @brief Adaptive silence detector.
  24. */
  25. #include <pjmedia/types.h>
  26. /**
  27. * @defgroup PJMEDIA_SILENCEDET Adaptive Silence Detection
  28. * @ingroup PJMEDIA_FRAME_OP
  29. * @brief Adaptive Silence Detector
  30. * @{
  31. */
  32. PJ_BEGIN_DECL
  33. /**
  34. * Opaque declaration for silence detector.
  35. */
  36. typedef struct pjmedia_silence_det pjmedia_silence_det;
  37. /**
  38. * Create voice activity detector with default settings. The default settings
  39. * are set to adaptive silence detection with the default threshold.
  40. *
  41. * @param pool Pool for allocating the structure.
  42. * @param clock_rate Clock rate.
  43. * @param samples_per_frame Number of samples per frame. The clock_rate and
  44. * samples_per_frame is only used to calculate the
  45. * frame time, from which some timing parameters
  46. * are calculated from.
  47. * @param p_sd Pointer to receive the silence detector instance.
  48. *
  49. * @return PJ_SUCCESS on success.
  50. */
  51. PJ_DECL(pj_status_t) pjmedia_silence_det_create( pj_pool_t *pool,
  52. unsigned clock_rate,
  53. unsigned samples_per_frame,
  54. pjmedia_silence_det **p_sd );
  55. /**
  56. * Set silence detector name to identify the particular silence detector
  57. * instance in the log.
  58. *
  59. * @param sd The silence detector.
  60. * @param name Name.
  61. *
  62. * @return PJ_SUCCESS on success.
  63. */
  64. PJ_DECL(pj_status_t) pjmedia_silence_det_set_name(pjmedia_silence_det *sd,
  65. const char *name);
  66. /**
  67. * Set the sd to operate in fixed threshold mode. With fixed threshold mode,
  68. * the threshold will not be changed adaptively.
  69. *
  70. * @param sd The silence detector
  71. * @param threshold The silence threshold, or -1 to use default
  72. * threshold.
  73. *
  74. * @return PJ_SUCCESS on success.
  75. */
  76. PJ_DECL(pj_status_t) pjmedia_silence_det_set_fixed( pjmedia_silence_det *sd,
  77. int threshold );
  78. /**
  79. * Set the sd to operate in adaptive mode. This is the default mode
  80. * when the silence detector is created.
  81. *
  82. * @param sd The silence detector
  83. * @param threshold Initial threshold to be set, or -1 to use default
  84. * threshold.
  85. *
  86. * @return PJ_SUCCESS on success.
  87. */
  88. PJ_DECL(pj_status_t) pjmedia_silence_det_set_adaptive(pjmedia_silence_det *sd,
  89. int threshold);
  90. /**
  91. * Set other silence detector parameters.
  92. *
  93. * @param sd The silence detector
  94. * @param before_silence Minimum duration of silence (in msec) before
  95. * silence is reported. If -1 is specified, then
  96. * the default value will be used. The default is
  97. * 400 msec.
  98. * @param recalc_time1 The interval (in msec) to recalculate threshold
  99. * in non-silence condition when adaptive silence
  100. * detection is set. If -1 is specified, then the
  101. * default value will be used. The default is 4000
  102. * (msec).
  103. * @param recalc_time2 The interval (in msec) to recalculate threshold
  104. * in silence condition when adaptive silence detection
  105. * is set. If -1 is specified, then the default value
  106. * will be used. The default value is 2000 (msec).
  107. *
  108. * @return PJ_SUCCESS on success.
  109. */
  110. PJ_DECL(pj_status_t) pjmedia_silence_det_set_params( pjmedia_silence_det *sd,
  111. int before_silence,
  112. int recalc_time1,
  113. int recalc_time2);
  114. /**
  115. * Disable the silence detector.
  116. *
  117. * @param sd The silence detector
  118. *
  119. * @return PJ_SUCCESS on success.
  120. */
  121. PJ_DECL(pj_status_t) pjmedia_silence_det_disable( pjmedia_silence_det *sd );
  122. /**
  123. * Perform voice activity detection on the given input samples. This
  124. * function uses #pjmedia_calc_avg_signal() and #pjmedia_silence_det_apply()
  125. * for its calculation.
  126. *
  127. * @param sd The silence detector instance.
  128. * @param samples Pointer to 16-bit PCM input samples.
  129. * @param count Number of samples in the input.
  130. * @param p_level Optional pointer to receive average signal level
  131. * of the input samples.
  132. *
  133. * @return Non zero if signal is silence.
  134. */
  135. PJ_DECL(pj_bool_t) pjmedia_silence_det_detect( pjmedia_silence_det *sd,
  136. const pj_int16_t samples[],
  137. pj_size_t count,
  138. pj_int32_t *p_level);
  139. /**
  140. * Calculate average signal level for the given samples.
  141. *
  142. * @param samples Pointer to 16-bit PCM samples.
  143. * @param count Number of samples in the input.
  144. *
  145. * @return The average signal level, which simply is total level
  146. * divided by number of samples.
  147. */
  148. PJ_DECL(pj_int32_t) pjmedia_calc_avg_signal( const pj_int16_t samples[],
  149. pj_size_t count );
  150. /**
  151. * Perform voice activity detection, given the specified average signal
  152. * level.
  153. *
  154. * @param sd The silence detector instance.
  155. * @param level Signal level.
  156. *
  157. * @return Non zero if signal is silence.
  158. */
  159. PJ_DECL(pj_bool_t) pjmedia_silence_det_apply( pjmedia_silence_det *sd,
  160. pj_uint32_t level);
  161. PJ_END_DECL
  162. /**
  163. * @}
  164. */
  165. #endif /* __PJMEDIA_SILENCE_DET_H__ */