volume-control.cpp 39 KB

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