volume-control.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444
  1. #include "window-basic-main.hpp"
  2. #include "volume-control.hpp"
  3. #include "qt-wrappers.hpp"
  4. #include "obs-app.hpp"
  5. #include "mute-checkbox.hpp"
  6. #include "slider-ignorewheel.hpp"
  7. #include "slider-absoluteset-style.hpp"
  8. #include <QFontDatabase>
  9. #include <QHBoxLayout>
  10. #include <QPushButton>
  11. #include <QLabel>
  12. #include <QPainter>
  13. #include <QStyleFactory>
  14. using namespace std;
  15. #define CLAMP(x, min, max) ((x) < (min) ? (min) : ((x) > (max) ? (max) : (x)))
  16. #define FADER_PRECISION 4096.0
  17. // Size of the audio indicator in pixels
  18. #define INDICATOR_THICKNESS 3
  19. // Padding on top and bottom of vertical meters
  20. #define METER_PADDING 1
  21. QWeakPointer<VolumeMeterTimer> VolumeMeter::updateTimer;
  22. void VolControl::OBSVolumeChanged(void *data, float db)
  23. {
  24. Q_UNUSED(db);
  25. VolControl *volControl = static_cast<VolControl *>(data);
  26. QMetaObject::invokeMethod(volControl, "VolumeChanged");
  27. }
  28. void VolControl::OBSVolumeLevel(void *data,
  29. const float magnitude[MAX_AUDIO_CHANNELS],
  30. const float peak[MAX_AUDIO_CHANNELS],
  31. const float inputPeak[MAX_AUDIO_CHANNELS])
  32. {
  33. VolControl *volControl = static_cast<VolControl *>(data);
  34. volControl->volMeter->setLevels(magnitude, peak, inputPeak);
  35. }
  36. void VolControl::OBSVolumeMuted(void *data, calldata_t *calldata)
  37. {
  38. VolControl *volControl = static_cast<VolControl *>(data);
  39. bool muted = calldata_bool(calldata, "muted");
  40. QMetaObject::invokeMethod(volControl, "VolumeMuted",
  41. Q_ARG(bool, muted));
  42. }
  43. void VolControl::VolumeChanged()
  44. {
  45. slider->blockSignals(true);
  46. slider->setValue(
  47. (int)(obs_fader_get_deflection(obs_fader) * FADER_PRECISION));
  48. slider->blockSignals(false);
  49. updateText();
  50. }
  51. void VolControl::VolumeMuted(bool muted)
  52. {
  53. if (mute->isChecked() != muted)
  54. mute->setChecked(muted);
  55. volMeter->muted = muted;
  56. }
  57. void VolControl::SetMuted(bool checked)
  58. {
  59. bool prev = obs_source_muted(source);
  60. obs_source_set_muted(source, checked);
  61. auto undo_redo = [](const std::string &name, bool val) {
  62. OBSSourceAutoRelease source =
  63. obs_get_source_by_name(name.c_str());
  64. obs_source_set_muted(source, val);
  65. };
  66. QString text =
  67. QTStr(checked ? "Undo.Volume.Mute" : "Undo.Volume.Unmute");
  68. const char *name = obs_source_get_name(source);
  69. OBSBasic::Get()->undo_s.add_action(
  70. text.arg(name),
  71. std::bind(undo_redo, std::placeholders::_1, prev),
  72. std::bind(undo_redo, std::placeholders::_1, checked), name,
  73. name);
  74. }
  75. void VolControl::SliderChanged(int vol)
  76. {
  77. float prev = obs_source_get_volume(source);
  78. obs_fader_set_deflection(obs_fader, float(vol) / FADER_PRECISION);
  79. updateText();
  80. auto undo_redo = [](const std::string &name, float val) {
  81. OBSSourceAutoRelease source =
  82. obs_get_source_by_name(name.c_str());
  83. obs_source_set_volume(source, val);
  84. };
  85. float val = obs_source_get_volume(source);
  86. const char *name = obs_source_get_name(source);
  87. OBSBasic::Get()->undo_s.add_action(
  88. QTStr("Undo.Volume.Change").arg(name),
  89. std::bind(undo_redo, std::placeholders::_1, prev),
  90. std::bind(undo_redo, std::placeholders::_1, val), name, name,
  91. true);
  92. }
  93. void VolControl::updateText()
  94. {
  95. QString text;
  96. float db = obs_fader_get_db(obs_fader);
  97. if (db < -96.0f)
  98. text = "-inf dB";
  99. else
  100. text = QString::number(db, 'f', 1).append(" dB");
  101. volLabel->setText(text);
  102. bool muted = obs_source_muted(source);
  103. const char *accTextLookup = muted ? "VolControl.SliderMuted"
  104. : "VolControl.SliderUnmuted";
  105. QString sourceName = obs_source_get_name(source);
  106. QString accText = QTStr(accTextLookup).arg(sourceName);
  107. slider->setAccessibleName(accText);
  108. }
  109. QString VolControl::GetName() const
  110. {
  111. return nameLabel->text();
  112. }
  113. void VolControl::SetName(const QString &newName)
  114. {
  115. nameLabel->setText(newName);
  116. }
  117. void VolControl::EmitConfigClicked()
  118. {
  119. emit ConfigClicked();
  120. }
  121. void VolControl::SetMeterDecayRate(qreal q)
  122. {
  123. volMeter->setPeakDecayRate(q);
  124. }
  125. void VolControl::setPeakMeterType(enum obs_peak_meter_type peakMeterType)
  126. {
  127. volMeter->setPeakMeterType(peakMeterType);
  128. }
  129. VolControl::VolControl(OBSSource source_, bool showConfig, bool vertical)
  130. : source(std::move(source_)),
  131. levelTotal(0.0f),
  132. levelCount(0.0f),
  133. obs_fader(obs_fader_create(OBS_FADER_LOG)),
  134. obs_volmeter(obs_volmeter_create(OBS_FADER_LOG)),
  135. vertical(vertical),
  136. contextMenu(nullptr)
  137. {
  138. nameLabel = new QLabel();
  139. volLabel = new QLabel();
  140. mute = new MuteCheckBox();
  141. QString sourceName = obs_source_get_name(source);
  142. setObjectName(sourceName);
  143. if (showConfig) {
  144. config = new QPushButton(this);
  145. config->setProperty("themeID", "menuIconSmall");
  146. config->setSizePolicy(QSizePolicy::Maximum,
  147. QSizePolicy::Maximum);
  148. config->setMaximumSize(22, 22);
  149. config->setAutoDefault(false);
  150. config->setAccessibleName(
  151. QTStr("VolControl.Properties").arg(sourceName));
  152. connect(config, &QAbstractButton::clicked, this,
  153. &VolControl::EmitConfigClicked);
  154. }
  155. QVBoxLayout *mainLayout = new QVBoxLayout;
  156. mainLayout->setContentsMargins(4, 4, 4, 4);
  157. mainLayout->setSpacing(2);
  158. if (vertical) {
  159. QHBoxLayout *nameLayout = new QHBoxLayout;
  160. QHBoxLayout *controlLayout = new QHBoxLayout;
  161. QHBoxLayout *volLayout = new QHBoxLayout;
  162. QHBoxLayout *meterLayout = new QHBoxLayout;
  163. volMeter = new VolumeMeter(nullptr, obs_volmeter, true);
  164. slider = new VolumeSlider(obs_fader, Qt::Vertical);
  165. nameLayout->setAlignment(Qt::AlignCenter);
  166. meterLayout->setAlignment(Qt::AlignCenter);
  167. controlLayout->setAlignment(Qt::AlignCenter);
  168. volLayout->setAlignment(Qt::AlignCenter);
  169. nameLayout->setContentsMargins(0, 0, 0, 0);
  170. nameLayout->setSpacing(0);
  171. nameLayout->addWidget(nameLabel);
  172. controlLayout->setContentsMargins(0, 0, 0, 0);
  173. controlLayout->setSpacing(0);
  174. if (showConfig)
  175. controlLayout->addWidget(config);
  176. controlLayout->addItem(new QSpacerItem(3, 0));
  177. // Add Headphone (audio monitoring) widget here
  178. controlLayout->addWidget(mute);
  179. meterLayout->setContentsMargins(0, 0, 0, 0);
  180. meterLayout->setSpacing(0);
  181. meterLayout->addWidget(volMeter);
  182. meterLayout->addWidget(slider);
  183. volLayout->setContentsMargins(0, 0, 0, 0);
  184. volLayout->setSpacing(0);
  185. volLayout->addWidget(volLabel);
  186. mainLayout->addItem(nameLayout);
  187. mainLayout->addItem(volLayout);
  188. mainLayout->addItem(meterLayout);
  189. mainLayout->addItem(controlLayout);
  190. volMeter->setFocusProxy(slider);
  191. // Default size can cause clipping of long names in vertical layout.
  192. QFont font = nameLabel->font();
  193. QFontInfo info(font);
  194. font.setPointSizeF(0.8 * info.pointSizeF());
  195. nameLabel->setFont(font);
  196. setMaximumWidth(110);
  197. } else {
  198. QHBoxLayout *textLayout = new QHBoxLayout;
  199. QHBoxLayout *botLayout = new QHBoxLayout;
  200. volMeter = new VolumeMeter(nullptr, obs_volmeter, false);
  201. slider = new VolumeSlider(obs_fader, Qt::Horizontal);
  202. textLayout->setContentsMargins(0, 0, 0, 0);
  203. textLayout->addWidget(nameLabel);
  204. textLayout->addWidget(volLabel);
  205. textLayout->setAlignment(nameLabel, Qt::AlignLeft);
  206. textLayout->setAlignment(volLabel, Qt::AlignRight);
  207. botLayout->setContentsMargins(0, 0, 0, 0);
  208. botLayout->setSpacing(5);
  209. botLayout->addWidget(slider);
  210. botLayout->addWidget(mute);
  211. botLayout->setAlignment(slider, Qt::AlignVCenter);
  212. botLayout->setAlignment(mute, Qt::AlignVCenter);
  213. if (showConfig) {
  214. botLayout->addWidget(config);
  215. botLayout->setAlignment(config, Qt::AlignVCenter);
  216. }
  217. mainLayout->addItem(textLayout);
  218. mainLayout->addWidget(volMeter);
  219. mainLayout->addItem(botLayout);
  220. volMeter->setFocusProxy(slider);
  221. }
  222. setLayout(mainLayout);
  223. nameLabel->setText(sourceName);
  224. slider->setMinimum(0);
  225. slider->setMaximum(int(FADER_PRECISION));
  226. bool muted = obs_source_muted(source);
  227. mute->setChecked(muted);
  228. volMeter->muted = muted;
  229. mute->setAccessibleName(QTStr("VolControl.Mute").arg(sourceName));
  230. obs_fader_add_callback(obs_fader, OBSVolumeChanged, this);
  231. obs_volmeter_add_callback(obs_volmeter, OBSVolumeLevel, this);
  232. signal_handler_connect(obs_source_get_signal_handler(source), "mute",
  233. OBSVolumeMuted, this);
  234. QWidget::connect(slider, SIGNAL(valueChanged(int)), this,
  235. SLOT(SliderChanged(int)));
  236. QWidget::connect(mute, SIGNAL(clicked(bool)), this,
  237. SLOT(SetMuted(bool)));
  238. obs_fader_attach_source(obs_fader, source);
  239. obs_volmeter_attach_source(obs_volmeter, source);
  240. QString styleName = slider->style()->objectName();
  241. QStyle *style;
  242. style = QStyleFactory::create(styleName);
  243. if (!style) {
  244. style = new SliderAbsoluteSetStyle();
  245. } else {
  246. style = new SliderAbsoluteSetStyle(style);
  247. }
  248. style->setParent(slider);
  249. slider->setStyle(style);
  250. /* Call volume changed once to init the slider position and label */
  251. VolumeChanged();
  252. }
  253. void VolControl::EnableSlider(bool enable)
  254. {
  255. slider->setEnabled(enable);
  256. }
  257. VolControl::~VolControl()
  258. {
  259. obs_fader_remove_callback(obs_fader, OBSVolumeChanged, this);
  260. obs_volmeter_remove_callback(obs_volmeter, OBSVolumeLevel, this);
  261. signal_handler_disconnect(obs_source_get_signal_handler(source), "mute",
  262. OBSVolumeMuted, this);
  263. obs_fader_destroy(obs_fader);
  264. obs_volmeter_destroy(obs_volmeter);
  265. if (contextMenu)
  266. contextMenu->close();
  267. }
  268. static inline QColor color_from_int(long long val)
  269. {
  270. QColor color(val & 0xff, (val >> 8) & 0xff, (val >> 16) & 0xff,
  271. (val >> 24) & 0xff);
  272. color.setAlpha(255);
  273. return color;
  274. }
  275. QColor VolumeMeter::getBackgroundNominalColor() const
  276. {
  277. return p_backgroundNominalColor;
  278. }
  279. QColor VolumeMeter::getBackgroundNominalColorDisabled() const
  280. {
  281. return backgroundNominalColorDisabled;
  282. }
  283. void VolumeMeter::setBackgroundNominalColor(QColor c)
  284. {
  285. p_backgroundNominalColor = std::move(c);
  286. if (config_get_bool(GetGlobalConfig(), "Accessibility",
  287. "OverrideColors")) {
  288. backgroundNominalColor = color_from_int(config_get_int(
  289. GetGlobalConfig(), "Accessibility", "MixerGreen"));
  290. } else {
  291. backgroundNominalColor = p_backgroundNominalColor;
  292. }
  293. }
  294. void VolumeMeter::setBackgroundNominalColorDisabled(QColor c)
  295. {
  296. backgroundNominalColorDisabled = std::move(c);
  297. }
  298. QColor VolumeMeter::getBackgroundWarningColor() const
  299. {
  300. return p_backgroundWarningColor;
  301. }
  302. QColor VolumeMeter::getBackgroundWarningColorDisabled() const
  303. {
  304. return backgroundWarningColorDisabled;
  305. }
  306. void VolumeMeter::setBackgroundWarningColor(QColor c)
  307. {
  308. p_backgroundWarningColor = std::move(c);
  309. if (config_get_bool(GetGlobalConfig(), "Accessibility",
  310. "OverrideColors")) {
  311. backgroundWarningColor = color_from_int(config_get_int(
  312. GetGlobalConfig(), "Accessibility", "MixerYellow"));
  313. } else {
  314. backgroundWarningColor = p_backgroundWarningColor;
  315. }
  316. }
  317. void VolumeMeter::setBackgroundWarningColorDisabled(QColor c)
  318. {
  319. backgroundWarningColorDisabled = std::move(c);
  320. }
  321. QColor VolumeMeter::getBackgroundErrorColor() const
  322. {
  323. return p_backgroundErrorColor;
  324. }
  325. QColor VolumeMeter::getBackgroundErrorColorDisabled() const
  326. {
  327. return backgroundErrorColorDisabled;
  328. }
  329. void VolumeMeter::setBackgroundErrorColor(QColor c)
  330. {
  331. p_backgroundErrorColor = std::move(c);
  332. if (config_get_bool(GetGlobalConfig(), "Accessibility",
  333. "OverrideColors")) {
  334. backgroundErrorColor = color_from_int(config_get_int(
  335. GetGlobalConfig(), "Accessibility", "MixerRed"));
  336. } else {
  337. backgroundErrorColor = p_backgroundErrorColor;
  338. }
  339. }
  340. void VolumeMeter::setBackgroundErrorColorDisabled(QColor c)
  341. {
  342. backgroundErrorColorDisabled = std::move(c);
  343. }
  344. QColor VolumeMeter::getForegroundNominalColor() const
  345. {
  346. return p_foregroundNominalColor;
  347. }
  348. QColor VolumeMeter::getForegroundNominalColorDisabled() const
  349. {
  350. return foregroundNominalColorDisabled;
  351. }
  352. void VolumeMeter::setForegroundNominalColor(QColor c)
  353. {
  354. p_foregroundNominalColor = std::move(c);
  355. if (config_get_bool(GetGlobalConfig(), "Accessibility",
  356. "OverrideColors")) {
  357. foregroundNominalColor = color_from_int(
  358. config_get_int(GetGlobalConfig(), "Accessibility",
  359. "MixerGreenActive"));
  360. } else {
  361. foregroundNominalColor = p_foregroundNominalColor;
  362. }
  363. }
  364. void VolumeMeter::setForegroundNominalColorDisabled(QColor c)
  365. {
  366. foregroundNominalColorDisabled = std::move(c);
  367. }
  368. QColor VolumeMeter::getForegroundWarningColor() const
  369. {
  370. return p_foregroundWarningColor;
  371. }
  372. QColor VolumeMeter::getForegroundWarningColorDisabled() const
  373. {
  374. return foregroundWarningColorDisabled;
  375. }
  376. void VolumeMeter::setForegroundWarningColor(QColor c)
  377. {
  378. p_foregroundWarningColor = std::move(c);
  379. if (config_get_bool(GetGlobalConfig(), "Accessibility",
  380. "OverrideColors")) {
  381. foregroundWarningColor = color_from_int(
  382. config_get_int(GetGlobalConfig(), "Accessibility",
  383. "MixerYellowActive"));
  384. } else {
  385. foregroundWarningColor = p_foregroundWarningColor;
  386. }
  387. }
  388. void VolumeMeter::setForegroundWarningColorDisabled(QColor c)
  389. {
  390. foregroundWarningColorDisabled = std::move(c);
  391. }
  392. QColor VolumeMeter::getForegroundErrorColor() const
  393. {
  394. return p_foregroundErrorColor;
  395. }
  396. QColor VolumeMeter::getForegroundErrorColorDisabled() const
  397. {
  398. return foregroundErrorColorDisabled;
  399. }
  400. void VolumeMeter::setForegroundErrorColor(QColor c)
  401. {
  402. p_foregroundErrorColor = std::move(c);
  403. if (config_get_bool(GetGlobalConfig(), "Accessibility",
  404. "OverrideColors")) {
  405. foregroundErrorColor = color_from_int(config_get_int(
  406. GetGlobalConfig(), "Accessibility", "MixerRedActive"));
  407. } else {
  408. foregroundErrorColor = p_foregroundErrorColor;
  409. }
  410. }
  411. void VolumeMeter::setForegroundErrorColorDisabled(QColor c)
  412. {
  413. foregroundErrorColorDisabled = std::move(c);
  414. }
  415. QColor VolumeMeter::getClipColor() const
  416. {
  417. return clipColor;
  418. }
  419. void VolumeMeter::setClipColor(QColor c)
  420. {
  421. clipColor = std::move(c);
  422. }
  423. QColor VolumeMeter::getMagnitudeColor() const
  424. {
  425. return magnitudeColor;
  426. }
  427. void VolumeMeter::setMagnitudeColor(QColor c)
  428. {
  429. magnitudeColor = std::move(c);
  430. }
  431. QColor VolumeMeter::getMajorTickColor() const
  432. {
  433. return majorTickColor;
  434. }
  435. void VolumeMeter::setMajorTickColor(QColor c)
  436. {
  437. majorTickColor = std::move(c);
  438. }
  439. QColor VolumeMeter::getMinorTickColor() const
  440. {
  441. return minorTickColor;
  442. }
  443. void VolumeMeter::setMinorTickColor(QColor c)
  444. {
  445. minorTickColor = std::move(c);
  446. }
  447. int VolumeMeter::getMeterThickness() const
  448. {
  449. return meterThickness;
  450. }
  451. void VolumeMeter::setMeterThickness(int v)
  452. {
  453. meterThickness = v;
  454. recalculateLayout = true;
  455. }
  456. qreal VolumeMeter::getMeterFontScaling() const
  457. {
  458. return meterFontScaling;
  459. }
  460. void VolumeMeter::setMeterFontScaling(qreal v)
  461. {
  462. meterFontScaling = v;
  463. recalculateLayout = true;
  464. }
  465. void VolControl::refreshColors()
  466. {
  467. volMeter->setBackgroundNominalColor(
  468. volMeter->getBackgroundNominalColor());
  469. volMeter->setBackgroundWarningColor(
  470. volMeter->getBackgroundWarningColor());
  471. volMeter->setBackgroundErrorColor(volMeter->getBackgroundErrorColor());
  472. volMeter->setForegroundNominalColor(
  473. volMeter->getForegroundNominalColor());
  474. volMeter->setForegroundWarningColor(
  475. volMeter->getForegroundWarningColor());
  476. volMeter->setForegroundErrorColor(volMeter->getForegroundErrorColor());
  477. }
  478. qreal VolumeMeter::getMinimumLevel() const
  479. {
  480. return minimumLevel;
  481. }
  482. void VolumeMeter::setMinimumLevel(qreal v)
  483. {
  484. minimumLevel = v;
  485. }
  486. qreal VolumeMeter::getWarningLevel() const
  487. {
  488. return warningLevel;
  489. }
  490. void VolumeMeter::setWarningLevel(qreal v)
  491. {
  492. warningLevel = v;
  493. }
  494. qreal VolumeMeter::getErrorLevel() const
  495. {
  496. return errorLevel;
  497. }
  498. void VolumeMeter::setErrorLevel(qreal v)
  499. {
  500. errorLevel = v;
  501. }
  502. qreal VolumeMeter::getClipLevel() const
  503. {
  504. return clipLevel;
  505. }
  506. void VolumeMeter::setClipLevel(qreal v)
  507. {
  508. clipLevel = v;
  509. }
  510. qreal VolumeMeter::getMinimumInputLevel() const
  511. {
  512. return minimumInputLevel;
  513. }
  514. void VolumeMeter::setMinimumInputLevel(qreal v)
  515. {
  516. minimumInputLevel = v;
  517. }
  518. qreal VolumeMeter::getPeakDecayRate() const
  519. {
  520. return peakDecayRate;
  521. }
  522. void VolumeMeter::setPeakDecayRate(qreal v)
  523. {
  524. peakDecayRate = v;
  525. }
  526. qreal VolumeMeter::getMagnitudeIntegrationTime() const
  527. {
  528. return magnitudeIntegrationTime;
  529. }
  530. void VolumeMeter::setMagnitudeIntegrationTime(qreal v)
  531. {
  532. magnitudeIntegrationTime = v;
  533. }
  534. qreal VolumeMeter::getPeakHoldDuration() const
  535. {
  536. return peakHoldDuration;
  537. }
  538. void VolumeMeter::setPeakHoldDuration(qreal v)
  539. {
  540. peakHoldDuration = v;
  541. }
  542. qreal VolumeMeter::getInputPeakHoldDuration() const
  543. {
  544. return inputPeakHoldDuration;
  545. }
  546. void VolumeMeter::setInputPeakHoldDuration(qreal v)
  547. {
  548. inputPeakHoldDuration = v;
  549. }
  550. void VolumeMeter::setPeakMeterType(enum obs_peak_meter_type peakMeterType)
  551. {
  552. obs_volmeter_set_peak_meter_type(obs_volmeter, peakMeterType);
  553. switch (peakMeterType) {
  554. case TRUE_PEAK_METER:
  555. // For true-peak meters EBU has defined the Permitted Maximum,
  556. // taking into account the accuracy of the meter and further
  557. // processing required by lossy audio compression.
  558. //
  559. // The alignment level was not specified, but I've adjusted
  560. // it compared to a sample-peak meter. Incidentally Youtube
  561. // uses this new Alignment Level as the maximum integrated
  562. // loudness of a video.
  563. //
  564. // * Permitted Maximum Level (PML) = -2.0 dBTP
  565. // * Alignment Level (AL) = -13 dBTP
  566. setErrorLevel(-2.0);
  567. setWarningLevel(-13.0);
  568. break;
  569. case SAMPLE_PEAK_METER:
  570. default:
  571. // For a sample Peak Meter EBU has the following level
  572. // definitions, taking into account inaccuracies of this meter:
  573. //
  574. // * Permitted Maximum Level (PML) = -9.0 dBFS
  575. // * Alignment Level (AL) = -20.0 dBFS
  576. setErrorLevel(-9.0);
  577. setWarningLevel(-20.0);
  578. break;
  579. }
  580. }
  581. void VolumeMeter::mousePressEvent(QMouseEvent *event)
  582. {
  583. setFocus(Qt::MouseFocusReason);
  584. event->accept();
  585. }
  586. void VolumeMeter::wheelEvent(QWheelEvent *event)
  587. {
  588. QApplication::sendEvent(focusProxy(), event);
  589. }
  590. VolumeMeter::VolumeMeter(QWidget *parent, obs_volmeter_t *obs_volmeter,
  591. bool vertical)
  592. : QWidget(parent), obs_volmeter(obs_volmeter), vertical(vertical)
  593. {
  594. setAttribute(Qt::WA_OpaquePaintEvent, true);
  595. // Default meter settings, they only show if
  596. // there is no stylesheet, do not remove.
  597. backgroundNominalColor.setRgb(0x26, 0x7f, 0x26); // Dark green
  598. backgroundWarningColor.setRgb(0x7f, 0x7f, 0x26); // Dark yellow
  599. backgroundErrorColor.setRgb(0x7f, 0x26, 0x26); // Dark red
  600. foregroundNominalColor.setRgb(0x4c, 0xff, 0x4c); // Bright green
  601. foregroundWarningColor.setRgb(0xff, 0xff, 0x4c); // Bright yellow
  602. foregroundErrorColor.setRgb(0xff, 0x4c, 0x4c); // Bright red
  603. backgroundNominalColorDisabled.setRgb(90, 90, 90);
  604. backgroundWarningColorDisabled.setRgb(117, 117, 117);
  605. backgroundErrorColorDisabled.setRgb(65, 65, 65);
  606. foregroundNominalColorDisabled.setRgb(163, 163, 163);
  607. foregroundWarningColorDisabled.setRgb(217, 217, 217);
  608. foregroundErrorColorDisabled.setRgb(113, 113, 113);
  609. clipColor.setRgb(0xff, 0xff, 0xff); // Bright white
  610. magnitudeColor.setRgb(0x00, 0x00, 0x00); // Black
  611. majorTickColor.setRgb(0xff, 0xff, 0xff); // Black
  612. minorTickColor.setRgb(0xcc, 0xcc, 0xcc); // Black
  613. minimumLevel = -60.0; // -60 dB
  614. warningLevel = -20.0; // -20 dB
  615. errorLevel = -9.0; // -9 dB
  616. clipLevel = -0.5; // -0.5 dB
  617. minimumInputLevel = -50.0; // -50 dB
  618. peakDecayRate = 11.76; // 20 dB / 1.7 sec
  619. magnitudeIntegrationTime = 0.3; // 99% in 300 ms
  620. peakHoldDuration = 20.0; // 20 seconds
  621. inputPeakHoldDuration = 1.0; // 1 second
  622. meterThickness = 3; // Bar thickness in pixels
  623. meterFontScaling =
  624. 0.7; // Font size for numbers is 70% of Widget's font size
  625. channels = (int)audio_output_get_channels(obs_get_audio());
  626. doLayout();
  627. updateTimerRef = updateTimer.toStrongRef();
  628. if (!updateTimerRef) {
  629. updateTimerRef = QSharedPointer<VolumeMeterTimer>::create();
  630. updateTimerRef->setTimerType(Qt::PreciseTimer);
  631. updateTimerRef->start(16);
  632. updateTimer = updateTimerRef;
  633. }
  634. updateTimerRef->AddVolControl(this);
  635. }
  636. VolumeMeter::~VolumeMeter()
  637. {
  638. updateTimerRef->RemoveVolControl(this);
  639. }
  640. void VolumeMeter::setLevels(const float magnitude[MAX_AUDIO_CHANNELS],
  641. const float peak[MAX_AUDIO_CHANNELS],
  642. const float inputPeak[MAX_AUDIO_CHANNELS])
  643. {
  644. uint64_t ts = os_gettime_ns();
  645. QMutexLocker locker(&dataMutex);
  646. currentLastUpdateTime = ts;
  647. for (int channelNr = 0; channelNr < MAX_AUDIO_CHANNELS; channelNr++) {
  648. currentMagnitude[channelNr] = magnitude[channelNr];
  649. currentPeak[channelNr] = peak[channelNr];
  650. currentInputPeak[channelNr] = inputPeak[channelNr];
  651. }
  652. // In case there are more updates then redraws we must make sure
  653. // that the ballistics of peak and hold are recalculated.
  654. locker.unlock();
  655. calculateBallistics(ts);
  656. }
  657. inline void VolumeMeter::resetLevels()
  658. {
  659. currentLastUpdateTime = 0;
  660. for (int channelNr = 0; channelNr < MAX_AUDIO_CHANNELS; channelNr++) {
  661. currentMagnitude[channelNr] = -M_INFINITE;
  662. currentPeak[channelNr] = -M_INFINITE;
  663. currentInputPeak[channelNr] = -M_INFINITE;
  664. displayMagnitude[channelNr] = -M_INFINITE;
  665. displayPeak[channelNr] = -M_INFINITE;
  666. displayPeakHold[channelNr] = -M_INFINITE;
  667. displayPeakHoldLastUpdateTime[channelNr] = 0;
  668. displayInputPeakHold[channelNr] = -M_INFINITE;
  669. displayInputPeakHoldLastUpdateTime[channelNr] = 0;
  670. }
  671. }
  672. bool VolumeMeter::needLayoutChange()
  673. {
  674. int currentNrAudioChannels = obs_volmeter_get_nr_channels(obs_volmeter);
  675. if (!currentNrAudioChannels) {
  676. struct obs_audio_info oai;
  677. obs_get_audio_info(&oai);
  678. currentNrAudioChannels = (oai.speakers == SPEAKERS_MONO) ? 1
  679. : 2;
  680. }
  681. if (displayNrAudioChannels != currentNrAudioChannels) {
  682. displayNrAudioChannels = currentNrAudioChannels;
  683. recalculateLayout = true;
  684. }
  685. return recalculateLayout;
  686. }
  687. // When this is called from the constructor, obs_volmeter_get_nr_channels has not
  688. // yet been called and Q_PROPERTY settings have not yet been read from the
  689. // stylesheet.
  690. inline void VolumeMeter::doLayout()
  691. {
  692. QMutexLocker locker(&dataMutex);
  693. recalculateLayout = false;
  694. tickFont = font();
  695. QFontInfo info(tickFont);
  696. tickFont.setPointSizeF(info.pointSizeF() * meterFontScaling);
  697. QFontMetrics metrics(tickFont);
  698. if (vertical) {
  699. // Each meter channel is meterThickness pixels wide, plus one pixel
  700. // between channels, but not after the last.
  701. // Add 4 pixels for ticks, space to hold our longest label in this font,
  702. // and a few pixels before the fader.
  703. QRect scaleBounds = metrics.boundingRect("-88");
  704. setMinimumSize(displayNrAudioChannels * (meterThickness + 1) -
  705. 1 + 4 + scaleBounds.width() + 2,
  706. 130);
  707. } else {
  708. // Each meter channel is meterThickness pixels high, plus one pixel
  709. // between channels, but not after the last.
  710. // Add 4 pixels for ticks, and space high enough to hold our label in
  711. // this font, presuming that digits don't have descenders.
  712. setMinimumSize(130,
  713. displayNrAudioChannels * (meterThickness + 1) -
  714. 1 + 4 + metrics.capHeight());
  715. }
  716. resetLevels();
  717. }
  718. inline bool VolumeMeter::detectIdle(uint64_t ts)
  719. {
  720. double timeSinceLastUpdate = (ts - currentLastUpdateTime) * 0.000000001;
  721. if (timeSinceLastUpdate > 0.5) {
  722. resetLevels();
  723. return true;
  724. } else {
  725. return false;
  726. }
  727. }
  728. inline void
  729. VolumeMeter::calculateBallisticsForChannel(int channelNr, uint64_t ts,
  730. qreal timeSinceLastRedraw)
  731. {
  732. if (currentPeak[channelNr] >= displayPeak[channelNr] ||
  733. isnan(displayPeak[channelNr])) {
  734. // Attack of peak is immediate.
  735. displayPeak[channelNr] = currentPeak[channelNr];
  736. } else {
  737. // Decay of peak is 40 dB / 1.7 seconds for Fast Profile
  738. // 20 dB / 1.7 seconds for Medium Profile (Type I PPM)
  739. // 24 dB / 2.8 seconds for Slow Profile (Type II PPM)
  740. float decay = float(peakDecayRate * timeSinceLastRedraw);
  741. displayPeak[channelNr] = CLAMP(displayPeak[channelNr] - decay,
  742. currentPeak[channelNr], 0);
  743. }
  744. if (currentPeak[channelNr] >= displayPeakHold[channelNr] ||
  745. !isfinite(displayPeakHold[channelNr])) {
  746. // Attack of peak-hold is immediate, but keep track
  747. // when it was last updated.
  748. displayPeakHold[channelNr] = currentPeak[channelNr];
  749. displayPeakHoldLastUpdateTime[channelNr] = ts;
  750. } else {
  751. // The peak and hold falls back to peak
  752. // after 20 seconds.
  753. qreal timeSinceLastPeak =
  754. (uint64_t)(ts -
  755. displayPeakHoldLastUpdateTime[channelNr]) *
  756. 0.000000001;
  757. if (timeSinceLastPeak > peakHoldDuration) {
  758. displayPeakHold[channelNr] = currentPeak[channelNr];
  759. displayPeakHoldLastUpdateTime[channelNr] = ts;
  760. }
  761. }
  762. if (currentInputPeak[channelNr] >= displayInputPeakHold[channelNr] ||
  763. !isfinite(displayInputPeakHold[channelNr])) {
  764. // Attack of peak-hold is immediate, but keep track
  765. // when it was last updated.
  766. displayInputPeakHold[channelNr] = currentInputPeak[channelNr];
  767. displayInputPeakHoldLastUpdateTime[channelNr] = ts;
  768. } else {
  769. // The peak and hold falls back to peak after 1 second.
  770. qreal timeSinceLastPeak =
  771. (uint64_t)(ts -
  772. displayInputPeakHoldLastUpdateTime[channelNr]) *
  773. 0.000000001;
  774. if (timeSinceLastPeak > inputPeakHoldDuration) {
  775. displayInputPeakHold[channelNr] =
  776. currentInputPeak[channelNr];
  777. displayInputPeakHoldLastUpdateTime[channelNr] = ts;
  778. }
  779. }
  780. if (!isfinite(displayMagnitude[channelNr])) {
  781. // The statements in the else-leg do not work with
  782. // NaN and infinite displayMagnitude.
  783. displayMagnitude[channelNr] = currentMagnitude[channelNr];
  784. } else {
  785. // A VU meter will integrate to the new value to 99% in 300 ms.
  786. // The calculation here is very simplified and is more accurate
  787. // with higher frame-rate.
  788. float attack =
  789. float((currentMagnitude[channelNr] -
  790. displayMagnitude[channelNr]) *
  791. (timeSinceLastRedraw / magnitudeIntegrationTime) *
  792. 0.99);
  793. displayMagnitude[channelNr] =
  794. CLAMP(displayMagnitude[channelNr] + attack,
  795. (float)minimumLevel, 0);
  796. }
  797. }
  798. inline void VolumeMeter::calculateBallistics(uint64_t ts,
  799. qreal timeSinceLastRedraw)
  800. {
  801. QMutexLocker locker(&dataMutex);
  802. for (int channelNr = 0; channelNr < MAX_AUDIO_CHANNELS; channelNr++)
  803. calculateBallisticsForChannel(channelNr, ts,
  804. timeSinceLastRedraw);
  805. }
  806. void VolumeMeter::paintInputMeter(QPainter &painter, int x, int y, int width,
  807. int height, float peakHold)
  808. {
  809. QMutexLocker locker(&dataMutex);
  810. QColor color;
  811. if (peakHold < minimumInputLevel)
  812. color = backgroundNominalColor;
  813. else if (peakHold < warningLevel)
  814. color = foregroundNominalColor;
  815. else if (peakHold < errorLevel)
  816. color = foregroundWarningColor;
  817. else if (peakHold <= clipLevel)
  818. color = foregroundErrorColor;
  819. else
  820. color = clipColor;
  821. painter.fillRect(x, y, width, height, color);
  822. }
  823. void VolumeMeter::paintHTicks(QPainter &painter, int x, int y, int width)
  824. {
  825. qreal scale = width / minimumLevel;
  826. painter.setFont(tickFont);
  827. QFontMetrics metrics(tickFont);
  828. painter.setPen(majorTickColor);
  829. // Draw major tick lines and numeric indicators.
  830. for (int i = 0; i >= minimumLevel; i -= 5) {
  831. int position = int(x + width - (i * scale) - 1);
  832. QString str = QString::number(i);
  833. // Center the number on the tick, but don't overflow
  834. QRect textBounds = metrics.boundingRect(str);
  835. int pos;
  836. if (i == 0) {
  837. pos = position - textBounds.width();
  838. } else {
  839. pos = position - (textBounds.width() / 2);
  840. if (pos < 0)
  841. pos = 0;
  842. }
  843. painter.drawText(pos, y + 4 + metrics.capHeight(), str);
  844. painter.drawLine(position, y, position, y + 2);
  845. }
  846. // Draw minor tick lines.
  847. painter.setPen(minorTickColor);
  848. for (int i = 0; i >= minimumLevel; i--) {
  849. int position = int(x + width - (i * scale) - 1);
  850. if (i % 5 != 0)
  851. painter.drawLine(position, y, position, y + 1);
  852. }
  853. }
  854. void VolumeMeter::paintVTicks(QPainter &painter, int x, int y, int height)
  855. {
  856. qreal scale = height / minimumLevel;
  857. painter.setFont(tickFont);
  858. QFontMetrics metrics(tickFont);
  859. painter.setPen(majorTickColor);
  860. // Draw major tick lines and numeric indicators.
  861. for (int i = 0; i >= minimumLevel; i -= 5) {
  862. int position = y + int(i * scale) + METER_PADDING;
  863. QString str = QString::number(i);
  864. // Center the number on the tick, but don't overflow
  865. if (i == 0) {
  866. painter.drawText(x + 6, position + metrics.capHeight(),
  867. str);
  868. } else {
  869. painter.drawText(x + 4,
  870. position + (metrics.capHeight() / 2),
  871. str);
  872. }
  873. painter.drawLine(x, position, x + 2, position);
  874. }
  875. // Draw minor tick lines.
  876. painter.setPen(minorTickColor);
  877. for (int i = 0; i >= minimumLevel; i--) {
  878. int position = y + int(i * scale) + METER_PADDING;
  879. if (i % 5 != 0)
  880. painter.drawLine(x, position, x + 1, position);
  881. }
  882. }
  883. #define CLIP_FLASH_DURATION_MS 1000
  884. void VolumeMeter::ClipEnding()
  885. {
  886. clipping = false;
  887. }
  888. inline int VolumeMeter::convertToInt(float number)
  889. {
  890. constexpr int min = std::numeric_limits<int>::min();
  891. constexpr int max = std::numeric_limits<int>::max();
  892. if (number > max)
  893. return max;
  894. else if (number < min)
  895. return min;
  896. else
  897. return int(number);
  898. }
  899. void VolumeMeter::paintHMeter(QPainter &painter, int x, int y, int width,
  900. int height, float magnitude, float peak,
  901. float peakHold)
  902. {
  903. qreal scale = width / minimumLevel;
  904. QMutexLocker locker(&dataMutex);
  905. int minimumPosition = x + 0;
  906. int maximumPosition = x + width;
  907. int magnitudePosition = x + width - convertToInt(magnitude * scale);
  908. int peakPosition = x + width - convertToInt(peak * scale);
  909. int peakHoldPosition = x + width - convertToInt(peakHold * scale);
  910. int warningPosition = x + width - convertToInt(warningLevel * scale);
  911. int errorPosition = x + width - convertToInt(errorLevel * scale);
  912. int nominalLength = warningPosition - minimumPosition;
  913. int warningLength = errorPosition - warningPosition;
  914. int errorLength = maximumPosition - errorPosition;
  915. locker.unlock();
  916. if (clipping) {
  917. peakPosition = maximumPosition;
  918. }
  919. if (peakPosition < minimumPosition) {
  920. painter.fillRect(minimumPosition, y, nominalLength, height,
  921. muted ? backgroundNominalColorDisabled
  922. : backgroundNominalColor);
  923. painter.fillRect(warningPosition, y, warningLength, height,
  924. muted ? backgroundWarningColorDisabled
  925. : backgroundWarningColor);
  926. painter.fillRect(errorPosition, y, errorLength, height,
  927. muted ? backgroundErrorColorDisabled
  928. : backgroundErrorColor);
  929. } else if (peakPosition < warningPosition) {
  930. painter.fillRect(minimumPosition, y,
  931. peakPosition - minimumPosition, height,
  932. muted ? foregroundNominalColorDisabled
  933. : foregroundNominalColor);
  934. painter.fillRect(peakPosition, y,
  935. warningPosition - peakPosition, height,
  936. muted ? backgroundNominalColorDisabled
  937. : backgroundNominalColor);
  938. painter.fillRect(warningPosition, y, warningLength, height,
  939. muted ? backgroundWarningColorDisabled
  940. : backgroundWarningColor);
  941. painter.fillRect(errorPosition, y, errorLength, height,
  942. muted ? backgroundErrorColorDisabled
  943. : backgroundErrorColor);
  944. } else if (peakPosition < errorPosition) {
  945. painter.fillRect(minimumPosition, y, nominalLength, height,
  946. muted ? foregroundNominalColorDisabled
  947. : foregroundNominalColor);
  948. painter.fillRect(warningPosition, y,
  949. peakPosition - warningPosition, height,
  950. muted ? foregroundWarningColorDisabled
  951. : foregroundWarningColor);
  952. painter.fillRect(peakPosition, y, errorPosition - peakPosition,
  953. height,
  954. muted ? backgroundWarningColorDisabled
  955. : backgroundWarningColor);
  956. painter.fillRect(errorPosition, y, errorLength, height,
  957. muted ? backgroundErrorColorDisabled
  958. : backgroundErrorColor);
  959. } else if (peakPosition < maximumPosition) {
  960. painter.fillRect(minimumPosition, y, nominalLength, height,
  961. muted ? foregroundNominalColorDisabled
  962. : foregroundNominalColor);
  963. painter.fillRect(warningPosition, y, warningLength, height,
  964. muted ? foregroundWarningColorDisabled
  965. : foregroundWarningColor);
  966. painter.fillRect(errorPosition, y, peakPosition - errorPosition,
  967. height,
  968. muted ? foregroundErrorColorDisabled
  969. : foregroundErrorColor);
  970. painter.fillRect(peakPosition, y,
  971. maximumPosition - peakPosition, height,
  972. muted ? backgroundErrorColorDisabled
  973. : backgroundErrorColor);
  974. } else if (int(magnitude) != 0) {
  975. if (!clipping) {
  976. QTimer::singleShot(CLIP_FLASH_DURATION_MS, this,
  977. SLOT(ClipEnding()));
  978. clipping = true;
  979. }
  980. int end = errorLength + warningLength + nominalLength;
  981. painter.fillRect(minimumPosition, y, end, height,
  982. QBrush(muted ? foregroundErrorColorDisabled
  983. : foregroundErrorColor));
  984. }
  985. if (peakHoldPosition - 3 < minimumPosition)
  986. ; // Peak-hold below minimum, no drawing.
  987. else if (peakHoldPosition < warningPosition)
  988. painter.fillRect(peakHoldPosition - 3, y, 3, height,
  989. muted ? foregroundNominalColorDisabled
  990. : foregroundNominalColor);
  991. else if (peakHoldPosition < errorPosition)
  992. painter.fillRect(peakHoldPosition - 3, y, 3, height,
  993. muted ? foregroundWarningColorDisabled
  994. : foregroundWarningColor);
  995. else
  996. painter.fillRect(peakHoldPosition - 3, y, 3, height,
  997. muted ? foregroundErrorColorDisabled
  998. : foregroundErrorColor);
  999. if (magnitudePosition - 3 >= minimumPosition)
  1000. painter.fillRect(magnitudePosition - 3, y, 3, height,
  1001. magnitudeColor);
  1002. }
  1003. void VolumeMeter::paintVMeter(QPainter &painter, int x, int y, int width,
  1004. int height, float magnitude, float peak,
  1005. float peakHold)
  1006. {
  1007. qreal scale = height / minimumLevel;
  1008. QMutexLocker locker(&dataMutex);
  1009. int minimumPosition = y + 0;
  1010. int maximumPosition = y + height;
  1011. int magnitudePosition = y + height - convertToInt(magnitude * scale);
  1012. int peakPosition = y + height - convertToInt(peak * scale);
  1013. int peakHoldPosition = y + height - convertToInt(peakHold * scale);
  1014. int warningPosition = y + height - convertToInt(warningLevel * scale);
  1015. int errorPosition = y + height - convertToInt(errorLevel * scale);
  1016. int nominalLength = warningPosition - minimumPosition;
  1017. int warningLength = errorPosition - warningPosition;
  1018. int errorLength = maximumPosition - errorPosition;
  1019. locker.unlock();
  1020. if (clipping) {
  1021. peakPosition = maximumPosition;
  1022. }
  1023. if (peakPosition < minimumPosition) {
  1024. painter.fillRect(x, minimumPosition, width, nominalLength,
  1025. muted ? backgroundNominalColorDisabled
  1026. : backgroundNominalColor);
  1027. painter.fillRect(x, warningPosition, width, warningLength,
  1028. muted ? backgroundWarningColorDisabled
  1029. : backgroundWarningColor);
  1030. painter.fillRect(x, errorPosition, width, errorLength,
  1031. muted ? backgroundErrorColorDisabled
  1032. : backgroundErrorColor);
  1033. } else if (peakPosition < warningPosition) {
  1034. painter.fillRect(x, minimumPosition, width,
  1035. peakPosition - minimumPosition,
  1036. muted ? foregroundNominalColorDisabled
  1037. : foregroundNominalColor);
  1038. painter.fillRect(x, peakPosition, width,
  1039. warningPosition - peakPosition,
  1040. muted ? backgroundNominalColorDisabled
  1041. : backgroundNominalColor);
  1042. painter.fillRect(x, warningPosition, width, warningLength,
  1043. muted ? backgroundWarningColorDisabled
  1044. : backgroundWarningColor);
  1045. painter.fillRect(x, errorPosition, width, errorLength,
  1046. muted ? backgroundErrorColorDisabled
  1047. : backgroundErrorColor);
  1048. } else if (peakPosition < errorPosition) {
  1049. painter.fillRect(x, minimumPosition, width, nominalLength,
  1050. muted ? foregroundNominalColorDisabled
  1051. : foregroundNominalColor);
  1052. painter.fillRect(x, warningPosition, width,
  1053. peakPosition - warningPosition,
  1054. muted ? foregroundWarningColorDisabled
  1055. : foregroundWarningColor);
  1056. painter.fillRect(x, peakPosition, width,
  1057. errorPosition - peakPosition,
  1058. muted ? backgroundWarningColorDisabled
  1059. : backgroundWarningColor);
  1060. painter.fillRect(x, errorPosition, width, errorLength,
  1061. muted ? backgroundErrorColorDisabled
  1062. : backgroundErrorColor);
  1063. } else if (peakPosition < maximumPosition) {
  1064. painter.fillRect(x, minimumPosition, width, nominalLength,
  1065. muted ? foregroundNominalColorDisabled
  1066. : foregroundNominalColor);
  1067. painter.fillRect(x, warningPosition, width, warningLength,
  1068. muted ? foregroundWarningColorDisabled
  1069. : foregroundWarningColor);
  1070. painter.fillRect(x, errorPosition, width,
  1071. peakPosition - errorPosition,
  1072. muted ? foregroundErrorColorDisabled
  1073. : foregroundErrorColor);
  1074. painter.fillRect(x, peakPosition, width,
  1075. maximumPosition - peakPosition,
  1076. muted ? backgroundErrorColorDisabled
  1077. : backgroundErrorColor);
  1078. } else {
  1079. if (!clipping) {
  1080. QTimer::singleShot(CLIP_FLASH_DURATION_MS, this,
  1081. SLOT(ClipEnding()));
  1082. clipping = true;
  1083. }
  1084. int end = errorLength + warningLength + nominalLength;
  1085. painter.fillRect(x, minimumPosition, width, end,
  1086. QBrush(muted ? foregroundErrorColorDisabled
  1087. : foregroundErrorColor));
  1088. }
  1089. if (peakHoldPosition - 3 < minimumPosition)
  1090. ; // Peak-hold below minimum, no drawing.
  1091. else if (peakHoldPosition < warningPosition)
  1092. painter.fillRect(x, peakHoldPosition - 3, width, 3,
  1093. muted ? foregroundNominalColorDisabled
  1094. : foregroundNominalColor);
  1095. else if (peakHoldPosition < errorPosition)
  1096. painter.fillRect(x, peakHoldPosition - 3, width, 3,
  1097. muted ? foregroundWarningColorDisabled
  1098. : foregroundWarningColor);
  1099. else
  1100. painter.fillRect(x, peakHoldPosition - 3, width, 3,
  1101. muted ? foregroundErrorColorDisabled
  1102. : foregroundErrorColor);
  1103. if (magnitudePosition - 3 >= minimumPosition)
  1104. painter.fillRect(x, magnitudePosition - 3, width, 3,
  1105. magnitudeColor);
  1106. }
  1107. void VolumeMeter::paintEvent(QPaintEvent *event)
  1108. {
  1109. uint64_t ts = os_gettime_ns();
  1110. qreal timeSinceLastRedraw = (ts - lastRedrawTime) * 0.000000001;
  1111. calculateBallistics(ts, timeSinceLastRedraw);
  1112. bool idle = detectIdle(ts);
  1113. QRect widgetRect = rect();
  1114. int width = widgetRect.width();
  1115. int height = widgetRect.height();
  1116. QPainter painter(this);
  1117. if (vertical)
  1118. height -= METER_PADDING * 2;
  1119. // timerEvent requests update of the bar(s) only, so we can avoid the
  1120. // overhead of repainting the scale and labels.
  1121. if (event->region().boundingRect() != getBarRect()) {
  1122. if (needLayoutChange())
  1123. doLayout();
  1124. // Paint window background color (as widget is opaque)
  1125. QColor background =
  1126. palette().color(QPalette::ColorRole::Window);
  1127. painter.fillRect(widgetRect, background);
  1128. if (vertical) {
  1129. paintVTicks(painter,
  1130. displayNrAudioChannels *
  1131. (meterThickness + 1) -
  1132. 1,
  1133. 0, height - (INDICATOR_THICKNESS + 3));
  1134. } else {
  1135. paintHTicks(painter, INDICATOR_THICKNESS + 3,
  1136. displayNrAudioChannels *
  1137. (meterThickness + 1) -
  1138. 1,
  1139. width - (INDICATOR_THICKNESS + 3));
  1140. }
  1141. }
  1142. if (vertical) {
  1143. // Invert the Y axis to ease the math
  1144. painter.translate(0, height + METER_PADDING);
  1145. painter.scale(1, -1);
  1146. }
  1147. for (int channelNr = 0; channelNr < displayNrAudioChannels;
  1148. channelNr++) {
  1149. int channelNrFixed =
  1150. (displayNrAudioChannels == 1 && channels > 2)
  1151. ? 2
  1152. : channelNr;
  1153. if (vertical)
  1154. paintVMeter(painter, channelNr * (meterThickness + 1),
  1155. INDICATOR_THICKNESS + 2, meterThickness,
  1156. height - (INDICATOR_THICKNESS + 2),
  1157. displayMagnitude[channelNrFixed],
  1158. displayPeak[channelNrFixed],
  1159. displayPeakHold[channelNrFixed]);
  1160. else
  1161. paintHMeter(painter, INDICATOR_THICKNESS + 2,
  1162. channelNr * (meterThickness + 1),
  1163. width - (INDICATOR_THICKNESS + 2),
  1164. meterThickness,
  1165. displayMagnitude[channelNrFixed],
  1166. displayPeak[channelNrFixed],
  1167. displayPeakHold[channelNrFixed]);
  1168. if (idle)
  1169. continue;
  1170. // By not drawing the input meter boxes the user can
  1171. // see that the audio stream has been stopped, without
  1172. // having too much visual impact.
  1173. if (vertical)
  1174. paintInputMeter(painter,
  1175. channelNr * (meterThickness + 1), 0,
  1176. meterThickness, INDICATOR_THICKNESS,
  1177. displayInputPeakHold[channelNrFixed]);
  1178. else
  1179. paintInputMeter(painter, 0,
  1180. channelNr * (meterThickness + 1),
  1181. INDICATOR_THICKNESS, meterThickness,
  1182. displayInputPeakHold[channelNrFixed]);
  1183. }
  1184. lastRedrawTime = ts;
  1185. }
  1186. QRect VolumeMeter::getBarRect() const
  1187. {
  1188. QRect rec = rect();
  1189. if (vertical)
  1190. rec.setWidth(displayNrAudioChannels * (meterThickness + 1) - 1);
  1191. else
  1192. rec.setHeight(displayNrAudioChannels * (meterThickness + 1) -
  1193. 1);
  1194. return rec;
  1195. }
  1196. void VolumeMeter::changeEvent(QEvent *e)
  1197. {
  1198. if (e->type() == QEvent::StyleChange)
  1199. recalculateLayout = true;
  1200. QWidget::changeEvent(e);
  1201. }
  1202. void VolumeMeterTimer::AddVolControl(VolumeMeter *meter)
  1203. {
  1204. volumeMeters.push_back(meter);
  1205. }
  1206. void VolumeMeterTimer::RemoveVolControl(VolumeMeter *meter)
  1207. {
  1208. volumeMeters.removeOne(meter);
  1209. }
  1210. void VolumeMeterTimer::timerEvent(QTimerEvent *)
  1211. {
  1212. for (VolumeMeter *meter : volumeMeters) {
  1213. if (meter->needLayoutChange()) {
  1214. // Tell paintEvent to update layout and paint everything
  1215. meter->update();
  1216. } else {
  1217. // Tell paintEvent to paint only the bars
  1218. meter->update(meter->getBarRect());
  1219. }
  1220. }
  1221. }