silencedet.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #ifndef __SILENCEDET_H__
  2. #define __SILENCEDET_H__
  3. #pragma once
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. /**
  8. * @brief Adaptive Silence Detector
  9. */
  10. typedef struct silence_det_t silence_det_t;
  11. /**
  12. * Create voice activity detector with default settings. The default settings
  13. * are set to adaptive silence detection with the default threshold.
  14. *
  15. * @param pool Pool for allocating the structure.
  16. * @param clock_rate Clock rate.
  17. * @param samples_per_frame Number of samples per frame. The clock_rate and
  18. * samples_per_frame is only used to calculate the
  19. * frame time, from which some timing parameters
  20. * are calculated from.
  21. * @param p_sd Pointer to receive the silence detector instance.
  22. *
  23. * @return 0 on success.
  24. */
  25. int silence_det_create(unsigned clock_rate,unsigned samples_per_frame,silence_det_t **p_sd );
  26. void silence_det_destory(silence_det_t *sd);
  27. /**
  28. * Set the sd to operate in fixed threshold mode. With fixed threshold mode,
  29. * the threshold will not be changed adaptively.
  30. *
  31. * @param sd The silence detector
  32. * @param threshold The silence threshold, or -1 to use default
  33. * threshold.
  34. *
  35. * @return 0 on success.
  36. */
  37. int silence_det_set_fixed( silence_det_t *sd,int threshold );
  38. /**
  39. * Set the sd to operate in adaptive mode. This is the default mode
  40. * when the silence detector is created.
  41. *
  42. * @param sd The silence detector
  43. * @param threshold Initial threshold to be set, or -1 to use default
  44. * threshold.
  45. *
  46. * @return 0 on success.
  47. */
  48. int silence_det_set_adaptive(silence_det_t *sd,int threshold);
  49. /**
  50. * Set other silence detector parameters.
  51. *
  52. * @param sd The silence detector
  53. * @param before_silence Minimum duration of silence (in msec) before
  54. * silence is reported. If -1 is specified, then
  55. * the default value will be used. The default is
  56. * 400 msec.
  57. * @param recalc_time1 The interval (in msec) to recalculate threshold
  58. * in non-silence condition when adaptive silence
  59. * detection is set. If -1 is specified, then the
  60. * default value will be used. The default is 4000
  61. * (msec).
  62. * @param recalc_time2 The interval (in msec) to recalculate threshold
  63. * in silence condition when adaptive silence detection
  64. * is set. If -1 is specified, then the default value
  65. * will be used. The default value is 2000 (msec).
  66. *
  67. * @return 0 on success.
  68. */
  69. int silence_det_set_params( silence_det_t *sd,int before_silence,int recalc_time1,int recalc_time2);
  70. /**
  71. * Disable the silence detector.
  72. *
  73. * @param sd The silence detector
  74. *
  75. * @return 0 on success.
  76. */
  77. int silence_det_disable( silence_det_t *sd );
  78. /**
  79. * Perform voice activity detection on the given input samples. This
  80. * function uses #calc_avg_signal() and #silence_det_apply()
  81. * for its calculation.
  82. *
  83. * @param sd The silence detector instance.
  84. * @param samples Pointer to 16-bit PCM input samples.
  85. * @param count Number of samples in the input.
  86. * @param p_level Optional pointer to receive average signal level
  87. * of the input samples.
  88. *
  89. * @return Non zero if signal is silence.
  90. */
  91. int silence_det_detect( silence_det_t *sd,
  92. const short samples[],
  93. size_t count,
  94. int *p_level);
  95. /**
  96. * Calculate average signal level for the given samples.
  97. *
  98. * @param samples Pointer to 16-bit PCM samples.
  99. * @param count Number of samples in the input.
  100. *
  101. * @return The average signal level, which simply is total level
  102. * divided by number of samples.
  103. */
  104. int calc_avg_signal( const short samples[],size_t count );
  105. /**
  106. * Perform voice activity detection, given the specified average signal
  107. * level.
  108. *
  109. * @param sd The silence detector instance.
  110. * @param level Signal level.
  111. *
  112. * @return Non zero if signal is silence.
  113. */
  114. int silence_det_apply( silence_det_t *sd,unsigned int level);
  115. #ifdef __cplusplus
  116. } // extern "C" {
  117. #endif
  118. #endif //__SILENCEDET_H__