volume-control.cpp 42 KB

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