VolControl.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. #include "VolControl.hpp"
  2. #include "VolumeMeter.hpp"
  3. #include "OBSBasic.hpp"
  4. #include <components/MuteCheckBox.hpp>
  5. #include <components/OBSSourceLabel.hpp>
  6. #include <components/VolumeSlider.hpp>
  7. #include <QMessageBox>
  8. #include "moc_VolControl.cpp"
  9. static inline Qt::CheckState GetCheckState(bool muted, bool unassigned)
  10. {
  11. if (muted)
  12. return Qt::Checked;
  13. else if (unassigned)
  14. return Qt::PartiallyChecked;
  15. else
  16. return Qt::Unchecked;
  17. }
  18. static inline bool IsSourceUnassigned(obs_source_t *source)
  19. {
  20. uint32_t mixes = (obs_source_get_audio_mixers(source) & ((1 << MAX_AUDIO_MIXES) - 1));
  21. obs_monitoring_type mt = obs_source_get_monitoring_type(source);
  22. return mixes == 0 && mt != OBS_MONITORING_TYPE_MONITOR_ONLY;
  23. }
  24. static void ShowUnassignedWarning(const char *name)
  25. {
  26. auto msgBox = [=]() {
  27. QMessageBox msgbox(App()->GetMainWindow());
  28. msgbox.setWindowTitle(QTStr("VolControl.UnassignedWarning.Title"));
  29. msgbox.setText(QTStr("VolControl.UnassignedWarning.Text").arg(name));
  30. msgbox.setIcon(QMessageBox::Icon::Information);
  31. msgbox.addButton(QMessageBox::Ok);
  32. QCheckBox *cb = new QCheckBox(QTStr("DoNotShowAgain"));
  33. msgbox.setCheckBox(cb);
  34. msgbox.exec();
  35. if (cb->isChecked()) {
  36. config_set_bool(App()->GetUserConfig(), "General", "WarnedAboutUnassignedSources", true);
  37. config_save_safe(App()->GetUserConfig(), "tmp", nullptr);
  38. }
  39. };
  40. QMetaObject::invokeMethod(App(), "Exec", Qt::QueuedConnection, Q_ARG(VoidFunc, msgBox));
  41. }
  42. void VolControl::OBSVolumeChanged(void *data, float db)
  43. {
  44. Q_UNUSED(db);
  45. VolControl *volControl = static_cast<VolControl *>(data);
  46. QMetaObject::invokeMethod(volControl, "VolumeChanged");
  47. }
  48. void VolControl::OBSVolumeLevel(void *data, const float magnitude[MAX_AUDIO_CHANNELS],
  49. const float peak[MAX_AUDIO_CHANNELS], const float inputPeak[MAX_AUDIO_CHANNELS])
  50. {
  51. VolControl *volControl = static_cast<VolControl *>(data);
  52. volControl->volMeter->setLevels(magnitude, peak, inputPeak);
  53. }
  54. void VolControl::OBSVolumeMuted(void *data, calldata_t *calldata)
  55. {
  56. VolControl *volControl = static_cast<VolControl *>(data);
  57. bool muted = calldata_bool(calldata, "muted");
  58. QMetaObject::invokeMethod(volControl, "VolumeMuted", Q_ARG(bool, muted));
  59. }
  60. void VolControl::VolumeChanged()
  61. {
  62. slider->blockSignals(true);
  63. slider->setValue((int)(obs_fader_get_deflection(obs_fader) * FADER_PRECISION));
  64. slider->blockSignals(false);
  65. updateText();
  66. }
  67. void VolControl::VolumeMuted(bool muted)
  68. {
  69. bool unassigned = IsSourceUnassigned(source);
  70. auto newState = GetCheckState(muted, unassigned);
  71. if (mute->checkState() != newState)
  72. mute->setCheckState(newState);
  73. volMeter->muted = muted || unassigned;
  74. }
  75. void VolControl::OBSMixersOrMonitoringChanged(void *data, calldata_t *)
  76. {
  77. VolControl *volControl = static_cast<VolControl *>(data);
  78. QMetaObject::invokeMethod(volControl, "MixersOrMonitoringChanged", Qt::QueuedConnection);
  79. }
  80. void VolControl::MixersOrMonitoringChanged()
  81. {
  82. bool muted = obs_source_muted(source);
  83. bool unassigned = IsSourceUnassigned(source);
  84. auto newState = GetCheckState(muted, unassigned);
  85. if (mute->checkState() != newState)
  86. mute->setCheckState(newState);
  87. volMeter->muted = muted || unassigned;
  88. }
  89. void VolControl::SetMuted(bool)
  90. {
  91. bool checked = mute->checkState() == Qt::Checked;
  92. bool prev = obs_source_muted(source);
  93. obs_source_set_muted(source, checked);
  94. bool unassigned = IsSourceUnassigned(source);
  95. if (!checked && unassigned) {
  96. mute->setCheckState(Qt::PartiallyChecked);
  97. /* Show notice about the source no being assigned to any tracks */
  98. bool has_shown_warning =
  99. config_get_bool(App()->GetUserConfig(), "General", "WarnedAboutUnassignedSources");
  100. if (!has_shown_warning)
  101. ShowUnassignedWarning(obs_source_get_name(source));
  102. }
  103. auto undo_redo = [](const std::string &uuid, bool val) {
  104. OBSSourceAutoRelease source = obs_get_source_by_uuid(uuid.c_str());
  105. obs_source_set_muted(source, val);
  106. };
  107. QString text = QTStr(checked ? "Undo.Volume.Mute" : "Undo.Volume.Unmute");
  108. const char *name = obs_source_get_name(source);
  109. const char *uuid = obs_source_get_uuid(source);
  110. OBSBasic::Get()->undo_s.add_action(text.arg(name), std::bind(undo_redo, std::placeholders::_1, prev),
  111. std::bind(undo_redo, std::placeholders::_1, checked), uuid, uuid);
  112. }
  113. void VolControl::SliderChanged(int vol)
  114. {
  115. float prev = obs_source_get_volume(source);
  116. obs_fader_set_deflection(obs_fader, float(vol) / FADER_PRECISION);
  117. updateText();
  118. auto undo_redo = [](const std::string &uuid, float val) {
  119. OBSSourceAutoRelease source = obs_get_source_by_uuid(uuid.c_str());
  120. obs_source_set_volume(source, val);
  121. };
  122. float val = obs_source_get_volume(source);
  123. const char *name = obs_source_get_name(source);
  124. const char *uuid = obs_source_get_uuid(source);
  125. OBSBasic::Get()->undo_s.add_action(QTStr("Undo.Volume.Change").arg(name),
  126. std::bind(undo_redo, std::placeholders::_1, prev),
  127. std::bind(undo_redo, std::placeholders::_1, val), uuid, uuid, true);
  128. }
  129. void VolControl::updateText()
  130. {
  131. QString text;
  132. float db = obs_fader_get_db(obs_fader);
  133. if (db < -96.0f)
  134. text = "-inf dB";
  135. else
  136. text = QString::number(db, 'f', 1).append(" dB");
  137. volLabel->setText(text);
  138. bool muted = obs_source_muted(source);
  139. const char *accTextLookup = muted ? "VolControl.SliderMuted" : "VolControl.SliderUnmuted";
  140. QString sourceName = obs_source_get_name(source);
  141. QString accText = QTStr(accTextLookup).arg(sourceName);
  142. slider->setAccessibleName(accText);
  143. }
  144. void VolControl::EmitConfigClicked()
  145. {
  146. emit ConfigClicked();
  147. }
  148. void VolControl::SetMeterDecayRate(qreal q)
  149. {
  150. volMeter->setPeakDecayRate(q);
  151. }
  152. void VolControl::setPeakMeterType(enum obs_peak_meter_type peakMeterType)
  153. {
  154. volMeter->setPeakMeterType(peakMeterType);
  155. }
  156. VolControl::VolControl(OBSSource source_, bool showConfig, bool vertical)
  157. : source(std::move(source_)),
  158. levelTotal(0.0f),
  159. levelCount(0.0f),
  160. obs_fader(obs_fader_create(OBS_FADER_LOG)),
  161. obs_volmeter(obs_volmeter_create(OBS_FADER_LOG)),
  162. vertical(vertical),
  163. contextMenu(nullptr)
  164. {
  165. nameLabel = new OBSSourceLabel(source);
  166. volLabel = new QLabel();
  167. mute = new MuteCheckBox();
  168. volLabel->setObjectName("volLabel");
  169. volLabel->setAlignment(Qt::AlignCenter);
  170. #ifdef __APPLE__
  171. mute->setAttribute(Qt::WA_LayoutUsesWidgetRect);
  172. #endif
  173. QString sourceName = obs_source_get_name(source);
  174. setObjectName(sourceName);
  175. if (showConfig) {
  176. config = new QPushButton(this);
  177. config->setProperty("class", "icon-dots-vert");
  178. config->setAutoDefault(false);
  179. config->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  180. config->setAccessibleName(QTStr("VolControl.Properties").arg(sourceName));
  181. connect(config, &QAbstractButton::clicked, this, &VolControl::EmitConfigClicked);
  182. }
  183. QVBoxLayout *mainLayout = new QVBoxLayout;
  184. mainLayout->setContentsMargins(0, 0, 0, 0);
  185. mainLayout->setSpacing(0);
  186. if (vertical) {
  187. QHBoxLayout *nameLayout = new QHBoxLayout;
  188. QHBoxLayout *controlLayout = new QHBoxLayout;
  189. QHBoxLayout *volLayout = new QHBoxLayout;
  190. QFrame *meterFrame = new QFrame;
  191. QHBoxLayout *meterLayout = new QHBoxLayout;
  192. volMeter = new VolumeMeter(nullptr, obs_volmeter, true);
  193. slider = new VolumeSlider(obs_fader, Qt::Vertical);
  194. slider->setLayoutDirection(Qt::LeftToRight);
  195. slider->setDisplayTicks(true);
  196. nameLayout->setAlignment(Qt::AlignCenter);
  197. meterLayout->setAlignment(Qt::AlignCenter);
  198. controlLayout->setAlignment(Qt::AlignCenter);
  199. volLayout->setAlignment(Qt::AlignCenter);
  200. meterFrame->setObjectName("volMeterFrame");
  201. nameLayout->setContentsMargins(0, 0, 0, 0);
  202. nameLayout->setSpacing(0);
  203. nameLayout->addWidget(nameLabel);
  204. controlLayout->setContentsMargins(0, 0, 0, 0);
  205. controlLayout->setSpacing(0);
  206. // Add Headphone (audio monitoring) widget here
  207. controlLayout->addWidget(mute);
  208. if (showConfig) {
  209. controlLayout->addWidget(config);
  210. }
  211. meterLayout->setContentsMargins(0, 0, 0, 0);
  212. meterLayout->setSpacing(0);
  213. meterLayout->addWidget(slider);
  214. meterLayout->addWidget(volMeter);
  215. meterFrame->setLayout(meterLayout);
  216. volLayout->setContentsMargins(0, 0, 0, 0);
  217. volLayout->setSpacing(0);
  218. volLayout->addWidget(volLabel);
  219. volLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::MinimumExpanding, QSizePolicy::Minimum));
  220. mainLayout->addItem(nameLayout);
  221. mainLayout->addItem(volLayout);
  222. mainLayout->addWidget(meterFrame);
  223. mainLayout->addItem(controlLayout);
  224. volMeter->setFocusProxy(slider);
  225. // Default size can cause clipping of long names in vertical layout.
  226. QFont font = nameLabel->font();
  227. QFontInfo info(font);
  228. nameLabel->setFont(font);
  229. setMaximumWidth(110);
  230. } else {
  231. QHBoxLayout *textLayout = new QHBoxLayout;
  232. QHBoxLayout *controlLayout = new QHBoxLayout;
  233. QFrame *meterFrame = new QFrame;
  234. QVBoxLayout *meterLayout = new QVBoxLayout;
  235. QVBoxLayout *buttonLayout = new QVBoxLayout;
  236. volMeter = new VolumeMeter(nullptr, obs_volmeter, false);
  237. volMeter->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
  238. slider = new VolumeSlider(obs_fader, Qt::Horizontal);
  239. slider->setLayoutDirection(Qt::LeftToRight);
  240. slider->setDisplayTicks(true);
  241. textLayout->setContentsMargins(0, 0, 0, 0);
  242. textLayout->addWidget(nameLabel);
  243. textLayout->addWidget(volLabel);
  244. textLayout->setAlignment(nameLabel, Qt::AlignLeft);
  245. textLayout->setAlignment(volLabel, Qt::AlignRight);
  246. meterFrame->setObjectName("volMeterFrame");
  247. meterFrame->setLayout(meterLayout);
  248. meterLayout->setContentsMargins(0, 0, 0, 0);
  249. meterLayout->setSpacing(0);
  250. meterLayout->addWidget(volMeter);
  251. meterLayout->addWidget(slider);
  252. buttonLayout->setContentsMargins(0, 0, 0, 0);
  253. buttonLayout->setSpacing(0);
  254. if (showConfig) {
  255. buttonLayout->addWidget(config);
  256. }
  257. buttonLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::MinimumExpanding));
  258. buttonLayout->addWidget(mute);
  259. controlLayout->addItem(buttonLayout);
  260. controlLayout->addWidget(meterFrame);
  261. mainLayout->addItem(textLayout);
  262. mainLayout->addItem(controlLayout);
  263. volMeter->setFocusProxy(slider);
  264. }
  265. setLayout(mainLayout);
  266. nameLabel->setText(sourceName);
  267. slider->setMinimum(0);
  268. slider->setMaximum(int(FADER_PRECISION));
  269. bool muted = obs_source_muted(source);
  270. bool unassigned = IsSourceUnassigned(source);
  271. mute->setCheckState(GetCheckState(muted, unassigned));
  272. volMeter->muted = muted || unassigned;
  273. mute->setAccessibleName(QTStr("VolControl.Mute").arg(sourceName));
  274. obs_fader_add_callback(obs_fader, OBSVolumeChanged, this);
  275. obs_volmeter_add_callback(obs_volmeter, OBSVolumeLevel, this);
  276. sigs.emplace_back(obs_source_get_signal_handler(source), "mute", OBSVolumeMuted, this);
  277. sigs.emplace_back(obs_source_get_signal_handler(source), "audio_mixers", OBSMixersOrMonitoringChanged, this);
  278. sigs.emplace_back(obs_source_get_signal_handler(source), "audio_monitoring", OBSMixersOrMonitoringChanged,
  279. this);
  280. QWidget::connect(slider, &VolumeSlider::valueChanged, this, &VolControl::SliderChanged);
  281. QWidget::connect(mute, &MuteCheckBox::clicked, this, &VolControl::SetMuted);
  282. obs_fader_attach_source(obs_fader, source);
  283. obs_volmeter_attach_source(obs_volmeter, source);
  284. /* Call volume changed once to init the slider position and label */
  285. VolumeChanged();
  286. }
  287. void VolControl::EnableSlider(bool enable)
  288. {
  289. slider->setEnabled(enable);
  290. }
  291. VolControl::~VolControl()
  292. {
  293. obs_fader_remove_callback(obs_fader, OBSVolumeChanged, this);
  294. obs_volmeter_remove_callback(obs_volmeter, OBSVolumeLevel, this);
  295. sigs.clear();
  296. if (contextMenu)
  297. contextMenu->close();
  298. }
  299. void VolControl::refreshColors()
  300. {
  301. volMeter->setBackgroundNominalColor(volMeter->getBackgroundNominalColor());
  302. volMeter->setBackgroundWarningColor(volMeter->getBackgroundWarningColor());
  303. volMeter->setBackgroundErrorColor(volMeter->getBackgroundErrorColor());
  304. volMeter->setForegroundNominalColor(volMeter->getForegroundNominalColor());
  305. volMeter->setForegroundWarningColor(volMeter->getForegroundWarningColor());
  306. volMeter->setForegroundErrorColor(volMeter->getForegroundErrorColor());
  307. }