volume-control.cpp 37 KB

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