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