adv-audio-control.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. #include <QHBoxLayout>
  2. #include <QGridLayout>
  3. #include <QLabel>
  4. #include <QSpinBox>
  5. #include <QComboBox>
  6. #include <QCheckBox>
  7. #include <cmath>
  8. #include "qt-wrappers.hpp"
  9. #include "obs-app.hpp"
  10. #include "adv-audio-control.hpp"
  11. #include "window-basic-main.hpp"
  12. #ifndef NSEC_PER_MSEC
  13. #define NSEC_PER_MSEC 1000000
  14. #endif
  15. #define MIN_DB -96.0
  16. #define MAX_DB 26.0
  17. OBSAdvAudioCtrl::OBSAdvAudioCtrl(QGridLayout *, obs_source_t *source_)
  18. : source(source_)
  19. {
  20. QHBoxLayout *hlayout;
  21. signal_handler_t *handler = obs_source_get_signal_handler(source);
  22. QString sourceName = QT_UTF8(obs_source_get_name(source));
  23. float vol = obs_source_get_volume(source);
  24. uint32_t flags = obs_source_get_flags(source);
  25. uint32_t mixers = obs_source_get_audio_mixers(source);
  26. mixerContainer = new QWidget();
  27. balanceContainer = new QWidget();
  28. labelL = new QLabel();
  29. labelR = new QLabel();
  30. iconLabel = new QLabel();
  31. nameLabel = new QLabel();
  32. active = new QLabel();
  33. stackedWidget = new QStackedWidget();
  34. volume = new QDoubleSpinBox();
  35. percent = new QSpinBox();
  36. forceMono = new QCheckBox();
  37. balance = new BalanceSlider();
  38. if (obs_audio_monitoring_available())
  39. monitoringType = new QComboBox();
  40. syncOffset = new QSpinBox();
  41. mixer1 = new QCheckBox();
  42. mixer2 = new QCheckBox();
  43. mixer3 = new QCheckBox();
  44. mixer4 = new QCheckBox();
  45. mixer5 = new QCheckBox();
  46. mixer6 = new QCheckBox();
  47. activateSignal.Connect(handler, "activate", OBSSourceActivated, this);
  48. deactivateSignal.Connect(handler, "deactivate", OBSSourceDeactivated,
  49. this);
  50. volChangedSignal.Connect(handler, "volume", OBSSourceVolumeChanged,
  51. this);
  52. syncOffsetSignal.Connect(handler, "audio_sync", OBSSourceSyncChanged,
  53. this);
  54. flagsSignal.Connect(handler, "update_flags", OBSSourceFlagsChanged,
  55. this);
  56. if (obs_audio_monitoring_available())
  57. monitoringTypeSignal.Connect(handler, "audio_monitoring",
  58. OBSSourceMonitoringTypeChanged,
  59. this);
  60. mixersSignal.Connect(handler, "audio_mixers", OBSSourceMixersChanged,
  61. this);
  62. balChangedSignal.Connect(handler, "audio_balance",
  63. OBSSourceBalanceChanged, this);
  64. renameSignal.Connect(handler, "rename", OBSSourceRenamed, this);
  65. hlayout = new QHBoxLayout();
  66. hlayout->setContentsMargins(0, 0, 0, 0);
  67. mixerContainer->setLayout(hlayout);
  68. hlayout = new QHBoxLayout();
  69. hlayout->setContentsMargins(0, 0, 0, 0);
  70. balanceContainer->setLayout(hlayout);
  71. balanceContainer->setFixedWidth(150);
  72. labelL->setText("L");
  73. labelR->setText("R");
  74. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  75. QIcon sourceIcon = main->GetSourceIcon(obs_source_get_id(source));
  76. QPixmap pixmap = sourceIcon.pixmap(QSize(16, 16));
  77. iconLabel->setPixmap(pixmap);
  78. iconLabel->setFixedSize(16, 16);
  79. iconLabel->setStyleSheet("background: none");
  80. SetSourceName(sourceName);
  81. nameLabel->setAlignment(Qt::AlignVCenter);
  82. bool isActive = obs_source_active(source);
  83. active->setText(isActive ? QTStr("Basic.Stats.Status.Active")
  84. : QTStr("Basic.Stats.Status.Inactive"));
  85. if (isActive)
  86. setThemeID(active, "error");
  87. active->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed);
  88. volume->setMinimum(MIN_DB - 0.1);
  89. volume->setMaximum(MAX_DB);
  90. volume->setSingleStep(0.1);
  91. volume->setDecimals(1);
  92. volume->setSuffix(" dB");
  93. volume->setValue(obs_mul_to_db(vol));
  94. volume->setAccessibleName(
  95. QTStr("Basic.AdvAudio.VolumeSource").arg(sourceName));
  96. if (volume->value() < MIN_DB) {
  97. volume->setSpecialValueText("-inf dB");
  98. volume->setAccessibleDescription("-inf dB");
  99. }
  100. percent->setMinimum(0);
  101. percent->setMaximum(2000);
  102. percent->setSuffix("%");
  103. percent->setValue((int)(obs_source_get_volume(source) * 100.0f));
  104. percent->setAccessibleName(
  105. QTStr("Basic.AdvAudio.VolumeSource").arg(sourceName));
  106. stackedWidget->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
  107. stackedWidget->setFixedWidth(100);
  108. stackedWidget->addWidget(volume);
  109. stackedWidget->addWidget(percent);
  110. VolumeType volType = (VolumeType)config_get_int(
  111. GetGlobalConfig(), "BasicWindow", "AdvAudioVolumeType");
  112. SetVolumeWidget(volType);
  113. forceMono->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed);
  114. forceMono->setChecked((flags & OBS_SOURCE_FLAG_FORCE_MONO) != 0);
  115. forceMono->setAccessibleName(
  116. QTStr("Basic.AdvAudio.MonoSource").arg(sourceName));
  117. balance->setOrientation(Qt::Horizontal);
  118. balance->setMinimum(0);
  119. balance->setMaximum(100);
  120. balance->setTickPosition(QSlider::TicksAbove);
  121. balance->setTickInterval(50);
  122. balance->setAccessibleName(
  123. QTStr("Basic.AdvAudio.BalanceSource").arg(sourceName));
  124. const char *speakers =
  125. config_get_string(main->Config(), "Audio", "ChannelSetup");
  126. if (strcmp(speakers, "Mono") == 0)
  127. balance->setEnabled(false);
  128. else
  129. balance->setEnabled(true);
  130. float bal = obs_source_get_balance_value(source) * 100.0f;
  131. balance->setValue((int)bal);
  132. int64_t cur_sync = obs_source_get_sync_offset(source);
  133. syncOffset->setMinimum(-950);
  134. syncOffset->setMaximum(20000);
  135. syncOffset->setSuffix(" ms");
  136. syncOffset->setValue(int(cur_sync / NSEC_PER_MSEC));
  137. syncOffset->setFixedWidth(100);
  138. syncOffset->setAccessibleName(
  139. QTStr("Basic.AdvAudio.SyncOffsetSource").arg(sourceName));
  140. int idx;
  141. if (obs_audio_monitoring_available()) {
  142. monitoringType->addItem(QTStr("Basic.AdvAudio.Monitoring.None"),
  143. (int)OBS_MONITORING_TYPE_NONE);
  144. monitoringType->addItem(
  145. QTStr("Basic.AdvAudio.Monitoring.MonitorOnly"),
  146. (int)OBS_MONITORING_TYPE_MONITOR_ONLY);
  147. monitoringType->addItem(
  148. QTStr("Basic.AdvAudio.Monitoring.Both"),
  149. (int)OBS_MONITORING_TYPE_MONITOR_AND_OUTPUT);
  150. int mt = (int)obs_source_get_monitoring_type(source);
  151. idx = monitoringType->findData(mt);
  152. monitoringType->setCurrentIndex(idx);
  153. monitoringType->setAccessibleName(
  154. QTStr("Basic.AdvAudio.MonitoringSource")
  155. .arg(sourceName));
  156. monitoringType->setSizePolicy(QSizePolicy::Maximum,
  157. QSizePolicy::Fixed);
  158. }
  159. mixer1->setText("1");
  160. mixer1->setChecked(mixers & (1 << 0));
  161. mixer1->setAccessibleName(
  162. QTStr("Basic.Settings.Output.Adv.Audio.Track1"));
  163. mixer2->setText("2");
  164. mixer2->setChecked(mixers & (1 << 1));
  165. mixer2->setAccessibleName(
  166. QTStr("Basic.Settings.Output.Adv.Audio.Track2"));
  167. mixer3->setText("3");
  168. mixer3->setChecked(mixers & (1 << 2));
  169. mixer3->setAccessibleName(
  170. QTStr("Basic.Settings.Output.Adv.Audio.Track3"));
  171. mixer4->setText("4");
  172. mixer4->setChecked(mixers & (1 << 3));
  173. mixer4->setAccessibleName(
  174. QTStr("Basic.Settings.Output.Adv.Audio.Track4"));
  175. mixer5->setText("5");
  176. mixer5->setChecked(mixers & (1 << 4));
  177. mixer5->setAccessibleName(
  178. QTStr("Basic.Settings.Output.Adv.Audio.Track5"));
  179. mixer6->setText("6");
  180. mixer6->setChecked(mixers & (1 << 5));
  181. mixer6->setAccessibleName(
  182. QTStr("Basic.Settings.Output.Adv.Audio.Track6"));
  183. balanceContainer->layout()->addWidget(labelL);
  184. balanceContainer->layout()->addWidget(balance);
  185. balanceContainer->layout()->addWidget(labelR);
  186. speaker_layout sl = obs_source_get_speaker_layout(source);
  187. if (sl != SPEAKERS_STEREO)
  188. balanceContainer->setEnabled(false);
  189. mixerContainer->layout()->addWidget(mixer1);
  190. mixerContainer->layout()->addWidget(mixer2);
  191. mixerContainer->layout()->addWidget(mixer3);
  192. mixerContainer->layout()->addWidget(mixer4);
  193. mixerContainer->layout()->addWidget(mixer5);
  194. mixerContainer->layout()->addWidget(mixer6);
  195. mixerContainer->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed);
  196. QWidget::connect(volume, SIGNAL(valueChanged(double)), this,
  197. SLOT(volumeChanged(double)));
  198. QWidget::connect(percent, SIGNAL(valueChanged(int)), this,
  199. SLOT(percentChanged(int)));
  200. QWidget::connect(forceMono, SIGNAL(clicked(bool)), this,
  201. SLOT(downmixMonoChanged(bool)));
  202. QWidget::connect(balance, SIGNAL(valueChanged(int)), this,
  203. SLOT(balanceChanged(int)));
  204. QWidget::connect(balance, SIGNAL(doubleClicked()), this,
  205. SLOT(ResetBalance()));
  206. QWidget::connect(syncOffset, SIGNAL(valueChanged(int)), this,
  207. SLOT(syncOffsetChanged(int)));
  208. if (obs_audio_monitoring_available())
  209. QWidget::connect(monitoringType,
  210. SIGNAL(currentIndexChanged(int)), this,
  211. SLOT(monitoringTypeChanged(int)));
  212. QWidget::connect(mixer1, SIGNAL(clicked(bool)), this,
  213. SLOT(mixer1Changed(bool)));
  214. QWidget::connect(mixer2, SIGNAL(clicked(bool)), this,
  215. SLOT(mixer2Changed(bool)));
  216. QWidget::connect(mixer3, SIGNAL(clicked(bool)), this,
  217. SLOT(mixer3Changed(bool)));
  218. QWidget::connect(mixer4, SIGNAL(clicked(bool)), this,
  219. SLOT(mixer4Changed(bool)));
  220. QWidget::connect(mixer5, SIGNAL(clicked(bool)), this,
  221. SLOT(mixer5Changed(bool)));
  222. QWidget::connect(mixer6, SIGNAL(clicked(bool)), this,
  223. SLOT(mixer6Changed(bool)));
  224. setObjectName(sourceName);
  225. }
  226. OBSAdvAudioCtrl::~OBSAdvAudioCtrl()
  227. {
  228. iconLabel->deleteLater();
  229. nameLabel->deleteLater();
  230. active->deleteLater();
  231. stackedWidget->deleteLater();
  232. forceMono->deleteLater();
  233. balanceContainer->deleteLater();
  234. syncOffset->deleteLater();
  235. if (obs_audio_monitoring_available())
  236. monitoringType->deleteLater();
  237. mixerContainer->deleteLater();
  238. }
  239. void OBSAdvAudioCtrl::ShowAudioControl(QGridLayout *layout)
  240. {
  241. int lastRow = layout->rowCount();
  242. int idx = 0;
  243. layout->addWidget(iconLabel, lastRow, idx++);
  244. layout->addWidget(nameLabel, lastRow, idx++);
  245. layout->addWidget(active, lastRow, idx++);
  246. layout->addWidget(stackedWidget, lastRow, idx++);
  247. layout->addWidget(forceMono, lastRow, idx++);
  248. layout->addWidget(balanceContainer, lastRow, idx++);
  249. layout->addWidget(syncOffset, lastRow, idx++);
  250. if (obs_audio_monitoring_available())
  251. layout->addWidget(monitoringType, lastRow, idx++);
  252. layout->addWidget(mixerContainer, lastRow, idx++);
  253. layout->layout()->setAlignment(mixerContainer, Qt::AlignVCenter);
  254. layout->setHorizontalSpacing(15);
  255. }
  256. /* ------------------------------------------------------------------------- */
  257. /* OBS source callbacks */
  258. void OBSAdvAudioCtrl::OBSSourceActivated(void *param, calldata_t *)
  259. {
  260. QMetaObject::invokeMethod(reinterpret_cast<OBSAdvAudioCtrl *>(param),
  261. "SourceActiveChanged", Q_ARG(bool, true));
  262. }
  263. void OBSAdvAudioCtrl::OBSSourceDeactivated(void *param, calldata_t *)
  264. {
  265. QMetaObject::invokeMethod(reinterpret_cast<OBSAdvAudioCtrl *>(param),
  266. "SourceActiveChanged", Q_ARG(bool, false));
  267. }
  268. void OBSAdvAudioCtrl::OBSSourceFlagsChanged(void *param, calldata_t *calldata)
  269. {
  270. uint32_t flags = (uint32_t)calldata_int(calldata, "flags");
  271. QMetaObject::invokeMethod(reinterpret_cast<OBSAdvAudioCtrl *>(param),
  272. "SourceFlagsChanged", Q_ARG(uint32_t, flags));
  273. }
  274. void OBSAdvAudioCtrl::OBSSourceVolumeChanged(void *param, calldata_t *calldata)
  275. {
  276. float volume = (float)calldata_float(calldata, "volume");
  277. QMetaObject::invokeMethod(reinterpret_cast<OBSAdvAudioCtrl *>(param),
  278. "SourceVolumeChanged", Q_ARG(float, volume));
  279. }
  280. void OBSAdvAudioCtrl::OBSSourceSyncChanged(void *param, calldata_t *calldata)
  281. {
  282. int64_t offset = calldata_int(calldata, "offset");
  283. QMetaObject::invokeMethod(reinterpret_cast<OBSAdvAudioCtrl *>(param),
  284. "SourceSyncChanged", Q_ARG(int64_t, offset));
  285. }
  286. void OBSAdvAudioCtrl::OBSSourceMonitoringTypeChanged(void *param,
  287. calldata_t *calldata)
  288. {
  289. int type = calldata_int(calldata, "type");
  290. QMetaObject::invokeMethod(reinterpret_cast<OBSAdvAudioCtrl *>(param),
  291. "SourceMonitoringTypeChanged",
  292. Q_ARG(int, type));
  293. }
  294. void OBSAdvAudioCtrl::OBSSourceMixersChanged(void *param, calldata_t *calldata)
  295. {
  296. uint32_t mixers = (uint32_t)calldata_int(calldata, "mixers");
  297. QMetaObject::invokeMethod(reinterpret_cast<OBSAdvAudioCtrl *>(param),
  298. "SourceMixersChanged",
  299. Q_ARG(uint32_t, mixers));
  300. }
  301. void OBSAdvAudioCtrl::OBSSourceBalanceChanged(void *param, calldata_t *calldata)
  302. {
  303. int balance = (float)calldata_float(calldata, "balance") * 100.0f;
  304. QMetaObject::invokeMethod(reinterpret_cast<OBSAdvAudioCtrl *>(param),
  305. "SourceBalanceChanged", Q_ARG(int, balance));
  306. }
  307. void OBSAdvAudioCtrl::OBSSourceRenamed(void *param, calldata_t *calldata)
  308. {
  309. QString newName = QT_UTF8(calldata_string(calldata, "new_name"));
  310. QMetaObject::invokeMethod(reinterpret_cast<OBSAdvAudioCtrl *>(param),
  311. "SetSourceName", Q_ARG(QString, newName));
  312. }
  313. /* ------------------------------------------------------------------------- */
  314. /* Qt event queue source callbacks */
  315. static inline void setCheckboxState(QCheckBox *checkbox, bool checked)
  316. {
  317. checkbox->blockSignals(true);
  318. checkbox->setChecked(checked);
  319. checkbox->blockSignals(false);
  320. }
  321. void OBSAdvAudioCtrl::SourceActiveChanged(bool isActive)
  322. {
  323. if (isActive) {
  324. active->setText(QTStr("Basic.Stats.Status.Active"));
  325. setThemeID(active, "error");
  326. } else {
  327. active->setText(QTStr("Basic.Stats.Status.Inactive"));
  328. setThemeID(active, "");
  329. }
  330. }
  331. void OBSAdvAudioCtrl::SourceFlagsChanged(uint32_t flags)
  332. {
  333. bool forceMonoVal = (flags & OBS_SOURCE_FLAG_FORCE_MONO) != 0;
  334. setCheckboxState(forceMono, forceMonoVal);
  335. }
  336. void OBSAdvAudioCtrl::SourceVolumeChanged(float value)
  337. {
  338. volume->blockSignals(true);
  339. percent->blockSignals(true);
  340. volume->setValue(obs_mul_to_db(value));
  341. percent->setValue((int)std::round(value * 100.0f));
  342. percent->blockSignals(false);
  343. volume->blockSignals(false);
  344. }
  345. void OBSAdvAudioCtrl::SourceBalanceChanged(int value)
  346. {
  347. balance->blockSignals(true);
  348. balance->setValue(value);
  349. balance->blockSignals(false);
  350. }
  351. void OBSAdvAudioCtrl::SourceSyncChanged(int64_t offset)
  352. {
  353. syncOffset->blockSignals(true);
  354. syncOffset->setValue(offset / NSEC_PER_MSEC);
  355. syncOffset->blockSignals(false);
  356. }
  357. void OBSAdvAudioCtrl::SourceMonitoringTypeChanged(int type)
  358. {
  359. int idx = monitoringType->findData(type);
  360. monitoringType->blockSignals(true);
  361. monitoringType->setCurrentIndex(idx);
  362. monitoringType->blockSignals(false);
  363. }
  364. void OBSAdvAudioCtrl::SourceMixersChanged(uint32_t mixers)
  365. {
  366. setCheckboxState(mixer1, mixers & (1 << 0));
  367. setCheckboxState(mixer2, mixers & (1 << 1));
  368. setCheckboxState(mixer3, mixers & (1 << 2));
  369. setCheckboxState(mixer4, mixers & (1 << 3));
  370. setCheckboxState(mixer5, mixers & (1 << 4));
  371. setCheckboxState(mixer6, mixers & (1 << 5));
  372. }
  373. /* ------------------------------------------------------------------------- */
  374. /* Qt control callbacks */
  375. void OBSAdvAudioCtrl::volumeChanged(double db)
  376. {
  377. float prev = obs_source_get_volume(source);
  378. if (db < MIN_DB) {
  379. volume->setSpecialValueText("-inf dB");
  380. db = -INFINITY;
  381. }
  382. float val = obs_db_to_mul(db);
  383. obs_source_set_volume(source, val);
  384. auto undo_redo = [](const std::string &name, float val) {
  385. OBSSourceAutoRelease source =
  386. obs_get_source_by_name(name.c_str());
  387. obs_source_set_volume(source, val);
  388. };
  389. const char *name = obs_source_get_name(source);
  390. OBSBasic *main = OBSBasic::Get();
  391. main->undo_s.add_action(
  392. QTStr("Undo.Volume.Change").arg(name),
  393. std::bind(undo_redo, std::placeholders::_1, prev),
  394. std::bind(undo_redo, std::placeholders::_1, val), name, name,
  395. true);
  396. }
  397. void OBSAdvAudioCtrl::percentChanged(int percent)
  398. {
  399. float prev = obs_source_get_volume(source);
  400. float val = (float)percent / 100.0f;
  401. obs_source_set_volume(source, val);
  402. auto undo_redo = [](const std::string &name, float val) {
  403. OBSSourceAutoRelease source =
  404. obs_get_source_by_name(name.c_str());
  405. obs_source_set_volume(source, val);
  406. };
  407. const char *name = obs_source_get_name(source);
  408. OBSBasic::Get()->undo_s.add_action(
  409. QTStr("Undo.Volume.Change").arg(name),
  410. std::bind(undo_redo, std::placeholders::_1, prev),
  411. std::bind(undo_redo, std::placeholders::_1, val), name, name,
  412. true);
  413. }
  414. static inline void set_mono(obs_source_t *source, bool mono)
  415. {
  416. uint32_t flags = obs_source_get_flags(source);
  417. if (mono)
  418. flags |= OBS_SOURCE_FLAG_FORCE_MONO;
  419. else
  420. flags &= ~OBS_SOURCE_FLAG_FORCE_MONO;
  421. obs_source_set_flags(source, flags);
  422. }
  423. void OBSAdvAudioCtrl::downmixMonoChanged(bool val)
  424. {
  425. uint32_t flags = obs_source_get_flags(source);
  426. bool forceMonoActive = (flags & OBS_SOURCE_FLAG_FORCE_MONO) != 0;
  427. if (forceMonoActive == val)
  428. return;
  429. if (val)
  430. flags |= OBS_SOURCE_FLAG_FORCE_MONO;
  431. else
  432. flags &= ~OBS_SOURCE_FLAG_FORCE_MONO;
  433. obs_source_set_flags(source, flags);
  434. auto undo_redo = [](const std::string &name, bool val) {
  435. OBSSourceAutoRelease source =
  436. obs_get_source_by_name(name.c_str());
  437. set_mono(source, val);
  438. };
  439. QString text = QTStr(val ? "Undo.ForceMono.On" : "Undo.ForceMono.Off");
  440. const char *name = obs_source_get_name(source);
  441. OBSBasic::Get()->undo_s.add_action(
  442. text.arg(name),
  443. std::bind(undo_redo, std::placeholders::_1, !val),
  444. std::bind(undo_redo, std::placeholders::_1, val), name, name);
  445. }
  446. void OBSAdvAudioCtrl::balanceChanged(int val)
  447. {
  448. float prev = obs_source_get_balance_value(source);
  449. float bal = (float)val / 100.0f;
  450. if (abs(50 - val) < 10) {
  451. balance->blockSignals(true);
  452. balance->setValue(50);
  453. bal = 0.5f;
  454. balance->blockSignals(false);
  455. }
  456. obs_source_set_balance_value(source, bal);
  457. auto undo_redo = [](const std::string &name, float val) {
  458. OBSSourceAutoRelease source =
  459. obs_get_source_by_name(name.c_str());
  460. obs_source_set_balance_value(source, val);
  461. };
  462. const char *name = obs_source_get_name(source);
  463. OBSBasic::Get()->undo_s.add_action(
  464. QTStr("Undo.Balance.Change").arg(name),
  465. std::bind(undo_redo, std::placeholders::_1, prev),
  466. std::bind(undo_redo, std::placeholders::_1, bal), name, name,
  467. true);
  468. }
  469. void OBSAdvAudioCtrl::ResetBalance()
  470. {
  471. balance->setValue(50);
  472. }
  473. void OBSAdvAudioCtrl::syncOffsetChanged(int milliseconds)
  474. {
  475. int64_t prev = obs_source_get_sync_offset(source);
  476. int64_t val = int64_t(milliseconds) * NSEC_PER_MSEC;
  477. if (prev / NSEC_PER_MSEC == milliseconds)
  478. return;
  479. obs_source_set_sync_offset(source, val);
  480. auto undo_redo = [](const std::string &name, int64_t val) {
  481. OBSSourceAutoRelease source =
  482. obs_get_source_by_name(name.c_str());
  483. obs_source_set_sync_offset(source, val);
  484. };
  485. const char *name = obs_source_get_name(source);
  486. OBSBasic::Get()->undo_s.add_action(
  487. QTStr("Undo.SyncOffset.Change").arg(name),
  488. std::bind(undo_redo, std::placeholders::_1, prev),
  489. std::bind(undo_redo, std::placeholders::_1, val), name, name,
  490. true);
  491. }
  492. void OBSAdvAudioCtrl::monitoringTypeChanged(int index)
  493. {
  494. obs_monitoring_type prev = obs_source_get_monitoring_type(source);
  495. obs_monitoring_type mt =
  496. (obs_monitoring_type)monitoringType->itemData(index).toInt();
  497. obs_source_set_monitoring_type(source, mt);
  498. const char *type = nullptr;
  499. switch (mt) {
  500. case OBS_MONITORING_TYPE_NONE:
  501. type = "none";
  502. break;
  503. case OBS_MONITORING_TYPE_MONITOR_ONLY:
  504. type = "monitor only";
  505. break;
  506. case OBS_MONITORING_TYPE_MONITOR_AND_OUTPUT:
  507. type = "monitor and output";
  508. break;
  509. }
  510. const char *name = obs_source_get_name(source);
  511. blog(LOG_INFO, "User changed audio monitoring for source '%s' to: %s",
  512. name ? name : "(null)", type);
  513. auto undo_redo = [](const std::string &name, obs_monitoring_type val) {
  514. OBSSourceAutoRelease source =
  515. obs_get_source_by_name(name.c_str());
  516. obs_source_set_monitoring_type(source, val);
  517. };
  518. OBSBasic::Get()->undo_s.add_action(
  519. QTStr("Undo.MonitoringType.Change").arg(name),
  520. std::bind(undo_redo, std::placeholders::_1, prev),
  521. std::bind(undo_redo, std::placeholders::_1, mt), name, name);
  522. }
  523. static inline void setMixer(obs_source_t *source, const int mixerIdx,
  524. const bool checked)
  525. {
  526. uint32_t mixers = obs_source_get_audio_mixers(source);
  527. uint32_t new_mixers = mixers;
  528. if (checked)
  529. new_mixers |= (1 << mixerIdx);
  530. else
  531. new_mixers &= ~(1 << mixerIdx);
  532. obs_source_set_audio_mixers(source, new_mixers);
  533. auto undo_redo = [](const std::string &name, uint32_t mixers) {
  534. OBSSourceAutoRelease source =
  535. obs_get_source_by_name(name.c_str());
  536. obs_source_set_audio_mixers(source, mixers);
  537. };
  538. const char *name = obs_source_get_name(source);
  539. OBSBasic::Get()->undo_s.add_action(
  540. QTStr("Undo.Mixers.Change").arg(name),
  541. std::bind(undo_redo, std::placeholders::_1, mixers),
  542. std::bind(undo_redo, std::placeholders::_1, new_mixers), name,
  543. name);
  544. }
  545. void OBSAdvAudioCtrl::mixer1Changed(bool checked)
  546. {
  547. setMixer(source, 0, checked);
  548. }
  549. void OBSAdvAudioCtrl::mixer2Changed(bool checked)
  550. {
  551. setMixer(source, 1, checked);
  552. }
  553. void OBSAdvAudioCtrl::mixer3Changed(bool checked)
  554. {
  555. setMixer(source, 2, checked);
  556. }
  557. void OBSAdvAudioCtrl::mixer4Changed(bool checked)
  558. {
  559. setMixer(source, 3, checked);
  560. }
  561. void OBSAdvAudioCtrl::mixer5Changed(bool checked)
  562. {
  563. setMixer(source, 4, checked);
  564. }
  565. void OBSAdvAudioCtrl::mixer6Changed(bool checked)
  566. {
  567. setMixer(source, 5, checked);
  568. }
  569. void OBSAdvAudioCtrl::SetVolumeWidget(VolumeType type)
  570. {
  571. switch (type) {
  572. case VolumeType::Percent:
  573. stackedWidget->setCurrentWidget(percent);
  574. break;
  575. case VolumeType::dB:
  576. stackedWidget->setCurrentWidget(volume);
  577. break;
  578. }
  579. }
  580. void OBSAdvAudioCtrl::SetIconVisible(bool visible)
  581. {
  582. visible ? iconLabel->show() : iconLabel->hide();
  583. }
  584. void OBSAdvAudioCtrl::SetSourceName(QString newName)
  585. {
  586. TruncateLabel(nameLabel, newName);
  587. }