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_period_size
);
71 MAKE_FUNC(snd_pcm_hw_params_get_access
);
72 MAKE_FUNC(snd_pcm_hw_params
);
73 MAKE_FUNC(snd_pcm_prepare
);
74 MAKE_FUNC(snd_pcm_start
);
75 MAKE_FUNC(snd_pcm_resume
);
76 MAKE_FUNC(snd_pcm_state
);
77 MAKE_FUNC(snd_pcm_avail_update
);
78 MAKE_FUNC(snd_pcm_areas_silence
);
79 MAKE_FUNC(snd_pcm_mmap_begin
);
80 MAKE_FUNC(snd_pcm_mmap_commit
);
81 MAKE_FUNC(snd_pcm_writei
);
82 MAKE_FUNC(snd_pcm_drain
);
83 MAKE_FUNC(snd_pcm_info_malloc
);
84 MAKE_FUNC(snd_pcm_info_free
);
85 MAKE_FUNC(snd_pcm_info_set_device
);
86 MAKE_FUNC(snd_pcm_info_set_subdevice
);
87 MAKE_FUNC(snd_pcm_info_set_stream
);
88 MAKE_FUNC(snd_pcm_info_get_name
);
89 MAKE_FUNC(snd_ctl_pcm_next_device
);
90 MAKE_FUNC(snd_ctl_pcm_info
);
91 MAKE_FUNC(snd_ctl_open
);
92 MAKE_FUNC(snd_ctl_close
);
93 MAKE_FUNC(snd_ctl_card_info_malloc
);
94 MAKE_FUNC(snd_ctl_card_info_free
);
95 MAKE_FUNC(snd_ctl_card_info
);
96 MAKE_FUNC(snd_ctl_card_info_get_name
);
97 MAKE_FUNC(snd_card_next
);
100 #define MAX_DEVICES 16
101 #define MAX_ALL_DEVICES 32
103 static DevMap allDevNameMap
[MAX_ALL_DEVICES
];
104 static ALCchar
*alsaDeviceList
[MAX_DEVICES
];
105 static DevMap allCaptureDevNameMap
[MAX_ALL_DEVICES
];
107 static int xrun_recovery(snd_pcm_t
*handle
, int err
)
111 err
= psnd_pcm_prepare(handle
);
113 AL_PRINT("prepare failed: %s\n", psnd_strerror(err
));
115 else if (err
== -ESTRPIPE
)
117 while ((err
= psnd_pcm_resume(handle
)) == -EAGAIN
)
118 Sleep(1); /* wait until the suspend flag is released */
121 err
= psnd_pcm_prepare(handle
);
123 AL_PRINT("prepare failed: %s\n", psnd_strerror(err
));
130 static ALuint
ALSAProc(ALvoid
*ptr
)
132 ALCdevice
*pDevice
= (ALCdevice
*)ptr
;
133 alsa_data
*data
= (alsa_data
*)pDevice
->ExtraData
;
134 const snd_pcm_channel_area_t
*areas
= NULL
;
135 snd_pcm_sframes_t avail
, commitres
;
136 snd_pcm_uframes_t offset
, frames
;
141 while(!data
->killNow
)
143 snd_pcm_state_t state
= psnd_pcm_state(data
->pcmHandle
);
144 if(state
== SND_PCM_STATE_XRUN
)
146 err
= xrun_recovery(data
->pcmHandle
, -EPIPE
);
149 AL_PRINT("XRUN recovery failed: %s\n", psnd_strerror(err
));
153 else if (state
== SND_PCM_STATE_SUSPENDED
)
155 err
= xrun_recovery(data
->pcmHandle
, -ESTRPIPE
);
158 AL_PRINT("SUSPEND recovery failed: %s\n", psnd_strerror(err
));
163 avail
= psnd_pcm_avail_update(data
->pcmHandle
);
166 err
= xrun_recovery(data
->pcmHandle
, avail
);
169 AL_PRINT("available update failed: %s\n", psnd_strerror(err
));
174 // make sure there's frames to process
177 if(state
!= SND_PCM_STATE_RUNNING
)
179 err
= psnd_pcm_start(data
->pcmHandle
);
181 err
= xrun_recovery(data
->pcmHandle
, err
);
184 AL_PRINT("start failed: %s\n", psnd_strerror(err
));
192 // it is possible that contiguous areas are smaller, thus we use a loop
197 err
= psnd_pcm_mmap_begin(data
->pcmHandle
, &areas
, &offset
, &frames
);
200 err
= xrun_recovery(data
->pcmHandle
, err
);
202 AL_PRINT("mmap begin error: %s\n", psnd_strerror(err
));
206 SuspendContext(NULL
);
207 WritePtr
= (char*)areas
->addr
+ (offset
* areas
->step
/ 8);
208 WriteCnt
= psnd_pcm_frames_to_bytes(data
->pcmHandle
, frames
);
209 aluMixData(pDevice
->Context
, WritePtr
, WriteCnt
, pDevice
->Format
);
210 ProcessContext(NULL
);
212 commitres
= psnd_pcm_mmap_commit(data
->pcmHandle
, offset
, frames
);
213 if (commitres
< 0 || (commitres
-frames
) != 0)
215 AL_PRINT("mmap commit error: %s\n",
216 psnd_strerror(commitres
>= 0 ? -EPIPE
: commitres
));
227 static ALuint
ALSANoMMapProc(ALvoid
*ptr
)
229 ALCdevice
*pDevice
= (ALCdevice
*)ptr
;
230 alsa_data
*data
= (alsa_data
*)pDevice
->ExtraData
;
231 snd_pcm_sframes_t avail
;
234 while(!data
->killNow
)
236 SuspendContext(NULL
);
237 aluMixData(pDevice
->Context
, data
->buffer
, data
->size
, pDevice
->Format
);
238 ProcessContext(NULL
);
240 WritePtr
= data
->buffer
;
241 avail
= (snd_pcm_uframes_t
)data
->size
/ psnd_pcm_frames_to_bytes(data
->pcmHandle
, 1);
244 int ret
= psnd_pcm_writei(data
->pcmHandle
, WritePtr
, avail
);
250 while((ret
=psnd_pcm_resume(data
->pcmHandle
)) == -EAGAIN
)
258 WritePtr
+= psnd_pcm_frames_to_bytes(data
->pcmHandle
, ret
);
265 ret
= psnd_pcm_prepare(data
->pcmHandle
);
275 static ALCboolean
alsa_open_playback(ALCdevice
*device
, const ALCchar
*deviceName
)
277 snd_pcm_uframes_t bufferSizeInFrames
;
278 snd_pcm_hw_params_t
*p
= NULL
;
279 snd_pcm_access_t access
;
280 unsigned int periods
;
291 strncpy(driver
, GetConfigValue("alsa", "device", "default"), sizeof(driver
)-1);
292 driver
[sizeof(driver
)-1] = 0;
297 for(idx
= 0;idx
< MAX_ALL_DEVICES
;idx
++)
299 if(allDevNameMap
[idx
].name
&&
300 strcmp(deviceName
, allDevNameMap
[idx
].name
) == 0)
302 device
->szDeviceName
= allDevNameMap
[idx
].name
;
304 sprintf(driver
, "hw:%d,%d", allDevNameMap
[idx
].card
, allDevNameMap
[idx
].dev
);
308 for(idx
= 0;idx
< MAX_DEVICES
;idx
++)
310 if(alsaDeviceList
[idx
] &&
311 strcmp(deviceName
, alsaDeviceList
[idx
]) == 0)
313 device
->szDeviceName
= alsaDeviceList
[idx
];
315 sprintf(driver
, "hw:%zd,0", idx
-1);
322 device
->szDeviceName
= alsaDeviceList
[0];
325 data
= (alsa_data
*)calloc(1, sizeof(alsa_data
));
327 i
= psnd_pcm_open(&data
->pcmHandle
, driver
, SND_PCM_STREAM_PLAYBACK
, SND_PCM_NONBLOCK
);
331 i
= psnd_pcm_open(&data
->pcmHandle
, driver
, SND_PCM_STREAM_PLAYBACK
, SND_PCM_NONBLOCK
);
335 i
= psnd_pcm_nonblock(data
->pcmHandle
, 0);
337 psnd_pcm_close(data
->pcmHandle
);
342 AL_PRINT("Could not open playback device '%s': %s\n", driver
, psnd_strerror(i
));
346 switch(aluBytesFromFormat(device
->Format
))
349 data
->format
= SND_PCM_FORMAT_U8
;
352 data
->format
= SND_PCM_FORMAT_S16
;
355 data
->format
= SND_PCM_FORMAT_UNKNOWN
;
356 AL_PRINT("Unknown format?! %x\n", device
->Format
);
359 periods
= GetConfigValueInt("alsa", "periods", 0);
360 bufferSizeInFrames
= device
->UpdateFreq
;
362 str
= GetConfigValue("alsa", "mmap", "true");
363 allowmmap
= (strcasecmp(str
, "true") == 0 ||
364 strcasecmp(str
, "yes") == 0 ||
365 strcasecmp(str
, "on") == 0 ||
368 psnd_pcm_hw_params_malloc(&p
);
369 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
370 /* start with the largest configuration space possible */
371 if(!(ok(psnd_pcm_hw_params_any(data
->pcmHandle
, p
), "any") &&
372 /* set interleaved access */
373 ((allowmmap
&& ok(psnd_pcm_hw_params_set_access(data
->pcmHandle
, p
, SND_PCM_ACCESS_MMAP_INTERLEAVED
), "set access")) ||
374 ok(psnd_pcm_hw_params_set_access(data
->pcmHandle
, p
, SND_PCM_ACCESS_RW_INTERLEAVED
), "set access")) &&
375 /* set format (implicitly sets sample bits) */
376 ok(psnd_pcm_hw_params_set_format(data
->pcmHandle
, p
, data
->format
), "set format") &&
377 /* set channels (implicitly sets frame bits) */
378 ok(psnd_pcm_hw_params_set_channels(data
->pcmHandle
, p
, aluChannelsFromFormat(device
->Format
)), "set channels") &&
379 /* set periods (implicitly constrains period/buffer parameters) */
380 (!periods
|| ok(psnd_pcm_hw_params_set_periods_near(data
->pcmHandle
, p
, &periods
, NULL
), "set periods near")) &&
381 /* set rate (implicitly constrains period/buffer parameters) */
382 ok(psnd_pcm_hw_params_set_rate_near(data
->pcmHandle
, p
, &device
->Frequency
, NULL
), "set rate near") &&
383 /* set buffer size in frame units (implicitly sets period size/bytes/time and buffer time/bytes) */
384 ok(psnd_pcm_hw_params_set_buffer_size_near(data
->pcmHandle
, p
, &bufferSizeInFrames
), "set buffer size near") &&
385 /* install and prepare hardware configuration */
386 ok(psnd_pcm_hw_params(data
->pcmHandle
, p
), "set params")))
388 AL_PRINT("%s failed: %s\n", err
, psnd_strerror(i
));
389 psnd_pcm_hw_params_free(p
);
390 psnd_pcm_close(data
->pcmHandle
);
396 if((i
=psnd_pcm_hw_params_get_access(p
, &access
)) < 0)
398 AL_PRINT("get_access failed: %s\n", psnd_strerror(i
));
399 psnd_pcm_hw_params_free(p
);
400 psnd_pcm_close(data
->pcmHandle
);
405 if((i
=psnd_pcm_hw_params_get_period_size(p
, &bufferSizeInFrames
, NULL
)) < 0)
407 AL_PRINT("get_period_size failed: %s\n", psnd_strerror(i
));
408 psnd_pcm_hw_params_free(p
);
409 psnd_pcm_close(data
->pcmHandle
);
414 psnd_pcm_hw_params_free(p
);
416 device
->UpdateFreq
= bufferSizeInFrames
;
418 data
->size
= psnd_pcm_frames_to_bytes(data
->pcmHandle
, device
->UpdateFreq
);
419 if(access
== SND_PCM_ACCESS_RW_INTERLEAVED
)
421 data
->buffer
= malloc(data
->size
);
424 AL_PRINT("buffer malloc failed\n");
425 psnd_pcm_close(data
->pcmHandle
);
432 i
= psnd_pcm_prepare(data
->pcmHandle
);
435 AL_PRINT("prepare error: %s\n", psnd_strerror(i
));
436 psnd_pcm_close(data
->pcmHandle
);
443 device
->ExtraData
= data
;
444 if(access
== SND_PCM_ACCESS_RW_INTERLEAVED
)
445 data
->thread
= StartThread(ALSANoMMapProc
, device
);
447 data
->thread
= StartThread(ALSAProc
, device
);
448 if(data
->thread
== NULL
)
450 psnd_pcm_close(data
->pcmHandle
);
451 device
->ExtraData
= NULL
;
460 static void alsa_close_playback(ALCdevice
*device
)
462 alsa_data
*data
= (alsa_data
*)device
->ExtraData
;
464 StopThread(data
->thread
);
465 psnd_pcm_close(data
->pcmHandle
);
469 device
->ExtraData
= NULL
;
473 static ALCboolean
alsa_open_capture(ALCdevice
*pDevice
, const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
475 snd_pcm_format_t alsaFormat
;
476 snd_pcm_hw_params_t
*p
;
477 snd_pcm_uframes_t bufferSizeInFrames
;
486 strncpy(driver
, GetConfigValue("alsa", "capture", "default"), sizeof(driver
)-1);
487 driver
[sizeof(driver
)-1] = 0;
492 for(idx
= 0;idx
< MAX_ALL_DEVICES
;idx
++)
494 if(allCaptureDevNameMap
[idx
].name
&&
495 strcmp(deviceName
, allCaptureDevNameMap
[idx
].name
) == 0)
497 pDevice
->szDeviceName
= allCaptureDevNameMap
[idx
].name
;
499 sprintf(driver
, "hw:%d,%d", allCaptureDevNameMap
[idx
].card
, allCaptureDevNameMap
[idx
].dev
);
506 pDevice
->szDeviceName
= allCaptureDevNameMap
[0].name
;
509 data
= (alsa_data
*)calloc(1, sizeof(alsa_data
));
511 i
= psnd_pcm_open(&data
->pcmHandle
, driver
, SND_PCM_STREAM_CAPTURE
, SND_PCM_NONBLOCK
);
515 i
= psnd_pcm_open(&data
->pcmHandle
, driver
, SND_PCM_STREAM_CAPTURE
, SND_PCM_NONBLOCK
);
519 i
= psnd_pcm_nonblock(data
->pcmHandle
, 0);
521 psnd_pcm_close(data
->pcmHandle
);
526 AL_PRINT("Could not open capture device '%s': %s\n", driver
, psnd_strerror(i
));
530 switch(aluBytesFromFormat(format
))
533 alsaFormat
= SND_PCM_FORMAT_U8
;
536 alsaFormat
= SND_PCM_FORMAT_S16
;
539 alsaFormat
= SND_PCM_FORMAT_UNKNOWN
;
540 AL_PRINT("Unknown format?! %x\n", format
);
543 bufferSizeInFrames
= SampleSize
;
545 psnd_pcm_hw_params_malloc(&p
);
546 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
547 /* start with the largest configuration space possible */
548 if(!(ok(psnd_pcm_hw_params_any(data
->pcmHandle
, p
), "any") &&
549 /* set interleaved access */
550 ok(psnd_pcm_hw_params_set_access(data
->pcmHandle
, p
, SND_PCM_ACCESS_MMAP_INTERLEAVED
), "set access") &&
551 /* set format (implicitly sets sample bits) */
552 ok(psnd_pcm_hw_params_set_format(data
->pcmHandle
, p
, alsaFormat
), "set format") &&
553 /* set channels (implicitly sets frame bits) */
554 ok(psnd_pcm_hw_params_set_channels(data
->pcmHandle
, p
, aluChannelsFromFormat(pDevice
->Format
)), "set channels") &&
555 /* set rate (implicitly constrains period/buffer parameters) */
556 ok(psnd_pcm_hw_params_set_rate(data
->pcmHandle
, p
, frequency
, 0), "set rate") &&
557 /* set buffer size in frame units (implicitly sets period size/bytes/time and buffer time/bytes) */
558 ok(psnd_pcm_hw_params_set_buffer_size_min(data
->pcmHandle
, p
, &bufferSizeInFrames
), "set buffer size min") &&
559 /* install and prepare hardware configuration */
560 ok(psnd_pcm_hw_params(data
->pcmHandle
, p
), "set params")))
562 AL_PRINT("%s failed: %s\n", err
, psnd_strerror(i
));
563 psnd_pcm_hw_params_free(p
);
564 psnd_pcm_close(data
->pcmHandle
);
569 psnd_pcm_hw_params_free(p
);
571 i
= psnd_pcm_prepare(data
->pcmHandle
);
574 AL_PRINT("prepare error: %s\n", psnd_strerror(i
));
575 psnd_pcm_close(data
->pcmHandle
);
580 pDevice
->ExtraData
= data
;
584 static void alsa_close_capture(ALCdevice
*pDevice
)
586 alsa_data
*data
= (alsa_data
*)pDevice
->ExtraData
;
587 psnd_pcm_close(data
->pcmHandle
);
590 pDevice
->ExtraData
= NULL
;
593 static void alsa_start_capture(ALCdevice
*pDevice
)
595 alsa_data
*data
= (alsa_data
*)pDevice
->ExtraData
;
596 psnd_pcm_start(data
->pcmHandle
);
599 static void alsa_stop_capture(ALCdevice
*pDevice
)
601 alsa_data
*data
= (alsa_data
*)pDevice
->ExtraData
;
602 psnd_pcm_drain(data
->pcmHandle
);
605 static void alsa_capture_samples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCuint lSamples
)
607 alsa_data
*data
= (alsa_data
*)pDevice
->ExtraData
;
608 const snd_pcm_channel_area_t
*areas
= NULL
;
609 snd_pcm_sframes_t frames
, commitres
;
610 snd_pcm_uframes_t size
, offset
;
613 frames
= psnd_pcm_avail_update(data
->pcmHandle
);
616 err
= xrun_recovery(data
->pcmHandle
, frames
);
618 AL_PRINT("available update failed: %s\n", psnd_strerror(err
));
620 frames
= psnd_pcm_avail_update(data
->pcmHandle
);
622 if (frames
< (snd_pcm_sframes_t
)lSamples
)
624 SetALCError(ALC_INVALID_VALUE
);
628 // it is possible that contiguous areas are smaller, thus we use a loop
635 err
= psnd_pcm_mmap_begin(data
->pcmHandle
, &areas
, &offset
, &size
);
638 err
= xrun_recovery(data
->pcmHandle
, err
);
641 AL_PRINT("mmap begin error: %s\n", psnd_strerror(err
));
647 Pointer
= (char*)areas
->addr
+ (offset
* areas
->step
/ 8);
648 Count
= size
* pDevice
->FrameSize
;
650 memcpy(pBuffer
, Pointer
, Count
);
651 pBuffer
= (char*)pBuffer
+ Count
;
653 commitres
= psnd_pcm_mmap_commit(data
->pcmHandle
, offset
, size
);
654 if (commitres
< 0 || (commitres
-size
) != 0)
656 AL_PRINT("mmap commit error: %s\n",
657 psnd_strerror(commitres
>= 0 ? -EPIPE
: commitres
));
665 static ALCuint
alsa_available_samples(ALCdevice
*pDevice
)
667 alsa_data
*data
= (alsa_data
*)pDevice
->ExtraData
;
668 snd_pcm_sframes_t frames
= psnd_pcm_avail_update(data
->pcmHandle
);
671 int err
= xrun_recovery(data
->pcmHandle
, frames
);
673 AL_PRINT("available update failed: %s\n", psnd_strerror(err
));
675 frames
= psnd_pcm_avail_update(data
->pcmHandle
);
676 if(frames
< 0) /* ew.. */
677 SetALCError(ALC_INVALID_DEVICE
);
679 return max(frames
, 0);
683 BackendFuncs alsa_funcs
= {
690 alsa_capture_samples
,
691 alsa_available_samples
694 void alc_alsa_init(BackendFuncs
*func_list
)
697 int card
, err
, dev
, idx
= 1;
698 snd_ctl_card_info_t
*info
;
699 snd_pcm_info_t
*pcminfo
;
700 snd_pcm_stream_t stream
= SND_PCM_STREAM_PLAYBACK
;
704 *func_list
= alsa_funcs
;
707 alsa_handle
= dlopen("libasound.so.2", RTLD_NOW
);
712 #define LOAD_FUNC(f) do { \
713 p##f = (typeof(f)*)dlsym(alsa_handle, #f); \
714 if((str=dlerror()) != NULL) \
716 dlclose(alsa_handle); \
717 alsa_handle = NULL; \
718 AL_PRINT("Could not load %s from libasound.so.2: %s\n", #f, str); \
724 alsa_handle
= 0xDEADBEEF;
725 #define LOAD_FUNC(f) p##f = f
728 LOAD_FUNC(snd_strerror
);
729 LOAD_FUNC(snd_pcm_open
);
730 LOAD_FUNC(snd_pcm_close
);
731 LOAD_FUNC(snd_pcm_nonblock
);
732 LOAD_FUNC(snd_pcm_frames_to_bytes
);
733 LOAD_FUNC(snd_pcm_hw_params_malloc
);
734 LOAD_FUNC(snd_pcm_hw_params_free
);
735 LOAD_FUNC(snd_pcm_hw_params_any
);
736 LOAD_FUNC(snd_pcm_hw_params_set_access
);
737 LOAD_FUNC(snd_pcm_hw_params_set_format
);
738 LOAD_FUNC(snd_pcm_hw_params_set_channels
);
739 LOAD_FUNC(snd_pcm_hw_params_set_periods_near
);
740 LOAD_FUNC(snd_pcm_hw_params_set_rate_near
);
741 LOAD_FUNC(snd_pcm_hw_params_set_rate
);
742 LOAD_FUNC(snd_pcm_hw_params_set_buffer_size_near
);
743 LOAD_FUNC(snd_pcm_hw_params_set_buffer_size_min
);
744 LOAD_FUNC(snd_pcm_hw_params_get_period_size
);
745 LOAD_FUNC(snd_pcm_hw_params_get_access
);
746 LOAD_FUNC(snd_pcm_hw_params
);
747 LOAD_FUNC(snd_pcm_prepare
);
748 LOAD_FUNC(snd_pcm_start
);
749 LOAD_FUNC(snd_pcm_resume
);
750 LOAD_FUNC(snd_pcm_state
);
751 LOAD_FUNC(snd_pcm_avail_update
);
752 LOAD_FUNC(snd_pcm_areas_silence
);
753 LOAD_FUNC(snd_pcm_mmap_begin
);
754 LOAD_FUNC(snd_pcm_mmap_commit
);
755 LOAD_FUNC(snd_pcm_writei
);
756 LOAD_FUNC(snd_pcm_drain
);
758 LOAD_FUNC(snd_pcm_info_malloc
);
759 LOAD_FUNC(snd_pcm_info_free
);
760 LOAD_FUNC(snd_pcm_info_set_device
);
761 LOAD_FUNC(snd_pcm_info_set_subdevice
);
762 LOAD_FUNC(snd_pcm_info_set_stream
);
763 LOAD_FUNC(snd_pcm_info_get_name
);
764 LOAD_FUNC(snd_ctl_pcm_next_device
);
765 LOAD_FUNC(snd_ctl_pcm_info
);
766 LOAD_FUNC(snd_ctl_open
);
767 LOAD_FUNC(snd_ctl_close
);
768 LOAD_FUNC(snd_ctl_card_info_malloc
);
769 LOAD_FUNC(snd_ctl_card_info_free
);
770 LOAD_FUNC(snd_ctl_card_info
);
771 LOAD_FUNC(snd_ctl_card_info_get_name
);
772 LOAD_FUNC(snd_card_next
);
776 psnd_ctl_card_info_malloc(&info
);
777 psnd_pcm_info_malloc(&pcminfo
);
780 if(psnd_card_next(&card
) < 0 || card
< 0)
781 AL_PRINT("no playback cards found...\n");
784 alsaDeviceList
[0] = AppendDeviceList("ALSA Software on default");
785 allDevNameMap
[0].name
= AppendAllDeviceList("ALSA Software on default");
789 sprintf(name
, "hw:%d", card
);
790 if ((err
= psnd_ctl_open(&handle
, name
, 0)) < 0) {
791 AL_PRINT("control open (%i): %s\n", card
, psnd_strerror(err
));
794 if ((err
= psnd_ctl_card_info(handle
, info
)) < 0) {
795 AL_PRINT("control hardware info (%i): %s\n", card
, psnd_strerror(err
));
796 psnd_ctl_close(handle
);
799 if(card
< MAX_DEVICES
-1) {
800 snprintf(name
, sizeof(name
), "ALSA Software on %s",
801 psnd_ctl_card_info_get_name(info
));
802 alsaDeviceList
[card
+1] = AppendDeviceList(name
);
806 while (idx
< MAX_ALL_DEVICES
) {
807 const char *cname
, *dname
;
809 if (psnd_ctl_pcm_next_device(handle
, &dev
)<0)
810 AL_PRINT("snd_ctl_pcm_next_device failed\n");
813 psnd_pcm_info_set_device(pcminfo
, dev
);
814 psnd_pcm_info_set_subdevice(pcminfo
, 0);
815 psnd_pcm_info_set_stream(pcminfo
, stream
);
816 if ((err
= psnd_ctl_pcm_info(handle
, pcminfo
)) < 0) {
818 AL_PRINT("control digital audio info (%i): %s\n", card
, psnd_strerror(err
));
822 cname
= psnd_ctl_card_info_get_name(info
);
823 dname
= psnd_pcm_info_get_name(pcminfo
);
824 snprintf(name
, sizeof(name
), "ALSA Software on %s [%s]",
826 allDevNameMap
[idx
].name
= AppendAllDeviceList(name
);
827 allDevNameMap
[idx
].card
= card
;
828 allDevNameMap
[idx
].dev
= dev
;
831 psnd_ctl_close(handle
);
833 if(psnd_card_next(&card
) < 0) {
834 AL_PRINT("snd_card_next failed\n");
840 stream
= SND_PCM_STREAM_CAPTURE
;
843 if(psnd_card_next(&card
) < 0 || card
< 0) {
844 AL_PRINT("no capture cards found...\n");
845 psnd_pcm_info_free(pcminfo
);
846 psnd_ctl_card_info_free(info
);
850 allCaptureDevNameMap
[0].name
= AppendCaptureDeviceList("ALSA Capture on default");
853 sprintf(name
, "hw:%d", card
);
855 if ((err
= psnd_ctl_open(&handle
, name
, 0)) < 0) {
856 AL_PRINT("control open (%i): %s\n", card
, psnd_strerror(err
));
858 if (err
>= 0 && (err
= psnd_ctl_card_info(handle
, info
)) < 0) {
859 AL_PRINT("control hardware info (%i): %s\n", card
, psnd_strerror(err
));
864 while (idx
< MAX_ALL_DEVICES
) {
865 const char *cname
, *dname
;
867 if (psnd_ctl_pcm_next_device(handle
, &dev
)<0)
868 AL_PRINT("snd_ctl_pcm_next_device failed\n");
871 psnd_pcm_info_set_device(pcminfo
, dev
);
872 psnd_pcm_info_set_subdevice(pcminfo
, 0);
873 psnd_pcm_info_set_stream(pcminfo
, stream
);
874 if ((err
= psnd_ctl_pcm_info(handle
, pcminfo
)) < 0) {
876 AL_PRINT("control digital audio info (%i): %s\n", card
, psnd_strerror(err
));
880 cname
= psnd_ctl_card_info_get_name(info
);
881 dname
= psnd_pcm_info_get_name(pcminfo
);
882 snprintf(name
, sizeof(name
), "ALSA Capture on %s [%s]",
884 allDevNameMap
[idx
].name
= AppendCaptureDeviceList(name
);
885 allDevNameMap
[idx
].card
= card
;
886 allDevNameMap
[idx
].dev
= dev
;
890 if(handle
) psnd_ctl_close(handle
);
891 if(psnd_card_next(&card
) < 0) {
892 AL_PRINT("snd_card_next failed\n");
896 psnd_pcm_info_free(pcminfo
);
897 psnd_ctl_card_info_free(info
);