VolumeMeter.cpp 25 KB

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