CalculateAudio.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #ifndef _CALULATEAUDIO_H_
  2. #define _CALULATEAUDIO_H_
  3. #include <string>
  4. #include "GlobalVariable.h"
  5. #include "RingQueueManualMutex.hpp"
  6. struct OneSecondData;
  7. /* =========================================================================================
  8. * 宏定义
  9. * =========================================================================================*/
  10. // 1秒钟30个音量值,相位值同样是30个
  11. #define VOLUME_INFO_NUM 30
  12. // 音量有效范围
  13. #define VOLUME_MAX_BD 0
  14. #define VOLUME_MIN_BD -90
  15. // 反相值范围
  16. #define REVERSED_MAX_VALUE 100
  17. #define REVERSED_MIN_VALUE -100
  18. // 数据库字符串长度
  19. #define DB_VARCHAR_LEN 32
  20. // 一次比较最多可得到多少个值(相似度)
  21. #define RESULT_NUM_OF_ONETIME_COMPARE 1
  22. /************************** 缩放比较相关宏 **************************/
  23. // 静音默认值
  24. #define SILENCE_DEF_VALUE true
  25. // 最大缓存180秒,3分钟
  26. #define CACHE_DATA_SECOND_MAX 180
  27. /**
  28. * @brief 音量设置参数
  29. *
  30. */
  31. struct StVolumeParam
  32. {
  33. StVolumeParam();
  34. StVolumeParam(const StVolumeParam& obj);
  35. StVolumeParam& operator=(const StVolumeParam& obj);
  36. void Init();
  37. /* -------------------- 静音 -------------------- */
  38. void SetSilentSwitch(bool b) { m_bSilentSwitch = b; }
  39. // 静音持续时间为0不进行静音判定
  40. bool GetSilentSwitch() const { return m_bSilentSwitch && m_iSilentDuration > 0; }
  41. int GetSilentThreshold() const { return m_iSilentThreshold; }
  42. void SetSilentThreshold(int ival) { m_iSilentThreshold = ival; }
  43. int GetSilentDuration() const { return m_iSilentDuration; }
  44. void SetSilentDuration(int ival) { m_iSilentDuration = ival; }
  45. int GetSilentNum() const;
  46. int GetSilentSensitivity() const { return m_nSilentSensitivity; }
  47. void SetSilentSensitivity(int ival) { m_nSilentSensitivity = ival; }
  48. /* -------------------- 过载 -------------------- */
  49. void SetOverloadSwitch(bool b) { m_bOverloadSwitch = b; }
  50. bool GetOverloadSwitch() const { return m_bOverloadSwitch && m_iOverloadDuration > 0; }
  51. int GetOverloadThreshold() const { return m_iOverloadThreshold; }
  52. void SetOverloadThreshold(int ival) { m_iOverloadThreshold = ival; }
  53. int GetOverloadDuration() const { return m_iOverloadDuration; }
  54. void SetOverloadDuration(int ival) { m_iOverloadDuration = ival; }
  55. int GetOverloadSensitivity() const { return m_nOverloadSensitivity; }
  56. void SetOverloadSensitivity(int ival) { m_nOverloadSensitivity = ival; }
  57. int GetOverloadNum() const;
  58. /* -------------------- 反相 -------------------- */
  59. void SetPhaseSwitch(bool b) { m_bPhaseSwitch = b; }
  60. bool GetPhaseSwitch() const { return m_bPhaseSwitch && m_iPhaseSensitivity > 0; }
  61. float GetPhaseThreshold() const { return m_iPhaseThreshold; }
  62. void SetPhaseThreshold(float ival) { m_iPhaseThreshold = ival; }
  63. int GetPhaseDuration() const { return m_iPhaseDuration; }
  64. void SetPhaseDuration(int ival) { m_iPhaseDuration = ival; }
  65. int GetPhaseSensitivity() const { return m_iPhaseSensitivity; }
  66. void SetPhaseSensitivity(int ival) { m_iPhaseSensitivity = ival; }
  67. int GetPhaseNum() const;
  68. int GetNoiseDuration() const { return 3; }
  69. private:
  70. /* -------------------- 静音 -------------------- */
  71. // 静音监测开关
  72. bool m_bSilentSwitch;
  73. int m_iSilentThreshold;
  74. // 持续时间(秒)
  75. int m_iSilentDuration;
  76. // 静音灵敏度(百分比)
  77. int m_nSilentSensitivity;
  78. /* -------------------- 过载 -------------------- */
  79. // 过载监测开关
  80. bool m_bOverloadSwitch;
  81. int m_iOverloadThreshold;
  82. // 持续时间(秒)
  83. int m_iOverloadDuration;
  84. // 过载灵敏度(百分比)
  85. int m_nOverloadSensitivity;
  86. /* -------------------- 反相 -------------------- */
  87. // 反相监测开关
  88. bool m_bPhaseSwitch;
  89. // 反相阀值(-1.0 – 1.0)
  90. float m_iPhaseThreshold;
  91. // 持续时间(秒)
  92. int m_iPhaseDuration;
  93. // 反相灵敏度(0 - 100%)
  94. int m_iPhaseSensitivity;
  95. };
  96. /**
  97. * @brief 音频数据数量信息
  98. *
  99. */
  100. struct StAudioNum
  101. {
  102. StAudioNum();
  103. StAudioNum(const StAudioNum& obj) { *this = obj; }
  104. SoundCardRoadInfo_t roadInfo; /* 通道ID */
  105. int nTotal; /* 总数据包数 */
  106. int nLDataNum; // 音频数据正数个数
  107. int nLUnDataNum; // 音频数据负数个数
  108. int nRDataNum; // 音频数据正数个数
  109. int nRUnDataNum; // 音频数据负数个数
  110. /* 判断条件 */
  111. int pcmErrorPercent = 0; // 音频不对称百分比
  112. int pcmLessPercent = 0; // 音频少于百分比
  113. // std::string strRoadName; // 通道名称
  114. StAudioNum& operator=(const StAudioNum& obj);
  115. bool AudioIsError(int nDataNum, int nUnDataNum, bool bLeft);
  116. // 有问题的音频
  117. bool IsErrorAudio();
  118. };
  119. /**
  120. * @brief 计算DB和反相的类
  121. *
  122. */
  123. class CAudio2ChanCorrelator
  124. {
  125. public:
  126. CAudio2ChanCorrelator();
  127. virtual ~CAudio2ChanCorrelator();
  128. public:
  129. // correlate data contained in A & B buffers
  130. int CorrelateChunks(const short * ptrBufferA, const short * ptrBufferB, int nBufferLength,
  131. short* pMaxA, short* pMaxB,
  132. short* pRMSA, short* pRMSB,
  133. StAudioNum &audioInfo);
  134. int CorrelateChunks(const short * ptrBufferA, const short * ptrBufferB, int nBufferLength,
  135. short* pMaxA, short* pMaxB,
  136. short* pRMSA, short* pRMSB,
  137. short* pLRAvg);
  138. // returns current correlation value (between -100 to 100)
  139. int GetCorrelationLevel(void);
  140. private:
  141. int m_nDisplayCutOffLevel; // levels below this value will not be processed
  142. bool m_bSignA; // phase of A sample
  143. float m_fCorrelationSum; // holds the summation of the correlation process
  144. int m_nCorrelationValue; // current correlation value
  145. float m_fDampingFactor; // value between 0 and 1
  146. };
  147. /*--------------------------------------------------------------------------
  148. * 全局函数定义
  149. *--------------------------------------------------------------------------*/
  150. /* 计算DB的全局函数 */
  151. int calculateDB(short val);
  152. /*--------------------------------------------------------------------------
  153. * 计算音量的静音、过载、反相等功能的类
  154. *--------------------------------------------------------------------------*/
  155. /**
  156. * @brief 封装一层音量计算需要的函数,方便计算
  157. *
  158. */
  159. class CaculateDBData
  160. {
  161. public:
  162. CaculateDBData();
  163. ~CaculateDBData();
  164. /* 是否有数据 */
  165. bool isEmpty() const { return ringQueue.isEmpty(); }
  166. /* 设置偏移值 */
  167. void setOffset(long offset);
  168. /* 计算静音 */
  169. bool calculateSilent(const StVolumeParam& param, int& startPos, int& endPos);
  170. /* 计算过载 */
  171. bool calculateOverload(const StVolumeParam& param, int& startPos, int& endPos, bool isLastOverload);
  172. /* 计算反相 */
  173. bool calculatePhase(const StVolumeParam& param, int& startPos, int& endPos, bool isLastReversed);
  174. /* 判断平均音量是否低于设置的值 */
  175. bool isAvgDBLessThan(const long minDBLongTime, const long minDB);
  176. /* 计算正弦波是否是噪音,正弦波几秒直接判断为噪音 */
  177. bool isSinDB(const int sinSeconds, const int calSeconds, int& leftDB, int& rightDB);
  178. RingQueueManualMutex<OneSecondData*> ringQueue;
  179. private:
  180. /* 偏移值,数据只访问最新的数据,所以这个是向前偏移 */
  181. int m_offset = 0;
  182. };
  183. #endif // _CALULATEAUDIO_H_