volume-control.cpp 37 KB

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