|
1 | 1 | #include "oslib/audiobackend_alsa.h" |
2 | 2 | #if USE_ALSA |
3 | 3 | #include <alsa/asoundlib.h> |
| 4 | +#include "cfg/cfg.h" |
4 | 5 |
|
5 | 6 | snd_pcm_t *handle; |
6 | 7 |
|
7 | 8 | // We're making these functions static - there's no need to pollute the global namespace |
8 | 9 | static void alsa_init() |
9 | 10 | { |
10 | | - |
11 | | - long loops; |
12 | | - int size; |
13 | | - |
14 | 11 | snd_pcm_hw_params_t *params; |
15 | 12 | unsigned int val; |
16 | 13 | int dir=-1; |
17 | 14 | snd_pcm_uframes_t frames; |
18 | 15 |
|
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", ""); |
21 | 17 |
|
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 | + } |
27 | 47 |
|
28 | 48 | if (rc < 0) |
29 | 49 | { |
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)); |
31 | 51 | return; |
32 | 52 | } |
33 | 53 |
|
| 54 | + printf("ALSA: Successfully initialized \"%s\"\n", device.c_str()); |
| 55 | + |
34 | 56 | /* Allocate a hardware parameters object. */ |
35 | 57 | snd_pcm_hw_params_alloca(¶ms); |
36 | 58 |
|
|
0 commit comments