From e958930a4155f1e799c61390b08963900911a510 Mon Sep 17 00:00:00 2001 From: Dimitris Panokostas Date: Thu, 14 Mar 2024 15:00:03 +0100 Subject: [PATCH] enhancement: Added option to use System Default audio device (fixes #1241) This option allows SDL2 to pick the system default audio device, which may not be listed in the enumerated ones shown in the dropdown. --- src/audio.cpp | 6 ++++++ src/include/options.h | 3 +++ src/osdep/ahi_v1.cpp | 4 ++-- src/osdep/amiberry.cpp | 3 +++ src/osdep/cda_play.cpp | 2 +- src/osdep/gui/Navigation.cpp | 5 +++-- src/osdep/gui/PanelSound.cpp | 27 +++++++++++++++++++++++---- src/sounddep/sound.cpp | 2 +- 8 files changed, 42 insertions(+), 10 deletions(-) diff --git a/src/audio.cpp b/src/audio.cpp index 30a35e4c7..3f7d9dcf8 100644 --- a/src/audio.cpp +++ b/src/audio.cpp @@ -2040,6 +2040,9 @@ static int sound_prefs_changed (void) return 0; if (changed_prefs.produce_sound != currprefs.produce_sound || changed_prefs.soundcard != currprefs.soundcard +#ifdef AMIBERRY + || changed_prefs.soundcard_default != currprefs.soundcard_default +#endif || changed_prefs.sound_stereo != currprefs.sound_stereo || changed_prefs.sound_maxbsiz != currprefs.sound_maxbsiz || changed_prefs.sound_freq != currprefs.sound_freq @@ -2129,6 +2132,9 @@ void set_audio (void) currprefs.produce_sound = changed_prefs.produce_sound; currprefs.soundcard = changed_prefs.soundcard; +#ifdef AMIBERRY + currprefs.soundcard_default = changed_prefs.soundcard_default; +#endif currprefs.sound_stereo = changed_prefs.sound_stereo; active_sound_stereo = currprefs.sound_stereo; currprefs.sound_auto = changed_prefs.sound_auto; diff --git a/src/include/options.h b/src/include/options.h index 1bacc9591..b2084d762 100644 --- a/src/include/options.h +++ b/src/include/options.h @@ -962,6 +962,9 @@ struct uae_prefs bool midirouter; int uaescsimode; int soundcard; +#ifdef AMIBERRY + bool soundcard_default; +#endif int samplersoundcard; bool blankmonitors; bool right_control_is_right_win_key; diff --git a/src/osdep/ahi_v1.cpp b/src/osdep/ahi_v1.cpp index 86bab450e..5af31247e 100644 --- a/src/osdep/ahi_v1.cpp +++ b/src/osdep/ahi_v1.cpp @@ -257,7 +257,7 @@ static int ahi_init_sound(void) if (!ahisndbuffer) return 0; - auto devname = sound_devices[currprefs.soundcard]->name; + const auto devname = sound_devices[currprefs.soundcard]->name; SDL_zero(ahi_want); ahi_want.freq = sound_freq_ahi; ahi_want.format = AUDIO_S16SYS; @@ -268,7 +268,7 @@ static int ahi_init_sound(void) sound_freq_ahi, sound_channels_ahi, sound_bits_ahi, amigablksize); if (ahi_dev == 0) - ahi_dev = SDL_OpenAudioDevice(devname, 0, &ahi_want, &ahi_have, 0); + ahi_dev = SDL_OpenAudioDevice(currprefs.soundcard_default ? nullptr : devname, 0, &ahi_want, &ahi_have, 0); if (ahi_dev == 0) { write_log(_T("AHI: Failed to open selected SDL2 device: %s, retrying with default device\n"), SDL_GetError()); diff --git a/src/osdep/amiberry.cpp b/src/osdep/amiberry.cpp index 14bd2aeb9..504338ad6 100644 --- a/src/osdep/amiberry.cpp +++ b/src/osdep/amiberry.cpp @@ -2414,6 +2414,7 @@ void target_save_options(struct zfile* f, struct uae_prefs* p) cfgfile_target_dwrite_bool(f, _T("borderless"), p->borderless); cfgfile_target_dwrite_bool(f, _T("blank_monitors"), p->blankmonitors); cfgfile_target_dwrite_str(f, _T("uaescsimode"), scsimode[p->uaescsimode]); + cfgfile_target_dwrite_bool(f, _T("soundcard_default"), p->soundcard_default); cfgfile_target_write(f, _T("soundcard"), _T("%d"), p->soundcard); if (p->soundcard >= 0 && p->soundcard < MAX_SOUND_DEVICES && sound_devices[p->soundcard]) cfgfile_target_write_str(f, _T("soundcardname"), sound_devices[p->soundcard]->cfgname); @@ -2608,6 +2609,8 @@ static int target_parse_option_host(struct uae_prefs *p, const TCHAR *option, co if (cfgfile_yesno(option, value, _T("rtg_scale_allow"), &p->rtgallowscaling)) return 1; + if (cfgfile_yesno(option, value, _T("soundcard_default"), &p->soundcard_default)) + return 1; if (cfgfile_intval(option, value, _T("soundcard"), &p->soundcard, 1)) { if (p->soundcard < 0 || p->soundcard >= MAX_SOUND_DEVICES || sound_devices[p->soundcard] == NULL) p->soundcard = 0; diff --git a/src/osdep/cda_play.cpp b/src/osdep/cda_play.cpp index fb35fc59f..cb2a117ff 100644 --- a/src/osdep/cda_play.cpp +++ b/src/osdep/cda_play.cpp @@ -67,7 +67,7 @@ cda_audio::cda_audio(int num_sectors, int sectorsize, int samplerate) cdda_want.callback = sdl2_cdaudio_callback; if (cdda_dev == 0) - cdda_dev = SDL_OpenAudioDevice(devname, 0, &cdda_want, &cdda_have, 0); + cdda_dev = SDL_OpenAudioDevice(currprefs.soundcard_default ? nullptr : devname, 0, &cdda_want, &cdda_have, 0); if (cdda_dev == 0) { write_log("Failed to open selected SDL2 device for CD-audio: %s, retrying with default device\n", SDL_GetError()); diff --git a/src/osdep/gui/Navigation.cpp b/src/osdep/gui/Navigation.cpp index aa78487a1..05cf45e17 100644 --- a/src/osdep/gui/Navigation.cpp +++ b/src/osdep/gui/Navigation.cpp @@ -287,8 +287,9 @@ static NavigationMap navMap[] = // active move left move right move up move down // PanelSound - { "cboSoundcard", "Sound", "Sound", "sldFloppySoundDisk", "sndDisable" }, - { "sndDisable", "Sound", "sldPaulaVol", "cboSoundcard", "sndDisEmu" }, + { "cboSoundcard", "Sound", "Sound", "sldFloppySoundDisk", "chkSystemDefault" }, + {"chkSystemDefault", "Sound", "Sound", "cboSoundcard", "sndDisable"}, + { "sndDisable", "Sound", "sldPaulaVol", "chkSystemDefault", "sndDisEmu" }, { "sndDisEmu", "Sound", "sldPaulaVol", "sndDisable", "sndEmulate" }, { "sndEmulate", "Sound", "sldPaulaVol", "sndDisEmu", "chkAutoSwitching" }, { "chkAutoSwitching", "Sound", "sldPaulaVol", "sndEmulate", "cboChannelMode" }, diff --git a/src/osdep/gui/PanelSound.cpp b/src/osdep/gui/PanelSound.cpp index a6cf3950e..f3a3879d6 100644 --- a/src/osdep/gui/PanelSound.cpp +++ b/src/osdep/gui/PanelSound.cpp @@ -54,7 +54,9 @@ static gcn::Slider* sldSoundBufferSize; static gcn::Label* lblSoundBufferSize; static gcn::RadioButton* optSoundPull; static gcn::RadioButton* optSoundPush; +static gcn::Label* lblSoundcard; static gcn::DropDown* cboSoundcard; +static gcn::CheckBox* chkSystemDefault; static gcn::Label* lblSwapChannels; static gcn::DropDown* cboSwapChannels; @@ -91,7 +93,11 @@ class SoundActionListener : public gcn::ActionListener public: void action(const gcn::ActionEvent& actionEvent) override { - if (actionEvent.getSource() == cboSoundcard) + if (actionEvent.getSource() == chkSystemDefault) + { + changed_prefs.soundcard_default = chkSystemDefault->isSelected(); + } + else if (actionEvent.getSource() == cboSoundcard) { const int soundcard = cboSoundcard->getSelected(); if (soundcard != changed_prefs.soundcard) @@ -302,13 +308,19 @@ void InitPanelSound(const config_category& category) sound_action_listener = new SoundActionListener(); + lblSoundcard = new gcn::Label("Device:"); + lblSoundcard->setAlignment(gcn::Graphics::RIGHT); cboSoundcard = new gcn::DropDown(&soundcard_list); - cboSoundcard->setSize(category.panel->getWidth() - DISTANCE_BORDER * 2, cboSoundcard->getHeight()); + cboSoundcard->setSize(category.panel->getWidth() - lblSoundcard->getWidth() - 8 - DISTANCE_BORDER * 2, cboSoundcard->getHeight()); cboSoundcard->setBaseColor(gui_baseCol); cboSoundcard->setBackgroundColor(colTextboxBackground); cboSoundcard->setId("cboSoundcard"); cboSoundcard->addActionListener(sound_action_listener); + chkSystemDefault = new gcn::CheckBox("System default"); + chkSystemDefault->setId("chkSystemDefault"); + chkSystemDefault->addActionListener(sound_action_listener); + optSoundDisabled = new gcn::RadioButton("Disabled", "radiosoundgroup"); optSoundDisabled->setId("sndDisable"); optSoundDisabled->addActionListener(sound_action_listener); @@ -525,8 +537,11 @@ void InitPanelSound(const config_category& category) grpSoundBufferSize->setBaseColor(gui_baseCol); int posY = DISTANCE_BORDER; - category.panel->add(cboSoundcard, DISTANCE_BORDER, posY); + category.panel->add(lblSoundcard, DISTANCE_BORDER, posY); + category.panel->add(cboSoundcard, lblSoundcard->getX() + lblSoundcard->getWidth() + 8, posY); posY += cboSoundcard->getHeight() + DISTANCE_NEXT_Y; + category.panel->add(chkSystemDefault, DISTANCE_BORDER, posY); + posY += chkSystemDefault->getHeight() + DISTANCE_NEXT_Y; category.panel->add(grpSound, DISTANCE_BORDER, posY); category.panel->add(grpVolume, grpSound->getX() + grpSound->getWidth() + DISTANCE_NEXT_X, posY); posY += grpSound->getHeight() + DISTANCE_NEXT_Y; @@ -558,7 +573,9 @@ void ExitPanelSound() { delete lblSwapChannels; delete cboSwapChannels; + delete lblSoundcard; delete cboSoundcard; + delete chkSystemDefault; delete optSoundDisabled; delete optSoundDisabledEmu; delete optSoundEmulated; @@ -618,7 +635,9 @@ void RefreshPanelSound() { char tmp[10]; - if (numdevs == 0) + chkSystemDefault->setSelected(changed_prefs.soundcard_default); + + if (numdevs == 0 || chkSystemDefault->isSelected()) { cboSoundcard->setEnabled(false); } diff --git a/src/sounddep/sound.cpp b/src/sounddep/sound.cpp index 9318c7526..3fa88a2df 100644 --- a/src/sounddep/sound.cpp +++ b/src/sounddep/sound.cpp @@ -321,7 +321,7 @@ static int open_audio_sdl2(struct sound_data* sd, int index) } if (s->dev == 0) - s->dev = SDL_OpenAudioDevice(devname, 0, &want, &have, 0); + s->dev = SDL_OpenAudioDevice(currprefs.soundcard_default ? nullptr : devname, 0, &want, &have, 0); if (s->dev == 0) { write_log("Failed to open selected SDL2 device for audio: %s, retrying with default device\n", SDL_GetError());