CalculateAudio.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #ifndef _CALULATEAUDIO_H_
  2. #define _CALULATEAUDIO_H_
  3. #include <string>
  4. #include "GlobalVariable.h"
  5. #include "ChannelParam.h"
  6. #include "RingQueueManualMutex.hpp"
  7. struct OneSecondData;
  8. /**
  9. * @brief 音频数据数量信息
  10. *
  11. */
  12. struct StAudioNum
  13. {
  14. StAudioNum();
  15. StAudioNum(const StAudioNum& obj) { *this = obj; }
  16. SoundCardRoadInfo_t roadInfo; /* 通道ID */
  17. int nTotal; /* 总数据包数 */
  18. int nLDataNum; // 音频数据正数个数
  19. int nLUnDataNum; // 音频数据负数个数
  20. int nRDataNum; // 音频数据正数个数
  21. int nRUnDataNum; // 音频数据负数个数
  22. /* 判断条件 */
  23. int pcmErrorPercent = 0; // 音频不对称百分比
  24. int pcmLessPercent = 0; // 音频少于百分比
  25. // std::string strRoadName; // 通道名称
  26. StAudioNum& operator=(const StAudioNum& obj);
  27. bool AudioIsError(int nDataNum, int nUnDataNum, bool bLeft);
  28. // 有问题的音频
  29. bool IsErrorAudio();
  30. };
  31. /**
  32. * @brief 计算DB和反相的类
  33. *
  34. */
  35. class CAudio2ChanCorrelator
  36. {
  37. public:
  38. CAudio2ChanCorrelator();
  39. virtual ~CAudio2ChanCorrelator();
  40. public:
  41. // correlate data contained in A & B buffers
  42. int CorrelateChunks(const short * ptrBufferA, const short * ptrBufferB, int nBufferLength,
  43. short* pMaxA, short* pMaxB,
  44. short* pRMSA, short* pRMSB,
  45. StAudioNum &audioInfo);
  46. int CorrelateChunks(const short * ptrBufferA, const short * ptrBufferB, int nBufferLength,
  47. short* pMaxA, short* pMaxB,
  48. short* pRMSA, short* pRMSB,
  49. short* pLRAvg);
  50. // returns current correlation value (between -100 to 100)
  51. int GetCorrelationLevel(void);
  52. private:
  53. int m_nDisplayCutOffLevel; // levels below this value will not be processed
  54. bool m_bSignA; // phase of A sample
  55. float m_fCorrelationSum; // holds the summation of the correlation process
  56. int m_nCorrelationValue; // current correlation value
  57. float m_fDampingFactor; // value between 0 and 1
  58. };
  59. /*--------------------------------------------------------------------------
  60. * 全局函数定义
  61. *--------------------------------------------------------------------------*/
  62. /* 计算DB的全局函数 */
  63. int calculateDB(short val);
  64. /*--------------------------------------------------------------------------
  65. * 计算音量的静音、过载、反相等功能的类
  66. *--------------------------------------------------------------------------*/
  67. /**
  68. * @brief 封装一层音量计算需要的函数,方便计算
  69. *
  70. */
  71. class CaculateDBData
  72. {
  73. public:
  74. CaculateDBData();
  75. ~CaculateDBData();
  76. /* 是否有数据 */
  77. bool isEmpty() const { return ringQueue.isEmpty(); }
  78. /* 设置偏移值 */
  79. void setOffset(long offset);
  80. /* 计算静音 */
  81. bool calculateSilent(const StVolumeParam& param, const int avgDBSeconds, int& startPos, int& endPos);
  82. /* 计算过载 */
  83. bool calculateOverload(const StVolumeParam& param, const int avgDBSeconds, int& startPos, int& endPos, bool isLastOverload);
  84. /* 计算反相 */
  85. bool calculatePhase(const StVolumeParam& param, const int avgDBSeconds, int& startPos, int& endPos, bool isLastReversed);
  86. /* 判断平均音量是否低于设置的值 */
  87. bool isAvgDBLessThan(const long minDBLongTime, const long minDB);
  88. /* 计算正弦波是否是噪音,正弦波几秒直接判断为噪音 */
  89. bool isSinDB(const int sinSeconds, const int calSeconds, int& leftDB, int& rightDB);
  90. RingQueueManualMutex<OneSecondData*> ringQueue;
  91. private:
  92. /* 偏移值,数据只访问最新的数据,所以这个是向前偏移 */
  93. int m_offset = 0;
  94. };
  95. #endif // _CALULATEAUDIO_H_