AudioMixer.cpp 31 KB

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