forked from mixxxdj/mixxx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdlgprefsound.cpp
More file actions
1264 lines (1131 loc) · 48.2 KB
/
Copy pathdlgprefsound.cpp
File metadata and controls
1264 lines (1131 loc) · 48.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "preferences/dialog/dlgprefsound.h"
#include <QBoxLayout>
#include <QCheckBox>
#include <QGroupBox>
#include <QMessageBox>
#include <QtDebug>
#include <algorithm>
#include <vector>
#include "control/controlproxy.h"
#include "defs_urls.h"
#include "engine/enginebuffer.h"
#include "engine/enginemixer.h"
#include "mixer/playermanager.h"
#include "moc_dlgprefsound.cpp"
#include "preferences/configobject.h"
#include "preferences/dialog/dlgprefsound.h"
#include "preferences/dialog/dlgprefsounditem.h"
#include "soundio/sounddevice.h"
#include "soundio/soundmanager.h"
#include "soundio/soundmanagerconfig.h"
#include "soundio/soundmanagerutil.h"
#include "util/cmdlineargs.h"
#include "util/rlimit.h"
#include "util/scopedoverridecursor.h"
#ifdef __RUBBERBAND__
#include "engine/bufferscalers/rubberbandworkerpool.h"
#endif
namespace {
const QString kAppGroup = QStringLiteral("[App]");
const QString kMasterGroup = QStringLiteral("[Master]");
const ConfigKey kKeylockEngingeCfgkey =
ConfigKey(kAppGroup, QStringLiteral("keylock_engine"));
const ConfigKey kKeylockMultiThreadingCfgkey =
ConfigKey(kAppGroup, QStringLiteral("keylock_multithreading"));
const ConfigKey kPipeWire =
ConfigKey(kAppGroup, QStringLiteral("pipewire"));
const ConfigKey kPipeWirePatchbay =
ConfigKey(kAppGroup, QStringLiteral("pipewire_patchbay_sync"));
bool soundItemAlreadyExists(const AudioPath& output, const QWidget& widget) {
for (const QObject* pObj : widget.children()) {
const auto* pItem = qobject_cast<const DlgPrefSoundItem*>(pObj);
if (!pItem || pItem->type() != output.getType()) {
continue;
}
if (!AudioPath::isIndexed(pItem->type()) || pItem->index() == output.getIndex()) {
return true;
}
}
return false;
}
#ifdef __RUBBERBAND__
const QString kKeylockMultiThreadedAvailable = QStringLiteral("<p>") +
QObject::tr(
"Distribute stereo channels into mono channels processed in "
"parallel.") +
QStringLiteral("</p><p><span style=\"font-weight:600;\">") +
QObject::tr("Warning!") + QStringLiteral("</span></p><p>") +
QObject::tr(
"Processing stereo signal as mono channel "
"may result in pitch and tone imperfection, and this "
"is "
"mono-incompatible, due to third party limitations.") +
QStringLiteral("</p>");
const QString kKeylockMultiThreadedUnavailableMono = QStringLiteral("<i>") +
QObject::tr(
"Dual threading mode is incompatible with mono main mix.") +
QStringLiteral("</i>");
const QString kKeylockMultiThreadedUnavailableRubberband =
QStringLiteral("<i>") +
QObject::tr("Dual threading mode is only available with RubberBand.") +
QStringLiteral("</i>");
#endif
} // namespace
/// Construct a new sound preferences pane. Initializes and populates
/// all the controls to the values obtained from SoundManager.
DlgPrefSound::DlgPrefSound(QWidget* pParent,
std::shared_ptr<SoundManager> pSoundManager,
UserSettingsPointer pSettings)
: DlgPreferencePage(pParent),
m_pSoundManager(pSoundManager),
m_pSettings(pSettings),
m_config(pSoundManager.get()),
m_pLatencyCompensation(kMasterGroup, QStringLiteral("microphoneLatencyCompensation")),
m_pMainDelay(kMasterGroup, QStringLiteral("delay")),
m_pHeadDelay(kMasterGroup, QStringLiteral("headDelay")),
m_pBoothDelay(kMasterGroup, QStringLiteral("boothDelay")),
m_pMicMonitorMode(kMasterGroup, QStringLiteral("talkover_mix")),
m_pKeylockEngine(kKeylockEngingeCfgkey),
m_settingsModified(false),
m_bLatencyChanged(false),
m_bSkipConfigClear(true),
m_loading(false),
m_configValid(true) {
setupUi(this);
// Create text color for the wiki links
createLinkColor();
connect(m_pSoundManager.get(),
&SoundManager::devicesUpdated,
this,
&DlgPrefSound::refreshDevices);
connect(m_pSoundManager.get(),
&SoundManager::deviceAdded,
this,
&DlgPrefSound::addDevice);
connect(m_pSoundManager.get(),
&SoundManager::deviceRemoved,
this,
&DlgPrefSound::removeDevice);
connect(m_pSoundManager.get(),
&SoundManager::deviceChannelsUpdated,
this,
&DlgPrefSound::updateDeviceChannels);
connect(m_pSoundManager.get(),
&SoundManager::configInvalidated,
this,
&DlgPrefSound::invalidateConfig);
apiComboBox->clear();
apiComboBox->addItem(SoundManagerConfig::kEmptyComboBox,
SoundManagerConfig::kAPINone);
updateAPIs();
connect(apiComboBox,
QOverload<int>::of(&QComboBox::currentIndexChanged),
this,
&DlgPrefSound::apiChanged);
apiLabel->setText(apiLabel->text() + QChar(' ') +
coloredLinkString(
m_pLinkColor,
QStringLiteral("(?)"),
MIXXX_MANUAL_SOUND_API_URL));
const auto sampleRates = m_pSoundManager->getSampleRates();
updateSampleRates(sampleRates);
connect(sampleRateComboBox,
QOverload<int>::of(&QComboBox::currentIndexChanged),
this,
&DlgPrefSound::sampleRateChanged);
connect(audioBufferComboBox,
QOverload<int>::of(&QComboBox::currentIndexChanged),
this,
&DlgPrefSound::audioBufferChanged);
deviceSyncComboBox->clear();
deviceSyncComboBox->addItem(tr("Default (long delay)"));
deviceSyncComboBox->addItem(tr("Experimental (no delay)"));
deviceSyncComboBox->addItem(tr("Disabled (short delay)"));
deviceSyncComboBox->setCurrentIndex(2);
connect(deviceSyncComboBox,
QOverload<int>::of(&QComboBox::currentIndexChanged),
this,
&DlgPrefSound::syncBuffersChanged);
engineClockComboBox->clear();
engineClockComboBox->addItem(tr("Soundcard Clock"));
engineClockComboBox->addItem(tr("Network Clock"));
engineClockComboBox->setCurrentIndex(0);
connect(engineClockComboBox,
QOverload<int>::of(&QComboBox::currentIndexChanged),
this,
&DlgPrefSound::engineClockChanged);
keylockComboBox->clear();
for (const auto engine : EngineBuffer::kKeylockEngines) {
if (EngineBuffer::isKeylockEngineAvailable(engine)) {
keylockComboBox->addItem(
EngineBuffer::getKeylockEngineName(engine), QVariant::fromValue(engine));
}
}
latencyCompensationSpinBox->setValue(m_pLatencyCompensation.get());
latencyCompensationWarningLabel->setWordWrap(true);
mainDelaySpinBox->setValue(m_pMainDelay.get());
headDelaySpinBox->setValue(m_pHeadDelay.get());
boothDelaySpinBox->setValue(m_pBoothDelay.get());
// TODO These settings are applied immediately via ControlProxies.
// While this is handy for testing the delays, it breaks the rule to
// apply only in slotApply(). Add hint to UI?
connect(latencyCompensationSpinBox,
QOverload<double>::of(&QDoubleSpinBox::valueChanged),
this,
&DlgPrefSound::latencyCompensationSpinboxChanged);
connect(mainDelaySpinBox,
QOverload<double>::of(&QDoubleSpinBox::valueChanged),
this,
&DlgPrefSound::mainDelaySpinboxChanged);
connect(headDelaySpinBox,
QOverload<double>::of(&QDoubleSpinBox::valueChanged),
this,
&DlgPrefSound::headDelaySpinboxChanged);
connect(boothDelaySpinBox,
QOverload<double>::of(&QDoubleSpinBox::valueChanged),
this,
&DlgPrefSound::boothDelaySpinboxChanged);
micMonitorModeComboBox->addItem(tr("Main output only"),
QVariant(static_cast<int>(EngineMixer::MicMonitorMode::Main)));
micMonitorModeComboBox->addItem(tr("Main and booth outputs"),
QVariant(static_cast<int>(EngineMixer::MicMonitorMode::MainAndBooth)));
micMonitorModeComboBox->addItem(tr("Direct monitor (recording and broadcasting only)"),
QVariant(static_cast<int>(EngineMixer::MicMonitorMode::DirectMonitor)));
int modeIndex = micMonitorModeComboBox->findData(
static_cast<int>(m_pMicMonitorMode.get()));
micMonitorModeComboBox->setCurrentIndex(modeIndex);
micMonitorModeComboBoxChanged(modeIndex);
connect(micMonitorModeComboBox,
QOverload<int>::of(&QComboBox::currentIndexChanged),
this,
&DlgPrefSound::micMonitorModeComboBoxChanged);
#ifdef __PIPEWIRE__
if (CmdlineArgs::Instance().getDeveloper()) {
m_pipewireCheckBox = make_parented<QCheckBox>(this);
m_pipewireCheckBox->setText(tr("Use PipeWire API"));
bool checked = m_pSoundManager->isPipewireSelected();
m_pipewireCheckBox->setChecked(checked);
apiComboBox->setDisabled(checked);
m_pipewireCheckBox->setSizePolicy(QSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed));
apiHBox->addWidget(m_pipewireCheckBox.get());
connect(m_pipewireCheckBox,
&QCheckBox::toggled,
this,
[this](bool) {
m_settingsModified = true;
QMessageBox::information(this,
tr("Information"),
tr("Mixxx must be restarted for the PipeWire "
"API selection to take effect."));
});
}
if (m_pSoundManager->isPipewireSelected()) {
m_pipewirePatchbayCheckBox = make_parented<QCheckBox>(this);
m_pipewirePatchbayCheckBox->setText(tr("Sync with external patchbay"));
m_pPipewirePatchbay = make_parented<ControlProxy>(
kPipeWirePatchbay.group, kPipeWirePatchbay.item, this);
connect(m_pipewirePatchbayCheckBox,
&QCheckBox::toggled,
this,
[this](bool checked) {
m_pSettings->setValue(kPipeWirePatchbay, checked);
m_pPipewirePatchbay->set(checked);
ioTabs->setDisabled(checked);
m_settingsModified = true;
});
auto pipewireGroupBox = make_parented<QGroupBox>("PipeWire Settings", this);
auto pipewireSettings = make_parented<QVBoxLayout>(pipewireGroupBox);
verticalLayout_2->insertWidget(2, pipewireGroupBox.get());
bool checked = m_pSettings->getValue(kPipeWirePatchbay, false);
m_pipewirePatchbayCheckBox->setChecked(checked);
pipewireSettings->addWidget(m_pipewirePatchbayCheckBox.get());
}
#endif
initializePaths();
loadSettings();
connect(apiComboBox,
QOverload<int>::of(&QComboBox::currentIndexChanged),
this,
&DlgPrefSound::settingChanged);
connect(sampleRateComboBox,
QOverload<int>::of(&QComboBox::currentIndexChanged),
this,
&DlgPrefSound::settingChanged);
connect(audioBufferComboBox,
QOverload<int>::of(&QComboBox::currentIndexChanged),
this,
&DlgPrefSound::settingChanged);
connect(deviceSyncComboBox,
QOverload<int>::of(&QComboBox::currentIndexChanged),
this,
&DlgPrefSound::settingChanged);
connect(engineClockComboBox,
QOverload<int>::of(&QComboBox::currentIndexChanged),
this,
&DlgPrefSound::settingChanged);
connect(keylockComboBox,
QOverload<int>::of(&QComboBox::currentIndexChanged),
this,
&DlgPrefSound::settingChanged);
#ifdef __RUBBERBAND__
connect(keylockComboBox,
QOverload<int>::of(&QComboBox::currentIndexChanged),
this,
&DlgPrefSound::updateKeylockDualThreadingCheckbox);
connect(keylockDualthreadedCheckBox,
&QCheckBox::clicked,
this,
&DlgPrefSound::updateKeylockMultithreading);
#else
keylockDualthreadedCheckBox->hide();
#endif
connect(queryButton, &QAbstractButton::clicked, this, &DlgPrefSound::queryClicked);
connect(m_pSoundManager.get(),
&SoundManager::outputRegistered,
this,
[this](const AudioOutput& output, AudioSource* source) {
Q_UNUSED(source);
addPath(output);
loadSettings();
});
connect(m_pSoundManager.get(),
&SoundManager::inputRegistered,
this,
[this](const AudioInput& input, AudioDestination* dest) {
Q_UNUSED(dest);
addPath(input);
loadSettings();
});
m_pAudioLatencyOverloadCount = make_parented<ControlProxy>(
kAppGroup, QStringLiteral("audio_latency_overload_count"), this);
m_pAudioLatencyOverloadCount->connectValueChanged(this, &DlgPrefSound::bufferUnderflow);
m_pOutputLatencyMs = make_parented<ControlProxy>(
kAppGroup, QStringLiteral("output_latency_ms"), this);
m_pOutputLatencyMs->connectValueChanged(this, &DlgPrefSound::outputLatencyChanged);
connect(btnResetBufferUnderflowCount,
&QPushButton::clicked,
this,
&DlgPrefSound::slotResetUnderflowCounter);
// TODO: remove this option by automatically disabling/enabling the main mix
// when recording, broadcasting, headphone, and main outputs are enabled/disabled
m_pMainEnabled =
make_parented<ControlProxy>(kMasterGroup, QStringLiteral("enabled"), this);
mainMixComboBox->addItem(tr("Disabled"));
mainMixComboBox->addItem(tr("Enabled"));
mainMixComboBox->setCurrentIndex(m_pMainEnabled->toBool() ? 1 : 0);
connect(mainMixComboBox,
QOverload<int>::of(&QComboBox::currentIndexChanged),
this,
&DlgPrefSound::mainMixChanged);
m_pMainEnabled->connectValueChanged(this, &DlgPrefSound::mainEnabledChanged);
m_pMainMonoMixdown =
make_parented<ControlProxy>(kMasterGroup, QStringLiteral("mono_mixdown"), this);
mainOutputModeComboBox->addItem(tr("Stereo"));
mainOutputModeComboBox->addItem(tr("Mono"));
mainOutputModeComboBox->setCurrentIndex(m_pMainMonoMixdown->toBool() ? 1 : 0);
connect(mainOutputModeComboBox,
QOverload<int>::of(&QComboBox::currentIndexChanged),
this,
&DlgPrefSound::mainOutputModeComboBoxChanged);
m_pMainMonoMixdown->connectValueChanged(this, &DlgPrefSound::mainMonoMixdownChanged);
#ifdef __LINUX__
qDebug() << "RLimit Cur " << RLimit::getCurRtPrio();
qDebug() << "RLimit Max " << RLimit::getMaxRtPrio();
if (RLimit::isRtPrioAllowed()) {
realtimeHint->setText(tr("Realtime scheduling is enabled."));
} else {
realtimeHint->setText(
tr("To enable Realtime scheduling (currently disabled), see the %1.")
.arg(coloredLinkString(
m_pLinkColor,
QStringLiteral("Mixxx Wiki"),
MIXXX_WIKI_AUDIO_LATENCY_URL)));
}
#else
// the limits warning is a Linux only thing
realtimeHint->hide();
#endif // __LINUX__
setScrollSafeGuardForAllInputWidgets(this);
micMonitorModeLabel->setText(micMonitorModeLabel->text() + QChar(' ') +
coloredLinkString(
m_pLinkColor,
QStringLiteral("(?)"),
MIXXX_MANUAL_MIC_MONITOR_MODES_URL));
latencyCompensationLabel->setText(latencyCompensationLabel->text() + QChar(' ') +
coloredLinkString(
m_pLinkColor,
QStringLiteral("(?)"),
MIXXX_MANUAL_MIC_LATENCY_URL));
hardwareGuide->setText(
tr("The %1 lists sound cards and controllers you may want to "
"consider for using Mixxx.")
.arg(coloredLinkString(
m_pLinkColor,
tr("Mixxx DJ Hardware Guide"),
MIXXX_WIKI_HARDWARE_COMPATIBILITY_URL)));
QString deckBusHintStr = deckBusHint->text();
deckBusHintStr += " " +
coloredLinkString(m_pLinkColor,
tr("Find details in the Mixxx user manual"),
MIXXX_MANUAL_OUTPUT_AND_INPUT_DEVICES);
deckBusHint->setText(deckBusHintStr);
// Append a ':' to separate latency/underflow labels from values.
// (append here to keep existing tr strings)
latencyLabel->setText(latencyLabel->text() + ':');
underflowLabel->setText(underflowLabel->text() + ':');
}
/// Slot called when the preferences dialog is opened.
void DlgPrefSound::slotUpdate() {
m_bSkipConfigClear = true;
loadSettings();
checkLatencyCompensation();
m_bSkipConfigClear = false;
}
/// Slot called when the Apply or OK button is pressed.
void DlgPrefSound::slotApply() {
if (!m_settingsModified) {
return;
}
m_config.clearInputs();
m_config.clearOutputs();
emit writePaths(&m_config);
SoundDeviceStatus status = SoundDeviceStatus::Ok;
{
ScopedWaitCursor cursor;
const auto keylockEngine =
keylockComboBox->currentData().value<EngineBuffer::KeylockEngine>();
// Temporary set an empty config to force the audio thread to stop and
// stay off while we are swapping the keylock settings. This is
// necessary because the audio thread doesn't have any synchronisation
// mechanism due to its realtime nature and editing the RubberBand
// config while it is running leads to race conditions.
m_pSoundManager->closeActiveConfig();
m_pKeylockEngine.set(static_cast<double>(keylockEngine));
m_pSettings->set(kKeylockEngingeCfgkey,
ConfigValue(static_cast<int>(keylockEngine)));
#ifdef __RUBBERBAND__
bool keylockMultithreading = m_pSettings->getValue(
kKeylockMultiThreadingCfgkey, false);
m_pSettings->setValue(kKeylockMultiThreadingCfgkey,
keylockDualthreadedCheckBox->isChecked() &&
keylockDualthreadedCheckBox->isEnabled());
if (keylockMultithreading !=
(keylockDualthreadedCheckBox->isChecked() &&
keylockDualthreadedCheckBox->isEnabled())) {
QMessageBox::information(this,
tr("Information"),
tr("Mixxx must be restarted before the multi-threaded "
"RubberBand setting change will take effect."));
}
#endif
status = m_pSoundManager->setConfig(m_config);
m_configValid = (status == SoundDeviceStatus::Ok);
}
if (status != SoundDeviceStatus::Ok) {
QString error = m_pSoundManager->getLastErrorMessage(status);
QMessageBox::warning(nullptr, tr("Configuration error"), error);
} else {
m_settingsModified = false;
m_bLatencyChanged = false;
}
#ifdef __PIPEWIRE__
if (CmdlineArgs::Instance().getDeveloper()) {
m_pSettings->set(kPipeWire, ConfigValue(m_pipewireCheckBox->isChecked()));
}
#endif
m_bSkipConfigClear = true;
loadSettings(); // in case SM decided to change anything it didn't like
checkLatencyCompensation();
#ifdef __RUBBERBAND__
updateKeylockDualThreadingCheckbox();
#endif
m_bSkipConfigClear = false;
}
QUrl DlgPrefSound::helpUrl() const {
return QUrl(MIXXX_MANUAL_SOUND_URL);
}
void DlgPrefSound::selectIOTab(mixxx::preferences::SoundHardwareTab tab) {
switch (tab) {
case mixxx::preferences::SoundHardwareTab::Input:
ioTabs->setCurrentWidget(inputTab);
return;
case mixxx::preferences::SoundHardwareTab::Output:
ioTabs->setCurrentWidget(outputTab);
return;
}
}
/// Initializes (and creates) all the path items. Each path item widget allows
/// the user to input a sound device name and channel number given a description
/// of what will be done with that info. Inputs and outputs are grouped by tab,
/// and each path item has an identifier (Master, Headphones, ...) and an index,
/// if necessary.
void DlgPrefSound::initializePaths() {
// Pre-sort paths so they're added in the order they'll appear later on
// so Tab key order matches order in layout:
// * by AudioPathType
// * identical types by index
auto sortFilterAdd = [this]<typename T>(const QList<T>& l) {
// we use a vec of ref_wrappers since copying the path is unnecessary
// and we really just want to change the order
auto ref_vec_to_sort = std::vector<std::reference_wrapper<const T>>(l.begin(), l.end());
std::sort(ref_vec_to_sort.begin(), ref_vec_to_sort.end());
for (const T& path : ref_vec_to_sort) {
if (!path.isHidden()) {
addPath(path);
}
}
};
sortFilterAdd(m_pSoundManager->registeredOutputs());
sortFilterAdd(m_pSoundManager->registeredInputs());
}
void DlgPrefSound::addPath(const AudioOutput& output) {
// if we already know about this output, don't make a new entry
if (soundItemAlreadyExists(output, *outputTab)) {
return;
}
AudioPathType type = output.getType();
// TODO who owns this?
DlgPrefSoundItem* pSoundItem = new DlgPrefSoundItem(outputTab,
type,
m_outputDevices,
false,
AudioPath::isIndexed(type) ? output.getIndex() : 0);
insertItem(pSoundItem, outputVLayout);
connectSoundItem(pSoundItem);
setScrollSafeGuardForAllInputWidgets(pSoundItem);
}
void DlgPrefSound::addPath(const AudioInput& input) {
if (soundItemAlreadyExists(input, *inputTab)) {
return;
}
AudioPathType type = input.getType();
// TODO: who owns this?
DlgPrefSoundItem* pSoundItem = new DlgPrefSoundItem(inputTab,
type,
m_inputDevices,
true,
AudioPath::isIndexed(type) ? input.getIndex() : 0);
connectSoundItem(pSoundItem);
insertItem(pSoundItem, inputVLayout);
setScrollSafeGuardForAllInputWidgets(pSoundItem);
}
void DlgPrefSound::connectSoundItem(DlgPrefSoundItem* pItem) {
connect(pItem,
&DlgPrefSoundItem::selectedDeviceChanged,
this,
&DlgPrefSound::deviceChanged);
connect(pItem,
&DlgPrefSoundItem::selectedChannelsChanged,
this,
&DlgPrefSound::deviceChannelsChanged);
connect(pItem,
&DlgPrefSoundItem::configuredDeviceNotFound,
this,
&DlgPrefSound::configuredDeviceNotFound);
connect(this, &DlgPrefSound::loadPaths, pItem, &DlgPrefSoundItem::loadPath);
connect(this, &DlgPrefSound::writePaths, pItem, &DlgPrefSoundItem::writePath);
if (pItem->isInput()) {
connect(this, &DlgPrefSound::refreshInputDevices, pItem, &DlgPrefSoundItem::refreshDevices);
connect(this, &DlgPrefSound::addInputDevice, pItem, &DlgPrefSoundItem::addDevice);
connect(this, &DlgPrefSound::removeInputDevice, pItem, &DlgPrefSoundItem::removeDevice);
} else {
connect(this,
&DlgPrefSound::refreshOutputDevices,
pItem,
&DlgPrefSoundItem::refreshDevices);
connect(this, &DlgPrefSound::addOutputDevice, pItem, &DlgPrefSoundItem::addDevice);
connect(this, &DlgPrefSound::removeOutputDevice, pItem, &DlgPrefSoundItem::removeDevice);
}
connect(this, &DlgPrefSound::updatingAPI, pItem, &DlgPrefSoundItem::save);
connect(this, &DlgPrefSound::updatedAPI, pItem, &DlgPrefSoundItem::reload);
connect(this,
&DlgPrefSound::deviceChannelsUpdated,
pItem,
&DlgPrefSoundItem::updateDeviceChannels);
}
void DlgPrefSound::insertItem(DlgPrefSoundItem *pItem, QVBoxLayout *pLayout) {
int pos;
for (pos = 0; pos < pLayout->count() - 1; ++pos) {
DlgPrefSoundItem *pOther(qobject_cast<DlgPrefSoundItem*>(
pLayout->itemAt(pos)->widget()));
if (!pOther) { // not a sound item, skip
continue;
}
if (pItem->type() < pOther->type()) {
break;
} else if (pItem->type() == pOther->type()
&& AudioPath::isIndexed(pItem->type())
&& pItem->index() < pOther->index()) {
break;
}
}
pLayout->insertWidget(pos, pItem);
}
/// Convenience overload to load settings from the SoundManagerConfig owned by
/// SoundManager.
void DlgPrefSound::loadSettings() {
loadSettings(m_pSoundManager->getConfig());
}
/// Loads the settings in the given SoundManagerConfig into the dialog.
void DlgPrefSound::loadSettings(const SoundManagerConfig& config) {
m_loading = true; // so settingsChanged ignores all our modifications here
m_config = config;
int apiIndex = apiComboBox->findData(m_config.getAPI());
if (apiIndex != -1) {
apiComboBox->setCurrentIndex(apiIndex);
}
int sampleRateIndex = sampleRateComboBox->findData(
QVariant::fromValue(m_config.getSampleRate()));
if (sampleRateIndex != -1) {
sampleRateComboBox->setCurrentIndex(sampleRateIndex);
if (audioBufferComboBox->count() <= 0) {
updateAudioBufferSizes(sampleRateIndex); // so the latency combo box is
// sure to be populated, if setCurrentIndex is called with the
// currentIndex, the currentIndexChanged signal won't fire and
// the updateLatencies slot won't run -- bkgood lp bug 689373
}
}
int sizeIndex = audioBufferComboBox->findData(m_config.getAudioBufferSizeIndex());
if (sizeIndex != -1) {
audioBufferComboBox->setCurrentIndex(sizeIndex);
}
// Setting the index of audioBufferComboBox here sets m_bLatencyChanged to true,
// but m_bLatencyChanged should only be true when the user has edited the
// buffer size or sample rate.
m_bLatencyChanged = false;
int syncBuffers = m_config.getSyncBuffers();
if (syncBuffers == 0) {
// "Experimental (no delay)"))
deviceSyncComboBox->setCurrentIndex(1);
} else if (syncBuffers == 1) {
// "Disabled (short delay)")) = 1 buffer
deviceSyncComboBox->setCurrentIndex(2);
} else {
// "Default (long delay)" = 2 buffer
deviceSyncComboBox->setCurrentIndex(0);
}
if (m_config.getForceNetworkClock()) {
engineClockComboBox->setCurrentIndex(1);
} else {
engineClockComboBox->setCurrentIndex(0);
}
// Default keylock engine is Rubberband Faster (v2)
const auto keylockEngine = static_cast<EngineBuffer::KeylockEngine>(
m_pSettings->getValue(kKeylockEngingeCfgkey,
static_cast<int>(EngineBuffer::defaultKeylockEngine())));
const auto keylockEngineVariant = QVariant::fromValue(keylockEngine);
const int index = keylockComboBox->findData(keylockEngineVariant);
if (index >= 0) {
keylockComboBox->setCurrentIndex(index);
} else {
keylockComboBox->addItem(
EngineBuffer::getKeylockEngineName(keylockEngine), keylockEngineVariant);
keylockComboBox->setCurrentIndex(keylockComboBox->count() - 1);
}
#ifdef __RUBBERBAND__
// Default is no multi threading on keylock
keylockDualthreadedCheckBox->setChecked(m_pSettings->getValue(
kKeylockMultiThreadingCfgkey,
false));
#endif
// Collect selected I/O channel indices for all non-empty device comboboxes
// in order to allow auto-selecting free channels when different devices are
// selected later on, when a different device is selected for any I/O.
m_selectedOutputChannelIndices.clear();
m_selectedInputChannelIndices.clear();
for (auto* ch : std::as_const(outputTab->children())) {
DlgPrefSoundItem* pItem = qobject_cast<DlgPrefSoundItem*>(ch);
if (pItem) {
auto id = pItem->getDeviceId();
if (id == SoundDeviceId()) {
continue;
}
m_selectedOutputChannelIndices.insert(pItem,
QPair<SoundDeviceId, int>(id, pItem->getChannelIndex()));
}
}
for (auto* ch : std::as_const(inputTab->children())) {
DlgPrefSoundItem* pItem = qobject_cast<DlgPrefSoundItem*>(ch);
if (pItem) {
auto id = pItem->getDeviceId();
if (id == SoundDeviceId()) {
continue;
}
m_selectedInputChannelIndices.insert(pItem,
QPair<SoundDeviceId, int>(id, pItem->getChannelIndex()));
}
}
m_loading = false;
// DlgPrefSoundItem has it's own inhibit flag
emit loadPaths(m_config);
}
/// Slot called when the user selects a different API, or the
/// software changes it programmatically (for instance, when it
/// loads a value from SoundManager). Refreshes the device lists
/// for the new API and pushes those to the path items.
void DlgPrefSound::apiChanged(int index) {
m_config.setAPI(apiComboBox->itemData(index).toString());
refreshDevices();
// JACK sets its own buffer size and sample rate that Mixxx cannot change.
// PortAudio is able to chop/combine the buffer but that will mess up the
// timing in Mixxx. When we request 0 (paFramesPerBufferUnspecified)
// https://github.com/PortAudio/portaudio/blob/v19.7.0/src/common/pa_process.c#L54
// PortAudio passes buffers up to 1024 frames through.
// For bigger buffers the user has to manually match the value with Jack.
// TODO(Be): Get the buffer size from JACK and update audioBufferComboBox.
// PortAudio as off v19.7.0 does not have a way to get the buffer size from JACK.
bool enable = m_config.getAPI() == SoundManagerConfig::kAPIJack ? false : true;
sampleRateComboBox->setEnabled(enable);
deviceSyncComboBox->setEnabled(enable);
engineClockComboBox->setEnabled(enable);
updateAudioBufferSizes(sampleRateComboBox->currentIndex());
}
/// Updates the list of APIs, trying to keep the API and device selections
/// constant if possible.
void DlgPrefSound::updateAPIs() {
QString currentAPI(apiComboBox->itemData(apiComboBox->currentIndex()).toString());
emit updatingAPI();
while (apiComboBox->count() > 1) {
apiComboBox->removeItem(apiComboBox->count() - 1);
}
foreach (QString api, m_pSoundManager->getHostAPIList()) {
apiComboBox->addItem(api, api);
}
int newIndex = apiComboBox->findData(currentAPI);
if (newIndex > -1) {
apiComboBox->setCurrentIndex(newIndex);
}
emit updatedAPI();
}
/// Slot called when the sample rate combo box changes to update the
/// sample rate in the config.
void DlgPrefSound::sampleRateChanged(int index) {
m_config.setSampleRate(sampleRateComboBox->itemData(index).value<mixxx::audio::SampleRate>());
m_bLatencyChanged = true;
updateAudioBufferSizes(index);
checkLatencyCompensation();
}
/// Slot called when the latency combo box is changed to update the
/// latency in the config.
void DlgPrefSound::audioBufferChanged(int index) {
m_config.setAudioBufferSizeIndex(
audioBufferComboBox->itemData(index).toUInt());
m_bLatencyChanged = true;
checkLatencyCompensation();
}
void DlgPrefSound::syncBuffersChanged(int index) {
if (index == 0) {
// "Default (long delay)" = 2 buffer
m_config.setSyncBuffers(2);
} else if (index == 1) {
// "Experimental (no delay)")) = 0 buffer
m_config.setSyncBuffers(0);
} else {
// "Disabled (short delay)")) = 1 buffer
m_config.setSyncBuffers(1);
}
}
void DlgPrefSound::engineClockChanged(int index) {
if (index == 0) {
// "Soundcard Clock"
m_config.setForceNetworkClock(false);
} else {
// "Network Clock"
m_config.setForceNetworkClock(true);
}
}
// Slot called whenever the selected sample rate is changed. Populates the
// audio buffer input box with SMConfig::kMaxLatency values, starting at 1ms,
// representing a number of frames per buffer, which will always be a power
// of 2 (so the values displayed in ms won't be constant between sample rates,
// but they'll be close).
void DlgPrefSound::updateAudioBufferSizes(int sampleRateIndex) {
QVariant oldSizeIndex = audioBufferComboBox->currentData();
audioBufferComboBox->clear();
if (m_config.getAPI() == SoundManagerConfig::kAPIJack) {
// in case of jack we configure the frames/period
// we cannot calc the resulting buffer size in ms because the
// Sample rate is not known yet. We assume 48000 KHz here
// to calculate the buffer size index
audioBufferComboBox->addItem(tr("auto (<= 1024 frames/period)"),
static_cast<unsigned int>(SoundManagerConfig::
JackAudioBufferSizeIndex::SizeAuto));
audioBufferComboBox->addItem(tr("2048 frames/period"),
static_cast<unsigned int>(SoundManagerConfig::
JackAudioBufferSizeIndex::Size2048fpp));
audioBufferComboBox->addItem(tr("4096 frames/period"),
static_cast<unsigned int>(SoundManagerConfig::
JackAudioBufferSizeIndex::Size4096fpp));
} else {
DEBUG_ASSERT(sampleRateComboBox->itemData(sampleRateIndex)
.canConvert<mixxx::audio::SampleRate>());
double sampleRate = sampleRateComboBox->itemData(sampleRateIndex)
.value<mixxx::audio::SampleRate>()
.toDouble();
unsigned int framesPerBuffer = 1; // start this at 0 and inf loop happens
// we don't want to display any sub-1ms buffer sizes (well maybe we do but I
// don't right now!), so we iterate over all the buffer sizes until we
// find the first that gives us a buffer size >= 1 ms -- bkgood
// no div-by-0 in the next line because we don't allow srates of 0 in our
// srate list when we construct it in the ctor -- bkgood
for (; framesPerBuffer / sampleRate * 1000 < 1.0; framesPerBuffer *= 2) {
}
for (unsigned int i = 0; i < SoundManagerConfig::kMaxAudioBufferSizeIndex; ++i) {
const auto latency = static_cast<float>(framesPerBuffer / sampleRate * 1000);
// i + 1 in the next line is a latency index as described in SSConfig
audioBufferComboBox->addItem(tr("%1 ms").arg(latency, 0, 'g', 3), i + 1);
framesPerBuffer <<= 1; // *= 2
}
}
int selectionIndex = audioBufferComboBox->findData(oldSizeIndex);
if (selectionIndex > -1) {
audioBufferComboBox->setCurrentIndex(selectionIndex);
} else {
// use our default of 5 (23 ms @ 48 kHz)
selectionIndex = audioBufferComboBox->findData(
SoundManagerConfig::kDefaultAudioBufferSizeIndex);
VERIFY_OR_DEBUG_ASSERT(selectionIndex > -1) {
return;
}
audioBufferComboBox->setCurrentIndex(selectionIndex);
}
}
/// Slot called when device lists go bad to refresh them, or the API
/// just changes and we need to display new devices.
void DlgPrefSound::refreshDevices() {
if (m_config.getAPI() == SoundManagerConfig::kAPINone) {
m_outputDevices.clear();
m_inputDevices.clear();
} else {
m_outputDevices =
m_pSoundManager->getDeviceList(m_config.getAPI(), true, false);
m_inputDevices =
m_pSoundManager->getDeviceList(m_config.getAPI(), false, true);
}
emit refreshOutputDevices(m_outputDevices);
emit refreshInputDevices(m_inputDevices);
}
void DlgPrefSound::addDevice(SoundDevicePointer pDevice) {
const bool hasInputs = pDevice->getNumInputChannels().isValid();
const bool hasOutputs = pDevice->getNumOutputChannels().isValid();
if (hasInputs) {
m_inputDevices.append(pDevice);
emit addInputDevice(pDevice);
}
if (hasOutputs) {
m_outputDevices.append(pDevice);
emit addOutputDevice(pDevice);
}
}
void DlgPrefSound::removeDevice(SoundDevicePointer pDevice) {
const bool hasInputs = pDevice->getNumInputChannels().isValid();
const bool hasOutputs = pDevice->getNumOutputChannels().isValid();
if (hasInputs && m_inputDevices.removeOne(pDevice)) {
emit removeInputDevice(pDevice);
}
if (hasOutputs && m_outputDevices.removeOne(pDevice)) {
emit removeOutputDevice(pDevice);
}
}
void DlgPrefSound::updateDeviceChannels(SoundDevicePointer pDevice) {
const bool hasInputs = pDevice->getNumInputChannels().isValid();
const bool hasOutputs = pDevice->getNumOutputChannels().isValid();
const bool hadInputs = m_inputDevices.contains(pDevice);
const bool hadOutputs = m_outputDevices.contains(pDevice);
const bool listsModified = (hasInputs ^ hadInputs) || (hasOutputs ^ hadOutputs);
if (!listsModified) {
emit deviceChannelsUpdated(pDevice);
return;
}
if (hadInputs && !hasInputs) {
m_inputDevices.removeOne(pDevice);
emit removeInputDevice(pDevice);
} else if (!hadInputs && hasInputs) {
m_inputDevices.append(pDevice);
emit addInputDevice(pDevice);
}
if (hadOutputs && !hasOutputs) {
m_outputDevices.removeOne(pDevice);
emit removeOutputDevice(pDevice);
} else if (!hadOutputs && hasOutputs) {
m_outputDevices.append(pDevice);
emit addOutputDevice(pDevice);
}
}
/// Called when any of the combo boxes in this dialog are changed. Enables the
/// apply button and marks that settings have been changed so that
/// DlgPrefSound::slotApply knows to apply them.
void DlgPrefSound::settingChanged() {
if (m_loading) {
return; // doesn't count if we're just loading prefs
}
m_settingsModified = true;
}
#ifdef __RUBBERBAND__
void DlgPrefSound::updateKeylockDualThreadingCheckbox() {
bool supportedScaler = keylockComboBox->currentData()
.value<EngineBuffer::KeylockEngine>() !=
EngineBuffer::KeylockEngine::SoundTouch;
bool monoMix = mainOutputModeComboBox->currentIndex() == 1;
keylockDualthreadedCheckBox->setEnabled(!monoMix && supportedScaler);
keylockDualthreadedCheckBox->setToolTip(monoMix
? kKeylockMultiThreadedUnavailableMono
: (supportedScaler
? kKeylockMultiThreadedAvailable
: kKeylockMultiThreadedUnavailableRubberband));
}
void DlgPrefSound::updateKeylockMultithreading(bool enabled) {
m_settingsModified = true;
if (!enabled) {
return;
}
QMessageBox msg;
msg.setIcon(QMessageBox::Warning);
msg.setWindowTitle(tr("Are you sure?"));
msg.setText(
QStringLiteral("<p>%1</p><p>%2</p>")
.arg(tr("Distribute stereo channels into mono channels for "
"parallel processing will result in a loss of "
"mono compatibility and a diffuse stereo "
"image. It is not recommended during "
"broadcasting or recording."),
tr("Are you sure you wish to proceed?")));
QPushButton* pNoBtn = msg.addButton(tr("No"), QMessageBox::AcceptRole);
QPushButton* pYesBtn = msg.addButton(
tr("Yes, I know what I am doing"), QMessageBox::RejectRole);
msg.setDefaultButton(pNoBtn);
msg.exec();
keylockDualthreadedCheckBox->setChecked(msg.clickedButton() == pYesBtn);
updateKeylockDualThreadingCheckbox();
}
#endif
/// Slot called when a device from the config can not be selected, i.e. is
/// currently not available. This may happen during startup when MixxxMainWindow
/// opens this page to allow users to make adjustments in case configured