VolumeMeter.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. #include "VolumeMeter.hpp"
  2. #include <OBSApp.hpp>
  3. #include <QEvent>
  4. #include <QMouseEvent>
  5. #include <QPainter>
  6. #include <QStyleOption>
  7. #include <QTimer>
  8. #include "moc_VolumeMeter.cpp"
  9. // Size of the audio indicator in pixels
  10. #define INDICATOR_THICKNESS 3
  11. QPointer<QTimer> VolumeMeter::updateTimer = nullptr;
  12. static inline QColor color_from_int(long long val)
  13. {
  14. QColor color(val & 0xff, (val >> 8) & 0xff, (val >> 16) & 0xff, (val >> 24) & 0xff);
  15. color.setAlpha(255);
  16. return color;
  17. }
  18. QColor VolumeMeter::getBackgroundNominalColor() const
  19. {
  20. return p_backgroundNominalColor;
  21. }
  22. QColor VolumeMeter::getBackgroundNominalColorDisabled() const
  23. {
  24. return backgroundNominalColorDisabled;
  25. }
  26. void VolumeMeter::setBackgroundNominalColor(QColor c)
  27. {
  28. p_backgroundNominalColor = std::move(c);
  29. if (config_get_bool(App()->GetUserConfig(), "Accessibility", "OverrideColors")) {
  30. backgroundNominalColor =
  31. color_from_int(config_get_int(App()->GetUserConfig(), "Accessibility", "MixerGreen"));
  32. } else {
  33. backgroundNominalColor = p_backgroundNominalColor;
  34. }
  35. }
  36. void VolumeMeter::setBackgroundNominalColorDisabled(QColor c)
  37. {
  38. backgroundNominalColorDisabled = std::move(c);
  39. }
  40. QColor VolumeMeter::getBackgroundWarningColor() const
  41. {
  42. return p_backgroundWarningColor;
  43. }
  44. QColor VolumeMeter::getBackgroundWarningColorDisabled() const
  45. {
  46. return backgroundWarningColorDisabled;
  47. }
  48. void VolumeMeter::setBackgroundWarningColor(QColor c)
  49. {
  50. p_backgroundWarningColor = std::move(c);
  51. if (config_get_bool(App()->GetUserConfig(), "Accessibility", "OverrideColors")) {
  52. backgroundWarningColor =
  53. color_from_int(config_get_int(App()->GetUserConfig(), "Accessibility", "MixerYellow"));
  54. } else {
  55. backgroundWarningColor = p_backgroundWarningColor;
  56. }
  57. }
  58. void VolumeMeter::setBackgroundWarningColorDisabled(QColor c)
  59. {
  60. backgroundWarningColorDisabled = std::move(c);
  61. }
  62. QColor VolumeMeter::getBackgroundErrorColor() const
  63. {
  64. return p_backgroundErrorColor;
  65. }
  66. QColor VolumeMeter::getBackgroundErrorColorDisabled() const
  67. {
  68. return backgroundErrorColorDisabled;
  69. }
  70. void VolumeMeter::setBackgroundErrorColor(QColor c)
  71. {
  72. p_backgroundErrorColor = std::move(c);
  73. if (config_get_bool(App()->GetUserConfig(), "Accessibility", "OverrideColors")) {
  74. backgroundErrorColor =
  75. color_from_int(config_get_int(App()->GetUserConfig(), "Accessibility", "MixerRed"));
  76. } else {
  77. backgroundErrorColor = p_backgroundErrorColor;
  78. }
  79. }
  80. void VolumeMeter::setBackgroundErrorColorDisabled(QColor c)
  81. {
  82. backgroundErrorColorDisabled = std::move(c);
  83. }
  84. QColor VolumeMeter::getForegroundNominalColor() const
  85. {
  86. return p_foregroundNominalColor;
  87. }
  88. QColor VolumeMeter::getForegroundNominalColorDisabled() const
  89. {
  90. return foregroundNominalColorDisabled;
  91. }
  92. void VolumeMeter::setForegroundNominalColor(QColor c)
  93. {
  94. p_foregroundNominalColor = std::move(c);
  95. if (config_get_bool(App()->GetUserConfig(), "Accessibility", "OverrideColors")) {
  96. foregroundNominalColor =
  97. color_from_int(config_get_int(App()->GetUserConfig(), "Accessibility", "MixerGreenActive"));
  98. } else {
  99. foregroundNominalColor = p_foregroundNominalColor;
  100. }
  101. }
  102. void VolumeMeter::setForegroundNominalColorDisabled(QColor c)
  103. {
  104. foregroundNominalColorDisabled = std::move(c);
  105. }
  106. QColor VolumeMeter::getForegroundWarningColor() const
  107. {
  108. return p_foregroundWarningColor;
  109. }
  110. QColor VolumeMeter::getForegroundWarningColorDisabled() const
  111. {
  112. return foregroundWarningColorDisabled;
  113. }
  114. void VolumeMeter::setForegroundWarningColor(QColor c)
  115. {
  116. p_foregroundWarningColor = std::move(c);
  117. if (config_get_bool(App()->GetUserConfig(), "Accessibility", "OverrideColors")) {
  118. foregroundWarningColor =
  119. color_from_int(config_get_int(App()->GetUserConfig(), "Accessibility", "MixerYellowActive"));
  120. } else {
  121. foregroundWarningColor = p_foregroundWarningColor;
  122. }
  123. }
  124. void VolumeMeter::setForegroundWarningColorDisabled(QColor c)
  125. {
  126. foregroundWarningColorDisabled = std::move(c);
  127. }
  128. QColor VolumeMeter::getForegroundErrorColor() const
  129. {
  130. return p_foregroundErrorColor;
  131. }
  132. QColor VolumeMeter::getForegroundErrorColorDisabled() const
  133. {
  134. return foregroundErrorColorDisabled;
  135. }
  136. void VolumeMeter::setForegroundErrorColor(QColor c)
  137. {
  138. p_foregroundErrorColor = std::move(c);
  139. if (config_get_bool(App()->GetUserConfig(), "Accessibility", "OverrideColors")) {
  140. foregroundErrorColor =
  141. color_from_int(config_get_int(App()->GetUserConfig(), "Accessibility", "MixerRedActive"));
  142. } else {
  143. foregroundErrorColor = p_foregroundErrorColor;
  144. }
  145. }
  146. void VolumeMeter::setForegroundErrorColorDisabled(QColor c)
  147. {
  148. foregroundErrorColorDisabled = std::move(c);
  149. }
  150. QColor VolumeMeter::getMagnitudeColor() const
  151. {
  152. return magnitudeColor;
  153. }
  154. void VolumeMeter::setMagnitudeColor(QColor c)
  155. {
  156. magnitudeColor = std::move(c);
  157. }
  158. QColor VolumeMeter::getMajorTickColor() const
  159. {
  160. return majorTickColor;
  161. }
  162. void VolumeMeter::setMajorTickColor(QColor c)
  163. {
  164. majorTickColor = std::move(c);
  165. }
  166. QColor VolumeMeter::getMinorTickColor() const
  167. {
  168. return minorTickColor;
  169. }
  170. void VolumeMeter::setMinorTickColor(QColor c)
  171. {
  172. minorTickColor = std::move(c);
  173. }
  174. qreal VolumeMeter::getWarningLevel() const
  175. {
  176. return warningLevel;
  177. }
  178. void VolumeMeter::setWarningLevel(qreal v)
  179. {
  180. warningLevel = v;
  181. }
  182. qreal VolumeMeter::getErrorLevel() const
  183. {
  184. return errorLevel;
  185. }
  186. void VolumeMeter::setErrorLevel(qreal v)
  187. {
  188. errorLevel = v;
  189. }
  190. void VolumeMeter::setPeakDecayRate(qreal decayRate)
  191. {
  192. peakDecayRate = decayRate;
  193. }
  194. void VolumeMeter::setPeakMeterType(enum obs_peak_meter_type peakMeterType)
  195. {
  196. obs_volmeter_set_peak_meter_type(obsVolumeMeter, peakMeterType);
  197. switch (peakMeterType) {
  198. case TRUE_PEAK_METER:
  199. // For true-peak meters EBU has defined the Permitted Maximum,
  200. // taking into account the accuracy of the meter and further
  201. // processing required by lossy audio compression.
  202. //
  203. // The alignment level was not specified, but I've adjusted
  204. // it compared to a sample-peak meter. Incidentally Youtube
  205. // uses this new Alignment Level as the maximum integrated
  206. // loudness of a video.
  207. //
  208. // * Permitted Maximum Level (PML) = -2.0 dBTP
  209. // * Alignment Level (AL) = -13 dBTP
  210. setErrorLevel(-2.0);
  211. setWarningLevel(-13.0);
  212. break;
  213. case SAMPLE_PEAK_METER:
  214. default:
  215. // For a sample Peak Meter EBU has the following level
  216. // definitions, taking into account inaccuracies of this meter:
  217. //
  218. // * Permitted Maximum Level (PML) = -9.0 dBFS
  219. // * Alignment Level (AL) = -20.0 dBFS
  220. setErrorLevel(-9.0);
  221. setWarningLevel(-20.0);
  222. break;
  223. }
  224. updateBackgroundCache();
  225. }
  226. void VolumeMeter::mousePressEvent(QMouseEvent *event)
  227. {
  228. setFocus(Qt::MouseFocusReason);
  229. event->accept();
  230. }
  231. void VolumeMeter::wheelEvent(QWheelEvent *event)
  232. {
  233. QApplication::sendEvent(focusProxy(), event);
  234. }
  235. VolumeMeter::VolumeMeter(QWidget *parent, obs_source_t *source)
  236. : QWidget(parent),
  237. weakSource(OBSGetWeakRef(source)),
  238. obsVolumeMeter(obs_volmeter_create(OBS_FADER_LOG))
  239. {
  240. setAttribute(Qt::WA_OpaquePaintEvent, true);
  241. setAttribute(Qt::WA_TransparentForMouseEvents);
  242. setFocusPolicy(Qt::NoFocus);
  243. // Default meter settings, they only show if
  244. // there is no stylesheet, do not remove.
  245. backgroundNominalColor.setRgb(0x26, 0x7f, 0x26); // Dark green
  246. backgroundWarningColor.setRgb(0x7f, 0x7f, 0x26); // Dark yellow
  247. backgroundErrorColor.setRgb(0x7f, 0x26, 0x26); // Dark red
  248. foregroundNominalColor.setRgb(0x4c, 0xff, 0x4c); // Bright green
  249. foregroundWarningColor.setRgb(0xff, 0xff, 0x4c); // Bright yellow
  250. foregroundErrorColor.setRgb(0xff, 0x4c, 0x4c); // Bright red
  251. backgroundNominalColorDisabled.setRgb(90, 90, 90);
  252. backgroundWarningColorDisabled.setRgb(117, 117, 117);
  253. backgroundErrorColorDisabled.setRgb(65, 65, 65);
  254. foregroundNominalColorDisabled.setRgb(163, 163, 163);
  255. foregroundWarningColorDisabled.setRgb(217, 217, 217);
  256. foregroundErrorColorDisabled.setRgb(113, 113, 113);
  257. clipColor.setRgb(0xff, 0xff, 0xff); // Bright white
  258. magnitudeColor.setRgb(0x00, 0x00, 0x00); // Black
  259. majorTickColor.setRgb(0x00, 0x00, 0x00); // Black
  260. minorTickColor.setRgb(0x32, 0x32, 0x32); // Dark gray
  261. minimumLevel = -60.0; // -60 dB
  262. warningLevel = -20.0; // -20 dB
  263. errorLevel = -9.0; // -9 dB
  264. clipLevel = 0.0; // 0 dB
  265. minimumInputLevel = -50.0; // -50 dB
  266. peakDecayRate = 11.76; // 20 dB / 1.7 sec
  267. magnitudeIntegrationTime = 0.3; // 99% in 300 ms
  268. peakHoldDuration = 20.0; // 20 seconds
  269. inputPeakHoldDuration = 1.0; // 1 second
  270. meterThickness = 3; // Bar thickness in pixels
  271. meterFontScaling = 0.8; // Font size for numbers is 80% of Widget's font size
  272. channels = (int)audio_output_get_channels(obs_get_audio());
  273. obs_volmeter_add_callback(obsVolumeMeter, obsVolMeterChanged, this);
  274. obs_volmeter_attach_source(obsVolumeMeter, source);
  275. destroyedSignal =
  276. OBSSignal(obs_source_get_signal_handler(source), "destroy", &VolumeMeter::obsSourceDestroyed, this);
  277. doLayout();
  278. if (!updateTimer) {
  279. updateTimer = new QTimer(qApp);
  280. updateTimer->setTimerType(Qt::PreciseTimer);
  281. updateTimer->start(16);
  282. }
  283. connect(updateTimer, &QTimer::timeout, this, [this]() {
  284. if (needLayoutChange()) {
  285. doLayout();
  286. }
  287. repaint();
  288. });
  289. connect(App(), &OBSApp::StyleChanged, this, &VolumeMeter::updateBackgroundCache);
  290. }
  291. VolumeMeter::~VolumeMeter()
  292. {
  293. obs_volmeter_remove_callback(obsVolumeMeter, obsVolMeterChanged, this);
  294. obs_volmeter_detach_source(obsVolumeMeter);
  295. }
  296. void VolumeMeter::obsSourceDestroyed(void *data, calldata_t *)
  297. {
  298. VolumeMeter *self = static_cast<VolumeMeter *>(data);
  299. QMetaObject::invokeMethod(self, "handleSourceDestroyed", Qt::QueuedConnection);
  300. }
  301. void VolumeMeter::setLevels(const float magnitude[MAX_AUDIO_CHANNELS], const float peak[MAX_AUDIO_CHANNELS],
  302. const float inputPeak[MAX_AUDIO_CHANNELS])
  303. {
  304. uint64_t ts = os_gettime_ns();
  305. QMutexLocker locker(&dataMutex);
  306. currentLastUpdateTime = ts;
  307. for (int channelNr = 0; channelNr < MAX_AUDIO_CHANNELS; channelNr++) {
  308. currentMagnitude[channelNr] = magnitude[channelNr];
  309. currentPeak[channelNr] = peak[channelNr];
  310. currentInputPeak[channelNr] = inputPeak[channelNr];
  311. }
  312. // In case there are more updates then redraws we must make sure
  313. // that the ballistics of peak and hold are recalculated.
  314. locker.unlock();
  315. calculateBallistics(ts);
  316. }
  317. void VolumeMeter::obsVolMeterChanged(void *data, const float magnitude[MAX_AUDIO_CHANNELS],
  318. const float peak[MAX_AUDIO_CHANNELS], const float inputPeak[MAX_AUDIO_CHANNELS])
  319. {
  320. VolumeMeter *meter = static_cast<VolumeMeter *>(data);
  321. meter->setLevels(magnitude, peak, inputPeak);
  322. }
  323. inline void VolumeMeter::resetLevels()
  324. {
  325. currentLastUpdateTime = 0;
  326. for (int channelNr = 0; channelNr < MAX_AUDIO_CHANNELS; channelNr++) {
  327. currentMagnitude[channelNr] = -M_INFINITE;
  328. currentPeak[channelNr] = -M_INFINITE;
  329. currentInputPeak[channelNr] = -M_INFINITE;
  330. displayMagnitude[channelNr] = -M_INFINITE;
  331. displayPeak[channelNr] = -M_INFINITE;
  332. displayPeakHold[channelNr] = -M_INFINITE;
  333. displayPeakHoldLastUpdateTime[channelNr] = 0;
  334. displayInputPeakHold[channelNr] = -M_INFINITE;
  335. displayInputPeakHoldLastUpdateTime[channelNr] = 0;
  336. }
  337. }
  338. bool VolumeMeter::needLayoutChange()
  339. {
  340. int currentNrAudioChannels = obs_volmeter_get_nr_channels(obsVolumeMeter);
  341. if (!currentNrAudioChannels) {
  342. struct obs_audio_info oai;
  343. obs_get_audio_info(&oai);
  344. currentNrAudioChannels = (oai.speakers == SPEAKERS_MONO) ? 1 : 2;
  345. }
  346. if (displayNrAudioChannels != currentNrAudioChannels) {
  347. displayNrAudioChannels = currentNrAudioChannels;
  348. recalculateLayout = true;
  349. }
  350. return recalculateLayout;
  351. }
  352. void VolumeMeter::setVertical(bool vertical_)
  353. {
  354. if (vertical == vertical_) {
  355. return;
  356. }
  357. vertical = vertical_;
  358. doLayout();
  359. }
  360. void VolumeMeter::setUseDisabledColors(bool enable)
  361. {
  362. if (useDisabledColors == enable) {
  363. return;
  364. }
  365. useDisabledColors = enable;
  366. }
  367. void VolumeMeter::setMuted(bool mute)
  368. {
  369. if (muted == mute) {
  370. return;
  371. }
  372. muted = mute;
  373. }
  374. void VolumeMeter::refreshColors()
  375. {
  376. setBackgroundNominalColor(getBackgroundNominalColor());
  377. setBackgroundWarningColor(getBackgroundWarningColor());
  378. setBackgroundErrorColor(getBackgroundErrorColor());
  379. setForegroundNominalColor(getForegroundNominalColor());
  380. setForegroundWarningColor(getForegroundWarningColor());
  381. setForegroundErrorColor(getForegroundErrorColor());
  382. updateBackgroundCache();
  383. }
  384. // When this is called from the constructor, obs_volmeter_get_nr_channels has not
  385. // yet been called and Q_PROPERTY settings have not yet been read from the
  386. // stylesheet.
  387. inline void VolumeMeter::doLayout()
  388. {
  389. QMutexLocker locker(&dataMutex);
  390. if (displayNrAudioChannels) {
  391. int meterSize = std::floor(22 / displayNrAudioChannels);
  392. meterThickness = std::clamp(meterSize, 3, 6);
  393. }
  394. recalculateLayout = false;
  395. tickFont = font();
  396. QFontInfo info(tickFont);
  397. tickFont.setPointSizeF(info.pointSizeF() * meterFontScaling);
  398. QFontMetrics metrics(tickFont);
  399. if (vertical) {
  400. // Each meter channel is meterThickness pixels wide, plus one pixel
  401. // between channels, but not after the last.
  402. // Add 4 pixels for ticks, space to hold our longest label in this font,
  403. // and a few pixels before the fader.
  404. QRect scaleBounds = metrics.boundingRect("-88");
  405. setMinimumSize(displayNrAudioChannels * (meterThickness + 1) - 1 + 10 + scaleBounds.width() + 2, 100);
  406. } else {
  407. // Each meter channel is meterThickness pixels high, plus one pixel
  408. // between channels, but not after the last.
  409. // Add 4 pixels for ticks, and space high enough to hold our label in
  410. // this font, presuming that digits don't have descenders.
  411. setMinimumSize(100, displayNrAudioChannels * (meterThickness + 1) - 1 + 4 + metrics.capHeight());
  412. }
  413. resetLevels();
  414. }
  415. inline bool VolumeMeter::detectIdle(uint64_t ts)
  416. {
  417. double secondsSinceLastUpdate = (ts - currentLastUpdateTime) * 0.000000001;
  418. if (secondsSinceLastUpdate > 0.5) {
  419. resetLevels();
  420. return true;
  421. } else {
  422. return false;
  423. }
  424. }
  425. inline void VolumeMeter::calculateBallisticsForChannel(int channelNr, uint64_t ts, qreal timeSinceLastRedraw)
  426. {
  427. if (currentPeak[channelNr] >= displayPeak[channelNr] || isnan(displayPeak[channelNr])) {
  428. // Attack of peak is immediate.
  429. displayPeak[channelNr] = currentPeak[channelNr];
  430. } else {
  431. // Decay of peak is 40 dB / 1.7 seconds for Fast Profile
  432. // 20 dB / 1.7 seconds for Medium Profile (Type I PPM)
  433. // 24 dB / 2.8 seconds for Slow Profile (Type II PPM)
  434. float decay = float(peakDecayRate * timeSinceLastRedraw);
  435. displayPeak[channelNr] =
  436. std::clamp(displayPeak[channelNr] - decay, std::min(currentPeak[channelNr], 0.f), 0.f);
  437. }
  438. if (currentPeak[channelNr] >= displayPeakHold[channelNr] || !isfinite(displayPeakHold[channelNr])) {
  439. // Attack of peak-hold is immediate, but keep track
  440. // when it was last updated.
  441. displayPeakHold[channelNr] = currentPeak[channelNr];
  442. displayPeakHoldLastUpdateTime[channelNr] = ts;
  443. } else {
  444. // The peak and hold falls back to peak
  445. // after 20 seconds.
  446. qreal timeSinceLastPeak = (uint64_t)(ts - displayPeakHoldLastUpdateTime[channelNr]) * 0.000000001;
  447. if (timeSinceLastPeak > peakHoldDuration) {
  448. displayPeakHold[channelNr] = currentPeak[channelNr];
  449. displayPeakHoldLastUpdateTime[channelNr] = ts;
  450. }
  451. }
  452. if (currentInputPeak[channelNr] >= displayInputPeakHold[channelNr] ||
  453. !isfinite(displayInputPeakHold[channelNr])) {
  454. // Attack of peak-hold is immediate, but keep track
  455. // when it was last updated.
  456. displayInputPeakHold[channelNr] = currentInputPeak[channelNr];
  457. displayInputPeakHoldLastUpdateTime[channelNr] = ts;
  458. } else {
  459. // The peak and hold falls back to peak after 1 second.
  460. qreal timeSinceLastPeak = (uint64_t)(ts - displayInputPeakHoldLastUpdateTime[channelNr]) * 0.000000001;
  461. if (timeSinceLastPeak > inputPeakHoldDuration) {
  462. displayInputPeakHold[channelNr] = currentInputPeak[channelNr];
  463. displayInputPeakHoldLastUpdateTime[channelNr] = ts;
  464. }
  465. }
  466. if (!isfinite(displayMagnitude[channelNr])) {
  467. // The statements in the else-leg do not work with
  468. // NaN and infinite displayMagnitude.
  469. displayMagnitude[channelNr] = currentMagnitude[channelNr];
  470. } else {
  471. // A VU meter will integrate to the new value to 99% in 300 ms.
  472. // The calculation here is very simplified and is more accurate
  473. // with higher frame-rate.
  474. float attack = float((currentMagnitude[channelNr] - displayMagnitude[channelNr]) *
  475. (timeSinceLastRedraw / magnitudeIntegrationTime) * 0.99);
  476. displayMagnitude[channelNr] =
  477. std::clamp(displayMagnitude[channelNr] + attack, (float)minimumLevel, 0.f);
  478. }
  479. }
  480. inline void VolumeMeter::calculateBallistics(uint64_t ts, qreal timeSinceLastRedraw)
  481. {
  482. QMutexLocker locker(&dataMutex);
  483. for (int channelNr = 0; channelNr < MAX_AUDIO_CHANNELS; channelNr++) {
  484. calculateBallisticsForChannel(channelNr, ts, timeSinceLastRedraw);
  485. }
  486. }
  487. QColor VolumeMeter::getPeakColor(float peakHold)
  488. {
  489. QColor color;
  490. if (peakHold < minimumInputLevel) {
  491. color = backgroundNominalColor;
  492. } else if (peakHold < warningLevel) {
  493. color = foregroundNominalColor;
  494. } else if (peakHold < errorLevel) {
  495. color = foregroundWarningColor;
  496. } else if (peakHold < clipLevel) {
  497. color = foregroundErrorColor;
  498. } else {
  499. color = clipColor;
  500. }
  501. return color;
  502. }
  503. void VolumeMeter::paintHTicks(QPainter &painter, int x, int y, int width)
  504. {
  505. qreal scale = width / minimumLevel;
  506. painter.setFont(tickFont);
  507. QFontMetrics metrics(tickFont);
  508. painter.setPen(majorTickColor);
  509. // Draw major tick lines and numeric indicators.
  510. for (int i = 0; i >= minimumLevel; i -= 6) {
  511. int position = int(x + width - (i * scale) - 1);
  512. QString str = QString::number(i);
  513. // Center the number on the tick, but don't overflow
  514. QRect textBounds = metrics.boundingRect(str);
  515. int pos;
  516. if (i == 0) {
  517. pos = position - textBounds.width();
  518. } else {
  519. pos = position - (textBounds.width() / 2);
  520. if (pos < 0) {
  521. pos = 0;
  522. }
  523. }
  524. painter.drawText(pos, y + 4 + metrics.capHeight(), str);
  525. painter.drawLine(position, y, position, y + 2);
  526. }
  527. }
  528. void VolumeMeter::paintVTicks(QPainter &painter, int x, int y, int height)
  529. {
  530. qreal scale = height / minimumLevel;
  531. painter.setFont(tickFont);
  532. QFontMetrics metrics(tickFont);
  533. painter.setPen(majorTickColor);
  534. // Draw major tick lines and numeric indicators.
  535. for (int i = 0; i >= minimumLevel; i -= 6) {
  536. int position = y + int(i * scale);
  537. QString str = QString::number(i);
  538. // Center the number on the tick, but don't overflow
  539. if (i == 0) {
  540. painter.drawText(x + 10, position + metrics.capHeight(), str);
  541. } else {
  542. painter.drawText(x + 8, position + (metrics.capHeight() / 2), str);
  543. }
  544. painter.drawLine(x, position, x + 2, position);
  545. }
  546. }
  547. void VolumeMeter::updateBackgroundCache()
  548. {
  549. QColor backgroundColor = palette().color(QPalette::Window);
  550. backgroundCache = QPixmap(size() * devicePixelRatioF());
  551. backgroundCache.setDevicePixelRatio(devicePixelRatioF());
  552. backgroundCache.fill(backgroundColor);
  553. QPainter bg{&backgroundCache};
  554. QRect widgetRect = rect();
  555. // Draw ticks
  556. if (vertical) {
  557. paintVTicks(bg, displayNrAudioChannels * (meterThickness + 1) - 1, 0,
  558. widgetRect.height() - (INDICATOR_THICKNESS + 3));
  559. } else {
  560. paintHTicks(bg, INDICATOR_THICKNESS + 3, displayNrAudioChannels * (meterThickness + 1) - 1,
  561. widgetRect.width() - (INDICATOR_THICKNESS + 3));
  562. }
  563. // Draw meter backgrounds
  564. bool disabledColors = muted || useDisabledColors;
  565. QColor nominal = disabledColors ? backgroundNominalColorDisabled : backgroundNominalColor;
  566. QColor warning = disabledColors ? backgroundWarningColorDisabled : backgroundWarningColor;
  567. QColor error = disabledColors ? backgroundErrorColorDisabled : backgroundErrorColor;
  568. int meterStart = INDICATOR_THICKNESS + 2;
  569. int meterLength = vertical ? rect().height() - (INDICATOR_THICKNESS + 2)
  570. : rect().width() - (INDICATOR_THICKNESS + 2);
  571. qreal scale = meterLength / minimumLevel;
  572. int warningPosition = meterLength - convertToInt(warningLevel * scale);
  573. int errorPosition = meterLength - convertToInt(errorLevel * scale);
  574. int nominalLength = warningPosition;
  575. int warningLength = nominalLength + (errorPosition - warningPosition);
  576. for (int channelNr = 0; channelNr < displayNrAudioChannels; channelNr++) {
  577. int channelOffset = channelNr * (meterThickness + 1);
  578. if (vertical) {
  579. bg.fillRect(channelOffset, meterLength, meterThickness, -meterLength, error);
  580. bg.fillRect(channelOffset, meterLength, meterThickness, -warningLength, warning);
  581. bg.fillRect(channelOffset, meterLength, meterThickness, -nominalLength, nominal);
  582. } else {
  583. bg.fillRect(meterStart, channelOffset, meterLength, meterThickness, error);
  584. bg.fillRect(meterStart, channelOffset, warningLength, meterThickness, warning);
  585. bg.fillRect(meterStart, channelOffset, nominalLength, meterThickness, nominal);
  586. }
  587. }
  588. }
  589. #define CLIP_FLASH_DURATION_MS 1000
  590. inline int VolumeMeter::convertToInt(float number)
  591. {
  592. constexpr int min = std::numeric_limits<int>::min();
  593. constexpr int max = std::numeric_limits<int>::max();
  594. // NOTE: Conversion from 'const int' to 'float' changes max value from 2147483647 to 2147483648
  595. if (number >= (float)max) {
  596. return max;
  597. } else if (number < min) {
  598. return min;
  599. } else {
  600. return int(number);
  601. }
  602. }
  603. void VolumeMeter::paintEvent(QPaintEvent *)
  604. {
  605. uint64_t ts = os_gettime_ns();
  606. qreal timeSinceLastRedraw = (ts - lastRedrawTime) * 0.000000001;
  607. calculateBallistics(ts, timeSinceLastRedraw);
  608. bool idle = detectIdle(ts);
  609. QPainter painter(this);
  610. bool disabledColors = muted || useDisabledColors;
  611. QColor nominal = disabledColors ? foregroundNominalColorDisabled : foregroundNominalColor;
  612. QColor warning = disabledColors ? foregroundWarningColorDisabled : foregroundWarningColor;
  613. QColor error = disabledColors ? foregroundErrorColorDisabled : foregroundErrorColor;
  614. int meterStart = INDICATOR_THICKNESS + 2;
  615. int meterLength = vertical ? rect().height() - (INDICATOR_THICKNESS + 2)
  616. : rect().width() - (INDICATOR_THICKNESS + 2);
  617. const qreal scale = meterLength / minimumLevel;
  618. // Paint cached background pixmap
  619. if (backgroundCache.isNull() || backgroundCache.size() != size()) {
  620. updateBackgroundCache();
  621. }
  622. painter.drawPixmap(0, 0, backgroundCache);
  623. // Draw dynamic audio meter bars
  624. int warningPosition = meterLength - convertToInt(warningLevel * scale);
  625. int errorPosition = meterLength - convertToInt(errorLevel * scale);
  626. int clipPosition = meterLength - convertToInt(clipLevel * scale);
  627. int nominalLength = warningPosition;
  628. int warningLength = nominalLength + (errorPosition - warningPosition);
  629. for (int channelNr = 0; channelNr < displayNrAudioChannels; channelNr++) {
  630. int channelNrFixed = (displayNrAudioChannels == 1 && channels > 2) ? 2 : channelNr;
  631. QMutexLocker locker(&dataMutex);
  632. float peak = displayPeak[channelNrFixed];
  633. float peakHold = displayPeakHold[channelNrFixed];
  634. float magnitude = displayMagnitude[channelNrFixed];
  635. int peakPosition = meterLength - convertToInt(peak * scale);
  636. int peakHoldPosition = meterLength - convertToInt(peakHold * scale);
  637. int magnitudePosition = meterLength - convertToInt(magnitude * scale);
  638. locker.unlock();
  639. if (clipping) {
  640. peakPosition = meterLength;
  641. }
  642. auto fill = [&](int pos, int length, const QColor &color) {
  643. if (vertical) {
  644. painter.fillRect(pos, meterLength, meterThickness, -length, color);
  645. } else {
  646. painter.fillRect(meterStart, pos, length, meterThickness, color);
  647. }
  648. };
  649. int channelOffset = channelNr * (meterThickness + 1);
  650. // Draw audio meter peak bars
  651. if (peakPosition >= clipPosition) {
  652. if (!clipping) {
  653. QTimer::singleShot(CLIP_FLASH_DURATION_MS, this, [&]() { clipping = false; });
  654. clipping = true;
  655. }
  656. fill(channelOffset, meterLength, error);
  657. } else {
  658. if (peakPosition > errorPosition) {
  659. fill(channelOffset, std::min(peakPosition, meterLength), error);
  660. }
  661. if (peakPosition > warningPosition) {
  662. fill(channelOffset, std::min(peakPosition, warningLength), warning);
  663. }
  664. if (peakPosition > meterStart) {
  665. fill(channelOffset, std::min(peakPosition, nominalLength), nominal);
  666. }
  667. }
  668. // Draw peak hold indicators
  669. QColor peakHoldColor = nominal;
  670. if (peakHoldPosition >= errorPosition) {
  671. peakHoldColor = error;
  672. } else if (peakHoldPosition >= warningPosition) {
  673. peakHoldColor = warning;
  674. }
  675. if (peakHoldPosition - 3 > 0) {
  676. if (vertical) {
  677. painter.fillRect(channelOffset, meterLength - peakHoldPosition - 3, meterThickness, 3,
  678. peakHoldColor);
  679. } else {
  680. painter.fillRect(meterStart + peakHoldPosition - 3, channelOffset, 3, meterThickness,
  681. peakHoldColor);
  682. }
  683. }
  684. // Draw magnitude indicator
  685. if (magnitudePosition - 3 >= 0) {
  686. if (vertical) {
  687. painter.fillRect(channelOffset, meterLength - magnitudePosition - 3, meterThickness, 3,
  688. magnitudeColor);
  689. } else {
  690. painter.fillRect(meterStart + magnitudePosition - 3, channelOffset, 3, meterThickness,
  691. magnitudeColor);
  692. }
  693. }
  694. if (idle) {
  695. continue;
  696. }
  697. // Draw audio input indicator
  698. if (vertical) {
  699. painter.fillRect(channelOffset, rect().height(), meterThickness, -INDICATOR_THICKNESS,
  700. getPeakColor(displayInputPeakHold[channelNrFixed]));
  701. } else {
  702. painter.fillRect(0, channelOffset, INDICATOR_THICKNESS, meterThickness,
  703. getPeakColor(displayInputPeakHold[channelNrFixed]));
  704. }
  705. }
  706. lastRedrawTime = ts;
  707. }
  708. QRect VolumeMeter::getBarRect() const
  709. {
  710. QRect rec = rect();
  711. if (vertical) {
  712. rec.setWidth(displayNrAudioChannels * (meterThickness + 1) - 1);
  713. } else {
  714. rec.setHeight(displayNrAudioChannels * (meterThickness + 1) - 1);
  715. }
  716. return rec;
  717. }
  718. void VolumeMeter::changeEvent(QEvent *e)
  719. {
  720. if (e->type() == QEvent::StyleChange) {
  721. recalculateLayout = true;
  722. }
  723. QWidget::changeEvent(e);
  724. }