Skip to content
This repository was archived by the owner on Sep 29, 2025. It is now read-only.

Commit 55a2261

Browse files
authored
Merge pull request #1485 from reicast/baka/alsa_cfg
ALSA: configurable output device
2 parents 229f729 + 1b8f45c commit 55a2261

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

core/oslib/audiobackend_alsa.cpp

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,58 @@
11
#include "oslib/audiobackend_alsa.h"
22
#if USE_ALSA
33
#include <alsa/asoundlib.h>
4+
#include "cfg/cfg.h"
45

56
snd_pcm_t *handle;
67

78
// We're making these functions static - there's no need to pollute the global namespace
89
static void alsa_init()
910
{
10-
11-
long loops;
12-
int size;
13-
1411
snd_pcm_hw_params_t *params;
1512
unsigned int val;
1613
int dir=-1;
1714
snd_pcm_uframes_t frames;
1815

19-
/* Open PCM device for playback. */
20-
int rc = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0);
16+
string device = cfgLoadStr("alsa", "device", "");
2117

22-
if (rc<0)
23-
rc = snd_pcm_open(&handle, "plughw:0,0,0", SND_PCM_STREAM_PLAYBACK, 0);
24-
25-
if (rc<0)
26-
rc = snd_pcm_open(&handle, "plughw:0,0", SND_PCM_STREAM_PLAYBACK, 0);
18+
int rc = -1;
19+
if (device == "")
20+
{
21+
printf("ALSA: trying to determine audio device\n");
22+
/* Open PCM device for playback. */
23+
device = "default";
24+
rc = snd_pcm_open(&handle, device.c_str(), SND_PCM_STREAM_PLAYBACK, 0);
25+
26+
if (rc < 0)
27+
{
28+
device = "plughw:0,0,0";
29+
rc = snd_pcm_open(&handle, device.c_str(), SND_PCM_STREAM_PLAYBACK, 0);
30+
}
31+
32+
if (rc < 0)
33+
{
34+
device = "plughw:0,0";
35+
rc = snd_pcm_open(&handle, device.c_str(), SND_PCM_STREAM_PLAYBACK, 0);
36+
}
37+
38+
if (rc >= 0)
39+
{
40+
// init successfull, write value back to config
41+
cfgSaveStr("alsa", "device", device.c_str());
42+
}
43+
}
44+
else {
45+
rc = snd_pcm_open(&handle, device.c_str(), SND_PCM_STREAM_PLAYBACK, 0);
46+
}
2747

2848
if (rc < 0)
2949
{
30-
fprintf(stderr, "unable to open PCM device: %s\n", snd_strerror(rc));
50+
fprintf(stderr, "unable to open PCM device %s: %s\n", device.c_str(), snd_strerror(rc));
3151
return;
3252
}
3353

54+
printf("ALSA: Successfully initialized \"%s\"\n", device.c_str());
55+
3456
/* Allocate a hardware parameters object. */
3557
snd_pcm_hw_params_alloca(&params);
3658

0 commit comments

Comments
 (0)