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
;
55 static void *alsa_handle
;
56 #define MAKE_FUNC(f) static typeof(f) * p##f
57 MAKE_FUNC(snd_strerror
);
58 MAKE_FUNC(snd_pcm_open
);
59 MAKE_FUNC(snd_pcm_close
);
60 MAKE_FUNC(snd_pcm_nonblock
);
61 MAKE_FUNC(snd_pcm_frames_to_bytes
);
62 MAKE_FUNC(snd_pcm_hw_params_malloc
);
63 MAKE_FUNC(snd_pcm_hw_params_free
);
64 MAKE_FUNC(snd_pcm_hw_params_any
);
65 MAKE_FUNC(snd_pcm_hw_params_set_access
);
66 MAKE_FUNC(snd_pcm_hw_params_set_format
);
67 MAKE_FUNC(snd_pcm_hw_params_set_channels
);
68 MAKE_FUNC(snd_pcm_hw_params_set_periods_near
);
69 MAKE_FUNC(snd_pcm_hw_params_set_rate_near
);
70 MAKE_FUNC(snd_pcm_hw_params_set_rate
);
71 MAKE_FUNC(snd_pcm_hw_params_set_buffer_size_near
);
72 MAKE_FUNC(snd_pcm_hw_params_set_buffer_size_min
);
73 MAKE_FUNC(snd_pcm_hw_params_get_buffer_size
);
74 MAKE_FUNC(snd_pcm_hw_params_get_period_size
);
75 MAKE_FUNC(snd_pcm_hw_params_get_access
);
76 MAKE_FUNC(snd_pcm_hw_params
);
77 MAKE_FUNC(snd_pcm_sw_params_malloc
);
78 MAKE_FUNC(snd_pcm_sw_params_current
);
79 MAKE_FUNC(snd_pcm_sw_params_set_avail_min
);
80 MAKE_FUNC(snd_pcm_sw_params
);
81 MAKE_FUNC(snd_pcm_sw_params_free
);
82 MAKE_FUNC(snd_pcm_prepare
);
83 MAKE_FUNC(snd_pcm_start
);
84 MAKE_FUNC(snd_pcm_resume
);
85 MAKE_FUNC(snd_pcm_wait
);
86 MAKE_FUNC(snd_pcm_state
);
87 MAKE_FUNC(snd_pcm_avail_update
);
88 MAKE_FUNC(snd_pcm_areas_silence
);
89 MAKE_FUNC(snd_pcm_mmap_begin
);
90 MAKE_FUNC(snd_pcm_mmap_commit
);
91 MAKE_FUNC(snd_pcm_readi
);
92 MAKE_FUNC(snd_pcm_writei
);
93 MAKE_FUNC(snd_pcm_drain
);
94 MAKE_FUNC(snd_pcm_info_malloc
);
95 MAKE_FUNC(snd_pcm_info_free
);
96 MAKE_FUNC(snd_pcm_info_set_device
);
97 MAKE_FUNC(snd_pcm_info_set_subdevice
);
98 MAKE_FUNC(snd_pcm_info_set_stream
);
99 MAKE_FUNC(snd_pcm_info_get_name
);
100 MAKE_FUNC(snd_ctl_pcm_next_device
);
101 MAKE_FUNC(snd_ctl_pcm_info
);
102 MAKE_FUNC(snd_ctl_open
);
103 MAKE_FUNC(snd_ctl_close
);
104 MAKE_FUNC(snd_ctl_card_info_malloc
);
105 MAKE_FUNC(snd_ctl_card_info_free
);
106 MAKE_FUNC(snd_ctl_card_info
);
107 MAKE_FUNC(snd_ctl_card_info_get_name
);
108 MAKE_FUNC(snd_card_next
);
111 #define MAX_ALL_DEVICES 32
113 static ALCchar
*alsaDevice
;
114 static DevMap allDevNameMap
[MAX_ALL_DEVICES
];
115 static DevMap allCaptureDevNameMap
[MAX_ALL_DEVICES
];
117 static int xrun_recovery(snd_pcm_t
*handle
, int err
)
121 err
= psnd_pcm_prepare(handle
);
123 AL_PRINT("prepare failed: %s\n", psnd_strerror(err
));
125 else if (err
== -ESTRPIPE
)
127 while ((err
= psnd_pcm_resume(handle
)) == -EAGAIN
)
128 Sleep(1); /* wait until the suspend flag is released */
131 err
= psnd_pcm_prepare(handle
);
133 AL_PRINT("prepare failed: %s\n", psnd_strerror(err
));
140 static ALuint
ALSAProc(ALvoid
*ptr
)
142 ALCdevice
*pDevice
= (ALCdevice
*)ptr
;
143 alsa_data
*data
= (alsa_data
*)pDevice
->ExtraData
;
144 const snd_pcm_channel_area_t
*areas
= NULL
;
145 snd_pcm_sframes_t avail
, commitres
;
146 snd_pcm_uframes_t offset
, frames
;
151 while(!data
->killNow
)
153 snd_pcm_state_t state
= psnd_pcm_state(data
->pcmHandle
);
154 if(state
== SND_PCM_STATE_XRUN
)
156 err
= xrun_recovery(data
->pcmHandle
, -EPIPE
);
159 AL_PRINT("XRUN recovery failed: %s\n", psnd_strerror(err
));
163 else if (state
== SND_PCM_STATE_SUSPENDED
)
165 err
= xrun_recovery(data
->pcmHandle
, -ESTRPIPE
);
168 AL_PRINT("SUSPEND recovery failed: %s\n", psnd_strerror(err
));
173 avail
= psnd_pcm_avail_update(data
->pcmHandle
);
176 err
= xrun_recovery(data
->pcmHandle
, avail
);
179 AL_PRINT("available update failed: %s\n", psnd_strerror(err
));
184 // make sure there's frames to process
185 if(avail
>= 0 && avail
< (snd_pcm_sframes_t
)pDevice
->UpdateSize
)
187 if(state
!= SND_PCM_STATE_RUNNING
)
189 err
= psnd_pcm_start(data
->pcmHandle
);
191 err
= xrun_recovery(data
->pcmHandle
, err
);
194 AL_PRINT("start failed: %s\n", psnd_strerror(err
));
198 else if(psnd_pcm_wait(data
->pcmHandle
, 1000) == 0)
199 AL_PRINT("Wait timeout... buffer size too low?\n");
202 avail
= pDevice
->UpdateSize
;
204 // it is possible that contiguous areas are smaller, thus we use a loop
209 SuspendContext(NULL
);
210 err
= psnd_pcm_mmap_begin(data
->pcmHandle
, &areas
, &offset
, &frames
);
213 err
= xrun_recovery(data
->pcmHandle
, err
);
215 AL_PRINT("mmap begin error: %s\n", psnd_strerror(err
));
216 ProcessContext(NULL
);
220 WritePtr
= (char*)areas
->addr
+ (offset
* areas
->step
/ 8);
221 WriteCnt
= psnd_pcm_frames_to_bytes(data
->pcmHandle
, frames
);
222 aluMixData(pDevice
->Context
, WritePtr
, WriteCnt
, pDevice
->Format
);
224 commitres
= psnd_pcm_mmap_commit(data
->pcmHandle
, offset
, frames
);
225 if (commitres
< 0 || (commitres
-frames
) != 0)
227 AL_PRINT("mmap commit error: %s\n",
228 psnd_strerror(commitres
>= 0 ? -EPIPE
: commitres
));
229 ProcessContext(NULL
);
232 ProcessContext(NULL
);
241 static ALuint
ALSANoMMapProc(ALvoid
*ptr
)
243 ALCdevice
*pDevice
= (ALCdevice
*)ptr
;
244 alsa_data
*data
= (alsa_data
*)pDevice
->ExtraData
;
245 snd_pcm_sframes_t avail
;
248 while(!data
->killNow
)
250 SuspendContext(NULL
);
251 aluMixData(pDevice
->Context
, data
->buffer
, data
->size
, pDevice
->Format
);
252 ProcessContext(NULL
);
254 WritePtr
= data
->buffer
;
255 avail
= (snd_pcm_uframes_t
)data
->size
/ psnd_pcm_frames_to_bytes(data
->pcmHandle
, 1);
258 int ret
= psnd_pcm_writei(data
->pcmHandle
, WritePtr
, avail
);
264 while((ret
=psnd_pcm_resume(data
->pcmHandle
)) == -EAGAIN
)
272 WritePtr
+= psnd_pcm_frames_to_bytes(data
->pcmHandle
, ret
);
279 ret
= psnd_pcm_prepare(data
->pcmHandle
);
289 static ALuint
ALSANoMMapCaptureProc(ALvoid
*ptr
)
291 ALCdevice
*pDevice
= (ALCdevice
*)ptr
;
292 alsa_data
*data
= (alsa_data
*)pDevice
->ExtraData
;
293 snd_pcm_sframes_t avail
;
295 while(!data
->killNow
)
297 avail
= (snd_pcm_uframes_t
)data
->size
/ psnd_pcm_frames_to_bytes(data
->pcmHandle
, 1);
298 avail
= psnd_pcm_readi(data
->pcmHandle
, data
->buffer
, avail
);
304 while((avail
=psnd_pcm_resume(data
->pcmHandle
)) == -EAGAIN
)
310 if (avail
>= 0 && data
->doCapture
)
311 WriteRingBuffer(data
->ring
, data
->buffer
, avail
);
316 avail
= psnd_pcm_prepare(data
->pcmHandle
);
318 AL_PRINT("prepare error: %s\n", psnd_strerror(avail
));
325 static ALCboolean
alsa_open_playback(ALCdevice
*device
, const ALCchar
*deviceName
)
327 snd_pcm_uframes_t bufferSizeInFrames
;
328 snd_pcm_sw_params_t
*sp
= NULL
;
329 snd_pcm_hw_params_t
*p
= NULL
;
330 snd_pcm_access_t access
;
331 unsigned int periods
;
343 strncpy(driver
, GetConfigValue("alsa", "device", "default"), sizeof(driver
)-1);
344 driver
[sizeof(driver
)-1] = 0;
349 for(idx
= 0;idx
< MAX_ALL_DEVICES
;idx
++)
351 if(allDevNameMap
[idx
].name
&&
352 strcmp(deviceName
, allDevNameMap
[idx
].name
) == 0)
354 device
->szDeviceName
= allDevNameMap
[idx
].name
;
356 sprintf(driver
, "hw:%d,%d", allDevNameMap
[idx
].card
, allDevNameMap
[idx
].dev
);
360 if(strcmp(deviceName
, alsaDevice
) == 0)
362 device
->szDeviceName
= alsaDevice
;
368 device
->szDeviceName
= alsaDevice
;
371 data
= (alsa_data
*)calloc(1, sizeof(alsa_data
));
373 i
= psnd_pcm_open(&data
->pcmHandle
, driver
, SND_PCM_STREAM_PLAYBACK
, SND_PCM_NONBLOCK
);
377 i
= psnd_pcm_open(&data
->pcmHandle
, driver
, SND_PCM_STREAM_PLAYBACK
, SND_PCM_NONBLOCK
);
381 i
= psnd_pcm_nonblock(data
->pcmHandle
, 0);
383 psnd_pcm_close(data
->pcmHandle
);
388 AL_PRINT("Could not open playback device '%s': %s\n", driver
, psnd_strerror(i
));
392 switch(aluBytesFromFormat(device
->Format
))
395 data
->format
= SND_PCM_FORMAT_U8
;
398 data
->format
= SND_PCM_FORMAT_S16
;
401 data
->format
= SND_PCM_FORMAT_UNKNOWN
;
402 AL_PRINT("Unknown format?! %x\n", device
->Format
);
405 periods
= GetConfigValueInt("alsa", "periods", 0);
406 bufferSizeInFrames
= device
->UpdateSize
;
407 rate
= device
->Frequency
;
409 str
= GetConfigValue("alsa", "mmap", "true");
410 allowmmap
= (strcasecmp(str
, "true") == 0 ||
411 strcasecmp(str
, "yes") == 0 ||
412 strcasecmp(str
, "on") == 0 ||
416 psnd_pcm_hw_params_malloc(&p
);
418 if((i
=psnd_pcm_hw_params_any(data
->pcmHandle
, p
)) < 0)
420 /* set interleaved access */
421 if(err
== NULL
&& (!allowmmap
|| (i
=psnd_pcm_hw_params_set_access(data
->pcmHandle
, p
, SND_PCM_ACCESS_MMAP_INTERLEAVED
)) < 0))
423 if((i
=psnd_pcm_hw_params_set_access(data
->pcmHandle
, p
, SND_PCM_ACCESS_RW_INTERLEAVED
)) < 0)
426 /* set format (implicitly sets sample bits) */
427 if(err
== NULL
&& (i
=psnd_pcm_hw_params_set_format(data
->pcmHandle
, p
, data
->format
)) < 0)
429 /* set channels (implicitly sets frame bits) */
430 if(err
== NULL
&& (i
=psnd_pcm_hw_params_set_channels(data
->pcmHandle
, p
, aluChannelsFromFormat(device
->Format
))) < 0)
431 err
= "set channels";
432 /* set periods (implicitly constrains period/buffer parameters) */
433 if(err
== NULL
&& periods
&& (i
=psnd_pcm_hw_params_set_periods_near(data
->pcmHandle
, p
, &periods
, NULL
)) < 0)
434 err
= "set periods near";
435 /* set rate (implicitly constrains period/buffer parameters) */
436 if(err
== NULL
&& (i
=psnd_pcm_hw_params_set_rate_near(data
->pcmHandle
, p
, &rate
, NULL
)) < 0)
437 err
= "set rate near";
438 /* set buffer size in frame units (implicitly sets period size/bytes/time and buffer time/bytes) */
439 if(err
== NULL
&& (i
=psnd_pcm_hw_params_set_buffer_size_near(data
->pcmHandle
, p
, &bufferSizeInFrames
)) < 0)
440 err
= "set buffer size near";
441 /* install and prepare hardware configuration */
442 if(err
== NULL
&& (i
=psnd_pcm_hw_params(data
->pcmHandle
, p
)) < 0)
444 if(err
== NULL
&& (i
=psnd_pcm_hw_params_get_access(p
, &access
)) < 0)
446 if(err
== NULL
&& (i
=psnd_pcm_hw_params_get_period_size(p
, &bufferSizeInFrames
, NULL
)) < 0)
447 err
= "get period size";
450 AL_PRINT("%s failed: %s\n", err
, psnd_strerror(i
));
451 psnd_pcm_hw_params_free(p
);
452 psnd_pcm_close(data
->pcmHandle
);
457 psnd_pcm_hw_params_free(p
);
460 psnd_pcm_sw_params_malloc(&sp
);
462 if((i
=psnd_pcm_sw_params_current(data
->pcmHandle
, sp
)) != 0)
464 if(err
== NULL
&& (i
=psnd_pcm_sw_params_set_avail_min(data
->pcmHandle
, sp
, bufferSizeInFrames
)) != 0)
465 err
= "sw set avail min";
466 if(err
== NULL
&& (i
=psnd_pcm_sw_params(data
->pcmHandle
, sp
)) != 0)
467 err
= "sw set params";
470 AL_PRINT("%s failed: %s\n", err
, psnd_strerror(i
));
471 psnd_pcm_sw_params_free(sp
);
472 psnd_pcm_close(data
->pcmHandle
);
477 psnd_pcm_sw_params_free(sp
);
479 data
->size
= psnd_pcm_frames_to_bytes(data
->pcmHandle
, bufferSizeInFrames
);
480 if(access
== SND_PCM_ACCESS_RW_INTERLEAVED
)
482 data
->buffer
= malloc(data
->size
);
485 AL_PRINT("buffer malloc failed\n");
486 psnd_pcm_close(data
->pcmHandle
);
493 i
= psnd_pcm_prepare(data
->pcmHandle
);
496 AL_PRINT("prepare error: %s\n", psnd_strerror(i
));
497 psnd_pcm_close(data
->pcmHandle
);
504 device
->ExtraData
= data
;
505 if(access
== SND_PCM_ACCESS_RW_INTERLEAVED
)
506 data
->thread
= StartThread(ALSANoMMapProc
, device
);
508 data
->thread
= StartThread(ALSAProc
, device
);
509 if(data
->thread
== NULL
)
511 AL_PRINT("Could not create playback thread\n");
512 psnd_pcm_close(data
->pcmHandle
);
513 device
->ExtraData
= NULL
;
519 device
->UpdateSize
= bufferSizeInFrames
;
520 device
->Frequency
= rate
;
525 static void alsa_close_playback(ALCdevice
*device
)
527 alsa_data
*data
= (alsa_data
*)device
->ExtraData
;
529 StopThread(data
->thread
);
530 psnd_pcm_close(data
->pcmHandle
);
534 device
->ExtraData
= NULL
;
538 static ALCboolean
alsa_open_capture(ALCdevice
*pDevice
, const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
540 snd_pcm_hw_params_t
*p
;
541 snd_pcm_uframes_t bufferSizeInFrames
;
542 snd_pcm_access_t access
;
553 strncpy(driver
, GetConfigValue("alsa", "capture", "default"), sizeof(driver
)-1);
554 driver
[sizeof(driver
)-1] = 0;
559 for(idx
= 0;idx
< MAX_ALL_DEVICES
;idx
++)
561 if(allCaptureDevNameMap
[idx
].name
&&
562 strcmp(deviceName
, allCaptureDevNameMap
[idx
].name
) == 0)
564 pDevice
->szDeviceName
= allCaptureDevNameMap
[idx
].name
;
566 sprintf(driver
, "plughw:%d,%d", allCaptureDevNameMap
[idx
].card
, allCaptureDevNameMap
[idx
].dev
);
573 pDevice
->szDeviceName
= allCaptureDevNameMap
[0].name
;
576 data
= (alsa_data
*)calloc(1, sizeof(alsa_data
));
578 i
= psnd_pcm_open(&data
->pcmHandle
, driver
, SND_PCM_STREAM_CAPTURE
, SND_PCM_NONBLOCK
);
582 i
= psnd_pcm_open(&data
->pcmHandle
, driver
, SND_PCM_STREAM_CAPTURE
, SND_PCM_NONBLOCK
);
586 i
= psnd_pcm_nonblock(data
->pcmHandle
, 0);
588 psnd_pcm_close(data
->pcmHandle
);
593 AL_PRINT("Could not open capture device '%s': %s\n", driver
, psnd_strerror(i
));
597 switch(aluBytesFromFormat(format
))
600 data
->format
= SND_PCM_FORMAT_U8
;
603 data
->format
= SND_PCM_FORMAT_S16
;
606 data
->format
= SND_PCM_FORMAT_UNKNOWN
;
607 AL_PRINT("Unknown format?! %x\n", format
);
610 str
= GetConfigValue("alsa", "mmap", "true");
611 allowmmap
= (strcasecmp(str
, "true") == 0 ||
612 strcasecmp(str
, "yes") == 0 ||
613 strcasecmp(str
, "on") == 0 ||
617 bufferSizeInFrames
= SampleSize
;
618 psnd_pcm_hw_params_malloc(&p
);
622 if((i
=psnd_pcm_hw_params_any(data
->pcmHandle
, p
)) < 0)
624 /* set interleaved access */
625 if(err
== NULL
&& (i
=psnd_pcm_hw_params_set_access(data
->pcmHandle
, p
, SND_PCM_ACCESS_MMAP_INTERLEAVED
)) < 0)
630 /* set format (implicitly sets sample bits) */
631 if(err
== NULL
&& (i
=psnd_pcm_hw_params_set_format(data
->pcmHandle
, p
, data
->format
)) < 0)
633 /* set channels (implicitly sets frame bits) */
634 if(err
== NULL
&& (i
=psnd_pcm_hw_params_set_channels(data
->pcmHandle
, p
, aluChannelsFromFormat(pDevice
->Format
))) < 0)
635 err
= "set channels";
636 /* set rate (implicitly constrains period/buffer parameters) */
637 if(err
== NULL
&& (i
=psnd_pcm_hw_params_set_rate(data
->pcmHandle
, p
, frequency
, 0)) < 0)
638 err
= "set rate near";
639 /* set buffer size in frame units (implicitly sets period size/bytes/time and buffer time/bytes) */
640 if(err
== NULL
&& (i
=psnd_pcm_hw_params_set_buffer_size_min(data
->pcmHandle
, p
, &bufferSizeInFrames
)) < 0)
642 err
= "set buffer size min";
645 /* install and prepare hardware configuration */
646 if(err
== NULL
&& (i
=psnd_pcm_hw_params(data
->pcmHandle
, p
)) < 0)
651 if((i
=psnd_pcm_hw_params_any(data
->pcmHandle
, p
)) < 0)
653 /* set interleaved access */
654 if(err
== NULL
&& (i
=psnd_pcm_hw_params_set_access(data
->pcmHandle
, p
, SND_PCM_ACCESS_RW_INTERLEAVED
)) < 0)
656 /* set format (implicitly sets sample bits) */
657 if(err
== NULL
&& (i
=psnd_pcm_hw_params_set_format(data
->pcmHandle
, p
, data
->format
)) < 0)
659 /* set channels (implicitly sets frame bits) */
660 if(err
== NULL
&& (i
=psnd_pcm_hw_params_set_channels(data
->pcmHandle
, p
, aluChannelsFromFormat(pDevice
->Format
))) < 0)
661 err
= "set channels";
662 /* set rate (implicitly constrains period/buffer parameters) */
663 if(err
== NULL
&& (i
=psnd_pcm_hw_params_set_rate(data
->pcmHandle
, p
, frequency
, 0)) < 0)
664 err
= "set rate near";
665 /* set buffer size in frame units (implicitly sets period size/bytes/time and buffer time/bytes) */
666 if(err
== NULL
&& (i
=psnd_pcm_hw_params_set_buffer_size_near(data
->pcmHandle
, p
, &bufferSizeInFrames
)) < 0)
667 err
= "set buffer size near";
668 /* install and prepare hardware configuration */
669 if(err
== NULL
&& (i
=psnd_pcm_hw_params(data
->pcmHandle
, p
)) < 0)
674 AL_PRINT("%s failed: %s\n", err
, psnd_strerror(i
));
675 psnd_pcm_hw_params_free(p
);
676 psnd_pcm_close(data
->pcmHandle
);
681 if((i
=psnd_pcm_hw_params_get_access(p
, &access
)) < 0)
683 AL_PRINT("get_access failed: %s\n", psnd_strerror(i
));
684 psnd_pcm_hw_params_free(p
);
685 psnd_pcm_close(data
->pcmHandle
);
689 if((i
=psnd_pcm_hw_params_get_period_size(p
, &bufferSizeInFrames
, NULL
)) < 0)
691 AL_PRINT("get size failed: %s\n", psnd_strerror(i
));
692 psnd_pcm_hw_params_free(p
);
693 psnd_pcm_close(data
->pcmHandle
);
698 psnd_pcm_hw_params_free(p
);
700 if(access
== SND_PCM_ACCESS_RW_INTERLEAVED
)
702 ALuint frameSize
= aluChannelsFromFormat(pDevice
->Format
);
703 frameSize
*= aluBytesFromFormat(pDevice
->Format
);
705 data
->ring
= CreateRingBuffer(frameSize
, SampleSize
);
708 AL_PRINT("ring buffer create failed\n");
709 psnd_pcm_close(data
->pcmHandle
);
714 data
->size
= psnd_pcm_frames_to_bytes(data
->pcmHandle
, bufferSizeInFrames
);
715 data
->buffer
= malloc(data
->size
);
718 AL_PRINT("buffer malloc failed\n");
719 psnd_pcm_close(data
->pcmHandle
);
720 DestroyRingBuffer(data
->ring
);
725 pDevice
->ExtraData
= data
;
726 data
->thread
= StartThread(ALSANoMMapCaptureProc
, pDevice
);
727 if(data
->thread
== NULL
)
729 AL_PRINT("Could not create capture thread\n");
730 pDevice
->ExtraData
= NULL
;
731 psnd_pcm_close(data
->pcmHandle
);
732 DestroyRingBuffer(data
->ring
);
740 i
= psnd_pcm_prepare(data
->pcmHandle
);
743 AL_PRINT("prepare error: %s\n", psnd_strerror(i
));
744 psnd_pcm_close(data
->pcmHandle
);
748 pDevice
->ExtraData
= data
;
754 static void alsa_close_capture(ALCdevice
*pDevice
)
756 alsa_data
*data
= (alsa_data
*)pDevice
->ExtraData
;
761 StopThread(data
->thread
);
762 DestroyRingBuffer(data
->ring
);
764 psnd_pcm_close(data
->pcmHandle
);
767 pDevice
->ExtraData
= NULL
;
770 static void alsa_start_capture(ALCdevice
*pDevice
)
772 alsa_data
*data
= (alsa_data
*)pDevice
->ExtraData
;
776 psnd_pcm_prepare(data
->pcmHandle
);
777 psnd_pcm_start(data
->pcmHandle
);
781 static void alsa_stop_capture(ALCdevice
*pDevice
)
783 alsa_data
*data
= (alsa_data
*)pDevice
->ExtraData
;
786 psnd_pcm_drain(data
->pcmHandle
);
789 static void alsa_capture_samples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCuint lSamples
)
791 alsa_data
*data
= (alsa_data
*)pDevice
->ExtraData
;
792 const snd_pcm_channel_area_t
*areas
= NULL
;
793 snd_pcm_sframes_t frames
, commitres
;
794 snd_pcm_uframes_t size
, offset
;
795 snd_pcm_state_t state
;
800 if(lSamples
<= (ALCuint
)RingBufferSize(data
->ring
))
801 ReadRingBuffer(data
->ring
, pBuffer
, lSamples
);
803 SetALCError(ALC_INVALID_VALUE
);
807 state
= psnd_pcm_state(data
->pcmHandle
);
808 if(state
== SND_PCM_STATE_XRUN
)
810 err
= xrun_recovery(data
->pcmHandle
, -EPIPE
);
813 AL_PRINT("XRUN recovery failed: %s\n", psnd_strerror(err
));
818 frames
= psnd_pcm_avail_update(data
->pcmHandle
);
821 err
= xrun_recovery(data
->pcmHandle
, frames
);
823 AL_PRINT("available update failed: %s\n", psnd_strerror(err
));
825 frames
= psnd_pcm_avail_update(data
->pcmHandle
);
827 if (frames
< (snd_pcm_sframes_t
)lSamples
)
829 SetALCError(ALC_INVALID_VALUE
);
833 // it is possible that contiguous areas are smaller, thus we use a loop
840 err
= psnd_pcm_mmap_begin(data
->pcmHandle
, &areas
, &offset
, &size
);
843 err
= xrun_recovery(data
->pcmHandle
, err
);
846 AL_PRINT("mmap begin error: %s\n", psnd_strerror(err
));
852 Pointer
= (char*)areas
->addr
+ (offset
* areas
->step
/ 8);
853 Count
= psnd_pcm_frames_to_bytes(data
->pcmHandle
, size
);
855 memcpy(pBuffer
, Pointer
, Count
);
856 pBuffer
= (char*)pBuffer
+ Count
;
858 commitres
= psnd_pcm_mmap_commit(data
->pcmHandle
, offset
, size
);
859 if (commitres
< 0 || (commitres
-size
) != 0)
861 AL_PRINT("mmap commit error: %s\n",
862 psnd_strerror(commitres
>= 0 ? -EPIPE
: commitres
));
870 static ALCuint
alsa_available_samples(ALCdevice
*pDevice
)
872 alsa_data
*data
= (alsa_data
*)pDevice
->ExtraData
;
873 snd_pcm_sframes_t frames
;
874 snd_pcm_state_t state
;
878 return RingBufferSize(data
->ring
);
880 state
= psnd_pcm_state(data
->pcmHandle
);
881 if(state
== SND_PCM_STATE_XRUN
)
883 err
= xrun_recovery(data
->pcmHandle
, -EPIPE
);
887 err
= psnd_pcm_start(data
->pcmHandle
);
891 AL_PRINT("XRUN recovery failed: %s\n", psnd_strerror(err
));
896 frames
= psnd_pcm_avail_update(data
->pcmHandle
);
899 err
= xrun_recovery(data
->pcmHandle
, frames
);
901 AL_PRINT("available update failed: %s\n", psnd_strerror(err
));
903 frames
= psnd_pcm_avail_update(data
->pcmHandle
);
905 return max(frames
, 0);
909 BackendFuncs alsa_funcs
= {
916 alsa_capture_samples
,
917 alsa_available_samples
920 void alc_alsa_init(BackendFuncs
*func_list
)
923 int card
, err
, dev
, idx
= 1;
924 snd_ctl_card_info_t
*info
;
925 snd_pcm_info_t
*pcminfo
;
926 snd_pcm_stream_t stream
= SND_PCM_STREAM_PLAYBACK
;
930 *func_list
= alsa_funcs
;
933 alsa_handle
= dlopen("libasound.so.2", RTLD_NOW
);
938 #define LOAD_FUNC(f) do { \
939 p##f = (typeof(f)*)dlsym(alsa_handle, #f); \
940 if((str=dlerror()) != NULL) \
942 dlclose(alsa_handle); \
943 alsa_handle = NULL; \
944 AL_PRINT("Could not load %s from libasound.so.2: %s\n", #f, str); \
950 alsa_handle
= (void*)0xDEADBEEF;
951 #define LOAD_FUNC(f) p##f = f
954 LOAD_FUNC(snd_strerror
);
955 LOAD_FUNC(snd_pcm_open
);
956 LOAD_FUNC(snd_pcm_close
);
957 LOAD_FUNC(snd_pcm_nonblock
);
958 LOAD_FUNC(snd_pcm_frames_to_bytes
);
959 LOAD_FUNC(snd_pcm_hw_params_malloc
);
960 LOAD_FUNC(snd_pcm_hw_params_free
);
961 LOAD_FUNC(snd_pcm_hw_params_any
);
962 LOAD_FUNC(snd_pcm_hw_params_set_access
);
963 LOAD_FUNC(snd_pcm_hw_params_set_format
);
964 LOAD_FUNC(snd_pcm_hw_params_set_channels
);
965 LOAD_FUNC(snd_pcm_hw_params_set_periods_near
);
966 LOAD_FUNC(snd_pcm_hw_params_set_rate_near
);
967 LOAD_FUNC(snd_pcm_hw_params_set_rate
);
968 LOAD_FUNC(snd_pcm_hw_params_set_buffer_size_near
);
969 LOAD_FUNC(snd_pcm_hw_params_set_buffer_size_min
);
970 LOAD_FUNC(snd_pcm_hw_params_get_buffer_size
);
971 LOAD_FUNC(snd_pcm_hw_params_get_period_size
);
972 LOAD_FUNC(snd_pcm_hw_params_get_access
);
973 LOAD_FUNC(snd_pcm_hw_params
);
974 LOAD_FUNC(snd_pcm_sw_params_malloc
);
975 LOAD_FUNC(snd_pcm_sw_params_current
);
976 LOAD_FUNC(snd_pcm_sw_params_set_avail_min
);
977 LOAD_FUNC(snd_pcm_sw_params
);
978 LOAD_FUNC(snd_pcm_sw_params_free
);
979 LOAD_FUNC(snd_pcm_prepare
);
980 LOAD_FUNC(snd_pcm_start
);
981 LOAD_FUNC(snd_pcm_resume
);
982 LOAD_FUNC(snd_pcm_wait
);
983 LOAD_FUNC(snd_pcm_state
);
984 LOAD_FUNC(snd_pcm_avail_update
);
985 LOAD_FUNC(snd_pcm_areas_silence
);
986 LOAD_FUNC(snd_pcm_mmap_begin
);
987 LOAD_FUNC(snd_pcm_mmap_commit
);
988 LOAD_FUNC(snd_pcm_readi
);
989 LOAD_FUNC(snd_pcm_writei
);
990 LOAD_FUNC(snd_pcm_drain
);
992 LOAD_FUNC(snd_pcm_info_malloc
);
993 LOAD_FUNC(snd_pcm_info_free
);
994 LOAD_FUNC(snd_pcm_info_set_device
);
995 LOAD_FUNC(snd_pcm_info_set_subdevice
);
996 LOAD_FUNC(snd_pcm_info_set_stream
);
997 LOAD_FUNC(snd_pcm_info_get_name
);
998 LOAD_FUNC(snd_ctl_pcm_next_device
);
999 LOAD_FUNC(snd_ctl_pcm_info
);
1000 LOAD_FUNC(snd_ctl_open
);
1001 LOAD_FUNC(snd_ctl_close
);
1002 LOAD_FUNC(snd_ctl_card_info_malloc
);
1003 LOAD_FUNC(snd_ctl_card_info_free
);
1004 LOAD_FUNC(snd_ctl_card_info
);
1005 LOAD_FUNC(snd_ctl_card_info_get_name
);
1006 LOAD_FUNC(snd_card_next
);
1010 psnd_ctl_card_info_malloc(&info
);
1011 psnd_pcm_info_malloc(&pcminfo
);
1014 if(psnd_card_next(&card
) < 0 || card
< 0)
1015 AL_PRINT("no playback cards found...\n");
1017 alsaDevice
= AppendDeviceList("ALSA Software");
1018 allDevNameMap
[0].name
= AppendAllDeviceList("ALSA Software on default");
1021 sprintf(name
, "hw:%d", card
);
1022 if ((err
= psnd_ctl_open(&handle
, name
, 0)) < 0) {
1023 AL_PRINT("control open (%i): %s\n", card
, psnd_strerror(err
));
1026 if ((err
= psnd_ctl_card_info(handle
, info
)) < 0) {
1027 AL_PRINT("control hardware info (%i): %s\n", card
, psnd_strerror(err
));
1028 psnd_ctl_close(handle
);
1033 while (idx
< MAX_ALL_DEVICES
) {
1034 const char *cname
, *dname
;
1036 if (psnd_ctl_pcm_next_device(handle
, &dev
)<0)
1037 AL_PRINT("snd_ctl_pcm_next_device failed\n");
1041 psnd_pcm_info_set_device(pcminfo
, dev
);
1042 psnd_pcm_info_set_subdevice(pcminfo
, 0);
1043 psnd_pcm_info_set_stream(pcminfo
, stream
);
1044 if ((err
= psnd_ctl_pcm_info(handle
, pcminfo
)) < 0) {
1046 AL_PRINT("control digital audio info (%i): %s\n", card
, psnd_strerror(err
));
1050 cname
= psnd_ctl_card_info_get_name(info
);
1051 dname
= psnd_pcm_info_get_name(pcminfo
);
1052 snprintf(name
, sizeof(name
), "ALSA Software on %s [%s] (hw:%d,%d)",
1053 cname
, dname
, card
, dev
);
1054 allDevNameMap
[idx
].name
= AppendAllDeviceList(name
);
1055 allDevNameMap
[idx
].card
= card
;
1056 allDevNameMap
[idx
].dev
= dev
;
1059 psnd_ctl_close(handle
);
1061 if(psnd_card_next(&card
) < 0) {
1062 AL_PRINT("snd_card_next failed\n");
1068 stream
= SND_PCM_STREAM_CAPTURE
;
1071 if(psnd_card_next(&card
) < 0 || card
< 0) {
1072 AL_PRINT("no capture cards found...\n");
1073 psnd_pcm_info_free(pcminfo
);
1074 psnd_ctl_card_info_free(info
);
1078 allCaptureDevNameMap
[0].name
= AppendCaptureDeviceList("ALSA Capture on default");
1082 sprintf(name
, "hw:%d", card
);
1084 if ((err
= psnd_ctl_open(&handle
, name
, 0)) < 0) {
1085 AL_PRINT("control open (%i): %s\n", card
, psnd_strerror(err
));
1087 if (err
>= 0 && (err
= psnd_ctl_card_info(handle
, info
)) < 0) {
1088 AL_PRINT("control hardware info (%i): %s\n", card
, psnd_strerror(err
));
1093 while (idx
< MAX_ALL_DEVICES
) {
1094 const char *cname
, *dname
;
1096 if (psnd_ctl_pcm_next_device(handle
, &dev
)<0)
1097 AL_PRINT("snd_ctl_pcm_next_device failed\n");
1100 psnd_pcm_info_set_device(pcminfo
, dev
);
1101 psnd_pcm_info_set_subdevice(pcminfo
, 0);
1102 psnd_pcm_info_set_stream(pcminfo
, stream
);
1103 if ((err
= psnd_ctl_pcm_info(handle
, pcminfo
)) < 0) {
1105 AL_PRINT("control digital audio info (%i): %s\n", card
, psnd_strerror(err
));
1109 cname
= psnd_ctl_card_info_get_name(info
);
1110 dname
= psnd_pcm_info_get_name(pcminfo
);
1111 snprintf(name
, sizeof(name
), "ALSA Capture on %s [%s] (hw:%d,%d)",
1112 cname
, dname
, card
, dev
);
1113 allCaptureDevNameMap
[idx
].name
= AppendCaptureDeviceList(name
);
1114 allCaptureDevNameMap
[idx
].card
= card
;
1115 allCaptureDevNameMap
[idx
].dev
= dev
;
1119 if(handle
) psnd_ctl_close(handle
);
1120 if(psnd_card_next(&card
) < 0) {
1121 AL_PRINT("snd_card_next failed\n");
1125 psnd_pcm_info_free(pcminfo
);
1126 psnd_ctl_card_info_free(info
);