volume-control.cpp 39 KB

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