volume-control.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344
  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 (displayNrAudioChannels != currentNrAudioChannels) {
  610. displayNrAudioChannels = currentNrAudioChannels;
  611. recalculateLayout = true;
  612. }
  613. return recalculateLayout;
  614. }
  615. // When this is called from the constructor, obs_volmeter_get_nr_channels returns 1
  616. // and Q_PROPERTY settings have not yet been read from the stylesheet.
  617. inline void VolumeMeter::doLayout()
  618. {
  619. QMutexLocker locker(&dataMutex);
  620. recalculateLayout = false;
  621. tickFont = font();
  622. QFontInfo info(tickFont);
  623. tickFont.setPointSizeF(info.pointSizeF() * meterFontScaling);
  624. QFontMetrics metrics(tickFont);
  625. if (vertical) {
  626. // Each meter channel is meterThickness pixels wide, plus one pixel
  627. // between channels, but not after the last.
  628. // Add 4 pixels for ticks, space to hold our longest label in this font,
  629. // and a few pixels before the fader.
  630. QRect scaleBounds = metrics.boundingRect("-88");
  631. setMinimumSize(displayNrAudioChannels * (meterThickness + 1) -
  632. 1 + 4 + scaleBounds.width() + 2,
  633. 130);
  634. } else {
  635. // Each meter channel is meterThickness pixels high, plus one pixel
  636. // between channels, but not after the last.
  637. // Add 4 pixels for ticks, and space high enough to hold our label in
  638. // this font, presuming that digits don't have descenders.
  639. setMinimumSize(130,
  640. displayNrAudioChannels * (meterThickness + 1) -
  641. 1 + 4 + metrics.capHeight());
  642. }
  643. resetLevels();
  644. }
  645. inline bool VolumeMeter::detectIdle(uint64_t ts)
  646. {
  647. double timeSinceLastUpdate = (ts - currentLastUpdateTime) * 0.000000001;
  648. if (timeSinceLastUpdate > 0.5) {
  649. resetLevels();
  650. return true;
  651. } else {
  652. return false;
  653. }
  654. }
  655. inline void
  656. VolumeMeter::calculateBallisticsForChannel(int channelNr, uint64_t ts,
  657. qreal timeSinceLastRedraw)
  658. {
  659. if (currentPeak[channelNr] >= displayPeak[channelNr] ||
  660. isnan(displayPeak[channelNr])) {
  661. // Attack of peak is immediate.
  662. displayPeak[channelNr] = currentPeak[channelNr];
  663. } else {
  664. // Decay of peak is 40 dB / 1.7 seconds for Fast Profile
  665. // 20 dB / 1.7 seconds for Medium Profile (Type I PPM)
  666. // 24 dB / 2.8 seconds for Slow Profile (Type II PPM)
  667. float decay = float(peakDecayRate * timeSinceLastRedraw);
  668. displayPeak[channelNr] = CLAMP(displayPeak[channelNr] - decay,
  669. currentPeak[channelNr], 0);
  670. }
  671. if (currentPeak[channelNr] >= displayPeakHold[channelNr] ||
  672. !isfinite(displayPeakHold[channelNr])) {
  673. // Attack of peak-hold is immediate, but keep track
  674. // when it was last updated.
  675. displayPeakHold[channelNr] = currentPeak[channelNr];
  676. displayPeakHoldLastUpdateTime[channelNr] = ts;
  677. } else {
  678. // The peak and hold falls back to peak
  679. // after 20 seconds.
  680. qreal timeSinceLastPeak =
  681. (uint64_t)(ts -
  682. displayPeakHoldLastUpdateTime[channelNr]) *
  683. 0.000000001;
  684. if (timeSinceLastPeak > peakHoldDuration) {
  685. displayPeakHold[channelNr] = currentPeak[channelNr];
  686. displayPeakHoldLastUpdateTime[channelNr] = ts;
  687. }
  688. }
  689. if (currentInputPeak[channelNr] >= displayInputPeakHold[channelNr] ||
  690. !isfinite(displayInputPeakHold[channelNr])) {
  691. // Attack of peak-hold is immediate, but keep track
  692. // when it was last updated.
  693. displayInputPeakHold[channelNr] = currentInputPeak[channelNr];
  694. displayInputPeakHoldLastUpdateTime[channelNr] = ts;
  695. } else {
  696. // The peak and hold falls back to peak after 1 second.
  697. qreal timeSinceLastPeak =
  698. (uint64_t)(ts -
  699. displayInputPeakHoldLastUpdateTime[channelNr]) *
  700. 0.000000001;
  701. if (timeSinceLastPeak > inputPeakHoldDuration) {
  702. displayInputPeakHold[channelNr] =
  703. currentInputPeak[channelNr];
  704. displayInputPeakHoldLastUpdateTime[channelNr] = ts;
  705. }
  706. }
  707. if (!isfinite(displayMagnitude[channelNr])) {
  708. // The statements in the else-leg do not work with
  709. // NaN and infinite displayMagnitude.
  710. displayMagnitude[channelNr] = currentMagnitude[channelNr];
  711. } else {
  712. // A VU meter will integrate to the new value to 99% in 300 ms.
  713. // The calculation here is very simplified and is more accurate
  714. // with higher frame-rate.
  715. float attack =
  716. float((currentMagnitude[channelNr] -
  717. displayMagnitude[channelNr]) *
  718. (timeSinceLastRedraw / magnitudeIntegrationTime) *
  719. 0.99);
  720. displayMagnitude[channelNr] =
  721. CLAMP(displayMagnitude[channelNr] + attack,
  722. (float)minimumLevel, 0);
  723. }
  724. }
  725. inline void VolumeMeter::calculateBallistics(uint64_t ts,
  726. qreal timeSinceLastRedraw)
  727. {
  728. QMutexLocker locker(&dataMutex);
  729. for (int channelNr = 0; channelNr < MAX_AUDIO_CHANNELS; channelNr++)
  730. calculateBallisticsForChannel(channelNr, ts,
  731. timeSinceLastRedraw);
  732. }
  733. void VolumeMeter::paintInputMeter(QPainter &painter, int x, int y, int width,
  734. int height, float peakHold)
  735. {
  736. QMutexLocker locker(&dataMutex);
  737. QColor color;
  738. if (peakHold < minimumInputLevel)
  739. color = backgroundNominalColor;
  740. else if (peakHold < warningLevel)
  741. color = foregroundNominalColor;
  742. else if (peakHold < errorLevel)
  743. color = foregroundWarningColor;
  744. else if (peakHold <= clipLevel)
  745. color = foregroundErrorColor;
  746. else
  747. color = clipColor;
  748. painter.fillRect(x, y, width, height, color);
  749. }
  750. void VolumeMeter::paintHTicks(QPainter &painter, int x, int y, int width)
  751. {
  752. qreal scale = width / minimumLevel;
  753. painter.setFont(tickFont);
  754. QFontMetrics metrics(tickFont);
  755. painter.setPen(majorTickColor);
  756. // Draw major tick lines and numeric indicators.
  757. for (int i = 0; i >= minimumLevel; i -= 5) {
  758. int position = int(x + width - (i * scale) - 1);
  759. QString str = QString::number(i);
  760. // Center the number on the tick, but don't overflow
  761. QRect textBounds = metrics.boundingRect(str);
  762. int pos;
  763. if (i == 0) {
  764. pos = position - textBounds.width();
  765. } else {
  766. pos = position - (textBounds.width() / 2);
  767. if (pos < 0)
  768. pos = 0;
  769. }
  770. painter.drawText(pos, y + 4 + metrics.capHeight(), str);
  771. painter.drawLine(position, y, position, y + 2);
  772. }
  773. // Draw minor tick lines.
  774. painter.setPen(minorTickColor);
  775. for (int i = 0; i >= minimumLevel; i--) {
  776. int position = int(x + width - (i * scale) - 1);
  777. if (i % 5 != 0)
  778. painter.drawLine(position, y, position, y + 1);
  779. }
  780. }
  781. void VolumeMeter::paintVTicks(QPainter &painter, int x, int y, int height)
  782. {
  783. qreal scale = height / minimumLevel;
  784. painter.setFont(tickFont);
  785. QFontMetrics metrics(tickFont);
  786. painter.setPen(majorTickColor);
  787. // Draw major tick lines and numeric indicators.
  788. for (int i = 0; i >= minimumLevel; i -= 5) {
  789. int position = y + int(i * scale);
  790. QString str = QString::number(i);
  791. // Center the number on the tick, but don't overflow
  792. if (i == 0) {
  793. painter.drawText(x + 6, position + metrics.capHeight(),
  794. str);
  795. } else {
  796. painter.drawText(x + 4,
  797. position + (metrics.capHeight() / 2),
  798. str);
  799. }
  800. painter.drawLine(x, position, x + 2, position);
  801. }
  802. // Draw minor tick lines.
  803. painter.setPen(minorTickColor);
  804. for (int i = 0; i >= minimumLevel; i--) {
  805. int position = y + int(i * scale);
  806. if (i % 5 != 0)
  807. painter.drawLine(x, position, x + 1, position);
  808. }
  809. }
  810. #define CLIP_FLASH_DURATION_MS 1000
  811. void VolumeMeter::ClipEnding()
  812. {
  813. clipping = false;
  814. }
  815. void VolumeMeter::paintHMeter(QPainter &painter, int x, int y, int width,
  816. int height, float magnitude, float peak,
  817. float peakHold)
  818. {
  819. qreal scale = width / minimumLevel;
  820. QMutexLocker locker(&dataMutex);
  821. int minimumPosition = x + 0;
  822. int maximumPosition = x + width;
  823. int magnitudePosition = int(x + width - (magnitude * scale));
  824. int peakPosition = int(x + width - (peak * scale));
  825. int peakHoldPosition = int(x + width - (peakHold * scale));
  826. int warningPosition = int(x + width - (warningLevel * scale));
  827. int errorPosition = int(x + width - (errorLevel * scale));
  828. int nominalLength = warningPosition - minimumPosition;
  829. int warningLength = errorPosition - warningPosition;
  830. int errorLength = maximumPosition - errorPosition;
  831. locker.unlock();
  832. if (clipping) {
  833. peakPosition = maximumPosition;
  834. }
  835. if (peakPosition < minimumPosition) {
  836. painter.fillRect(minimumPosition, y, nominalLength, height,
  837. muted ? backgroundNominalColorDisabled
  838. : backgroundNominalColor);
  839. painter.fillRect(warningPosition, y, warningLength, height,
  840. muted ? backgroundWarningColorDisabled
  841. : backgroundWarningColor);
  842. painter.fillRect(errorPosition, y, errorLength, height,
  843. muted ? backgroundErrorColorDisabled
  844. : backgroundErrorColor);
  845. } else if (peakPosition < warningPosition) {
  846. painter.fillRect(minimumPosition, y,
  847. peakPosition - minimumPosition, height,
  848. muted ? foregroundNominalColorDisabled
  849. : foregroundNominalColor);
  850. painter.fillRect(peakPosition, y,
  851. warningPosition - peakPosition, height,
  852. muted ? backgroundNominalColorDisabled
  853. : backgroundNominalColor);
  854. painter.fillRect(warningPosition, y, warningLength, height,
  855. muted ? backgroundWarningColorDisabled
  856. : backgroundWarningColor);
  857. painter.fillRect(errorPosition, y, errorLength, height,
  858. muted ? backgroundErrorColorDisabled
  859. : backgroundErrorColor);
  860. } else if (peakPosition < errorPosition) {
  861. painter.fillRect(minimumPosition, y, nominalLength, height,
  862. muted ? foregroundNominalColorDisabled
  863. : foregroundNominalColor);
  864. painter.fillRect(warningPosition, y,
  865. peakPosition - warningPosition, height,
  866. muted ? foregroundWarningColorDisabled
  867. : foregroundWarningColor);
  868. painter.fillRect(peakPosition, y, errorPosition - peakPosition,
  869. height,
  870. muted ? backgroundWarningColorDisabled
  871. : backgroundWarningColor);
  872. painter.fillRect(errorPosition, y, errorLength, height,
  873. muted ? backgroundErrorColorDisabled
  874. : backgroundErrorColor);
  875. } else if (peakPosition < maximumPosition) {
  876. painter.fillRect(minimumPosition, y, nominalLength, height,
  877. muted ? foregroundNominalColorDisabled
  878. : foregroundNominalColor);
  879. painter.fillRect(warningPosition, y, warningLength, height,
  880. muted ? foregroundWarningColorDisabled
  881. : foregroundWarningColor);
  882. painter.fillRect(errorPosition, y, peakPosition - errorPosition,
  883. height,
  884. muted ? foregroundErrorColorDisabled
  885. : foregroundErrorColor);
  886. painter.fillRect(peakPosition, y,
  887. maximumPosition - peakPosition, height,
  888. muted ? backgroundErrorColorDisabled
  889. : backgroundErrorColor);
  890. } else if (int(magnitude) != 0) {
  891. if (!clipping) {
  892. QTimer::singleShot(CLIP_FLASH_DURATION_MS, this,
  893. SLOT(ClipEnding()));
  894. clipping = true;
  895. }
  896. int end = errorLength + warningLength + nominalLength;
  897. painter.fillRect(minimumPosition, y, end, height,
  898. QBrush(muted ? foregroundErrorColorDisabled
  899. : foregroundErrorColor));
  900. }
  901. if (peakHoldPosition - 3 < minimumPosition)
  902. ; // Peak-hold below minimum, no drawing.
  903. else if (peakHoldPosition < warningPosition)
  904. painter.fillRect(peakHoldPosition - 3, y, 3, height,
  905. muted ? foregroundNominalColorDisabled
  906. : foregroundNominalColor);
  907. else if (peakHoldPosition < errorPosition)
  908. painter.fillRect(peakHoldPosition - 3, y, 3, height,
  909. muted ? foregroundWarningColorDisabled
  910. : foregroundWarningColor);
  911. else
  912. painter.fillRect(peakHoldPosition - 3, y, 3, height,
  913. muted ? foregroundErrorColorDisabled
  914. : foregroundErrorColor);
  915. if (magnitudePosition - 3 >= minimumPosition)
  916. painter.fillRect(magnitudePosition - 3, y, 3, height,
  917. magnitudeColor);
  918. }
  919. void VolumeMeter::paintVMeter(QPainter &painter, int x, int y, int width,
  920. int height, float magnitude, float peak,
  921. float peakHold)
  922. {
  923. qreal scale = height / minimumLevel;
  924. QMutexLocker locker(&dataMutex);
  925. int minimumPosition = y + 0;
  926. int maximumPosition = y + height;
  927. int magnitudePosition = int(y + height - (magnitude * scale));
  928. int peakPosition = int(y + height - (peak * scale));
  929. int peakHoldPosition = int(y + height - (peakHold * scale));
  930. int warningPosition = int(y + height - (warningLevel * scale));
  931. int errorPosition = int(y + height - (errorLevel * scale));
  932. int nominalLength = warningPosition - minimumPosition;
  933. int warningLength = errorPosition - warningPosition;
  934. int errorLength = maximumPosition - errorPosition;
  935. locker.unlock();
  936. if (clipping) {
  937. peakPosition = maximumPosition;
  938. }
  939. if (peakPosition < minimumPosition) {
  940. painter.fillRect(x, minimumPosition, width, nominalLength,
  941. muted ? backgroundNominalColorDisabled
  942. : backgroundNominalColor);
  943. painter.fillRect(x, warningPosition, width, warningLength,
  944. muted ? backgroundWarningColorDisabled
  945. : backgroundWarningColor);
  946. painter.fillRect(x, errorPosition, width, errorLength,
  947. muted ? backgroundErrorColorDisabled
  948. : backgroundErrorColor);
  949. } else if (peakPosition < warningPosition) {
  950. painter.fillRect(x, minimumPosition, width,
  951. peakPosition - minimumPosition,
  952. muted ? foregroundNominalColorDisabled
  953. : foregroundNominalColor);
  954. painter.fillRect(x, peakPosition, width,
  955. warningPosition - peakPosition,
  956. muted ? backgroundNominalColorDisabled
  957. : backgroundNominalColor);
  958. painter.fillRect(x, warningPosition, width, warningLength,
  959. muted ? backgroundWarningColorDisabled
  960. : backgroundWarningColor);
  961. painter.fillRect(x, errorPosition, width, errorLength,
  962. muted ? backgroundErrorColorDisabled
  963. : backgroundErrorColor);
  964. } else if (peakPosition < errorPosition) {
  965. painter.fillRect(x, minimumPosition, width, nominalLength,
  966. muted ? foregroundNominalColorDisabled
  967. : foregroundNominalColor);
  968. painter.fillRect(x, warningPosition, width,
  969. peakPosition - warningPosition,
  970. muted ? foregroundWarningColorDisabled
  971. : foregroundWarningColor);
  972. painter.fillRect(x, peakPosition, width,
  973. errorPosition - peakPosition,
  974. muted ? backgroundWarningColorDisabled
  975. : backgroundWarningColor);
  976. painter.fillRect(x, errorPosition, width, errorLength,
  977. muted ? backgroundErrorColorDisabled
  978. : backgroundErrorColor);
  979. } else if (peakPosition < maximumPosition) {
  980. painter.fillRect(x, minimumPosition, width, nominalLength,
  981. muted ? foregroundNominalColorDisabled
  982. : foregroundNominalColor);
  983. painter.fillRect(x, warningPosition, width, warningLength,
  984. muted ? foregroundWarningColorDisabled
  985. : foregroundWarningColor);
  986. painter.fillRect(x, errorPosition, width,
  987. peakPosition - errorPosition,
  988. muted ? foregroundErrorColorDisabled
  989. : foregroundErrorColor);
  990. painter.fillRect(x, peakPosition, width,
  991. maximumPosition - peakPosition,
  992. muted ? backgroundErrorColorDisabled
  993. : backgroundErrorColor);
  994. } else {
  995. if (!clipping) {
  996. QTimer::singleShot(CLIP_FLASH_DURATION_MS, this,
  997. SLOT(ClipEnding()));
  998. clipping = true;
  999. }
  1000. int end = errorLength + warningLength + nominalLength;
  1001. painter.fillRect(x, minimumPosition, width, end,
  1002. QBrush(muted ? foregroundErrorColorDisabled
  1003. : foregroundErrorColor));
  1004. }
  1005. if (peakHoldPosition - 3 < minimumPosition)
  1006. ; // Peak-hold below minimum, no drawing.
  1007. else if (peakHoldPosition < warningPosition)
  1008. painter.fillRect(x, peakHoldPosition - 3, width, 3,
  1009. muted ? foregroundNominalColorDisabled
  1010. : foregroundNominalColor);
  1011. else if (peakHoldPosition < errorPosition)
  1012. painter.fillRect(x, peakHoldPosition - 3, width, 3,
  1013. muted ? foregroundWarningColorDisabled
  1014. : foregroundWarningColor);
  1015. else
  1016. painter.fillRect(x, peakHoldPosition - 3, width, 3,
  1017. muted ? foregroundErrorColorDisabled
  1018. : foregroundErrorColor);
  1019. if (magnitudePosition - 3 >= minimumPosition)
  1020. painter.fillRect(x, magnitudePosition - 3, width, 3,
  1021. magnitudeColor);
  1022. }
  1023. void VolumeMeter::paintEvent(QPaintEvent *event)
  1024. {
  1025. uint64_t ts = os_gettime_ns();
  1026. qreal timeSinceLastRedraw = (ts - lastRedrawTime) * 0.000000001;
  1027. calculateBallistics(ts, timeSinceLastRedraw);
  1028. bool idle = detectIdle(ts);
  1029. QRect widgetRect = rect();
  1030. int width = widgetRect.width();
  1031. int height = widgetRect.height();
  1032. QPainter painter(this);
  1033. // timerEvent requests update of the bar(s) only, so we can avoid the
  1034. // overhead of repainting the scale and labels.
  1035. if (event->region().boundingRect() != getBarRect()) {
  1036. if (needLayoutChange())
  1037. doLayout();
  1038. // Paint window background color (as widget is opaque)
  1039. QColor background =
  1040. palette().color(QPalette::ColorRole::Window);
  1041. painter.fillRect(widgetRect, background);
  1042. if (vertical) {
  1043. paintVTicks(painter,
  1044. displayNrAudioChannels *
  1045. (meterThickness + 1) -
  1046. 1,
  1047. 0, height - (INDICATOR_THICKNESS + 3));
  1048. } else {
  1049. paintHTicks(painter, INDICATOR_THICKNESS + 3,
  1050. displayNrAudioChannels *
  1051. (meterThickness + 1) -
  1052. 1,
  1053. width - (INDICATOR_THICKNESS + 3));
  1054. }
  1055. }
  1056. if (vertical) {
  1057. // Invert the Y axis to ease the math
  1058. painter.translate(0, height);
  1059. painter.scale(1, -1);
  1060. }
  1061. for (int channelNr = 0; channelNr < displayNrAudioChannels;
  1062. channelNr++) {
  1063. int channelNrFixed =
  1064. (displayNrAudioChannels == 1 && channels > 2)
  1065. ? 2
  1066. : channelNr;
  1067. if (vertical)
  1068. paintVMeter(painter, channelNr * (meterThickness + 1),
  1069. INDICATOR_THICKNESS + 2, meterThickness,
  1070. height - (INDICATOR_THICKNESS + 2),
  1071. displayMagnitude[channelNrFixed],
  1072. displayPeak[channelNrFixed],
  1073. displayPeakHold[channelNrFixed]);
  1074. else
  1075. paintHMeter(painter, INDICATOR_THICKNESS + 2,
  1076. channelNr * (meterThickness + 1),
  1077. width - (INDICATOR_THICKNESS + 2),
  1078. meterThickness,
  1079. displayMagnitude[channelNrFixed],
  1080. displayPeak[channelNrFixed],
  1081. displayPeakHold[channelNrFixed]);
  1082. if (idle)
  1083. continue;
  1084. // By not drawing the input meter boxes the user can
  1085. // see that the audio stream has been stopped, without
  1086. // having too much visual impact.
  1087. if (vertical)
  1088. paintInputMeter(painter,
  1089. channelNr * (meterThickness + 1), 0,
  1090. meterThickness, INDICATOR_THICKNESS,
  1091. displayInputPeakHold[channelNrFixed]);
  1092. else
  1093. paintInputMeter(painter, 0,
  1094. channelNr * (meterThickness + 1),
  1095. INDICATOR_THICKNESS, meterThickness,
  1096. displayInputPeakHold[channelNrFixed]);
  1097. }
  1098. lastRedrawTime = ts;
  1099. }
  1100. QRect VolumeMeter::getBarRect() const
  1101. {
  1102. QRect rec = rect();
  1103. if (vertical)
  1104. rec.setWidth(displayNrAudioChannels * (meterThickness + 1) - 1);
  1105. else
  1106. rec.setHeight(displayNrAudioChannels * (meterThickness + 1) -
  1107. 1);
  1108. return rec;
  1109. }
  1110. void VolumeMeter::changeEvent(QEvent *e)
  1111. {
  1112. if (e->type() == QEvent::StyleChange)
  1113. recalculateLayout = true;
  1114. QWidget::changeEvent(e);
  1115. }
  1116. void VolumeMeterTimer::AddVolControl(VolumeMeter *meter)
  1117. {
  1118. volumeMeters.push_back(meter);
  1119. }
  1120. void VolumeMeterTimer::RemoveVolControl(VolumeMeter *meter)
  1121. {
  1122. volumeMeters.removeOne(meter);
  1123. }
  1124. void VolumeMeterTimer::timerEvent(QTimerEvent *)
  1125. {
  1126. for (VolumeMeter *meter : volumeMeters) {
  1127. if (meter->needLayoutChange()) {
  1128. // Tell paintEvent to update layout and paint everything
  1129. meter->update();
  1130. } else {
  1131. // Tell paintEvent to paint only the bars
  1132. meter->update(meter->getBarRect());
  1133. }
  1134. }
  1135. }