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
33 #include <alsa/asoundlib.h>
38 snd_pcm_format_t format
;
52 static void *alsa_handle
;
53 #define MAKE_FUNC(f) static typeof(f) * p##f
54 MAKE_FUNC(snd_strerror
);
55 MAKE_FUNC(snd_pcm_open
);
56 MAKE_FUNC(snd_pcm_close
);
57 MAKE_FUNC(snd_pcm_nonblock
);
58 MAKE_FUNC(snd_pcm_frames_to_bytes
);
59 MAKE_FUNC(snd_pcm_hw_params_malloc
);
60 MAKE_FUNC(snd_pcm_hw_params_free
);
61 MAKE_FUNC(snd_pcm_hw_params_any
);
62 MAKE_FUNC(snd_pcm_hw_params_set_access
);
63 MAKE_FUNC(snd_pcm_hw_params_set_format
);
64 MAKE_FUNC(snd_pcm_hw_params_set_channels
);
65 MAKE_FUNC(snd_pcm_hw_params_set_periods_near
);
66 MAKE_FUNC(snd_pcm_hw_params_set_rate_near
);
67 MAKE_FUNC(snd_pcm_hw_params_set_rate
);
68 MAKE_FUNC(snd_pcm_hw_params_set_buffer_size_near
);
69 MAKE_FUNC(snd_pcm_hw_params_set_buffer_size_min
);
70 MAKE_FUNC(snd_pcm_hw_params_get_access
);
71 MAKE_FUNC(snd_pcm_hw_params
);
72 MAKE_FUNC(snd_pcm_prepare
);
73 MAKE_FUNC(snd_pcm_start
);
74 MAKE_FUNC(snd_pcm_resume
);
75 MAKE_FUNC(snd_pcm_state
);
76 MAKE_FUNC(snd_pcm_avail_update
);
77 MAKE_FUNC(snd_pcm_areas_silence
);
78 MAKE_FUNC(snd_pcm_mmap_begin
);
79 MAKE_FUNC(snd_pcm_mmap_commit
);
80 MAKE_FUNC(snd_pcm_writei
);
81 MAKE_FUNC(snd_pcm_drain
);
82 MAKE_FUNC(snd_pcm_info_malloc
);
83 MAKE_FUNC(snd_pcm_info_free
);
84 MAKE_FUNC(snd_pcm_info_set_device
);
85 MAKE_FUNC(snd_pcm_info_set_subdevice
);
86 MAKE_FUNC(snd_pcm_info_set_stream
);
87 MAKE_FUNC(snd_pcm_info_get_name
);
88 MAKE_FUNC(snd_ctl_pcm_next_device
);
89 MAKE_FUNC(snd_ctl_pcm_info
);
90 MAKE_FUNC(snd_ctl_open
);
91 MAKE_FUNC(snd_ctl_close
);
92 MAKE_FUNC(snd_ctl_card_info_malloc
);
93 MAKE_FUNC(snd_ctl_card_info_free
);
94 MAKE_FUNC(snd_ctl_card_info
);
95 MAKE_FUNC(snd_ctl_card_info_get_name
);
96 MAKE_FUNC(snd_card_next
);
99 #define MAX_DEVICES 16
100 #define MAX_ALL_DEVICES 32
102 static DevMap allDevNameMap
[MAX_ALL_DEVICES
];
103 static ALCchar
*alsaDeviceList
[MAX_DEVICES
];
104 static ALCchar
*alsaCaptureDeviceList
[MAX_DEVICES
];
106 static int xrun_recovery(snd_pcm_t
*handle
, int err
)
110 err
= psnd_pcm_prepare(handle
);
112 err
= psnd_pcm_start(handle
);
114 AL_PRINT("prepare failed: %s\n", psnd_strerror(err
));
116 else if (err
== -ESTRPIPE
)
118 while ((err
= psnd_pcm_resume(handle
)) == -EAGAIN
)
119 usleep(1); /* wait until the suspend flag is released */
122 err
= psnd_pcm_prepare(handle
);
124 err
= psnd_pcm_start(handle
);
126 AL_PRINT("prepare failed: %s\n", psnd_strerror(err
));
133 static ALuint
ALSAProc(ALvoid
*ptr
)
135 ALCdevice
*pDevice
= (ALCdevice
*)ptr
;
136 alsa_data
*data
= (alsa_data
*)pDevice
->ExtraData
;
137 const snd_pcm_channel_area_t
*areas
= NULL
;
138 snd_pcm_sframes_t avail
, commitres
;
139 snd_pcm_uframes_t offset
, frames
;
144 while(!data
->killNow
)
146 snd_pcm_state_t state
= psnd_pcm_state(data
->pcmHandle
);
147 if(state
== SND_PCM_STATE_XRUN
)
149 err
= xrun_recovery(data
->pcmHandle
, -EPIPE
);
152 AL_PRINT("XRUN recovery failed: %s\n", psnd_strerror(err
));
156 else if (state
== SND_PCM_STATE_SUSPENDED
)
158 err
= xrun_recovery(data
->pcmHandle
, -ESTRPIPE
);
161 AL_PRINT("SUSPEND recovery failed: %s\n", psnd_strerror(err
));
166 avail
= psnd_pcm_avail_update(data
->pcmHandle
);
169 err
= xrun_recovery(data
->pcmHandle
, avail
);
172 AL_PRINT("available update failed: %s\n", psnd_strerror(err
));
177 // make sure there's frames to process
184 // it is possible that contiguous areas are smaller, thus we use a loop
189 err
= psnd_pcm_mmap_begin(data
->pcmHandle
, &areas
, &offset
, &frames
);
192 err
= xrun_recovery(data
->pcmHandle
, err
);
194 AL_PRINT("mmap begin error: %s\n", psnd_strerror(err
));
198 SuspendContext(NULL
);
199 WritePtr
= (char*)areas
->addr
+ (offset
* areas
->step
/ 8);
200 WriteCnt
= psnd_pcm_frames_to_bytes(data
->pcmHandle
, frames
);
201 aluMixData(pDevice
->Context
, WritePtr
, WriteCnt
, pDevice
->Format
);
202 ProcessContext(NULL
);
204 commitres
= psnd_pcm_mmap_commit(data
->pcmHandle
, offset
, frames
);
205 if (commitres
< 0 || (commitres
-frames
) != 0)
207 AL_PRINT("mmap commit error: %s\n",
208 psnd_strerror(commitres
>= 0 ? -EPIPE
: commitres
));
219 static ALuint
ALSANoMMapProc(ALvoid
*ptr
)
221 ALCdevice
*pDevice
= (ALCdevice
*)ptr
;
222 alsa_data
*data
= (alsa_data
*)pDevice
->ExtraData
;
223 snd_pcm_sframes_t avail
;
226 while(!data
->killNow
)
228 SuspendContext(NULL
);
229 aluMixData(pDevice
->Context
, data
->buffer
, data
->size
, pDevice
->Format
);
230 ProcessContext(NULL
);
232 WritePtr
= data
->buffer
;
233 avail
= (snd_pcm_uframes_t
)data
->size
/ psnd_pcm_frames_to_bytes(data
->pcmHandle
, 1);
236 int ret
= psnd_pcm_writei(data
->pcmHandle
, WritePtr
, avail
);
243 ret
= psnd_pcm_resume(data
->pcmHandle
);
244 } while(ret
== -EAGAIN
);
251 WritePtr
+= psnd_pcm_frames_to_bytes(data
->pcmHandle
, ret
);
258 ret
= psnd_pcm_prepare(data
->pcmHandle
);
268 static void fill_silence(snd_pcm_t
*pcmHandle
, snd_pcm_format_t alsaFormat
, int channels
)
270 const snd_pcm_channel_area_t
*areas
= NULL
;
271 snd_pcm_sframes_t avail
, commitres
;
272 snd_pcm_uframes_t offset
, frames
;
275 avail
= psnd_pcm_avail_update(pcmHandle
);
278 err
= xrun_recovery(pcmHandle
, avail
);
281 AL_PRINT("available update failed: %s\n", psnd_strerror(err
));
286 // it is possible that contiguous areas are smaller, thus we use a loop
291 err
= psnd_pcm_mmap_begin(pcmHandle
, &areas
, &offset
, &frames
);
294 err
= xrun_recovery(pcmHandle
, err
);
297 AL_PRINT("mmap begin error: %s\n", psnd_strerror(err
));
303 psnd_pcm_areas_silence(areas
, offset
, channels
, frames
, alsaFormat
);
305 commitres
= psnd_pcm_mmap_commit(pcmHandle
, offset
, frames
);
306 if (commitres
< 0 || (commitres
-frames
) != 0)
308 AL_PRINT("mmap commit error: %s\n",
309 psnd_strerror(commitres
>= 0 ? -EPIPE
: commitres
));
317 static ALCboolean
alsa_open_playback(ALCdevice
*device
, const ALCchar
*deviceName
)
319 snd_pcm_uframes_t bufferSizeInFrames
;
320 snd_pcm_hw_params_t
*p
= NULL
;
321 snd_pcm_access_t access
;
322 unsigned int periods
;
328 strncpy(driver
, GetConfigValue("alsa", "device", "default"), sizeof(driver
)-1);
329 driver
[sizeof(driver
)-1] = 0;
334 for(idx
= 0;idx
< MAX_ALL_DEVICES
;idx
++)
336 if(allDevNameMap
[idx
].name
&&
337 strcmp(deviceName
, allDevNameMap
[idx
].name
) == 0)
339 device
->szDeviceName
= allDevNameMap
[idx
].name
;
341 sprintf(driver
, "hw:%d,%d", allDevNameMap
[idx
].card
, allDevNameMap
[idx
].dev
);
345 for(idx
= 0;idx
< MAX_DEVICES
;idx
++)
347 if(alsaDeviceList
[idx
] &&
348 strcmp(deviceName
, alsaDeviceList
[idx
]) == 0)
350 device
->szDeviceName
= alsaDeviceList
[idx
];
352 sprintf(driver
, "hw:%zd,0", idx
-1);
359 device
->szDeviceName
= alsaDeviceList
[0];
362 data
= (alsa_data
*)calloc(1, sizeof(alsa_data
));
364 i
= psnd_pcm_open(&data
->pcmHandle
, driver
, SND_PCM_STREAM_PLAYBACK
, SND_PCM_NONBLOCK
);
368 i
= psnd_pcm_open(&data
->pcmHandle
, driver
, SND_PCM_STREAM_PLAYBACK
, SND_PCM_NONBLOCK
);
372 i
= psnd_pcm_nonblock(data
->pcmHandle
, 0);
374 psnd_pcm_close(data
->pcmHandle
);
379 AL_PRINT("Could not open playback device '%s': %s\n", driver
, psnd_strerror(i
));
383 switch(aluBytesFromFormat(device
->Format
))
386 data
->format
= SND_PCM_FORMAT_U8
;
389 data
->format
= SND_PCM_FORMAT_S16
;
392 data
->format
= SND_PCM_FORMAT_UNKNOWN
;
393 AL_PRINT("Unknown format?! %x\n", device
->Format
);
396 periods
= GetConfigValueInt("alsa", "periods", 4);
397 if((int)periods
<= 0)
399 bufferSizeInFrames
= device
->UpdateFreq
;
401 psnd_pcm_hw_params_malloc(&p
);
402 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
403 /* start with the largest configuration space possible */
404 if(!(ok(psnd_pcm_hw_params_any(data
->pcmHandle
, p
), "any") &&
405 /* set interleaved access */
406 (ok(psnd_pcm_hw_params_set_access(data
->pcmHandle
, p
, SND_PCM_ACCESS_MMAP_INTERLEAVED
), "set access") ||
407 ok(psnd_pcm_hw_params_set_access(data
->pcmHandle
, p
, SND_PCM_ACCESS_RW_INTERLEAVED
), "set access")) &&
408 /* set format (implicitly sets sample bits) */
409 ok(psnd_pcm_hw_params_set_format(data
->pcmHandle
, p
, data
->format
), "set format") &&
410 /* set channels (implicitly sets frame bits) */
411 ok(psnd_pcm_hw_params_set_channels(data
->pcmHandle
, p
, device
->Channels
), "set channels") &&
412 /* set periods (implicitly constrains period/buffer parameters) */
413 ok(psnd_pcm_hw_params_set_periods_near(data
->pcmHandle
, p
, &periods
, NULL
), "set periods near") &&
414 /* set rate (implicitly constrains period/buffer parameters) */
415 ok(psnd_pcm_hw_params_set_rate_near(data
->pcmHandle
, p
, &device
->Frequency
, NULL
), "set rate near") &&
416 /* set buffer size in frame units (implicitly sets period size/bytes/time and buffer time/bytes) */
417 ok(psnd_pcm_hw_params_set_buffer_size_near(data
->pcmHandle
, p
, &bufferSizeInFrames
), "set buffer size near") &&
418 /* install and prepare hardware configuration */
419 ok(psnd_pcm_hw_params(data
->pcmHandle
, p
), "set params")))
421 AL_PRINT("%s failed: %s\n", err
, psnd_strerror(i
));
422 psnd_pcm_hw_params_free(p
);
423 psnd_pcm_close(data
->pcmHandle
);
429 if((i
=psnd_pcm_hw_params_get_access(p
, &access
)) < 0)
431 AL_PRINT("get_access failed: %s\n", psnd_strerror(i
));
432 psnd_pcm_hw_params_free(p
);
433 psnd_pcm_close(data
->pcmHandle
);
438 psnd_pcm_hw_params_free(p
);
440 device
->MaxNoOfSources
= 256;
441 device
->UpdateFreq
= bufferSizeInFrames
;
443 data
->size
= psnd_pcm_frames_to_bytes(data
->pcmHandle
, bufferSizeInFrames
);
444 if(access
== SND_PCM_ACCESS_RW_INTERLEAVED
)
446 data
->buffer
= malloc(data
->size
);
449 AL_PRINT("buffer malloc failed\n");
450 psnd_pcm_close(data
->pcmHandle
);
457 i
= psnd_pcm_prepare(data
->pcmHandle
);
460 AL_PRINT("prepare error: %s\n", psnd_strerror(i
));
461 psnd_pcm_close(data
->pcmHandle
);
467 fill_silence(data
->pcmHandle
, data
->format
, device
->Channels
);
469 i
= psnd_pcm_start(data
->pcmHandle
);
472 AL_PRINT("start error: %s\n", psnd_strerror(i
));
473 psnd_pcm_close(data
->pcmHandle
);
480 device
->ExtraData
= data
;
481 if(access
== SND_PCM_ACCESS_RW_INTERLEAVED
)
482 data
->thread
= StartThread(ALSANoMMapProc
, device
);
484 data
->thread
= StartThread(ALSAProc
, device
);
485 if(data
->thread
== NULL
)
487 psnd_pcm_close(data
->pcmHandle
);
488 device
->ExtraData
= NULL
;
497 static void alsa_close_playback(ALCdevice
*device
)
499 alsa_data
*data
= (alsa_data
*)device
->ExtraData
;
501 StopThread(data
->thread
);
502 psnd_pcm_close(data
->pcmHandle
);
506 device
->ExtraData
= NULL
;
510 static ALCboolean
alsa_open_capture(ALCdevice
*pDevice
, const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
512 snd_pcm_format_t alsaFormat
;
513 snd_pcm_hw_params_t
*p
;
514 unsigned int periods
= 4;
515 snd_pcm_uframes_t bufferSizeInFrames
= SampleSize
;
521 strncpy(driver
, GetConfigValue("alsa", "capture", "default"), sizeof(driver
)-1);
522 driver
[sizeof(driver
)-1] = 0;
527 for(idx
= 0;idx
< MAX_DEVICES
;idx
++)
529 if(alsaCaptureDeviceList
[idx
] &&
530 strcmp(deviceName
, alsaCaptureDeviceList
[idx
]) == 0)
532 pDevice
->szDeviceName
= alsaCaptureDeviceList
[idx
];
534 sprintf(driver
, "hw:%zd,0", idx
-1);
541 pDevice
->szDeviceName
= alsaCaptureDeviceList
[0];
544 data
= (alsa_data
*)calloc(1, sizeof(alsa_data
));
546 i
= psnd_pcm_open(&data
->pcmHandle
, driver
, SND_PCM_STREAM_CAPTURE
, SND_PCM_NONBLOCK
);
550 i
= psnd_pcm_open(&data
->pcmHandle
, driver
, SND_PCM_STREAM_CAPTURE
, SND_PCM_NONBLOCK
);
554 i
= psnd_pcm_nonblock(data
->pcmHandle
, 0);
556 psnd_pcm_close(data
->pcmHandle
);
561 AL_PRINT("Could not open capture device '%s': %s\n", driver
, psnd_strerror(i
));
565 switch(aluBytesFromFormat(format
))
568 alsaFormat
= SND_PCM_FORMAT_U8
;
571 alsaFormat
= SND_PCM_FORMAT_S16
;
574 alsaFormat
= SND_PCM_FORMAT_UNKNOWN
;
575 AL_PRINT("Unknown format?! %x\n", format
);
578 psnd_pcm_hw_params_malloc(&p
);
579 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
580 /* start with the largest configuration space possible */
581 if(!(ok(psnd_pcm_hw_params_any(data
->pcmHandle
, p
), "any") &&
582 /* set interleaved access */
583 ok(psnd_pcm_hw_params_set_access(data
->pcmHandle
, p
, SND_PCM_ACCESS_MMAP_INTERLEAVED
), "set access") &&
584 /* set format (implicitly sets sample bits) */
585 ok(psnd_pcm_hw_params_set_format(data
->pcmHandle
, p
, alsaFormat
), "set format") &&
586 /* set channels (implicitly sets frame bits) */
587 ok(psnd_pcm_hw_params_set_channels(data
->pcmHandle
, p
, pDevice
->Channels
), "set channels") &&
588 /* set periods (implicitly constrains period/buffer parameters) */
589 ok(psnd_pcm_hw_params_set_periods_near(data
->pcmHandle
, p
, &periods
, NULL
), "set periods near") &&
590 /* set rate (implicitly constrains period/buffer parameters) */
591 ok(psnd_pcm_hw_params_set_rate(data
->pcmHandle
, p
, frequency
, 0), "set rate") &&
592 /* set buffer size in frame units (implicitly sets period size/bytes/time and buffer time/bytes) */
593 ok(psnd_pcm_hw_params_set_buffer_size_min(data
->pcmHandle
, p
, &bufferSizeInFrames
), "set buffer size min") &&
594 /* install and prepare hardware configuration */
595 ok(psnd_pcm_hw_params(data
->pcmHandle
, p
), "set params")))
597 AL_PRINT("%s failed: %s\n", err
, psnd_strerror(i
));
598 psnd_pcm_hw_params_free(p
);
599 psnd_pcm_close(data
->pcmHandle
);
604 psnd_pcm_hw_params_free(p
);
606 i
= psnd_pcm_prepare(data
->pcmHandle
);
609 AL_PRINT("prepare error: %s\n", psnd_strerror(i
));
610 psnd_pcm_close(data
->pcmHandle
);
615 pDevice
->ExtraData
= data
;
619 static void alsa_close_capture(ALCdevice
*pDevice
)
621 alsa_data
*data
= (alsa_data
*)pDevice
->ExtraData
;
622 psnd_pcm_close(data
->pcmHandle
);
625 pDevice
->ExtraData
= NULL
;
628 static void alsa_start_capture(ALCdevice
*pDevice
)
630 alsa_data
*data
= (alsa_data
*)pDevice
->ExtraData
;
631 psnd_pcm_start(data
->pcmHandle
);
634 static void alsa_stop_capture(ALCdevice
*pDevice
)
636 alsa_data
*data
= (alsa_data
*)pDevice
->ExtraData
;
637 psnd_pcm_drain(data
->pcmHandle
);
640 static void alsa_capture_samples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCuint lSamples
)
642 alsa_data
*data
= (alsa_data
*)pDevice
->ExtraData
;
643 const snd_pcm_channel_area_t
*areas
= NULL
;
644 snd_pcm_sframes_t frames
, commitres
;
645 snd_pcm_uframes_t size
, offset
;
648 frames
= psnd_pcm_avail_update(data
->pcmHandle
);
651 err
= xrun_recovery(data
->pcmHandle
, frames
);
653 AL_PRINT("available update failed: %s\n", psnd_strerror(err
));
655 frames
= psnd_pcm_avail_update(data
->pcmHandle
);
657 if (frames
< (snd_pcm_sframes_t
)lSamples
)
659 SetALCError(ALC_INVALID_VALUE
);
663 // it is possible that contiguous areas are smaller, thus we use a loop
670 err
= psnd_pcm_mmap_begin(data
->pcmHandle
, &areas
, &offset
, &size
);
673 err
= xrun_recovery(data
->pcmHandle
, err
);
676 AL_PRINT("mmap begin error: %s\n", psnd_strerror(err
));
682 Pointer
= (char*)areas
->addr
+ (offset
* areas
->step
/ 8);
683 Count
= size
* pDevice
->FrameSize
;
685 memcpy(pBuffer
, Pointer
, Count
);
686 pBuffer
= (char*)pBuffer
+ Count
;
688 commitres
= psnd_pcm_mmap_commit(data
->pcmHandle
, offset
, size
);
689 if (commitres
< 0 || (commitres
-size
) != 0)
691 AL_PRINT("mmap commit error: %s\n",
692 psnd_strerror(commitres
>= 0 ? -EPIPE
: commitres
));
700 static ALCuint
alsa_available_samples(ALCdevice
*pDevice
)
702 alsa_data
*data
= (alsa_data
*)pDevice
->ExtraData
;
703 snd_pcm_sframes_t frames
= psnd_pcm_avail_update(data
->pcmHandle
);
706 int err
= xrun_recovery(data
->pcmHandle
, frames
);
708 AL_PRINT("available update failed: %s\n", psnd_strerror(err
));
710 frames
= psnd_pcm_avail_update(data
->pcmHandle
);
711 if(frames
< 0) /* ew.. */
712 SetALCError(ALC_INVALID_DEVICE
);
714 return max(frames
, 0);
718 BackendFuncs alsa_funcs
= {
725 alsa_capture_samples
,
726 alsa_available_samples
729 void alc_alsa_init(BackendFuncs
*func_list
)
732 int card
, err
, dev
, idx
= 1;
733 snd_ctl_card_info_t
*info
;
734 snd_pcm_info_t
*pcminfo
;
735 snd_pcm_stream_t stream
= SND_PCM_STREAM_PLAYBACK
;
739 *func_list
= alsa_funcs
;
742 alsa_handle
= dlopen("libasound.so.2", RTLD_NOW
);
747 #define LOAD_FUNC(f) do { \
748 p##f = (typeof(f)*)dlsym(alsa_handle, #f); \
749 if((str=dlerror()) != NULL) \
751 dlclose(alsa_handle); \
752 alsa_handle = NULL; \
753 AL_PRINT("Could not load %s from libasound.so.2: %s\n", #f, str); \
760 #define LOAD_FUNC(f) p##f = f
763 LOAD_FUNC(snd_strerror
);
764 LOAD_FUNC(snd_pcm_open
);
765 LOAD_FUNC(snd_pcm_close
);
766 LOAD_FUNC(snd_pcm_nonblock
);
767 LOAD_FUNC(snd_pcm_frames_to_bytes
);
768 LOAD_FUNC(snd_pcm_hw_params_malloc
);
769 LOAD_FUNC(snd_pcm_hw_params_free
);
770 LOAD_FUNC(snd_pcm_hw_params_any
);
771 LOAD_FUNC(snd_pcm_hw_params_set_access
);
772 LOAD_FUNC(snd_pcm_hw_params_set_format
);
773 LOAD_FUNC(snd_pcm_hw_params_set_channels
);
774 LOAD_FUNC(snd_pcm_hw_params_set_periods_near
);
775 LOAD_FUNC(snd_pcm_hw_params_set_rate_near
);
776 LOAD_FUNC(snd_pcm_hw_params_set_rate
);
777 LOAD_FUNC(snd_pcm_hw_params_set_buffer_size_near
);
778 LOAD_FUNC(snd_pcm_hw_params_set_buffer_size_min
);
779 LOAD_FUNC(snd_pcm_hw_params_get_access
);
780 LOAD_FUNC(snd_pcm_hw_params
);
781 LOAD_FUNC(snd_pcm_prepare
);
782 LOAD_FUNC(snd_pcm_start
);
783 LOAD_FUNC(snd_pcm_resume
);
784 LOAD_FUNC(snd_pcm_state
);
785 LOAD_FUNC(snd_pcm_avail_update
);
786 LOAD_FUNC(snd_pcm_areas_silence
);
787 LOAD_FUNC(snd_pcm_mmap_begin
);
788 LOAD_FUNC(snd_pcm_mmap_commit
);
789 LOAD_FUNC(snd_pcm_writei
);
790 LOAD_FUNC(snd_pcm_drain
);
792 LOAD_FUNC(snd_pcm_info_malloc
);
793 LOAD_FUNC(snd_pcm_info_free
);
794 LOAD_FUNC(snd_pcm_info_set_device
);
795 LOAD_FUNC(snd_pcm_info_set_subdevice
);
796 LOAD_FUNC(snd_pcm_info_set_stream
);
797 LOAD_FUNC(snd_pcm_info_get_name
);
798 LOAD_FUNC(snd_ctl_pcm_next_device
);
799 LOAD_FUNC(snd_ctl_pcm_info
);
800 LOAD_FUNC(snd_ctl_open
);
801 LOAD_FUNC(snd_ctl_close
);
802 LOAD_FUNC(snd_ctl_card_info_malloc
);
803 LOAD_FUNC(snd_ctl_card_info_free
);
804 LOAD_FUNC(snd_ctl_card_info
);
805 LOAD_FUNC(snd_ctl_card_info_get_name
);
806 LOAD_FUNC(snd_card_next
);
810 psnd_ctl_card_info_malloc(&info
);
811 psnd_pcm_info_malloc(&pcminfo
);
814 if(psnd_card_next(&card
) < 0 || card
< 0)
815 AL_PRINT("no playback cards found...\n");
818 alsaDeviceList
[0] = AppendDeviceList("ALSA Software on default");
819 allDevNameMap
[0].name
= AppendAllDeviceList("ALSA Software on default");
823 sprintf(name
, "hw:%d", card
);
824 if ((err
= psnd_ctl_open(&handle
, name
, 0)) < 0) {
825 AL_PRINT("control open (%i): %s\n", card
, psnd_strerror(err
));
828 if ((err
= psnd_ctl_card_info(handle
, info
)) < 0) {
829 AL_PRINT("control hardware info (%i): %s\n", card
, psnd_strerror(err
));
830 psnd_ctl_close(handle
);
833 if(card
< MAX_DEVICES
-1) {
834 snprintf(name
, sizeof(name
), "ALSA Software on %s",
835 psnd_ctl_card_info_get_name(info
));
836 alsaDeviceList
[card
+1] = AppendDeviceList(name
);
840 while (idx
< MAX_ALL_DEVICES
) {
841 const char *cname
, *dname
;
843 if (psnd_ctl_pcm_next_device(handle
, &dev
)<0)
844 AL_PRINT("snd_ctl_pcm_next_device failed\n");
847 psnd_pcm_info_set_device(pcminfo
, dev
);
848 psnd_pcm_info_set_subdevice(pcminfo
, 0);
849 psnd_pcm_info_set_stream(pcminfo
, stream
);
850 if ((err
= psnd_ctl_pcm_info(handle
, pcminfo
)) < 0) {
852 AL_PRINT("control digital audio info (%i): %s\n", card
, psnd_strerror(err
));
856 cname
= psnd_ctl_card_info_get_name(info
);
857 dname
= psnd_pcm_info_get_name(pcminfo
);
858 snprintf(name
, sizeof(name
), "ALSA Software on %s [%s]",
860 allDevNameMap
[idx
].name
= AppendAllDeviceList(name
);
861 allDevNameMap
[idx
].card
= card
;
862 allDevNameMap
[idx
].dev
= dev
;
865 psnd_ctl_close(handle
);
867 if(psnd_card_next(&card
) < 0) {
868 AL_PRINT("snd_card_next failed\n");
874 stream
= SND_PCM_STREAM_CAPTURE
;
877 if(psnd_card_next(&card
) < 0 || card
< 0) {
878 AL_PRINT("no capture cards found...\n");
879 psnd_pcm_info_free(pcminfo
);
880 psnd_ctl_card_info_free(info
);
884 alsaCaptureDeviceList
[0] = AppendCaptureDeviceList("ALSA Capture on default");
887 sprintf(name
, "hw:%d", card
);
889 if ((err
= psnd_ctl_open(&handle
, name
, 0)) < 0) {
890 AL_PRINT("control open (%i): %s\n", card
, psnd_strerror(err
));
892 if (err
>= 0 && (err
= psnd_ctl_card_info(handle
, info
)) < 0) {
893 AL_PRINT("control hardware info (%i): %s\n", card
, psnd_strerror(err
));
894 psnd_ctl_close(handle
);
896 else if (err
>= 0 && card
< MAX_DEVICES
-1)
898 snprintf(name
, sizeof(name
), "ALSA Capture on %s",
899 psnd_ctl_card_info_get_name(info
));
900 alsaCaptureDeviceList
[card
+1] = AppendCaptureDeviceList(name
);
902 if(handle
) psnd_ctl_close(handle
);
903 if(psnd_card_next(&card
) < 0) {
904 AL_PRINT("snd_card_next failed\n");
908 psnd_pcm_info_free(pcminfo
);
909 psnd_ctl_card_info_free(info
);