123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- #ifndef _CALULATEAUDIO_H_
- #define _CALULATEAUDIO_H_
- #include <string>
- #include "GlobalVariable.h"
- #include "RingQueueManualMutex.hpp"
- struct OneSecondData;
- /* =========================================================================================
- * 宏定义
- * =========================================================================================*/
- // 1秒钟30个音量值,相位值同样是30个
- #define VOLUME_INFO_NUM 30
- // 音量有效范围
- #define VOLUME_MAX_BD 0
- #define VOLUME_MIN_BD -90
- // 反相值范围
- #define REVERSED_MAX_VALUE 100
- #define REVERSED_MIN_VALUE -100
- // 数据库字符串长度
- #define DB_VARCHAR_LEN 32
- // 一次比较最多可得到多少个值(相似度)
- #define RESULT_NUM_OF_ONETIME_COMPARE 1
- /************************** 缩放比较相关宏 **************************/
- // 静音默认值
- #define SILENCE_DEF_VALUE true
- // 最大缓存180秒,3分钟
- #define CACHE_DATA_SECOND_MAX 180
- /**
- * @brief 音量设置参数
- *
- */
- struct StVolumeParam
- {
- StVolumeParam();
- StVolumeParam(const StVolumeParam& obj);
- StVolumeParam& operator=(const StVolumeParam& obj);
- void Init();
- /* -------------------- 静音 -------------------- */
- void SetSilentSwitch(bool b) { m_bSilentSwitch = b; }
- // 静音持续时间为0不进行静音判定
- bool GetSilentSwitch() const { return m_bSilentSwitch && m_iSilentDuration > 0; }
- int GetSilentThreshold() const { return m_iSilentThreshold; }
- void SetSilentThreshold(int ival) { m_iSilentThreshold = ival; }
- int GetSilentDuration() const { return m_iSilentDuration; }
- void SetSilentDuration(int ival) { m_iSilentDuration = ival; }
- int GetSilentNum() const;
- int GetSilentSensitivity() const { return m_nSilentSensitivity; }
- void SetSilentSensitivity(int ival) { m_nSilentSensitivity = ival; }
- /* -------------------- 过载 -------------------- */
- void SetOverloadSwitch(bool b) { m_bOverloadSwitch = b; }
- bool GetOverloadSwitch() const { return m_bOverloadSwitch && m_iOverloadDuration > 0; }
- int GetOverloadThreshold() const { return m_iOverloadThreshold; }
- void SetOverloadThreshold(int ival) { m_iOverloadThreshold = ival; }
- int GetOverloadDuration() const { return m_iOverloadDuration; }
- void SetOverloadDuration(int ival) { m_iOverloadDuration = ival; }
- int GetOverloadSensitivity() const { return m_nOverloadSensitivity; }
- void SetOverloadSensitivity(int ival) { m_nOverloadSensitivity = ival; }
- int GetOverloadNum() const;
- /* -------------------- 反相 -------------------- */
- void SetPhaseSwitch(bool b) { m_bPhaseSwitch = b; }
- bool GetPhaseSwitch() const { return m_bPhaseSwitch && m_iPhaseSensitivity > 0; }
- float GetPhaseThreshold() const { return m_iPhaseThreshold; }
- void SetPhaseThreshold(float ival) { m_iPhaseThreshold = ival; }
- int GetPhaseDuration() const { return m_iPhaseDuration; }
- void SetPhaseDuration(int ival) { m_iPhaseDuration = ival; }
- int GetPhaseSensitivity() const { return m_iPhaseSensitivity; }
- void SetPhaseSensitivity(int ival) { m_iPhaseSensitivity = ival; }
- int GetPhaseNum() const;
- int GetNoiseDuration() const { return 3; }
- private:
- /* -------------------- 静音 -------------------- */
- // 静音监测开关
- bool m_bSilentSwitch;
- int m_iSilentThreshold;
- // 持续时间(秒)
- int m_iSilentDuration;
- // 静音灵敏度(百分比)
- int m_nSilentSensitivity;
- /* -------------------- 过载 -------------------- */
- // 过载监测开关
- bool m_bOverloadSwitch;
- int m_iOverloadThreshold;
- // 持续时间(秒)
- int m_iOverloadDuration;
- // 过载灵敏度(百分比)
- int m_nOverloadSensitivity;
- /* -------------------- 反相 -------------------- */
- // 反相监测开关
- bool m_bPhaseSwitch;
- // 反相阀值(-1.0 – 1.0)
- float m_iPhaseThreshold;
- // 持续时间(秒)
- int m_iPhaseDuration;
- // 反相灵敏度(0 - 100%)
- int m_iPhaseSensitivity;
- };
- /**
- * @brief 音频数据数量信息
- *
- */
- struct StAudioNum
- {
- StAudioNum();
- StAudioNum(const StAudioNum& obj) { *this = obj; }
- SoundCardRoadInfo_t roadInfo; /* 通道ID */
- int nTotal; /* 总数据包数 */
- int nLDataNum; // 音频数据正数个数
- int nLUnDataNum; // 音频数据负数个数
- int nRDataNum; // 音频数据正数个数
- int nRUnDataNum; // 音频数据负数个数
- /* 判断条件 */
- int pcmErrorPercent = 0; // 音频不对称百分比
- int pcmLessPercent = 0; // 音频少于百分比
- // std::string strRoadName; // 通道名称
- StAudioNum& operator=(const StAudioNum& obj);
- bool AudioIsError(int nDataNum, int nUnDataNum, bool bLeft);
- // 有问题的音频
- bool IsErrorAudio();
- };
- /**
- * @brief 计算DB和反相的类
- *
- */
- class CAudio2ChanCorrelator
- {
- public:
- CAudio2ChanCorrelator();
- virtual ~CAudio2ChanCorrelator();
- public:
- // correlate data contained in A & B buffers
- int CorrelateChunks(const short * ptrBufferA, const short * ptrBufferB, int nBufferLength,
- short* pMaxA, short* pMaxB,
- short* pRMSA, short* pRMSB,
- StAudioNum &audioInfo);
- int CorrelateChunks(const short * ptrBufferA, const short * ptrBufferB, int nBufferLength,
- short* pMaxA, short* pMaxB,
- short* pRMSA, short* pRMSB,
- short* pLRAvg);
- // returns current correlation value (between -100 to 100)
- int GetCorrelationLevel(void);
- private:
- int m_nDisplayCutOffLevel; // levels below this value will not be processed
- bool m_bSignA; // phase of A sample
- float m_fCorrelationSum; // holds the summation of the correlation process
- int m_nCorrelationValue; // current correlation value
- float m_fDampingFactor; // value between 0 and 1
- };
- /*--------------------------------------------------------------------------
- * 全局函数定义
- *--------------------------------------------------------------------------*/
- /* 计算DB的全局函数 */
- int calculateDB(short val);
- /*--------------------------------------------------------------------------
- * 计算音量的静音、过载、反相等功能的类
- *--------------------------------------------------------------------------*/
- /**
- * @brief 封装一层音量计算需要的函数,方便计算
- *
- */
- class CaculateDBData
- {
- public:
- CaculateDBData();
- ~CaculateDBData();
- /* 是否有数据 */
- bool isEmpty() const { return ringQueue.isEmpty(); }
- /* 设置偏移值 */
- void setOffset(long offset);
- /* 计算静音 */
- bool calculateSilent(const StVolumeParam& param, int& startPos, int& endPos);
- /* 计算过载 */
- bool calculateOverload(const StVolumeParam& param, int& startPos, int& endPos, bool isLastOverload);
- /* 计算反相 */
- bool calculatePhase(const StVolumeParam& param, int& startPos, int& endPos, bool isLastReversed);
- /* 判断平均音量是否低于设置的值 */
- bool isAvgDBLessThan(const long minDBLongTime, const long minDB);
- /* 计算正弦波是否是噪音,正弦波几秒直接判断为噪音 */
- bool isSinDB(const int sinSeconds, const int calSeconds, int& leftDB, int& rightDB);
- RingQueueManualMutex<OneSecondData*> ringQueue;
- private:
- /* 偏移值,数据只访问最新的数据,所以这个是向前偏移 */
- int m_offset = 0;
- };
- #endif // _CALULATEAUDIO_H_
|