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).
This commit is contained in:
Christoph "baka0815" Schwerdtfeger 2019-01-16 19:04:32 +01:00
parent 3c57177d38
commit 347fcf5c6c
1 changed files with 33 additions and 7 deletions

View File

@ -1,6 +1,7 @@
#include "oslib/audiobackend_alsa.h"
#if USE_ALSA
#include <alsa/asoundlib.h>
#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(&params);