AudioMixer.cpp 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  1. /******************************************************************************
  2. Copyright (C) 2025 by Taylor Giampaolo <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "AudioMixer.hpp"
  15. #include <components/MenuCheckBox.hpp>
  16. #include <dialogs/NameDialog.hpp>
  17. #include <utility/item-widget-helpers.hpp>
  18. #include <widgets/OBSBasic.hpp>
  19. #include <Idian/Utils.hpp>
  20. #include <QAction>
  21. #include <QCheckBox>
  22. #include <QHBoxLayout>
  23. #include <QMenu>
  24. #include <QPointer>
  25. #include <QPushButton>
  26. #include <QScrollArea>
  27. #include <QStackedWidget>
  28. #include <QTimer>
  29. #include <QToolBar>
  30. #include <QVBoxLayout>
  31. #include <QWidgetAction>
  32. #include "moc_AudioMixer.cpp"
  33. constexpr int GLOBAL_SOURCE_TOTAL = 6;
  34. namespace {
  35. bool isHiddenInMixer(obs_source_t *source)
  36. {
  37. OBSDataAutoRelease priv_settings = obs_source_get_private_settings(source);
  38. bool hidden = obs_data_get_bool(priv_settings, "mixer_hidden");
  39. return hidden;
  40. }
  41. bool isPinnedInMixer(obs_source_t *source)
  42. {
  43. OBSDataAutoRelease priv_settings = obs_source_get_private_settings(source);
  44. bool hidden = obs_data_get_bool(priv_settings, "mixer_pinned");
  45. return hidden;
  46. }
  47. bool isSourceAudioActive(obs_source_t *source)
  48. {
  49. bool active = obs_source_active(source) && obs_source_audio_active(source);
  50. return active;
  51. }
  52. bool isVolumeLocked(obs_source_t *source)
  53. {
  54. OBSDataAutoRelease priv_settings = obs_source_get_private_settings(source);
  55. bool lock = obs_data_get_bool(priv_settings, "volume_locked");
  56. return lock;
  57. }
  58. } // namespace
  59. AudioMixer::AudioMixer(QWidget *parent) : QFrame(parent)
  60. {
  61. mixerVertical = config_get_bool(App()->GetUserConfig(), "BasicWindow", "VerticalVolumeControl");
  62. showInactive = config_get_bool(App()->GetUserConfig(), "BasicWindow", "MixerShowInactive");
  63. keepInactiveLast = config_get_bool(App()->GetUserConfig(), "BasicWindow", "MixerKeepInactiveLast");
  64. showHidden = config_get_bool(App()->GetUserConfig(), "BasicWindow", "MixerShowHidden");
  65. keepHiddenLast = config_get_bool(App()->GetUserConfig(), "BasicWindow", "MixerKeepHiddenLast");
  66. mainLayout = new QVBoxLayout(this);
  67. mainLayout->setContentsMargins(0, 0, 0, 0);
  68. mainLayout->setSpacing(0);
  69. setLayout(mainLayout);
  70. setFrameShape(QFrame::NoFrame);
  71. setLineWidth(0);
  72. stackedMixerArea = new QStackedWidget(this);
  73. stackedMixerArea->setObjectName("stackedMixerArea");
  74. // Horizontal Widgets
  75. hMixerScrollArea = new QScrollArea(this);
  76. hMixerScrollArea->setObjectName("hMixerScrollArea");
  77. hMixerScrollArea->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  78. hMixerScrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
  79. hMixerScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  80. hMixerScrollArea->setWidgetResizable(true);
  81. hMixerScrollArea->setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu);
  82. hVolumeWidgets = new QWidget(this);
  83. hVolumeWidgets->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum);
  84. hVolumeWidgets->setObjectName("hVolumeWidgets");
  85. hVolumeControlLayout = new QVBoxLayout(hVolumeWidgets);
  86. hVolumeWidgets->setLayout(hVolumeControlLayout);
  87. hVolumeControlLayout->setContentsMargins(0, 0, 0, 0);
  88. hVolumeControlLayout->setSpacing(0);
  89. hMixerScrollArea->setWidget(hVolumeWidgets);
  90. // Vertical Widgets
  91. vMixerScrollArea = new QScrollArea(this);
  92. vMixerScrollArea->setObjectName("vMixerScrollArea");
  93. vMixerScrollArea->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  94. vMixerScrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  95. vMixerScrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
  96. vMixerScrollArea->setWidgetResizable(true);
  97. vMixerScrollArea->setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu);
  98. vVolumeWidgets = new QWidget(this);
  99. vVolumeWidgets->setObjectName("vVolumeWidgets");
  100. vVolumeControlLayout = new QHBoxLayout(vVolumeWidgets);
  101. vVolumeWidgets->setLayout(vVolumeControlLayout);
  102. vVolumeControlLayout->setContentsMargins(0, 0, 0, 0);
  103. vVolumeControlLayout->setSpacing(0);
  104. vVolumeControlLayout->setAlignment(Qt::AlignLeft);
  105. vMixerScrollArea->setWidget(vVolumeWidgets);
  106. stackedMixerArea->addWidget(hMixerScrollArea);
  107. stackedMixerArea->addWidget(vMixerScrollArea);
  108. mixerToolbar = new QToolBar(this);
  109. mixerToolbar->setIconSize(QSize(16, 16));
  110. mixerToolbar->setFloatable(false);
  111. mainLayout->addWidget(stackedMixerArea);
  112. mainLayout->addWidget(mixerToolbar);
  113. advAudio = new QAction(this);
  114. advAudio->setText(QTStr("Basic.AdvAudio"));
  115. advAudio->setToolTip(QTStr("Basic.AdvAudio"));
  116. QIcon advIcon;
  117. advIcon.addFile(QString::fromUtf8(":/settings/images/settings/advanced.svg"), QSize(16, 16),
  118. QIcon::Mode::Normal, QIcon::State::Off);
  119. advAudio->setIcon(advIcon);
  120. advAudio->setObjectName("actionMixerToolbarAdvAudio");
  121. layoutButton = new QAction(this);
  122. layoutButton->setText("");
  123. layoutButton->setToolTip(QTStr("Basic.AudioMixer.Layout.Vertical"));
  124. QIcon layoutIcon;
  125. layoutIcon.addFile(QString::fromUtf8(":/res/images/layout-vertical.svg"), QSize(16, 16), QIcon::Mode::Normal,
  126. QIcon::State::Off);
  127. layoutButton->setIcon(layoutIcon);
  128. layoutButton->setObjectName("actionMixerToolbarToggleLayout");
  129. QWidget *spacer = new QWidget(mixerToolbar);
  130. spacer->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Preferred);
  131. optionsButton = new QPushButton(mixerToolbar);
  132. optionsButton->setText(QTStr("Basic.AudioMixer.Options"));
  133. idian::Utils::addClass(optionsButton, "toolbar-button");
  134. idian::Utils::addClass(optionsButton, "text-bold");
  135. createMixerContextMenu();
  136. optionsButton->setMenu(mixerMenu);
  137. toggleHiddenButton = new QPushButton(mixerToolbar);
  138. toggleHiddenButton->setCheckable(true);
  139. toggleHiddenButton->setChecked(showHidden);
  140. toggleHiddenButton->setText(QTStr("Basic.AudioMixer.HiddenTotal").arg(0));
  141. toggleHiddenButton->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred);
  142. QIcon hiddenIcon;
  143. hiddenIcon.addFile(QString::fromUtf8(":/res/images/hidden.svg"), QSize(16, 16), QIcon::Mode::Normal,
  144. QIcon::State::Off);
  145. toggleHiddenButton->setIcon(hiddenIcon);
  146. idian::Utils::addClass(toggleHiddenButton, "toolbar-button");
  147. idian::Utils::addClass(toggleHiddenButton, "toggle-hidden");
  148. mixerToolbar->addWidget(toggleHiddenButton);
  149. mixerToolbar->addSeparator();
  150. mixerToolbar->addWidget(spacer);
  151. mixerToolbar->addSeparator();
  152. mixerToolbar->addAction(layoutButton);
  153. mixerToolbar->addSeparator();
  154. mixerToolbar->addAction(advAudio);
  155. mixerToolbar->addSeparator();
  156. mixerToolbar->addWidget(optionsButton);
  157. // Setting this property on the QAction itself does not seem to work despite
  158. // the UI files doing exactly that, so we set it on the action widget directly
  159. QWidget *advAudioWidget = mixerToolbar->widgetForAction(advAudio);
  160. idian::Utils::addClass(advAudioWidget, "icon-cogs");
  161. // Connect to OBS signals
  162. signalHandlers.reserve(signalHandlers.size() + 8);
  163. signalHandlers.emplace_back(obs_get_signal_handler(), "source_create", AudioMixer::obsSourceCreate, this);
  164. signalHandlers.emplace_back(obs_get_signal_handler(), "source_remove", AudioMixer::obsSourceRemove, this);
  165. signalHandlers.emplace_back(obs_get_signal_handler(), "source_destroy", AudioMixer::obsSourceRemove, this);
  166. signalHandlers.emplace_back(obs_get_signal_handler(), "source_rename", AudioMixer::obsSourceRename, this);
  167. signalHandlers.emplace_back(obs_get_signal_handler(), "source_activate", AudioMixer::obsSourceActivated, this);
  168. signalHandlers.emplace_back(obs_get_signal_handler(), "source_deactivate", AudioMixer::obsSourceDeactivated,
  169. this);
  170. signalHandlers.emplace_back(obs_get_signal_handler(), "source_audio_activate",
  171. AudioMixer::obsSourceAudioActivated, this);
  172. signalHandlers.emplace_back(obs_get_signal_handler(), "source_audio_deactivate",
  173. AudioMixer::obsSourceAudioDeactivated, this);
  174. obs_frontend_add_event_callback(AudioMixer::onFrontendEvent, this);
  175. // Connect to Qt signals
  176. connect(hMixerScrollArea, &QScrollArea::customContextMenuRequested, this,
  177. &AudioMixer::mixerContextMenuRequested);
  178. connect(vMixerScrollArea, &QScrollArea::customContextMenuRequested, this,
  179. &AudioMixer::mixerContextMenuRequested);
  180. connect(&updateTimer, &QTimer::timeout, this, &AudioMixer::updateVolumeLayouts);
  181. updateTimer.setSingleShot(true);
  182. OBSBasic *main = OBSBasic::Get();
  183. if (main) {
  184. connect(main, &OBSBasic::userSettingChanged, this,
  185. [this](const std::string &category, const std::string &name) {
  186. if (category == "BasicWindow" && name == "VerticalVolumeControl") {
  187. updateLayout();
  188. } else if (category == "BasicWindow" && name == "MixerShowInactive") {
  189. updateShowInactive();
  190. } else if (category == "BasicWindow" && name == "MixerKeepInactiveLast") {
  191. updateKeepInactiveLast();
  192. } else if (category == "BasicWindow" && name == "MixerShowHidden") {
  193. updateShowHidden();
  194. } else if (category == "BasicWindow" && name == "MixerKeepHiddenLast") {
  195. updateKeepHiddenLast();
  196. } else if (category == "BasicWindow" && name == "ShowListboxToolbars") {
  197. updateShowToolbar();
  198. } else if (category == "Accessibility" && name == "SettingsChanged") {
  199. refreshVolumeColors();
  200. }
  201. });
  202. connect(main, &OBSBasic::mixerStatusChanged, this, &AudioMixer::queueLayoutUpdate);
  203. connect(advAudio, &QAction::triggered, main, &OBSBasic::on_actionAdvAudioProperties_triggered,
  204. Qt::DirectConnection);
  205. connect(toggleHiddenButton, &QPushButton::clicked, this, &AudioMixer::toggleShowHidden);
  206. connect(layoutButton, &QAction::triggered, main, &OBSBasic::toggleMixerLayout);
  207. }
  208. updateShowToolbar();
  209. updatePreviewSources();
  210. updateGlobalSources();
  211. reloadVolumeControls();
  212. }
  213. AudioMixer::~AudioMixer()
  214. {
  215. signalHandlers.clear();
  216. previewSources.clear();
  217. globalSources.clear();
  218. clearVolumeControls();
  219. obs_frontend_remove_event_callback(AudioMixer::onFrontendEvent, this);
  220. }
  221. void AudioMixer::updateLayout()
  222. {
  223. bool vertical = config_get_bool(App()->GetUserConfig(), "BasicWindow", "VerticalVolumeControl");
  224. setMixerLayoutVertical(vertical);
  225. updateVolumeLayouts();
  226. }
  227. void AudioMixer::toggleShowInactive(bool checked)
  228. {
  229. config_set_bool(App()->GetUserConfig(), "BasicWindow", "MixerShowInactive", checked);
  230. OBSBasic *main = OBSBasic::Get();
  231. if (main) {
  232. emit main->userSettingChanged("BasicWindow", "MixerShowInactive");
  233. }
  234. }
  235. void AudioMixer::toggleKeepInactiveLast(bool checked)
  236. {
  237. config_set_bool(App()->GetUserConfig(), "BasicWindow", "MixerKeepInactiveLast", checked);
  238. OBSBasic *main = OBSBasic::Get();
  239. if (main) {
  240. emit main->userSettingChanged("BasicWindow", "MixerKeepInactiveLast");
  241. }
  242. }
  243. void AudioMixer::toggleShowHidden(bool checked)
  244. {
  245. config_set_bool(App()->GetUserConfig(), "BasicWindow", "MixerShowHidden", checked);
  246. OBSBasic *main = OBSBasic::Get();
  247. if (main) {
  248. emit main->userSettingChanged("BasicWindow", "MixerShowHidden");
  249. }
  250. }
  251. void AudioMixer::toggleKeepHiddenLast(bool checked)
  252. {
  253. config_set_bool(App()->GetUserConfig(), "BasicWindow", "MixerKeepHiddenLast", checked);
  254. OBSBasic *main = OBSBasic::Get();
  255. if (main) {
  256. emit main->userSettingChanged("BasicWindow", "MixerKeepHiddenLast");
  257. }
  258. }
  259. VolumeControl *AudioMixer::createVolumeControl(obs_source_t *source)
  260. {
  261. bool vertical = config_get_bool(App()->GetUserConfig(), "BasicWindow", "VerticalVolumeControl");
  262. VolumeControl *control = new VolumeControl(source, this, vertical);
  263. control->setGlobalInMixer(isSourceGlobal(source));
  264. control->setHiddenInMixer(isHiddenInMixer(source));
  265. control->setPinnedInMixer(isPinnedInMixer(source));
  266. control->enableSlider(!isVolumeLocked(source));
  267. OBSBasic *main = OBSBasic::Get();
  268. double meterDecayRate = config_get_double(main->Config(), "Audio", "MeterDecayRate");
  269. control->setMeterDecayRate(meterDecayRate);
  270. uint64_t peakMeterTypeIdx = config_get_uint(main->Config(), "Audio", "PeakMeterType");
  271. obs_peak_meter_type peakMeterType;
  272. switch (peakMeterTypeIdx) {
  273. case 0:
  274. peakMeterType = SAMPLE_PEAK_METER;
  275. break;
  276. case 1:
  277. peakMeterType = TRUE_PEAK_METER;
  278. break;
  279. default:
  280. peakMeterType = SAMPLE_PEAK_METER;
  281. break;
  282. }
  283. control->setPeakMeterType(peakMeterType);
  284. connect(control, &VolumeControl::unhideAll, this, &AudioMixer::unhideAllAudioControls);
  285. return control;
  286. }
  287. void AudioMixer::updateControlVisibility(QString uuid)
  288. {
  289. auto item = volumeList.find(uuid);
  290. if (item == volumeList.end()) {
  291. return;
  292. }
  293. VolumeControl *control = item->second;
  294. bool show = getMixerVisibilityForControl(control);
  295. if (show) {
  296. control->show();
  297. } else {
  298. control->hide();
  299. }
  300. queueLayoutUpdate();
  301. }
  302. void AudioMixer::sourceCreated(QString uuid)
  303. {
  304. addControlForUuid(uuid);
  305. updatePreviewSources();
  306. updateGlobalSources();
  307. }
  308. void AudioMixer::sourceRemoved(QString uuid)
  309. {
  310. removeControlForUuid(uuid);
  311. updatePreviewSources();
  312. updateGlobalSources();
  313. }
  314. void AudioMixer::updatePreviewSources()
  315. {
  316. bool isStudioMode = obs_frontend_preview_program_mode_active();
  317. clearPreviewSources();
  318. if (isStudioMode) {
  319. OBSSourceAutoRelease previewSource = obs_frontend_get_current_preview_scene();
  320. if (!previewSource) {
  321. return;
  322. }
  323. obs_scene_t *previewScene = obs_scene_from_source(previewSource);
  324. if (!previewScene) {
  325. return;
  326. }
  327. if (!previewScene) {
  328. return;
  329. }
  330. auto getPreviewSources = [this](obs_scene_t *, obs_sceneitem_t *item) {
  331. obs_source_t *source = obs_sceneitem_get_source(item);
  332. if (!source) {
  333. return true;
  334. }
  335. uint32_t flags = obs_source_get_output_flags(source);
  336. if ((flags & OBS_SOURCE_AUDIO) == 0) {
  337. return true;
  338. }
  339. auto uuidPointer = obs_source_get_uuid(source);
  340. if (uuidPointer && *uuidPointer) {
  341. previewSources.insert(QString::fromUtf8(uuidPointer));
  342. }
  343. return true;
  344. };
  345. using getPreviewSources_t = decltype(getPreviewSources);
  346. auto previewEnum = [](obs_scene_t *scene, obs_sceneitem_t *item, void *data) -> bool {
  347. return (*static_cast<getPreviewSources_t *>(data))(scene, item);
  348. };
  349. obs_scene_enum_items(previewScene, previewEnum, &getPreviewSources);
  350. }
  351. }
  352. void AudioMixer::updateGlobalSources()
  353. {
  354. globalSources.clear();
  355. for (int i = 1; i <= GLOBAL_SOURCE_TOTAL; i++) {
  356. OBSSourceAutoRelease source = obs_get_output_source(i);
  357. if (source) {
  358. auto uuidPointer = obs_source_get_uuid(source);
  359. if (uuidPointer && *uuidPointer) {
  360. globalSources.insert(QString::fromUtf8(uuidPointer));
  361. }
  362. }
  363. }
  364. queueLayoutUpdate();
  365. }
  366. QBoxLayout *AudioMixer::activeLayout() const
  367. {
  368. bool vertical = config_get_bool(App()->GetUserConfig(), "BasicWindow", "VerticalVolumeControl");
  369. QBoxLayout *layout = vertical ? static_cast<QBoxLayout *>(vVolumeControlLayout)
  370. : static_cast<QBoxLayout *>(hVolumeControlLayout);
  371. return layout;
  372. }
  373. void AudioMixer::reloadVolumeControls()
  374. {
  375. clearVolumeControls();
  376. auto createMixerControls = [](void *param, obs_source_t *source) -> bool {
  377. AudioMixer *mixer = static_cast<AudioMixer *>(param);
  378. uint32_t flags = obs_source_get_output_flags(source);
  379. if ((flags & OBS_SOURCE_AUDIO) == 0) {
  380. return true;
  381. }
  382. auto uuidPointer = obs_source_get_uuid(source);
  383. if (!uuidPointer || !*uuidPointer) {
  384. return true;
  385. }
  386. mixer->addControlForUuid(QString::fromUtf8(uuidPointer));
  387. return true;
  388. };
  389. obs_enum_sources(createMixerControls, this);
  390. queueLayoutUpdate();
  391. }
  392. bool AudioMixer::getMixerVisibilityForControl(VolumeControl *control)
  393. {
  394. bool isPinned = control->mixerStatus().has(VolumeControl::MixerStatus::Pinned);
  395. bool isPreviewed = control->mixerStatus().has(VolumeControl::MixerStatus::Preview);
  396. bool isHidden = control->mixerStatus().has(VolumeControl::MixerStatus::Hidden);
  397. bool isAudioActive = control->mixerStatus().has(VolumeControl::MixerStatus::Active);
  398. if (isPinned) {
  399. return true;
  400. }
  401. if (isHidden && showHidden) {
  402. return true;
  403. }
  404. if (!isAudioActive && showInactive) {
  405. return !isHidden;
  406. }
  407. if (isAudioActive) {
  408. return !isHidden;
  409. }
  410. if (isPreviewed) {
  411. return !isHidden;
  412. }
  413. return false;
  414. }
  415. void AudioMixer::clearPreviewSources()
  416. {
  417. previewSources.clear();
  418. }
  419. bool AudioMixer::isSourcePreviewed(obs_source_t *source)
  420. {
  421. if (!source) {
  422. return false;
  423. }
  424. auto uuidPointer = obs_source_get_uuid(source);
  425. if (!uuidPointer || !*uuidPointer) {
  426. return false;
  427. }
  428. if (previewSources.find(QString::fromUtf8(uuidPointer)) != previewSources.end()) {
  429. return true;
  430. }
  431. return false;
  432. }
  433. bool AudioMixer::isSourceGlobal(obs_source_t *source)
  434. {
  435. if (!source) {
  436. return false;
  437. }
  438. auto uuidPointer = obs_source_get_uuid(source);
  439. if (!uuidPointer || !*uuidPointer) {
  440. return false;
  441. }
  442. if (globalSources.find(QString::fromUtf8(uuidPointer)) != globalSources.end()) {
  443. return true;
  444. }
  445. return false;
  446. }
  447. void AudioMixer::clearVolumeControls()
  448. {
  449. for (const auto &[uuid, control] : volumeList) {
  450. if (control) {
  451. control->deleteLater();
  452. }
  453. }
  454. volumeList.clear();
  455. }
  456. void AudioMixer::refreshVolumeColors()
  457. {
  458. for (const auto &[uuid, control] : volumeList) {
  459. control->refreshColors();
  460. }
  461. }
  462. void AudioMixer::unhideAllAudioControls()
  463. {
  464. for (const auto &[uuid, control] : volumeList) {
  465. control->setHiddenInMixer(false);
  466. }
  467. queueLayoutUpdate();
  468. }
  469. void AudioMixer::queueLayoutUpdate()
  470. {
  471. if (!updateTimer.isActive()) {
  472. updateTimer.start(0);
  473. }
  474. }
  475. void AudioMixer::updateVolumeLayouts()
  476. {
  477. setUpdatesEnabled(false);
  478. hiddenCount = 0;
  479. bool vertical = config_get_bool(App()->GetUserConfig(), "BasicWindow", "VerticalVolumeControl");
  480. std::vector<RankedVolume> rankedVolumes;
  481. rankedVolumes.reserve(volumeList.size());
  482. for (const auto &entry : volumeList) {
  483. VolumeControl *control = entry.second;
  484. if (control) {
  485. int sortingWeight = 0;
  486. OBSSource source = OBSGetStrongRef(control->weakSource());
  487. if (!source) {
  488. const char *cachedName = control->getCachedName().toUtf8().constData();
  489. blog(LOG_INFO, "Tried to sort VolumeControl for '%s' but source is null", cachedName);
  490. continue;
  491. }
  492. bool isPreviewed = isSourcePreviewed(source);
  493. bool isGlobal = isSourceGlobal(source);
  494. bool isPinned = isPinnedInMixer(source);
  495. bool isHidden = isHiddenInMixer(source);
  496. bool isAudioActive = isSourceAudioActive(source);
  497. bool isLocked = isVolumeLocked(source);
  498. control->mixerStatus().set(VolumeControl::MixerStatus::Preview, isPreviewed);
  499. control->mixerStatus().set(VolumeControl::MixerStatus::Global, isGlobal);
  500. control->mixerStatus().set(VolumeControl::MixerStatus::Pinned, isPinned);
  501. control->mixerStatus().set(VolumeControl::MixerStatus::Hidden, isHidden);
  502. control->mixerStatus().set(VolumeControl::MixerStatus::Active, isAudioActive);
  503. control->mixerStatus().set(VolumeControl::MixerStatus::Locked, isLocked);
  504. if (isHidden) {
  505. hiddenCount += 1;
  506. }
  507. if (!isGlobal) {
  508. sortingWeight += 20;
  509. }
  510. if (!isPinned) {
  511. sortingWeight += 20;
  512. }
  513. if (isHidden && keepHiddenLast) {
  514. sortingWeight += 20;
  515. if (isPreviewed) {
  516. sortingWeight -= 10;
  517. }
  518. }
  519. if (!isAudioActive && keepInactiveLast) {
  520. sortingWeight += 50;
  521. if (isPreviewed) {
  522. sortingWeight -= 10;
  523. }
  524. }
  525. rankedVolumes.push_back({control, sortingWeight});
  526. }
  527. }
  528. std::sort(rankedVolumes.begin(), rankedVolumes.end(), [](const RankedVolume &a, const RankedVolume &b) {
  529. const QString &nameA = a.control->getCachedName();
  530. const QString &nameB = b.control->getCachedName();
  531. if (a.sortingWeight == b.sortingWeight) {
  532. return nameA.toLower() < nameB.toLower();
  533. }
  534. return a.sortingWeight < b.sortingWeight;
  535. });
  536. VolumeControl *prevControl = nullptr;
  537. int index = 0;
  538. QBoxLayout *layout = activeLayout();
  539. vMixerScrollArea->setWidgetResizable(false);
  540. hMixerScrollArea->setWidgetResizable(false);
  541. for (const auto &entry : rankedVolumes) {
  542. VolumeControl *volControl = entry.control;
  543. if (!volControl) {
  544. continue;
  545. }
  546. layout->insertWidget(index, volControl);
  547. volControl->setVertical(vertical);
  548. volControl->updateName();
  549. volControl->updateMixerState();
  550. bool showControl = getMixerVisibilityForControl(volControl);
  551. if (showControl) {
  552. volControl->show();
  553. } else {
  554. volControl->hide();
  555. }
  556. if (prevControl == nullptr) {
  557. setTabOrder(previousInFocusChain(), volControl->firstWidget());
  558. } else {
  559. setTabOrder(prevControl->lastWidget(), volControl->firstWidget());
  560. }
  561. volControl->updateTabOrder();
  562. prevControl = volControl;
  563. ++index;
  564. }
  565. toggleHiddenButton->setText(QTStr("Basic.AudioMixer.HiddenTotal").arg(hiddenCount));
  566. if (hiddenCount == 0) {
  567. toggleHiddenButton->setDisabled(true);
  568. idian::Utils::toggleClass(toggleHiddenButton, "text-muted", true);
  569. } else {
  570. toggleHiddenButton->setDisabled(false);
  571. idian::Utils::toggleClass(toggleHiddenButton, "text-muted", false);
  572. }
  573. vMixerScrollArea->setWidgetResizable(true);
  574. hMixerScrollArea->setWidgetResizable(true);
  575. setUpdatesEnabled(true);
  576. }
  577. void AudioMixer::mixerContextMenuRequested()
  578. {
  579. showMixerContextMenu();
  580. }
  581. void AudioMixer::setMixerLayoutVertical(bool vertical)
  582. {
  583. mixerVertical = vertical;
  584. if (vertical) {
  585. stackedMixerArea->setMinimumSize(180, 220);
  586. stackedMixerArea->setCurrentIndex(1);
  587. QIcon layoutIcon;
  588. layoutIcon.addFile(QString::fromUtf8(":/res/images/layout-horizontal.svg"), QSize(16, 16),
  589. QIcon::Mode::Normal, QIcon::State::Off);
  590. layoutButton->setIcon(layoutIcon);
  591. layoutButton->setToolTip(QTStr("Basic.AudioMixer.Layout.Horizontal"));
  592. } else {
  593. stackedMixerArea->setMinimumSize(220, 0);
  594. stackedMixerArea->setCurrentIndex(0);
  595. QIcon layoutIcon;
  596. layoutIcon.addFile(QString::fromUtf8(":/res/images/layout-vertical.svg"), QSize(16, 16),
  597. QIcon::Mode::Normal, QIcon::State::Off);
  598. layoutButton->setIcon(layoutIcon);
  599. layoutButton->setToolTip(QTStr("Basic.AudioMixer.Layout.Vertical"));
  600. }
  601. // Qt caches the size of QWidgetActions so this is the simplest way to update the text
  602. // of the checkboxes in the menu and make sure the menu size is recalculated.
  603. createMixerContextMenu();
  604. QWidget *buttonWidget = mixerToolbar->widgetForAction(layoutButton);
  605. if (buttonWidget) {
  606. idian::Utils::toggleClass(buttonWidget, "icon-layout-horizontal", vertical);
  607. idian::Utils::toggleClass(buttonWidget, "icon-layout-vertical", !vertical);
  608. }
  609. }
  610. void AudioMixer::createMixerContextMenu()
  611. {
  612. if (mixerMenu) {
  613. mixerMenu->deleteLater();
  614. }
  615. mixerMenu = new QMenu(this);
  616. // Create menu actions
  617. QAction *unhideAllAction = new QAction(QTStr("UnhideAll"), mixerMenu);
  618. showHiddenCheckBox = new MenuCheckBox(QTStr("Basic.AudioMixer.ShowHidden"), mixerMenu);
  619. QWidgetAction *showHiddenAction = new QWidgetAction(mixerMenu);
  620. showHiddenCheckBox->setAction(showHiddenAction);
  621. showHiddenCheckBox->setChecked(showHidden);
  622. showHiddenAction->setDefaultWidget(showHiddenCheckBox);
  623. QWidgetAction *showInactiveAction = new QWidgetAction(mixerMenu);
  624. MenuCheckBox *showInactiveCheckBox = new MenuCheckBox(QTStr("Basic.AudioMixer.ShowInactive"), mixerMenu);
  625. showInactiveCheckBox->setAction(showInactiveAction);
  626. showInactiveCheckBox->setChecked(showInactive);
  627. showInactiveAction->setDefaultWidget(showInactiveCheckBox);
  628. QWidgetAction *hiddenLastAction = new QWidgetAction(mixerMenu);
  629. const char *hiddenLastString = mixerVertical ? "Basic.AudioMixer.KeepHiddenRight"
  630. : "Basic.AudioMixer.KeepHiddenBottom";
  631. MenuCheckBox *hiddenLastCheckBox = new MenuCheckBox(QTStr(hiddenLastString), mixerMenu);
  632. hiddenLastCheckBox->setAction(hiddenLastAction);
  633. hiddenLastCheckBox->setChecked(keepHiddenLast);
  634. hiddenLastAction->setDefaultWidget(hiddenLastCheckBox);
  635. QWidgetAction *inactiveLastAction = new QWidgetAction(mixerMenu);
  636. const char *inactiveLastString = mixerVertical ? "Basic.AudioMixer.KeepInactiveRight"
  637. : "Basic.AudioMixer.KeepInactiveBottom";
  638. MenuCheckBox *inactiveLastCheckBox = new MenuCheckBox(QTStr(inactiveLastString), mixerMenu);
  639. inactiveLastCheckBox->setAction(inactiveLastAction);
  640. inactiveLastCheckBox->setChecked(keepInactiveLast);
  641. inactiveLastAction->setDefaultWidget(inactiveLastCheckBox);
  642. // Connect menu actions
  643. connect(unhideAllAction, &QAction::triggered, this, &AudioMixer::unhideAllAudioControls, Qt::DirectConnection);
  644. connect(showHiddenCheckBox, &QCheckBox::toggled, this, &AudioMixer::toggleShowHidden, Qt::DirectConnection);
  645. connect(hiddenLastCheckBox, &QCheckBox::toggled, this, &AudioMixer::toggleKeepHiddenLast, Qt::DirectConnection);
  646. connect(showInactiveCheckBox, &QCheckBox::toggled, this, &AudioMixer::toggleShowInactive, Qt::DirectConnection);
  647. connect(inactiveLastCheckBox, &QCheckBox::toggled, this, &AudioMixer::toggleKeepInactiveLast,
  648. Qt::DirectConnection);
  649. // Build menu and show
  650. mixerMenu->addAction(unhideAllAction);
  651. mixerMenu->addSeparator();
  652. mixerMenu->addAction(showHiddenAction);
  653. mixerMenu->addAction(showInactiveAction);
  654. mixerMenu->addAction(hiddenLastAction);
  655. mixerMenu->addAction(inactiveLastAction);
  656. optionsButton->setMenu(mixerMenu);
  657. }
  658. void AudioMixer::showMixerContextMenu()
  659. {
  660. createMixerContextMenu();
  661. mixerMenu->popup(QCursor::pos());
  662. }
  663. void AudioMixer::addControlForUuid(QString uuid)
  664. {
  665. OBSSourceAutoRelease source = obs_get_source_by_uuid(uuid.toUtf8().constData());
  666. QPointer<VolumeControl> newControl = createVolumeControl(source);
  667. volumeList.insert({uuid, newControl});
  668. queueLayoutUpdate();
  669. }
  670. void AudioMixer::removeControlForUuid(QString uuid)
  671. {
  672. auto item = volumeList.find(uuid);
  673. if (item != volumeList.end()) {
  674. VolumeControl *widget = item->second;
  675. if (widget) {
  676. activeLayout()->removeWidget(widget);
  677. widget->deleteLater();
  678. }
  679. volumeList.erase(item);
  680. }
  681. previewSources.erase(uuid);
  682. globalSources.erase(uuid);
  683. }
  684. void AudioMixer::onFrontendEvent(obs_frontend_event event, void *data)
  685. {
  686. AudioMixer *mixer = static_cast<AudioMixer *>(data);
  687. mixer->handleFrontendEvent(event);
  688. }
  689. void AudioMixer::handleFrontendEvent(obs_frontend_event event)
  690. {
  691. switch (event) {
  692. case OBS_FRONTEND_EVENT_PREVIEW_SCENE_CHANGED:
  693. case OBS_FRONTEND_EVENT_STUDIO_MODE_ENABLED:
  694. case OBS_FRONTEND_EVENT_STUDIO_MODE_DISABLED:
  695. updatePreviewSources();
  696. queueLayoutUpdate();
  697. break;
  698. case OBS_FRONTEND_EVENT_EXIT:
  699. obs_frontend_remove_event_callback(AudioMixer::onFrontendEvent, this);
  700. break;
  701. default:
  702. break;
  703. }
  704. }
  705. void AudioMixer::updateShowInactive()
  706. {
  707. bool settingShowInactive = config_get_bool(App()->GetUserConfig(), "BasicWindow", "MixerShowInactive");
  708. if (showInactive == settingShowInactive) {
  709. return;
  710. }
  711. showInactive = settingShowInactive;
  712. queueLayoutUpdate();
  713. }
  714. void AudioMixer::updateKeepInactiveLast()
  715. {
  716. bool settingKeepInactiveLast = config_get_bool(App()->GetUserConfig(), "BasicWindow", "MixerKeepInactiveLast");
  717. if (keepInactiveLast == settingKeepInactiveLast) {
  718. return;
  719. }
  720. keepInactiveLast = settingKeepInactiveLast;
  721. queueLayoutUpdate();
  722. }
  723. void AudioMixer::updateShowHidden()
  724. {
  725. bool settingShowHidden = config_get_bool(App()->GetUserConfig(), "BasicWindow", "MixerShowHidden");
  726. if (showHidden == settingShowHidden) {
  727. return;
  728. }
  729. showHidden = settingShowHidden;
  730. toggleHiddenButton->setText(QTStr("Basic.AudioMixer.HiddenTotal").arg(hiddenCount));
  731. toggleHiddenButton->setChecked(showHidden);
  732. showHiddenCheckBox->setChecked(showHidden);
  733. queueLayoutUpdate();
  734. }
  735. void AudioMixer::updateKeepHiddenLast()
  736. {
  737. bool settingKeepHiddenLast = config_get_bool(App()->GetUserConfig(), "BasicWindow", "MixerKeepHiddenLast");
  738. if (keepHiddenLast == settingKeepHiddenLast) {
  739. return;
  740. }
  741. keepHiddenLast = settingKeepHiddenLast;
  742. queueLayoutUpdate();
  743. }
  744. void AudioMixer::updateShowToolbar()
  745. {
  746. bool settingShowToolbar = config_get_bool(App()->GetUserConfig(), "BasicWindow", "ShowListboxToolbars");
  747. if (showToolbar == settingShowToolbar) {
  748. return;
  749. }
  750. showToolbar = settingShowToolbar;
  751. showToolbar ? mixerToolbar->show() : mixerToolbar->hide();
  752. }
  753. void AudioMixer::obsSourceActivated(void *data, calldata_t *params)
  754. {
  755. obs_source_t *source = static_cast<obs_source_t *>(calldata_ptr(params, "source"));
  756. uint32_t flags = obs_source_get_output_flags(source);
  757. if (flags & OBS_SOURCE_AUDIO) {
  758. auto uuidPointer = obs_source_get_uuid(source);
  759. QMetaObject::invokeMethod(static_cast<AudioMixer *>(data), "updateControlVisibility",
  760. Qt::QueuedConnection, Q_ARG(QString, QString::fromUtf8(uuidPointer)));
  761. }
  762. }
  763. void AudioMixer::obsSourceDeactivated(void *data, calldata_t *params)
  764. {
  765. obs_source_t *source = static_cast<obs_source_t *>(calldata_ptr(params, "source"));
  766. uint32_t flags = obs_source_get_output_flags(source);
  767. if (flags & OBS_SOURCE_AUDIO) {
  768. auto uuidPointer = obs_source_get_uuid(source);
  769. QMetaObject::invokeMethod(static_cast<AudioMixer *>(data), "updateControlVisibility",
  770. Qt::QueuedConnection, Q_ARG(QString, QString::fromUtf8(uuidPointer)));
  771. }
  772. }
  773. void AudioMixer::obsSourceAudioActivated(void *data, calldata_t *params)
  774. {
  775. obs_source_t *source = static_cast<obs_source_t *>(calldata_ptr(params, "source"));
  776. if (obs_source_active(source)) {
  777. auto uuidPointer = obs_source_get_uuid(source);
  778. QMetaObject::invokeMethod(static_cast<AudioMixer *>(data), "updateControlVisibility",
  779. Qt::QueuedConnection, Q_ARG(QString, QString::fromUtf8(uuidPointer)));
  780. }
  781. }
  782. void AudioMixer::obsSourceAudioDeactivated(void *data, calldata_t *params)
  783. {
  784. obs_source_t *source = static_cast<obs_source_t *>(calldata_ptr(params, "source"));
  785. auto uuidPointer = obs_source_get_uuid(source);
  786. QMetaObject::invokeMethod(static_cast<AudioMixer *>(data), "updateControlVisibility", Qt::QueuedConnection,
  787. Q_ARG(QString, QString::fromUtf8(uuidPointer)));
  788. }
  789. void AudioMixer::obsSourceCreate(void *data, calldata_t *params)
  790. {
  791. obs_source_t *source = static_cast<obs_source_t *>(calldata_ptr(params, "source"));
  792. uint32_t flags = obs_source_get_output_flags(source);
  793. if (flags & OBS_SOURCE_AUDIO) {
  794. auto uuidPointer = obs_source_get_uuid(source);
  795. QMetaObject::invokeMethod(static_cast<AudioMixer *>(data), "sourceCreated", Qt::QueuedConnection,
  796. Q_ARG(QString, QString::fromUtf8(uuidPointer)));
  797. }
  798. }
  799. void AudioMixer::obsSourceRemove(void *data, calldata_t *params)
  800. {
  801. obs_source_t *source = static_cast<obs_source_t *>(calldata_ptr(params, "source"));
  802. uint32_t flags = obs_source_get_output_flags(source);
  803. if (flags & OBS_SOURCE_AUDIO) {
  804. auto uuidPointer = obs_source_get_uuid(source);
  805. QMetaObject::invokeMethod(static_cast<AudioMixer *>(data), "sourceRemoved", Qt::QueuedConnection,
  806. Q_ARG(QString, QString::fromUtf8(uuidPointer)));
  807. }
  808. }
  809. void AudioMixer::obsSourceRename(void *data, calldata_t *)
  810. {
  811. QMetaObject::invokeMethod(static_cast<AudioMixer *>(data), "queueLayoutUpdate", Qt::QueuedConnection);
  812. }