Skip to content

Commit

Permalink
enhancement: Added option to use System Default audio device (fixes #…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
midwan committed Mar 14, 2024
1 parent 5dcfe62 commit e958930
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 10 deletions.
6 changes: 6 additions & 0 deletions src/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/include/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/osdep/ahi_v1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
Expand Down
3 changes: 3 additions & 0 deletions src/osdep/amiberry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/osdep/cda_play.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
5 changes: 3 additions & 2 deletions src/osdep/gui/Navigation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down
27 changes: 23 additions & 4 deletions src/osdep/gui/PanelSound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -558,7 +573,9 @@ void ExitPanelSound()
{
delete lblSwapChannels;
delete cboSwapChannels;
delete lblSoundcard;
delete cboSoundcard;
delete chkSystemDefault;
delete optSoundDisabled;
delete optSoundDisabledEmu;
delete optSoundEmulated;
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/sounddep/sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit e958930

Please sign in to comment.