Don't check explicitly against formats, but rather their byte/channel count
[openal-soft.git] / Alc / alsa.c
bloba6035d4609e664fea5fafece79fd561f6aeac671
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include "config.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <memory.h>
26 #ifdef HAVE_DLFCN_H
27 #include <dlfcn.h>
28 #endif
29 #include "alMain.h"
30 #include "AL/al.h"
31 #include "AL/alc.h"
33 #include <alsa/asoundlib.h>
36 typedef struct {
37 snd_pcm_t *pcmHandle;
38 snd_pcm_format_t format;
39 int killNow;
40 ALvoid *thread;
41 } alsa_data;
43 typedef struct {
44 ALCchar *name;
45 int card, dev;
46 } DevMap;
48 static void *alsa_handle;
49 #define MAKE_FUNC(f) static typeof(f) * p##f
50 MAKE_FUNC(snd_strerror);
51 MAKE_FUNC(snd_pcm_open);
52 MAKE_FUNC(snd_pcm_close);
53 MAKE_FUNC(snd_pcm_nonblock);
54 MAKE_FUNC(snd_pcm_frames_to_bytes);
55 MAKE_FUNC(snd_pcm_hw_params_malloc);
56 MAKE_FUNC(snd_pcm_hw_params_free);
57 MAKE_FUNC(snd_pcm_hw_params_any);
58 MAKE_FUNC(snd_pcm_hw_params_set_access);
59 MAKE_FUNC(snd_pcm_hw_params_set_format);
60 MAKE_FUNC(snd_pcm_hw_params_set_channels);
61 MAKE_FUNC(snd_pcm_hw_params_set_periods_near);
62 MAKE_FUNC(snd_pcm_hw_params_set_rate_near);
63 MAKE_FUNC(snd_pcm_hw_params_set_rate);
64 MAKE_FUNC(snd_pcm_hw_params_set_buffer_size_near);
65 MAKE_FUNC(snd_pcm_hw_params_set_buffer_size_min);
66 MAKE_FUNC(snd_pcm_hw_params);
67 MAKE_FUNC(snd_pcm_prepare);
68 MAKE_FUNC(snd_pcm_start);
69 MAKE_FUNC(snd_pcm_resume);
70 MAKE_FUNC(snd_pcm_state);
71 MAKE_FUNC(snd_pcm_avail_update);
72 MAKE_FUNC(snd_pcm_areas_silence);
73 MAKE_FUNC(snd_pcm_mmap_begin);
74 MAKE_FUNC(snd_pcm_mmap_commit);
75 MAKE_FUNC(snd_pcm_drain);
76 MAKE_FUNC(snd_pcm_info_malloc);
77 MAKE_FUNC(snd_pcm_info_free);
78 MAKE_FUNC(snd_pcm_info_set_device);
79 MAKE_FUNC(snd_pcm_info_set_subdevice);
80 MAKE_FUNC(snd_pcm_info_set_stream);
81 MAKE_FUNC(snd_pcm_info_get_name);
82 MAKE_FUNC(snd_ctl_pcm_next_device);
83 MAKE_FUNC(snd_ctl_pcm_info);
84 MAKE_FUNC(snd_ctl_open);
85 MAKE_FUNC(snd_ctl_close);
86 MAKE_FUNC(snd_ctl_card_info_malloc);
87 MAKE_FUNC(snd_ctl_card_info_free);
88 MAKE_FUNC(snd_ctl_card_info);
89 MAKE_FUNC(snd_ctl_card_info_get_name);
90 MAKE_FUNC(snd_card_next);
91 #undef MAKE_FUNC
93 #define MAX_DEVICES 16
94 #define MAX_ALL_DEVICES 32
96 static DevMap allDevNameMap[MAX_ALL_DEVICES];
97 static ALCchar *alsaDeviceList[MAX_DEVICES];
98 static ALCchar *alsaCaptureDeviceList[MAX_DEVICES];
100 static int xrun_recovery(snd_pcm_t *handle, int err)
102 if (err == -EPIPE)
103 { /* under-run */
104 err = psnd_pcm_prepare(handle);
105 if(err >= 0)
106 err = psnd_pcm_start(handle);
107 if (err < 0)
108 AL_PRINT("prepare failed: %s\n", psnd_strerror(err));
110 else if (err == -ESTRPIPE)
112 while ((err = psnd_pcm_resume(handle)) == -EAGAIN)
113 usleep(1); /* wait until the suspend flag is released */
114 if (err < 0)
116 err = psnd_pcm_prepare(handle);
117 if(err >= 0)
118 err = psnd_pcm_start(handle);
119 if (err < 0)
120 AL_PRINT("prepare failed: %s\n", psnd_strerror(err));
123 return err;
127 static ALuint ALSAProc(ALvoid *ptr)
129 ALCdevice *pDevice = (ALCdevice*)ptr;
130 alsa_data *data = (alsa_data*)pDevice->ExtraData;
131 const snd_pcm_channel_area_t *areas = NULL;
132 snd_pcm_sframes_t avail, commitres;
133 snd_pcm_uframes_t offset, frames;
134 char *WritePtr;
135 int WriteCnt;
136 int err;
138 while(!data->killNow)
140 snd_pcm_state_t state = psnd_pcm_state(data->pcmHandle);
141 if(state == SND_PCM_STATE_XRUN)
143 err = xrun_recovery(data->pcmHandle, -EPIPE);
144 if (err < 0)
146 AL_PRINT("XRUN recovery failed: %s\n", psnd_strerror(err));
147 break;
150 else if (state == SND_PCM_STATE_SUSPENDED)
152 err = xrun_recovery(data->pcmHandle, -ESTRPIPE);
153 if (err < 0)
155 AL_PRINT("SUSPEND recovery failed: %s\n", psnd_strerror(err));
156 break;
160 avail = psnd_pcm_avail_update(data->pcmHandle);
161 if(avail < 0)
163 err = xrun_recovery(data->pcmHandle, avail);
164 if (err < 0)
166 AL_PRINT("available update failed: %s\n", psnd_strerror(err));
167 break;
171 // make sure there's frames to process
172 if(avail == 0)
174 usleep(1000);
175 continue;
178 // it is possible that contiguous areas are smaller, thus we use a loop
179 while (avail > 0)
181 frames = avail;
183 err = psnd_pcm_mmap_begin(data->pcmHandle, &areas, &offset, &frames);
184 if (err < 0)
186 err = xrun_recovery(data->pcmHandle, err);
187 if (err < 0)
188 AL_PRINT("mmap begin error: %s\n", psnd_strerror(err));
189 break;
192 SuspendContext(NULL);
193 WritePtr = (char*)areas->addr + (offset * areas->step / 8);
194 WriteCnt = psnd_pcm_frames_to_bytes(data->pcmHandle, frames);
195 aluMixData(pDevice->Context, WritePtr, WriteCnt, pDevice->Format);
196 ProcessContext(NULL);
198 commitres = psnd_pcm_mmap_commit(data->pcmHandle, offset, frames);
199 if (commitres < 0 || (commitres-frames) != 0)
201 AL_PRINT("mmap commit error: %s\n",
202 psnd_strerror(commitres >= 0 ? -EPIPE : commitres));
203 break;
206 avail -= frames;
210 return 0;
213 static void fill_silence(snd_pcm_t *pcmHandle, snd_pcm_format_t alsaFormat, int channels)
215 const snd_pcm_channel_area_t *areas = NULL;
216 snd_pcm_sframes_t avail, commitres;
217 snd_pcm_uframes_t offset, frames;
218 int err;
220 avail = psnd_pcm_avail_update(pcmHandle);
221 if(avail < 0)
223 err = xrun_recovery(pcmHandle, avail);
224 if (err < 0)
226 AL_PRINT("available update failed: %s\n", psnd_strerror(err));
227 return;
231 // it is possible that contiguous areas are smaller, thus we use a loop
232 while (avail > 0)
234 frames = avail;
236 err = psnd_pcm_mmap_begin(pcmHandle, &areas, &offset, &frames);
237 if (err < 0)
239 err = xrun_recovery(pcmHandle, err);
240 if (err < 0)
242 AL_PRINT("mmap begin error: %s\n", psnd_strerror(err));
243 break;
245 continue;
248 psnd_pcm_areas_silence(areas, offset, channels, frames, alsaFormat);
250 commitres = psnd_pcm_mmap_commit(pcmHandle, offset, frames);
251 if (commitres < 0 || (commitres-frames) != 0)
253 AL_PRINT("mmap commit error: %s\n",
254 psnd_strerror(commitres >= 0 ? -EPIPE : commitres));
255 break;
258 avail -= frames;
262 static ALCboolean alsa_open_playback(ALCdevice *device, const ALCchar *deviceName)
264 snd_pcm_uframes_t bufferSizeInFrames;
265 snd_pcm_hw_params_t *p = NULL;
266 unsigned int periods;
267 alsa_data *data;
268 char driver[64];
269 char *err;
270 int i;
272 strncpy(driver, GetConfigValue("alsa", "default", "default"), sizeof(driver)-1);
273 driver[sizeof(driver)-1] = 0;
274 if(deviceName)
276 size_t idx;
278 for(idx = 0;idx < MAX_ALL_DEVICES;idx++)
280 if(allDevNameMap[idx].name &&
281 strcmp(deviceName, allDevNameMap[idx].name) == 0)
283 device->szDeviceName = allDevNameMap[idx].name;
284 if(idx > 0)
285 sprintf(driver, "hw:%d,%d", allDevNameMap[idx].card, allDevNameMap[idx].dev);
286 goto open_alsa;
289 for(idx = 0;idx < MAX_DEVICES;idx++)
291 if(alsaDeviceList[idx] &&
292 strcmp(deviceName, alsaDeviceList[idx]) == 0)
294 device->szDeviceName = alsaDeviceList[idx];
295 if(idx > 0)
296 sprintf(driver, "hw:%zd,0", idx-1);
297 goto open_alsa;
300 return ALC_FALSE;
302 else
303 device->szDeviceName = alsaDeviceList[0];
305 open_alsa:
306 data = (alsa_data*)calloc(1, sizeof(alsa_data));
308 i = psnd_pcm_open(&data->pcmHandle, driver, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
309 if(i < 0)
311 usleep(200000);
312 i = psnd_pcm_open(&data->pcmHandle, driver, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
314 if(i >= 0)
316 i = psnd_pcm_nonblock(data->pcmHandle, 0);
317 if(i < 0)
318 psnd_pcm_close(data->pcmHandle);
320 if(i < 0)
322 free(data);
323 AL_PRINT("Could not open playback device '%s': %s\n", driver, psnd_strerror(i));
324 return ALC_FALSE;
327 switch(aluBytesFromFormat(device->Format))
329 case 1:
330 data->format = SND_PCM_FORMAT_U8;
331 break;
332 case 2:
333 data->format = SND_PCM_FORMAT_S16;
334 break;
335 default:
336 data->format = SND_PCM_FORMAT_UNKNOWN;
337 AL_PRINT("Unknown format?! %x\n", device->Format);
340 periods = GetConfigValueInt("alsa", "periods", 4);
341 if((int)periods <= 0)
342 periods = 4;
343 bufferSizeInFrames = device->UpdateFreq;
345 psnd_pcm_hw_params_malloc(&p);
346 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
347 /* start with the largest configuration space possible */
348 if(!(ok(psnd_pcm_hw_params_any(data->pcmHandle, p), "any") &&
349 /* set interleaved access */
350 ok(psnd_pcm_hw_params_set_access(data->pcmHandle, p, SND_PCM_ACCESS_MMAP_INTERLEAVED), "set access") &&
351 /* set format (implicitly sets sample bits) */
352 ok(psnd_pcm_hw_params_set_format(data->pcmHandle, p, data->format), "set format") &&
353 /* set channels (implicitly sets frame bits) */
354 ok(psnd_pcm_hw_params_set_channels(data->pcmHandle, p, device->Channels), "set channels") &&
355 /* set periods (implicitly constrains period/buffer parameters) */
356 ok(psnd_pcm_hw_params_set_periods_near(data->pcmHandle, p, &periods, NULL), "set periods near") &&
357 /* set rate (implicitly constrains period/buffer parameters) */
358 ok(psnd_pcm_hw_params_set_rate_near(data->pcmHandle, p, &device->Frequency, NULL), "set rate near") &&
359 /* set buffer size in frame units (implicitly sets period size/bytes/time and buffer time/bytes) */
360 ok(psnd_pcm_hw_params_set_buffer_size_near(data->pcmHandle, p, &bufferSizeInFrames), "set buffer size near") &&
361 /* install and prepare hardware configuration */
362 ok(psnd_pcm_hw_params(data->pcmHandle, p), "set params")))
364 AL_PRINT("%s failed: %s\n", err, psnd_strerror(i));
365 psnd_pcm_hw_params_free(p);
366 psnd_pcm_close(data->pcmHandle);
367 free(data);
368 return ALC_FALSE;
370 #undef ok
371 psnd_pcm_hw_params_free(p);
373 device->MaxNoOfSources = 256;
374 device->UpdateFreq = bufferSizeInFrames;
376 i = psnd_pcm_prepare(data->pcmHandle);
377 if(i < 0)
379 AL_PRINT("prepare error: %s\n", psnd_strerror(i));
380 psnd_pcm_close(data->pcmHandle);
381 free(data);
382 return ALC_FALSE;
385 fill_silence(data->pcmHandle, data->format, device->Channels);
386 i = psnd_pcm_start(data->pcmHandle);
387 if(i < 0)
389 AL_PRINT("start error: %s\n", psnd_strerror(i));
390 psnd_pcm_close(data->pcmHandle);
391 free(data);
392 return ALC_FALSE;
395 device->ExtraData = data;
396 data->thread = StartThread(ALSAProc, device);
397 if(data->thread == NULL)
399 psnd_pcm_close(data->pcmHandle);
400 device->ExtraData = NULL;
401 free(data);
402 return ALC_FALSE;
405 return ALC_TRUE;
408 static void alsa_close_playback(ALCdevice *device)
410 alsa_data *data = (alsa_data*)device->ExtraData;
411 data->killNow = 1;
412 StopThread(data->thread);
413 psnd_pcm_close(data->pcmHandle);
415 free(data);
416 device->ExtraData = NULL;
420 static ALCboolean alsa_open_capture(ALCdevice *pDevice, const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
422 snd_pcm_format_t alsaFormat;
423 snd_pcm_hw_params_t *p;
424 unsigned int periods = 4;
425 snd_pcm_uframes_t bufferSizeInFrames = SampleSize;
426 alsa_data *data;
427 char driver[64];
428 char *err;
429 int i;
431 strncpy(driver, GetConfigValue("alsa", "capture", "default"), sizeof(driver)-1);
432 driver[sizeof(driver)-1] = 0;
433 if(deviceName)
435 size_t idx;
437 for(idx = 0;idx < MAX_DEVICES;idx++)
439 if(alsaCaptureDeviceList[idx] &&
440 strcmp(deviceName, alsaCaptureDeviceList[idx]) == 0)
442 pDevice->szDeviceName = alsaCaptureDeviceList[idx];
443 if(idx > 0)
444 sprintf(driver, "hw:%zd,0", idx-1);
445 goto open_alsa;
448 return ALC_FALSE;
450 else
451 pDevice->szDeviceName = alsaCaptureDeviceList[0];
453 open_alsa:
454 data = (alsa_data*)calloc(1, sizeof(alsa_data));
456 i = psnd_pcm_open(&data->pcmHandle, driver, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
457 if(i < 0)
459 usleep(200000);
460 i = psnd_pcm_open(&data->pcmHandle, driver, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
462 if(i >= 0)
464 i = psnd_pcm_nonblock(data->pcmHandle, 0);
465 if(i < 0)
466 psnd_pcm_close(data->pcmHandle);
468 if(i < 0)
470 free(data);
471 AL_PRINT("Could not open capture device '%s': %s\n", driver, psnd_strerror(i));
472 return ALC_FALSE;
475 switch(aluBytesFromFormat(format))
477 case 1:
478 alsaFormat = SND_PCM_FORMAT_U8;
479 break;
480 case 2:
481 alsaFormat = SND_PCM_FORMAT_S16;
482 break;
483 default:
484 alsaFormat = SND_PCM_FORMAT_UNKNOWN;
485 AL_PRINT("Unknown format?! %x\n", format);
488 psnd_pcm_hw_params_malloc(&p);
489 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
490 /* start with the largest configuration space possible */
491 if(!(ok(psnd_pcm_hw_params_any(data->pcmHandle, p), "any") &&
492 /* set interleaved access */
493 ok(psnd_pcm_hw_params_set_access(data->pcmHandle, p, SND_PCM_ACCESS_MMAP_INTERLEAVED), "set access") &&
494 /* set format (implicitly sets sample bits) */
495 ok(psnd_pcm_hw_params_set_format(data->pcmHandle, p, alsaFormat), "set format") &&
496 /* set channels (implicitly sets frame bits) */
497 ok(psnd_pcm_hw_params_set_channels(data->pcmHandle, p, pDevice->Channels), "set channels") &&
498 /* set periods (implicitly constrains period/buffer parameters) */
499 ok(psnd_pcm_hw_params_set_periods_near(data->pcmHandle, p, &periods, NULL), "set periods near") &&
500 /* set rate (implicitly constrains period/buffer parameters) */
501 ok(psnd_pcm_hw_params_set_rate(data->pcmHandle, p, frequency, 0), "set rate") &&
502 /* set buffer size in frame units (implicitly sets period size/bytes/time and buffer time/bytes) */
503 ok(psnd_pcm_hw_params_set_buffer_size_min(data->pcmHandle, p, &bufferSizeInFrames), "set buffer size min") &&
504 /* install and prepare hardware configuration */
505 ok(psnd_pcm_hw_params(data->pcmHandle, p), "set params")))
507 AL_PRINT("%s failed: %s\n", err, psnd_strerror(i));
508 psnd_pcm_hw_params_free(p);
509 psnd_pcm_close(data->pcmHandle);
510 free(data);
511 return ALC_FALSE;
513 #undef ok
514 psnd_pcm_hw_params_free(p);
516 i = psnd_pcm_prepare(data->pcmHandle);
517 if(i < 0)
519 AL_PRINT("prepare error: %s\n", psnd_strerror(i));
520 psnd_pcm_close(data->pcmHandle);
521 free(data);
522 return ALC_FALSE;
525 pDevice->ExtraData = data;
526 return ALC_TRUE;
529 static void alsa_close_capture(ALCdevice *pDevice)
531 alsa_data *data = (alsa_data*)pDevice->ExtraData;
532 psnd_pcm_close(data->pcmHandle);
534 free(data);
535 pDevice->ExtraData = NULL;
538 static void alsa_start_capture(ALCdevice *pDevice)
540 alsa_data *data = (alsa_data*)pDevice->ExtraData;
541 psnd_pcm_start(data->pcmHandle);
544 static void alsa_stop_capture(ALCdevice *pDevice)
546 alsa_data *data = (alsa_data*)pDevice->ExtraData;
547 psnd_pcm_drain(data->pcmHandle);
550 static void alsa_capture_samples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
552 alsa_data *data = (alsa_data*)pDevice->ExtraData;
553 const snd_pcm_channel_area_t *areas = NULL;
554 snd_pcm_sframes_t frames, commitres;
555 snd_pcm_uframes_t size, offset;
556 int err;
558 frames = psnd_pcm_avail_update(data->pcmHandle);
559 if(frames < 0)
561 err = xrun_recovery(data->pcmHandle, frames);
562 if (err < 0)
563 AL_PRINT("available update failed: %s\n", psnd_strerror(err));
564 else
565 frames = psnd_pcm_avail_update(data->pcmHandle);
567 if (frames < (snd_pcm_sframes_t)lSamples)
569 SetALCError(ALC_INVALID_VALUE);
570 return;
573 // it is possible that contiguous areas are smaller, thus we use a loop
574 while (lSamples > 0)
576 char *Pointer;
577 int Count;
579 size = lSamples;
580 err = psnd_pcm_mmap_begin(data->pcmHandle, &areas, &offset, &size);
581 if (err < 0)
583 err = xrun_recovery(data->pcmHandle, err);
584 if (err < 0)
586 AL_PRINT("mmap begin error: %s\n", psnd_strerror(err));
587 break;
589 continue;
592 Pointer = (char*)areas->addr + (offset * areas->step / 8);
593 Count = size * pDevice->FrameSize;
595 memcpy(pBuffer, Pointer, Count);
596 pBuffer = (char*)pBuffer + Count;
598 commitres = psnd_pcm_mmap_commit(data->pcmHandle, offset, size);
599 if (commitres < 0 || (commitres-size) != 0)
601 AL_PRINT("mmap commit error: %s\n",
602 psnd_strerror(commitres >= 0 ? -EPIPE : commitres));
603 break;
606 lSamples -= size;
610 static ALCuint alsa_available_samples(ALCdevice *pDevice)
612 alsa_data *data = (alsa_data*)pDevice->ExtraData;
613 snd_pcm_sframes_t frames = psnd_pcm_avail_update(data->pcmHandle);
614 if(frames < 0)
616 int err = xrun_recovery(data->pcmHandle, frames);
617 if (err < 0)
618 AL_PRINT("available update failed: %s\n", psnd_strerror(err));
619 else
620 frames = psnd_pcm_avail_update(data->pcmHandle);
621 if(frames < 0) /* ew.. */
622 SetALCError(ALC_INVALID_DEVICE);
624 return max(frames, 0);
628 BackendFuncs alsa_funcs = {
629 alsa_open_playback,
630 alsa_close_playback,
631 alsa_open_capture,
632 alsa_close_capture,
633 alsa_start_capture,
634 alsa_stop_capture,
635 alsa_capture_samples,
636 alsa_available_samples
639 void alc_alsa_init(BackendFuncs *func_list)
641 snd_ctl_t *handle;
642 int card, err, dev, idx = 1;
643 snd_ctl_card_info_t *info;
644 snd_pcm_info_t *pcminfo;
645 snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK;
646 char name[128];
647 char *str;
649 *func_list = alsa_funcs;
651 #ifdef HAVE_DLFCN_H
652 alsa_handle = dlopen("libasound.so.2", RTLD_NOW);
653 if(!alsa_handle)
654 return;
655 dlerror();
657 #define LOAD_FUNC(f) do { \
658 p##f = (typeof(f)*)dlsym(alsa_handle, #f); \
659 if((str=dlerror()) != NULL) \
661 dlclose(alsa_handle); \
662 alsa_handle = NULL; \
663 AL_PRINT("Could not load %s from libasound.so.2: %s\n", #f, str); \
664 return; \
666 } while(0)
667 #else
668 str = NULL;
669 alsa_handle = NULL;
670 #define LOAD_FUNC(f) p##f = f
671 #endif
673 LOAD_FUNC(snd_strerror);
674 LOAD_FUNC(snd_pcm_open);
675 LOAD_FUNC(snd_pcm_close);
676 LOAD_FUNC(snd_pcm_nonblock);
677 LOAD_FUNC(snd_pcm_frames_to_bytes);
678 LOAD_FUNC(snd_pcm_hw_params_malloc);
679 LOAD_FUNC(snd_pcm_hw_params_free);
680 LOAD_FUNC(snd_pcm_hw_params_any);
681 LOAD_FUNC(snd_pcm_hw_params_set_access);
682 LOAD_FUNC(snd_pcm_hw_params_set_format);
683 LOAD_FUNC(snd_pcm_hw_params_set_channels);
684 LOAD_FUNC(snd_pcm_hw_params_set_periods_near);
685 LOAD_FUNC(snd_pcm_hw_params_set_rate_near);
686 LOAD_FUNC(snd_pcm_hw_params_set_rate);
687 LOAD_FUNC(snd_pcm_hw_params_set_buffer_size_near);
688 LOAD_FUNC(snd_pcm_hw_params_set_buffer_size_min);
689 LOAD_FUNC(snd_pcm_hw_params);
690 LOAD_FUNC(snd_pcm_prepare);
691 LOAD_FUNC(snd_pcm_start);
692 LOAD_FUNC(snd_pcm_resume);
693 LOAD_FUNC(snd_pcm_state);
694 LOAD_FUNC(snd_pcm_avail_update);
695 LOAD_FUNC(snd_pcm_areas_silence);
696 LOAD_FUNC(snd_pcm_mmap_begin);
697 LOAD_FUNC(snd_pcm_mmap_commit);
698 LOAD_FUNC(snd_pcm_drain);
700 LOAD_FUNC(snd_pcm_info_malloc);
701 LOAD_FUNC(snd_pcm_info_free);
702 LOAD_FUNC(snd_pcm_info_set_device);
703 LOAD_FUNC(snd_pcm_info_set_subdevice);
704 LOAD_FUNC(snd_pcm_info_set_stream);
705 LOAD_FUNC(snd_pcm_info_get_name);
706 LOAD_FUNC(snd_ctl_pcm_next_device);
707 LOAD_FUNC(snd_ctl_pcm_info);
708 LOAD_FUNC(snd_ctl_open);
709 LOAD_FUNC(snd_ctl_close);
710 LOAD_FUNC(snd_ctl_card_info_malloc);
711 LOAD_FUNC(snd_ctl_card_info_free);
712 LOAD_FUNC(snd_ctl_card_info);
713 LOAD_FUNC(snd_ctl_card_info_get_name);
714 LOAD_FUNC(snd_card_next);
716 #undef LOAD_FUNC
718 psnd_ctl_card_info_malloc(&info);
719 psnd_pcm_info_malloc(&pcminfo);
721 card = -1;
722 if(psnd_card_next(&card) < 0 || card < 0)
723 AL_PRINT("no playback cards found...\n");
724 else
726 alsaDeviceList[0] = AppendDeviceList("ALSA Software on default");
727 allDevNameMap[0].name = AppendAllDeviceList("ALSA Software on default");
730 while (card >= 0) {
731 sprintf(name, "hw:%d", card);
732 if ((err = psnd_ctl_open(&handle, name, 0)) < 0) {
733 AL_PRINT("control open (%i): %s\n", card, psnd_strerror(err));
734 goto next_card;
736 if ((err = psnd_ctl_card_info(handle, info)) < 0) {
737 AL_PRINT("control hardware info (%i): %s\n", card, psnd_strerror(err));
738 psnd_ctl_close(handle);
739 goto next_card;
741 if(card < MAX_DEVICES-1) {
742 snprintf(name, sizeof(name), "ALSA Software on %s",
743 psnd_ctl_card_info_get_name(info));
744 alsaDeviceList[card+1] = AppendDeviceList(name);
747 dev = -1;
748 while (idx < MAX_ALL_DEVICES) {
749 const char *cname, *dname;
751 if (psnd_ctl_pcm_next_device(handle, &dev)<0)
752 AL_PRINT("snd_ctl_pcm_next_device failed\n");
753 if (dev < 0)
754 break;
755 psnd_pcm_info_set_device(pcminfo, dev);
756 psnd_pcm_info_set_subdevice(pcminfo, 0);
757 psnd_pcm_info_set_stream(pcminfo, stream);
758 if ((err = psnd_ctl_pcm_info(handle, pcminfo)) < 0) {
759 if (err != -ENOENT)
760 AL_PRINT("control digital audio info (%i): %s\n", card, psnd_strerror(err));
761 continue;
764 cname = psnd_ctl_card_info_get_name(info);
765 dname = psnd_pcm_info_get_name(pcminfo);
766 snprintf(name, sizeof(name), "ALSA Software on %s [%s]",
767 cname, dname);
768 allDevNameMap[idx].name = AppendAllDeviceList(name);
769 allDevNameMap[idx].card = card;
770 allDevNameMap[idx].dev = dev;
771 idx++;
773 psnd_ctl_close(handle);
774 next_card:
775 if(psnd_card_next(&card) < 0) {
776 AL_PRINT("snd_card_next failed\n");
777 break;
782 stream = SND_PCM_STREAM_CAPTURE;
784 card = -1;
785 if(psnd_card_next(&card) < 0 || card < 0) {
786 AL_PRINT("no capture cards found...\n");
787 psnd_pcm_info_free(pcminfo);
788 psnd_ctl_card_info_free(info);
789 return;
792 alsaCaptureDeviceList[0] = AppendCaptureDeviceList("ALSA Capture on default");
794 while (card >= 0) {
795 sprintf(name, "hw:%d", card);
796 handle = NULL;
797 if ((err = psnd_ctl_open(&handle, name, 0)) < 0) {
798 AL_PRINT("control open (%i): %s\n", card, psnd_strerror(err));
800 if (err >= 0 && (err = psnd_ctl_card_info(handle, info)) < 0) {
801 AL_PRINT("control hardware info (%i): %s\n", card, psnd_strerror(err));
802 psnd_ctl_close(handle);
804 else if (err >= 0 && card < MAX_DEVICES-1)
806 snprintf(name, sizeof(name), "ALSA Capture on %s",
807 psnd_ctl_card_info_get_name(info));
808 alsaCaptureDeviceList[card+1] = AppendCaptureDeviceList(name);
810 if(handle) psnd_ctl_close(handle);
811 if(psnd_card_next(&card) < 0) {
812 AL_PRINT("snd_card_next failed\n");
813 break;
816 psnd_pcm_info_free(pcminfo);
817 psnd_ctl_card_info_free(info);