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