From 347fcf5c6c5ef57b7bb6b48352e1fc7230b66717 Mon Sep 17 00:00:00 2001 From: "Christoph \"baka0815\" Schwerdtfeger" Date: Wed, 16 Jan 2019 19:04:32 +0100 Subject: [PATCH] ALSA: make device configurable Trying to load the device from the emu.cfg and if not set (should be the default for everyone currently) use the existing procedure to try to determine the device (default > plughw:0,0,0 > plughw:0,0). --- core/oslib/audiobackend_alsa.cpp | 40 ++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/core/oslib/audiobackend_alsa.cpp b/core/oslib/audiobackend_alsa.cpp index 870682e53..099d13a5f 100644 --- a/core/oslib/audiobackend_alsa.cpp +++ b/core/oslib/audiobackend_alsa.cpp @@ -1,6 +1,7 @@ #include "oslib/audiobackend_alsa.h" #if USE_ALSA #include +#include "cfg/cfg.h" snd_pcm_t *handle; @@ -16,21 +17,46 @@ static void alsa_init() int dir=-1; snd_pcm_uframes_t frames; - /* Open PCM device for playback. */ - int rc = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0); + string device = cfgLoadStr("alsa", "device", ""); - if (rc<0) - rc = snd_pcm_open(&handle, "plughw:0,0,0", SND_PCM_STREAM_PLAYBACK, 0); + int rc = -1; + if (device == "") + { + printf("ALSA: trying to determine audio device\n"); + /* Open PCM device for playback. */ + device = "default"; + rc = snd_pcm_open(&handle, device.c_str(), SND_PCM_STREAM_PLAYBACK, 0); - if (rc<0) - rc = snd_pcm_open(&handle, "plughw:0,0", SND_PCM_STREAM_PLAYBACK, 0); + if (rc < 0) + { + device = "plughw:0,0,0"; + rc = snd_pcm_open(&handle, device.c_str(), SND_PCM_STREAM_PLAYBACK, 0); + } + + if (rc < 0) + { + device = "plughw:0,0"; + rc = snd_pcm_open(&handle, device.c_str(), SND_PCM_STREAM_PLAYBACK, 0); + } + + if (rc >= 0) + { + // init successfull, write value back to config + cfgSaveStr("alsa", "device", device.c_str()); + } + } + else { + rc = snd_pcm_open(&handle, device.c_str(), SND_PCM_STREAM_PLAYBACK, 0); + } if (rc < 0) { - fprintf(stderr, "unable to open PCM device: %s\n", snd_strerror(rc)); + fprintf(stderr, "unable to open PCM device %s: %s\n", device.c_str(), snd_strerror(rc)); return; } + printf("ALSA: Successfully initialized \"%s\"\n", device.c_str()); + /* Allocate a hardware parameters object. */ snd_pcm_hw_params_alloca(¶ms);