CalculateAudio.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. #include "CalculateAudio.h"
  2. #include "spdlog/spdlog.h"
  3. #include "AudioData.h"
  4. #include <string>
  5. /* --------------------------------------------------------------------------------
  6. * 音量设置相关参数
  7. * -------------------------------------------------------------------------------- */
  8. StVolumeParam::StVolumeParam()
  9. {
  10. Init();
  11. }
  12. void StVolumeParam::Init()
  13. {
  14. m_bSilentSwitch = false;
  15. m_iSilentThreshold = 0;
  16. m_iSilentDuration = 0;
  17. m_nSilentSensitivity = 80;
  18. m_bOverloadSwitch = false;
  19. m_iOverloadThreshold = 0;
  20. m_iOverloadDuration = 0;
  21. // 100% 全部过载才表示过载
  22. m_nOverloadSensitivity = 40;
  23. m_bPhaseSwitch = false;
  24. m_iPhaseThreshold = 0.0;
  25. m_iPhaseDuration = 0;
  26. m_iPhaseSensitivity = 40;
  27. }
  28. StVolumeParam::StVolumeParam(const StVolumeParam& obj)
  29. {
  30. *this = obj;
  31. }
  32. StVolumeParam& StVolumeParam::operator=(const StVolumeParam& obj)
  33. {
  34. m_bSilentSwitch = obj.m_bSilentSwitch;
  35. m_iSilentThreshold = obj.m_iSilentThreshold;
  36. m_iSilentDuration = obj.m_iSilentDuration;
  37. m_nSilentSensitivity = obj.m_nSilentSensitivity;
  38. m_bOverloadSwitch = obj.m_bOverloadSwitch;
  39. m_iOverloadThreshold = obj.m_iOverloadThreshold;
  40. m_iOverloadDuration = obj.m_iOverloadDuration;
  41. m_nOverloadSensitivity = obj.m_nOverloadSensitivity;
  42. m_bPhaseSwitch = obj.m_bPhaseSwitch;
  43. m_iPhaseThreshold = obj.m_iPhaseThreshold;
  44. m_iPhaseDuration = obj.m_iPhaseDuration;
  45. m_iPhaseSensitivity = obj.m_iPhaseSensitivity;
  46. return *this;
  47. }
  48. int StVolumeParam::GetSilentNum() const
  49. {
  50. // return m_iSilentDuration * GInfo.silentSensitivity();
  51. return m_iSilentDuration * m_nSilentSensitivity;
  52. }
  53. int StVolumeParam::GetOverloadNum() const
  54. {
  55. // return m_iOverloadDuration * GInfo.overloadSensitivity();
  56. return m_iOverloadDuration * m_nOverloadSensitivity;
  57. }
  58. int StVolumeParam::GetPhaseNum() const
  59. {
  60. // return m_iPhaseDuration * GInfo.iPhaseSensitivity();
  61. return m_iPhaseDuration * m_iPhaseSensitivity;
  62. }
  63. StAudioNum::StAudioNum()
  64. {
  65. nTotal = 0;
  66. nLDataNum = 0;
  67. nLUnDataNum = 0;
  68. nRDataNum = 0;
  69. nRUnDataNum = 0;
  70. }
  71. StAudioNum& StAudioNum::operator=(const StAudioNum& obj)
  72. {
  73. roadInfo = obj.roadInfo;
  74. nTotal = obj.nTotal;
  75. nLDataNum = obj.nLDataNum;
  76. nLUnDataNum = obj.nLUnDataNum;
  77. nRDataNum = obj.nRDataNum;
  78. nRUnDataNum = obj.nRUnDataNum;
  79. return *this;
  80. }
  81. bool StAudioNum::AudioIsError(int nDataNum, int nUnDataNum, bool bLeft)
  82. {
  83. std::string strRoadName = roadInfo.strSoundCardName.toStdString() + ":" + std::to_string(roadInfo.roadInfo.nRoadNum);
  84. if (nDataNum > 0 && nUnDataNum > 0 && pcmErrorPercent > 0)
  85. {
  86. int nMax = nDataNum > nUnDataNum ? nDataNum : nUnDataNum;
  87. int nMin = nDataNum < nUnDataNum ? nDataNum : nUnDataNum;
  88. nMin *= 100;
  89. int nPer = nMin / nMax;
  90. if (nPer < pcmErrorPercent )
  91. {
  92. if (bLeft)
  93. {
  94. SPDLOG_INFO("通道{}: 左声道数据不对称,判断为噪音, Left: {}, {}", strRoadName, nLDataNum, nLUnDataNum);
  95. } else
  96. {
  97. SPDLOG_INFO("通道{}: 右声道数据不对称,判断为噪音, Right: {}, {}", strRoadName, nRDataNum, nRUnDataNum);
  98. }
  99. return true;
  100. }
  101. }
  102. return false;
  103. }
  104. // 有问题的音频
  105. bool StAudioNum::IsErrorAudio()
  106. {
  107. std::string strRoadName = roadInfo.strSoundCardName.toStdString() + ":" + std::to_string(roadInfo.roadInfo.nRoadNum);
  108. const int nMute = 200;
  109. if (nLDataNum > nMute || nLUnDataNum > nMute || nRDataNum > nMute || nRUnDataNum > nMute)
  110. {
  111. if (pcmLessPercent > 0)
  112. {
  113. const int nMinTotal = nTotal / pcmLessPercent;
  114. if ((nLDataNum > 0 || nLUnDataNum > 0) && (nLDataNum < nMinTotal || nLUnDataNum < nMinTotal) )
  115. {
  116. SPDLOG_INFO("通道{} : 左声道数据很少, Left: {}, {}, Right: {}, {}", strRoadName, nLDataNum, nLUnDataNum, nRDataNum, nRUnDataNum);
  117. // 不是静音,但数据很少,直接判断为有误
  118. return true;
  119. }
  120. if ((nRDataNum > 0 || nRUnDataNum > 0) && (nRDataNum < nMinTotal || nRUnDataNum < nMinTotal))
  121. {
  122. SPDLOG_INFO("通道{} : 右声道数据很少, Left: {}, {}, Right: {}, {}", strRoadName, nLDataNum, nLUnDataNum, nRDataNum, nRUnDataNum);
  123. // 不是静音,但数据很少,直接判断为有误
  124. return true;
  125. }
  126. }
  127. } else
  128. {
  129. SPDLOG_INFO("通道 {} : 静音了, Left: {}, {}, Right: {}, {}", strRoadName, nLDataNum, nLUnDataNum, nRDataNum, nRUnDataNum);
  130. return false; // 静音了
  131. }
  132. if (AudioIsError(nLDataNum, nLUnDataNum, true))
  133. {
  134. return true;
  135. }
  136. if (AudioIsError(nRDataNum, nRUnDataNum, false))
  137. {
  138. return true;
  139. }
  140. return false;
  141. }
  142. /*--------------------------------------------------------------------------
  143. * CAudio2ChanCorrelator 成员函数
  144. *--------------------------------------------------------------------------*/
  145. CAudio2ChanCorrelator::CAudio2ChanCorrelator()
  146. : m_nDisplayCutOffLevel(128) // 以前是64,音量为-50时反相不准确,所以修改为128 20150508 jinhl
  147. , m_bSignA(false)
  148. , m_fCorrelationSum(0.0f)
  149. , m_nCorrelationValue(0)
  150. , m_fDampingFactor(0.3f)
  151. {
  152. }
  153. CAudio2ChanCorrelator::~CAudio2ChanCorrelator()
  154. {
  155. }
  156. /**
  157. * @brief correlate data contained in A & B buffers ( range -100 to 100 )
  158. *
  159. * @param ptrBufferA
  160. * @param ptrBufferB
  161. * @param nBufferLength
  162. * @param pMaxA
  163. * @param pMaxB
  164. * @param pRMSA
  165. * @param pRMSB
  166. * @param audioInfo
  167. * @return int
  168. */
  169. int CAudio2ChanCorrelator::CorrelateChunks(const short * ptrBufferA,
  170. const short * ptrBufferB,
  171. int nBufferLength,
  172. short* pMaxA, short* pMaxB,
  173. short* pRMSA, short* pRMSB,
  174. StAudioNum &audioInfo)
  175. {
  176. int64_t nTotalA = 0, nTotlB = 0;
  177. *pMaxA = *pMaxB = *pRMSA = *pRMSB = 0;
  178. m_fCorrelationSum = 0; // reset correlation sum
  179. int nSamplesProcessedCount = 0;
  180. if( ptrBufferB )
  181. {
  182. for ( int i = 0; i < nBufferLength; i+=2)// run through the buffer
  183. {
  184. if( abs((int)*ptrBufferA) > *pMaxA )
  185. {
  186. *pMaxA = abs((int)*ptrBufferA);
  187. }
  188. nTotalA += (*ptrBufferA)*(*ptrBufferA);
  189. if( abs((int)*ptrBufferB) > *pMaxB )
  190. {
  191. *pMaxB = abs((int)*ptrBufferB);
  192. }
  193. nTotlB += (*ptrBufferB)*(*ptrBufferB);
  194. //test sample A against threshold and if above record it's sign
  195. if (*ptrBufferA < m_nDisplayCutOffLevel && *ptrBufferA > -m_nDisplayCutOffLevel)
  196. {
  197. ptrBufferA += 2;// increment buffer pointers for next sample in the loop
  198. ptrBufferB += 2;
  199. continue; //level A below display threshold, hence no more processing required
  200. }
  201. if (*ptrBufferA > m_nDisplayCutOffLevel )
  202. {
  203. // 金海林添加 2022-11-17 统计音频数据正负数
  204. ++audioInfo.nLDataNum;
  205. m_bSignA = true; // A sample is positive
  206. }
  207. else
  208. {
  209. // 金海林添加 2022-11-17 统计音频数据正负数
  210. ++audioInfo.nLUnDataNum;
  211. m_bSignA =false; // A sample is negative
  212. }
  213. //test sample B against threshold
  214. if (*ptrBufferB < m_nDisplayCutOffLevel && *ptrBufferB > -m_nDisplayCutOffLevel)
  215. {
  216. ptrBufferA += 2;// increment buffer pointers for next sample in the loop
  217. ptrBufferB += 2;
  218. continue; //level B below display threshold, hence no more processing required
  219. }
  220. if (*ptrBufferB > m_nDisplayCutOffLevel ) //B sample is positive
  221. {
  222. // 金海林添加 2022-11-17 统计音频数据正负数
  223. ++audioInfo.nRDataNum;
  224. if(m_bSignA == true) // A & B samples are positive
  225. m_fCorrelationSum++;
  226. else
  227. m_fCorrelationSum--; // A negative & B positive
  228. }
  229. else //B sample is negative
  230. {
  231. // 金海林添加 2022-11-17 统计音频数据正负数
  232. ++audioInfo.nRUnDataNum;
  233. if(m_bSignA == false) // A & B samples are negative
  234. m_fCorrelationSum++;
  235. else
  236. m_fCorrelationSum--; // A positive & B negative
  237. }
  238. // increment the buffers for next sample in the for loop
  239. ptrBufferA += 2;
  240. ptrBufferB += 2;
  241. nSamplesProcessedCount++;
  242. }//end of for loop
  243. m_nCorrelationValue = (int)(100 * (1- m_fDampingFactor) *m_fCorrelationSum/nSamplesProcessedCount + 1)
  244. + (int)(m_nCorrelationValue * m_fDampingFactor);
  245. *pRMSA = sqrt((long double)nTotalA/nBufferLength);
  246. *pRMSB = sqrt((long double)nTotlB/nBufferLength);
  247. }
  248. else
  249. {
  250. for ( int i = 0; i < nBufferLength; i++)// run through the buffer
  251. {
  252. if( abs((int)*ptrBufferA) > *pMaxA )
  253. {
  254. *pMaxA = abs((int)*ptrBufferA);
  255. }
  256. nTotalA += (*ptrBufferA)*(*ptrBufferA);
  257. // increment the buffers for next sample in the for loop
  258. ptrBufferA += 1;
  259. }//end of for loop
  260. m_nCorrelationValue = 100;
  261. *pRMSA = sqrt((long double)nTotalA/nBufferLength);
  262. }
  263. return m_nCorrelationValue;
  264. }
  265. int CAudio2ChanCorrelator::CorrelateChunks(const short * ptrBufferA,
  266. const short * ptrBufferB,
  267. int nBufferLength,
  268. short* pMaxA, short* pMaxB,
  269. short* pRMSA, short* pRMSB,
  270. short* pLRAvg) //L+R的平均值
  271. {
  272. int64_t nTotalA = 0, nTotlB = 0;
  273. *pMaxA = *pMaxB = *pRMSA = *pRMSB = *pLRAvg = 0;
  274. m_fCorrelationSum = 0; // reset correlation sum
  275. int nSamplesProcessedCount = 0;
  276. if( ptrBufferB )
  277. {
  278. for ( int i = 0; i < nBufferLength; i+=2)// run through the buffer
  279. {
  280. if( abs((int)*ptrBufferA) > *pMaxA )
  281. {
  282. *pMaxA = abs((int)*ptrBufferA);
  283. }
  284. nTotalA += (*ptrBufferA)*(*ptrBufferA);
  285. if( abs((int)*ptrBufferB) > *pMaxB )
  286. {
  287. *pMaxB = abs((int)*ptrBufferB);
  288. }
  289. nTotlB += (*ptrBufferB)*(*ptrBufferB);
  290. if( abs((int)*ptrBufferA+(int)*ptrBufferB)/2 > *pLRAvg )
  291. {
  292. *pLRAvg = abs((int)*ptrBufferA+(int)*ptrBufferB)/2;
  293. }
  294. //test sample A against threshold and if above record it's sign
  295. if (*ptrBufferA < m_nDisplayCutOffLevel && *ptrBufferA > -m_nDisplayCutOffLevel)
  296. {
  297. ptrBufferA += 2;// increment buffer pointers for next sample in the loop
  298. ptrBufferB += 2;
  299. continue; //level A below display threshold, hence no more processing required
  300. }
  301. if (*ptrBufferA > m_nDisplayCutOffLevel )
  302. m_bSignA = true; // A sample is positive
  303. else
  304. m_bSignA =false; // A sample is negative
  305. //test sample B against threshold
  306. if (*ptrBufferB < m_nDisplayCutOffLevel && *ptrBufferB > -m_nDisplayCutOffLevel)
  307. {
  308. ptrBufferA += 2;// increment buffer pointers for next sample in the loop
  309. ptrBufferB += 2;
  310. continue; //level B below display threshold, hence no more processing required
  311. }
  312. if (*ptrBufferB > m_nDisplayCutOffLevel ) //B sample is positive
  313. {
  314. if(m_bSignA == true) // A & B samples are positive
  315. m_fCorrelationSum++;
  316. else
  317. m_fCorrelationSum--; // A negative & B positive
  318. }
  319. else //B sample is negative
  320. {
  321. if(m_bSignA == false) // A & B samples are negative
  322. m_fCorrelationSum++;
  323. else
  324. m_fCorrelationSum--; // A positive & B negative
  325. }
  326. // increment the buffers for next sample in the for loop
  327. ptrBufferA += 2;
  328. ptrBufferB += 2;
  329. nSamplesProcessedCount++;
  330. }//end of for loop
  331. m_nCorrelationValue = (int)(100 * (1- m_fDampingFactor) *m_fCorrelationSum/nSamplesProcessedCount + 1)
  332. + (int)(m_nCorrelationValue * m_fDampingFactor);
  333. *pRMSA = sqrt((long double)nTotalA/nBufferLength);
  334. *pRMSB = sqrt((long double)nTotlB/nBufferLength);
  335. }
  336. else
  337. {
  338. for ( int i = 0; i < nBufferLength; i++)// run through the buffer
  339. {
  340. if( abs((int)*ptrBufferA) > *pMaxA )
  341. {
  342. *pMaxA = abs((int)*ptrBufferA);
  343. }
  344. nTotalA += (*ptrBufferA)*(*ptrBufferA);
  345. // increment the buffers for next sample in the for loop
  346. ptrBufferA += 1;
  347. }//end of for loop
  348. m_nCorrelationValue = 100;
  349. *pRMSA = sqrt((long double)nTotalA/nBufferLength);
  350. }
  351. return m_nCorrelationValue;
  352. }
  353. /**
  354. * @brief returns current correlation value (between -100 to 100)
  355. *
  356. * @return int
  357. */
  358. int CAudio2ChanCorrelator::GetCorrelationLevel(void)
  359. {
  360. return m_nCorrelationValue;
  361. }
  362. /*--------------------------------------------------------------------------
  363. * 全局函数
  364. *--------------------------------------------------------------------------*/
  365. int calculateDB(short val)
  366. {
  367. if( val == 0 )
  368. {
  369. return -90;
  370. }
  371. if( val < 0 )
  372. {
  373. val = -val;
  374. }
  375. int iDB = ( 20*log10((long double)val) - 90.3 );
  376. if(iDB > VOLUME_MAX_BD || iDB < VOLUME_MIN_BD)
  377. {
  378. iDB = VOLUME_MIN_BD;
  379. }
  380. return iDB;
  381. }
  382. /*--------------------------------------------------------------------------
  383. * 计算音量的静音、过载、反相等功能的类
  384. *--------------------------------------------------------------------------*/
  385. CaculateDBData::CaculateDBData()
  386. {
  387. /* 初始化环形队列 */
  388. ringQueue.setQueueCapacity(CACHE_DATA_SECOND_MAX);
  389. ringQueue.setDefaultValue(nullptr);
  390. }
  391. CaculateDBData::~CaculateDBData()
  392. {
  393. if(!ringQueue.isEmpty())
  394. {
  395. OneSecondData* data = nullptr;
  396. while(!ringQueue.isEmpty())
  397. {
  398. data = ringQueue.front_pop();
  399. if(data)
  400. {
  401. delete data; // 释放内存
  402. data = nullptr;
  403. }
  404. }
  405. ringQueue.clearQueue(); // 清空队列
  406. }
  407. }
  408. /* 设置偏移值 */
  409. void CaculateDBData::setOffset(long offset)
  410. {
  411. /* 将ms转换成秒 */
  412. int offsetSeconds = offset / 1000;
  413. if(offsetSeconds < ringQueue.QueueSize())
  414. {
  415. m_offset = offsetSeconds;
  416. }
  417. }
  418. /**
  419. * @brief 根据参数计算静音的起始和结束位置
  420. *
  421. * @param param 这个频道的音量参数
  422. * @param avgDBSeconds 计算静音的平均音量秒数
  423. * @param startPos 开始位置
  424. * @param endPos 结束位置
  425. * @return true 静音
  426. * @return false 非静音
  427. */
  428. bool CaculateDBData::calculateSilent(const StVolumeParam& param, int& startPos, int& endPos)
  429. {
  430. startPos = -1;
  431. endPos = -1;
  432. if(!param.GetSilentSwitch())
  433. {
  434. return false;
  435. }
  436. const int silentDuration = param.GetSilentDuration();
  437. if (silentDuration >= CACHE_DATA_SECOND_MAX)
  438. {
  439. SPDLOG_WARN("静音持续时间 {}秒 不能大于等于最大缓存{}秒", silentDuration, CACHE_DATA_SECOND_MAX);
  440. return false;
  441. }
  442. if (ringQueue.QueueSize() < (silentDuration + m_offset) || silentDuration <= 0)
  443. {
  444. SPDLOG_DEBUG("队列中数据暂不满足静音持续时间检测 {}秒 的要求,当前队列大小为{}秒", silentDuration + m_offset, ringQueue.QueueSize());
  445. return false;
  446. }
  447. // if (silentDuration >= avgDBSeconds)
  448. // {
  449. // SPDLOG_WARN("静音持续时间({})不能大于等于{}秒", silentDuration, avgDBSeconds);
  450. // return false;
  451. // }
  452. int nNum = 0;
  453. /* 计算起始位置,获取最新的需要用到的silentDuration个数据,环形队列数据永远从0开始获取,
  454. * 如果需要获取最新的数据,那么需要从队列的尾部开始获取
  455. * 如果有偏移,那么起始点就是如下计算方式 */
  456. int queueSize = ringQueue.QueueSize();
  457. int startIndex = queueSize - silentDuration - m_offset;
  458. for(int j = startIndex; j < queueSize; ++j)
  459. {
  460. OneSecondData *data = ringQueue[j];
  461. if(data == nullptr)
  462. {
  463. continue;
  464. }
  465. if (data->CalcDBOneSecond(param))
  466. {
  467. endPos = j;
  468. if (startPos < 0)
  469. {
  470. startPos = j;
  471. }
  472. ++nNum;
  473. }
  474. }
  475. nNum = nNum * 100;
  476. // SPDLOG_TRACE("num: {}, threhold: {}, startPos: {}, endPos: {}", nNum, param.GetSilentNum(), startPos, endPos);
  477. if (nNum >= param.GetSilentNum())
  478. {
  479. return true;
  480. }else {
  481. return false;
  482. }
  483. }
  484. /**
  485. * @brief 计算过载
  486. *
  487. * @param param 计算参数
  488. * @param avgDBSeconds 计算音量值需要的秒数
  489. * @param startPos 过载开始位置
  490. * @param endPos 过载结束位置
  491. * @param lastReversed 上次是否是过载
  492. * @return true
  493. * @return false
  494. */
  495. bool CaculateDBData::calculateOverload(const StVolumeParam& param, int& startPos, int& endPos, bool bLastOverload)
  496. {
  497. startPos = -1;
  498. endPos = -1;
  499. if(!param.GetOverloadSwitch())
  500. {
  501. return false; // 没有开启过载检测
  502. }
  503. /* 获取需要计算的音量秒数 */
  504. const int calculateDuration = param.GetOverloadDuration();
  505. if (calculateDuration >= CACHE_DATA_SECOND_MAX)
  506. {
  507. SPDLOG_WARN("过载持续时间 {}秒 不能大于等于最大缓存 {}秒", calculateDuration, CACHE_DATA_SECOND_MAX);
  508. return false;
  509. }
  510. /* 判断队列中数据数量是否满足计算需要的数目 */
  511. if(ringQueue.QueueSize() < calculateDuration || calculateDuration <= 0)
  512. {
  513. SPDLOG_DEBUG("队列中数据暂不满足过载持续时间检测 {}秒 的要求,当前队列大小为 {}秒", calculateDuration, ringQueue.QueueSize());
  514. return false;
  515. }
  516. // if (calculateDuration >= avgDBSeconds)
  517. // {
  518. // SPDLOG_WARN("过载持续时间({})不能大于等于{}秒", calculateDuration, avgDBSeconds);
  519. // return false;
  520. // }
  521. /* 计算起始位置,获取最新的需要用到的 overloadDuration 个数据,环形队列数据永远从0开始获取,
  522. * 如果需要获取最新的数据,那么需要从队列的尾部开始获取 */
  523. int queueSize = ringQueue.QueueSize();
  524. int startIndex = queueSize - calculateDuration;
  525. // 第一秒和最后1秒都是过载的,如果中间有一秒不是过载的,那么中间这秒也仍然认为是过载的
  526. int nMidOverload = 0;
  527. int nOverloadNum = 0;
  528. for(int j = startIndex; j < queueSize; ++j)
  529. {
  530. OneSecondData *data = ringQueue[j];
  531. if(data == nullptr)
  532. {
  533. continue; // 没有获取到最新数据,继续等待
  534. }
  535. if (data->CalcOverloadOneSecond(param))
  536. {
  537. // 记录过载的起始位置
  538. if (startPos < 0)
  539. {
  540. startPos = j;
  541. }
  542. endPos = j;
  543. ++nOverloadNum;
  544. }
  545. else
  546. {
  547. if (j > 0 && j < calculateDuration - 1)
  548. {
  549. ++nMidOverload;
  550. }
  551. }
  552. //nOverloadNum += data->CalcOverload(param);
  553. }
  554. // 第一秒和最后1秒都是过载的,如果中间有一秒不是过载的,那么中间这秒也仍然认为是过载的
  555. if (1 == nMidOverload && calculateDuration == nOverloadNum + 1)
  556. {
  557. nOverloadNum = calculateDuration;
  558. }
  559. // 如果上一次是过载,那么允许后面的过载少一个
  560. if (bLastOverload && calculateDuration == nOverloadNum + 1)
  561. {
  562. nOverloadNum = calculateDuration;
  563. }
  564. nOverloadNum = nOverloadNum * 100;
  565. if (nOverloadNum >= param.GetOverloadNum())
  566. {
  567. return true;
  568. }else {
  569. return false;
  570. }
  571. }
  572. /* 计算反相 */
  573. bool CaculateDBData::calculatePhase(const StVolumeParam& param, int& startPos, int& endPos, bool isLastReversed)
  574. {
  575. startPos = -1;
  576. endPos = -1;
  577. if(!param.GetPhaseSwitch())
  578. {
  579. return false; // 没有开启反相检测
  580. }
  581. /* 获取需要计算的音量秒数 */
  582. const int calculateDuration = param.GetPhaseDuration();
  583. if (calculateDuration >= CACHE_DATA_SECOND_MAX)
  584. {
  585. SPDLOG_WARN("反相持续时间 {}秒 不能大于等于最大缓存{}秒", calculateDuration, CACHE_DATA_SECOND_MAX);
  586. return false;
  587. }
  588. /* 判断队列中数据数量是否满足计算需要的数目 */
  589. if(ringQueue.QueueSize() < calculateDuration || calculateDuration <= 0)
  590. {
  591. SPDLOG_DEBUG("队列中数据暂不满足反相持续时间检测 {}秒 的要求,当前队列大小为 {}秒", calculateDuration, ringQueue.QueueSize());
  592. return false;
  593. }
  594. // if (calculateDuration >= avgDBSeconds)
  595. // {
  596. // SPDLOG_WARN("反相持续时间({})不能大于等于{}秒", calculateDuration, avgDBSeconds);
  597. // return false;
  598. // }
  599. StPhase phaseData;
  600. //1、上一秒是否反相,如果是,当反相值比较小时也表示反相
  601. bool bLastTest = isLastReversed;
  602. /* 计算起始位置,获取最新的需要用到的 overloadDuration 个数据,环形队列数据永远从0开始获取,
  603. * 如果需要获取最新的数据,那么需要从队列的尾部开始获取 */
  604. int queueSize = ringQueue.QueueSize();
  605. int startIndex = queueSize - calculateDuration;
  606. for(int j = startIndex; j < queueSize; ++j)
  607. {
  608. OneSecondData *data = ringQueue[j];
  609. if(data == nullptr)
  610. {
  611. continue; // 没有获取到最新数据,继续等待
  612. }
  613. if (data->CalcPhaseOneSecond(param, bLastTest))
  614. {
  615. // 记录反相的起始位置
  616. if (startPos < 0)
  617. {
  618. startPos = j;
  619. }
  620. endPos = j;
  621. ++phaseData.iReversedNum;
  622. bLastTest = true;
  623. }
  624. else
  625. {
  626. bLastTest = false;
  627. ++phaseData.iUnReversedNum;
  628. }
  629. //stPhase phase;
  630. //data->CalcPhase(param, phase, bLastReversed);
  631. //stTotal.iReversedNum += phase.iReversedNum;
  632. //stTotal.iUnReversedNum += phase.iUnReversedNum;
  633. }
  634. // 反相百分比
  635. int iRate = phaseData.iReversedNum * 100;
  636. return iRate >= param.GetPhaseNum();
  637. }
  638. /**
  639. * @brief 判断平均音量是否低于设置的值
  640. *
  641. * @param minDBLongTime 最小声音时长
  642. * @param minDB 最小声音
  643. * @return true
  644. * @return false
  645. */
  646. bool CaculateDBData::isAvgDBLessThan(const long minDBLongTime, const long minDB)
  647. {
  648. if (ringQueue.QueueSize() < minDBLongTime || minDBLongTime <= 0)
  649. {
  650. return false;
  651. }
  652. int nNum = 0;
  653. double dTotalLeftDB = 0;
  654. double dTotalRightDB = 0;
  655. /* 计算起始位置,获取最新的minDBLongTime个数据,这里不需要管偏移值 */
  656. int startIndex = ringQueue.QueueSize() - minDBLongTime;
  657. for(int j = startIndex; j < minDBLongTime; ++j)
  658. {
  659. OneSecondData *data = ringQueue[j];
  660. if(data == nullptr)
  661. {
  662. continue;
  663. }
  664. for(int i = 0; i < VOLUME_INFO_NUM; ++i)
  665. {
  666. dTotalLeftDB += data->aryLeftDB[i];
  667. dTotalRightDB += data->aryRightDB[i];
  668. ++nNum;
  669. }
  670. }
  671. if (nNum > 0)
  672. {
  673. // 求平均值
  674. int leftDB = dTotalLeftDB / nNum;
  675. int rightDB = dTotalRightDB / nNum;
  676. if (leftDB <= minDB && rightDB <= minDB)
  677. {
  678. return true;
  679. }
  680. }
  681. return false;
  682. }
  683. /**
  684. * @brief 计算正弦波是否是噪音,正弦波几秒直接判断为噪音
  685. *
  686. * @param sinSeconds 判断是否是正弦波的条件秒数
  687. * @param calSeconds 自定义的计算描述
  688. * @param leftDB
  689. * @param rightDB
  690. * @return true
  691. * @return false
  692. */
  693. bool CaculateDBData::isSinDB(const int sinSeconds, const int calSeconds, int& leftDB, int& rightDB)
  694. {
  695. int overloadDuration = sinSeconds;
  696. if (ringQueue.QueueSize() < overloadDuration || overloadDuration <= 0)
  697. {
  698. return false;
  699. }
  700. if (overloadDuration >= calSeconds)
  701. {
  702. overloadDuration = calSeconds - 5;
  703. }
  704. /* 计算起始位置,获取最新的minDBLongTime个数据,这里不需要管偏移值 */
  705. int startIndex = ringQueue.QueueSize() - overloadDuration;
  706. for(int j = startIndex; j < overloadDuration; ++j)
  707. {
  708. OneSecondData *data = ringQueue[j];
  709. if(data == nullptr)
  710. {
  711. continue;
  712. }
  713. int nLeftDBTemp, nRightDBTmp;
  714. if (!data->isConstDB(nLeftDBTemp, nRightDBTmp))
  715. {
  716. // 不是正弦波,固定音量值
  717. return false;
  718. }
  719. if (0 == j)
  720. {
  721. leftDB = nLeftDBTemp;
  722. rightDB = nRightDBTmp;
  723. }
  724. // 虽然是固定音量值,但是音量值不相等
  725. else if (leftDB != nLeftDBTemp || rightDB != nRightDBTmp)
  726. {
  727. return false;
  728. }
  729. }
  730. return true;
  731. }