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_prepare
);
78 MAKE_FUNC(snd_pcm_start
);
79 MAKE_FUNC(snd_pcm_resume
);
80 MAKE_FUNC(snd_pcm_wait
);
81 MAKE_FUNC(snd_pcm_state
);
82 MAKE_FUNC(snd_pcm_avail_update
);
83 MAKE_FUNC(snd_pcm_areas_silence
);
84 MAKE_FUNC(snd_pcm_mmap_begin
);
85 MAKE_FUNC(snd_pcm_mmap_commit
);
86 MAKE_FUNC(snd_pcm_readi
);
87 MAKE_FUNC(snd_pcm_writei
);
88 MAKE_FUNC(snd_pcm_drain
);
89 MAKE_FUNC(snd_pcm_info_malloc
);
90 MAKE_FUNC(snd_pcm_info_free
);
91 MAKE_FUNC(snd_pcm_info_set_device
);
92 MAKE_FUNC(snd_pcm_info_set_subdevice
);
93 MAKE_FUNC(snd_pcm_info_set_stream
);
94 MAKE_FUNC(snd_pcm_info_get_name
);
95 MAKE_FUNC(snd_ctl_pcm_next_device
);
96 MAKE_FUNC(snd_ctl_pcm_info
);
97 MAKE_FUNC(snd_ctl_open
);
98 MAKE_FUNC(snd_ctl_close
);
99 MAKE_FUNC(snd_ctl_card_info_malloc
);
100 MAKE_FUNC(snd_ctl_card_info_free
);
101 MAKE_FUNC(snd_ctl_card_info
);
102 MAKE_FUNC(snd_ctl_card_info_get_name
);
103 MAKE_FUNC(snd_card_next
);
106 #define MAX_ALL_DEVICES 32
108 static ALCchar
*alsaDevice
;
109 static DevMap allDevNameMap
[MAX_ALL_DEVICES
];
110 static DevMap allCaptureDevNameMap
[MAX_ALL_DEVICES
];
112 static int xrun_recovery(snd_pcm_t
*handle
, int err
)
116 err
= psnd_pcm_prepare(handle
);
118 AL_PRINT("prepare failed: %s\n", psnd_strerror(err
));
120 else if (err
== -ESTRPIPE
)
122 while ((err
= psnd_pcm_resume(handle
)) == -EAGAIN
)
123 Sleep(1); /* wait until the suspend flag is released */
126 err
= psnd_pcm_prepare(handle
);
128 AL_PRINT("prepare failed: %s\n", psnd_strerror(err
));
135 static ALuint
ALSAProc(ALvoid
*ptr
)
137 ALCdevice
*pDevice
= (ALCdevice
*)ptr
;
138 alsa_data
*data
= (alsa_data
*)pDevice
->ExtraData
;
139 const snd_pcm_channel_area_t
*areas
= NULL
;
140 snd_pcm_sframes_t avail
, commitres
;
141 snd_pcm_uframes_t offset
, frames
;
146 while(!data
->killNow
)
148 snd_pcm_state_t state
= psnd_pcm_state(data
->pcmHandle
);
149 if(state
== SND_PCM_STATE_XRUN
)
151 err
= xrun_recovery(data
->pcmHandle
, -EPIPE
);
154 AL_PRINT("XRUN recovery failed: %s\n", psnd_strerror(err
));
158 else if (state
== SND_PCM_STATE_SUSPENDED
)
160 err
= xrun_recovery(data
->pcmHandle
, -ESTRPIPE
);
163 AL_PRINT("SUSPEND recovery failed: %s\n", psnd_strerror(err
));
168 avail
= psnd_pcm_avail_update(data
->pcmHandle
);
171 err
= xrun_recovery(data
->pcmHandle
, avail
);
174 AL_PRINT("available update failed: %s\n", psnd_strerror(err
));
179 // make sure there's frames to process
182 if(state
!= SND_PCM_STATE_RUNNING
)
184 err
= psnd_pcm_start(data
->pcmHandle
);
186 err
= xrun_recovery(data
->pcmHandle
, err
);
189 AL_PRINT("start failed: %s\n", psnd_strerror(err
));
193 else if(psnd_pcm_wait(data
->pcmHandle
, 1000) == 0)
194 AL_PRINT("Wait timeout... buffer size too low?\n");
198 // it is possible that contiguous areas are smaller, thus we use a loop
203 err
= psnd_pcm_mmap_begin(data
->pcmHandle
, &areas
, &offset
, &frames
);
206 err
= xrun_recovery(data
->pcmHandle
, err
);
208 AL_PRINT("mmap begin error: %s\n", psnd_strerror(err
));
212 SuspendContext(NULL
);
213 WritePtr
= (char*)areas
->addr
+ (offset
* areas
->step
/ 8);
214 WriteCnt
= psnd_pcm_frames_to_bytes(data
->pcmHandle
, frames
);
215 aluMixData(pDevice
->Context
, WritePtr
, WriteCnt
, pDevice
->Format
);
216 ProcessContext(NULL
);
218 commitres
= psnd_pcm_mmap_commit(data
->pcmHandle
, offset
, frames
);
219 if (commitres
< 0 || (commitres
-frames
) != 0)
221 AL_PRINT("mmap commit error: %s\n",
222 psnd_strerror(commitres
>= 0 ? -EPIPE
: commitres
));
233 static ALuint
ALSANoMMapProc(ALvoid
*ptr
)
235 ALCdevice
*pDevice
= (ALCdevice
*)ptr
;
236 alsa_data
*data
= (alsa_data
*)pDevice
->ExtraData
;
237 snd_pcm_sframes_t avail
;
240 while(!data
->killNow
)
242 SuspendContext(NULL
);
243 aluMixData(pDevice
->Context
, data
->buffer
, data
->size
, pDevice
->Format
);
244 ProcessContext(NULL
);
246 WritePtr
= data
->buffer
;
247 avail
= (snd_pcm_uframes_t
)data
->size
/ psnd_pcm_frames_to_bytes(data
->pcmHandle
, 1);
250 int ret
= psnd_pcm_writei(data
->pcmHandle
, WritePtr
, avail
);
256 while((ret
=psnd_pcm_resume(data
->pcmHandle
)) == -EAGAIN
)
264 WritePtr
+= psnd_pcm_frames_to_bytes(data
->pcmHandle
, ret
);
271 ret
= psnd_pcm_prepare(data
->pcmHandle
);
281 static ALuint
ALSANoMMapCaptureProc(ALvoid
*ptr
)
283 ALCdevice
*pDevice
= (ALCdevice
*)ptr
;
284 alsa_data
*data
= (alsa_data
*)pDevice
->ExtraData
;
285 snd_pcm_sframes_t avail
;
287 while(!data
->killNow
)
289 avail
= (snd_pcm_uframes_t
)data
->size
/ psnd_pcm_frames_to_bytes(data
->pcmHandle
, 1);
290 avail
= psnd_pcm_readi(data
->pcmHandle
, data
->buffer
, avail
);
296 while((avail
=psnd_pcm_resume(data
->pcmHandle
)) == -EAGAIN
)
302 if (avail
>= 0 && data
->doCapture
)
303 WriteRingBuffer(data
->ring
, data
->buffer
, avail
);
308 avail
= psnd_pcm_prepare(data
->pcmHandle
);
310 AL_PRINT("prepare error: %s\n", psnd_strerror(avail
));
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
;
334 strncpy(driver
, GetConfigValue("alsa", "device", "default"), sizeof(driver
)-1);
335 driver
[sizeof(driver
)-1] = 0;
340 for(idx
= 0;idx
< MAX_ALL_DEVICES
;idx
++)
342 if(allDevNameMap
[idx
].name
&&
343 strcmp(deviceName
, allDevNameMap
[idx
].name
) == 0)
345 device
->szDeviceName
= allDevNameMap
[idx
].name
;
347 sprintf(driver
, "hw:%d,%d", allDevNameMap
[idx
].card
, allDevNameMap
[idx
].dev
);
351 if(strcmp(deviceName
, alsaDevice
) == 0)
353 device
->szDeviceName
= alsaDevice
;
359 device
->szDeviceName
= alsaDevice
;
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", 0);
397 bufferSizeInFrames
= device
->UpdateSize
;
398 rate
= device
->Frequency
;
400 str
= GetConfigValue("alsa", "mmap", "true");
401 allowmmap
= (strcasecmp(str
, "true") == 0 ||
402 strcasecmp(str
, "yes") == 0 ||
403 strcasecmp(str
, "on") == 0 ||
406 psnd_pcm_hw_params_malloc(&p
);
407 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
408 /* start with the largest configuration space possible */
409 if(!(ok(psnd_pcm_hw_params_any(data
->pcmHandle
, p
), "any") &&
410 /* set interleaved access */
411 ((allowmmap
&& ok(psnd_pcm_hw_params_set_access(data
->pcmHandle
, p
, SND_PCM_ACCESS_MMAP_INTERLEAVED
), "set access")) ||
412 ok(psnd_pcm_hw_params_set_access(data
->pcmHandle
, p
, SND_PCM_ACCESS_RW_INTERLEAVED
), "set access")) &&
413 /* set format (implicitly sets sample bits) */
414 ok(psnd_pcm_hw_params_set_format(data
->pcmHandle
, p
, data
->format
), "set format") &&
415 /* set channels (implicitly sets frame bits) */
416 ok(psnd_pcm_hw_params_set_channels(data
->pcmHandle
, p
, aluChannelsFromFormat(device
->Format
)), "set channels") &&
417 /* set periods (implicitly constrains period/buffer parameters) */
418 (!periods
|| ok(psnd_pcm_hw_params_set_periods_near(data
->pcmHandle
, p
, &periods
, NULL
), "set periods near")) &&
419 /* set rate (implicitly constrains period/buffer parameters) */
420 ok(psnd_pcm_hw_params_set_rate_near(data
->pcmHandle
, p
, &rate
, NULL
), "set rate near") &&
421 /* set buffer size in frame units (implicitly sets period size/bytes/time and buffer time/bytes) */
422 ok(psnd_pcm_hw_params_set_buffer_size_near(data
->pcmHandle
, p
, &bufferSizeInFrames
), "set buffer size near") &&
423 /* install and prepare hardware configuration */
424 ok(psnd_pcm_hw_params(data
->pcmHandle
, p
), "set params")))
426 AL_PRINT("%s failed: %s\n", err
, psnd_strerror(i
));
427 psnd_pcm_hw_params_free(p
);
428 psnd_pcm_close(data
->pcmHandle
);
434 if((i
=psnd_pcm_hw_params_get_access(p
, &access
)) < 0)
436 AL_PRINT("get_access failed: %s\n", psnd_strerror(i
));
437 psnd_pcm_hw_params_free(p
);
438 psnd_pcm_close(data
->pcmHandle
);
443 if((i
=psnd_pcm_hw_params_get_period_size(p
, &bufferSizeInFrames
, NULL
)) < 0)
445 AL_PRINT("get_period_size failed: %s\n", psnd_strerror(i
));
446 psnd_pcm_hw_params_free(p
);
447 psnd_pcm_close(data
->pcmHandle
);
452 psnd_pcm_hw_params_free(p
);
454 data
->size
= psnd_pcm_frames_to_bytes(data
->pcmHandle
, device
->UpdateSize
);
455 if(access
== SND_PCM_ACCESS_RW_INTERLEAVED
)
457 data
->buffer
= malloc(data
->size
);
460 AL_PRINT("buffer malloc failed\n");
461 psnd_pcm_close(data
->pcmHandle
);
468 i
= psnd_pcm_prepare(data
->pcmHandle
);
471 AL_PRINT("prepare error: %s\n", psnd_strerror(i
));
472 psnd_pcm_close(data
->pcmHandle
);
479 device
->ExtraData
= data
;
480 if(access
== SND_PCM_ACCESS_RW_INTERLEAVED
)
481 data
->thread
= StartThread(ALSANoMMapProc
, device
);
483 data
->thread
= StartThread(ALSAProc
, device
);
484 if(data
->thread
== NULL
)
486 AL_PRINT("Could not create playback thread\n");
487 psnd_pcm_close(data
->pcmHandle
);
488 device
->ExtraData
= NULL
;
494 device
->UpdateSize
= bufferSizeInFrames
;
495 device
->Frequency
= rate
;
500 static void alsa_close_playback(ALCdevice
*device
)
502 alsa_data
*data
= (alsa_data
*)device
->ExtraData
;
504 StopThread(data
->thread
);
505 psnd_pcm_close(data
->pcmHandle
);
509 device
->ExtraData
= NULL
;
513 static ALCboolean
alsa_open_capture(ALCdevice
*pDevice
, const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
515 snd_pcm_format_t alsaFormat
;
516 snd_pcm_hw_params_t
*p
;
517 snd_pcm_uframes_t bufferSizeInFrames
;
518 snd_pcm_access_t access
;
529 strncpy(driver
, GetConfigValue("alsa", "capture", "default"), sizeof(driver
)-1);
530 driver
[sizeof(driver
)-1] = 0;
535 for(idx
= 0;idx
< MAX_ALL_DEVICES
;idx
++)
537 if(allCaptureDevNameMap
[idx
].name
&&
538 strcmp(deviceName
, allCaptureDevNameMap
[idx
].name
) == 0)
540 pDevice
->szDeviceName
= allCaptureDevNameMap
[idx
].name
;
542 sprintf(driver
, "plughw:%d,%d", allCaptureDevNameMap
[idx
].card
, allCaptureDevNameMap
[idx
].dev
);
549 pDevice
->szDeviceName
= allCaptureDevNameMap
[0].name
;
552 data
= (alsa_data
*)calloc(1, sizeof(alsa_data
));
554 i
= psnd_pcm_open(&data
->pcmHandle
, driver
, SND_PCM_STREAM_CAPTURE
, SND_PCM_NONBLOCK
);
558 i
= psnd_pcm_open(&data
->pcmHandle
, driver
, SND_PCM_STREAM_CAPTURE
, SND_PCM_NONBLOCK
);
562 i
= psnd_pcm_nonblock(data
->pcmHandle
, 0);
564 psnd_pcm_close(data
->pcmHandle
);
569 AL_PRINT("Could not open capture device '%s': %s\n", driver
, psnd_strerror(i
));
573 switch(aluBytesFromFormat(format
))
576 alsaFormat
= SND_PCM_FORMAT_U8
;
579 alsaFormat
= SND_PCM_FORMAT_S16
;
582 alsaFormat
= SND_PCM_FORMAT_UNKNOWN
;
583 AL_PRINT("Unknown format?! %x\n", format
);
586 str
= GetConfigValue("alsa", "mmap", "true");
587 allowmmap
= (strcasecmp(str
, "true") == 0 ||
588 strcasecmp(str
, "yes") == 0 ||
589 strcasecmp(str
, "on") == 0 ||
592 bufferSizeInFrames
= SampleSize
;
593 psnd_pcm_hw_params_malloc(&p
);
594 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
595 /* start with the largest configuration space possible */
597 ok(psnd_pcm_hw_params_any(data
->pcmHandle
, p
), "any") &&
598 /* set interleaved access */
599 ok(psnd_pcm_hw_params_set_access(data
->pcmHandle
, p
, SND_PCM_ACCESS_MMAP_INTERLEAVED
), "set access") &&
600 /* set format (implicitly sets sample bits) */
601 ok(psnd_pcm_hw_params_set_format(data
->pcmHandle
, p
, alsaFormat
), "set format") &&
602 /* set channels (implicitly sets frame bits) */
603 ok(psnd_pcm_hw_params_set_channels(data
->pcmHandle
, p
, aluChannelsFromFormat(pDevice
->Format
)), "set channels") &&
604 /* set rate (implicitly constrains period/buffer parameters) */
605 ok(psnd_pcm_hw_params_set_rate(data
->pcmHandle
, p
, frequency
, 0), "set rate") &&
606 /* set buffer size in frame units (implicitly sets period size/bytes/time and buffer time/bytes) */
607 ok(psnd_pcm_hw_params_set_buffer_size_min(data
->pcmHandle
, p
, &bufferSizeInFrames
), "set buffer size min") &&
608 /* install and prepare hardware configuration */
609 ok(psnd_pcm_hw_params(data
->pcmHandle
, p
), "set params")))
612 AL_PRINT("%s failed: %s\n", err
, psnd_strerror(i
));
613 bufferSizeInFrames
= SampleSize
;
614 if(!(ok(psnd_pcm_hw_params_any(data
->pcmHandle
, p
), "any") &&
615 ok(psnd_pcm_hw_params_set_access(data
->pcmHandle
, p
, SND_PCM_ACCESS_RW_INTERLEAVED
), "set access") &&
616 ok(psnd_pcm_hw_params_set_format(data
->pcmHandle
, p
, alsaFormat
), "set format") &&
617 ok(psnd_pcm_hw_params_set_channels(data
->pcmHandle
, p
, aluChannelsFromFormat(pDevice
->Format
)), "set channels") &&
618 ok(psnd_pcm_hw_params_set_rate(data
->pcmHandle
, p
, frequency
, 0), "set rate") &&
619 ok(psnd_pcm_hw_params_set_buffer_size_near(data
->pcmHandle
, p
, &bufferSizeInFrames
), "set buffer size near") &&
620 ok(psnd_pcm_hw_params(data
->pcmHandle
, p
), "set params")))
622 AL_PRINT("%s failed: %s\n", err
, psnd_strerror(i
));
623 psnd_pcm_hw_params_free(p
);
624 psnd_pcm_close(data
->pcmHandle
);
631 if((i
=psnd_pcm_hw_params_get_access(p
, &access
)) < 0)
633 AL_PRINT("get_access failed: %s\n", psnd_strerror(i
));
634 psnd_pcm_hw_params_free(p
);
635 psnd_pcm_close(data
->pcmHandle
);
639 if((i
=psnd_pcm_hw_params_get_period_size(p
, &bufferSizeInFrames
, NULL
)) < 0)
641 AL_PRINT("get size failed: %s\n", psnd_strerror(i
));
642 psnd_pcm_hw_params_free(p
);
643 psnd_pcm_close(data
->pcmHandle
);
648 psnd_pcm_hw_params_free(p
);
650 if(access
== SND_PCM_ACCESS_RW_INTERLEAVED
)
652 ALuint frameSize
= aluChannelsFromFormat(pDevice
->Format
);
653 frameSize
*= aluBytesFromFormat(pDevice
->Format
);
655 data
->ring
= CreateRingBuffer(frameSize
, SampleSize
);
658 AL_PRINT("ring buffer create failed\n");
659 psnd_pcm_close(data
->pcmHandle
);
664 data
->size
= psnd_pcm_frames_to_bytes(data
->pcmHandle
, bufferSizeInFrames
);
665 data
->buffer
= malloc(data
->size
);
668 AL_PRINT("buffer malloc failed\n");
669 psnd_pcm_close(data
->pcmHandle
);
670 DestroyRingBuffer(data
->ring
);
675 pDevice
->ExtraData
= data
;
676 data
->thread
= StartThread(ALSANoMMapCaptureProc
, pDevice
);
677 if(data
->thread
== NULL
)
679 AL_PRINT("Could not create capture thread\n");
680 pDevice
->ExtraData
= NULL
;
681 psnd_pcm_close(data
->pcmHandle
);
682 DestroyRingBuffer(data
->ring
);
690 i
= psnd_pcm_prepare(data
->pcmHandle
);
693 AL_PRINT("prepare error: %s\n", psnd_strerror(i
));
694 psnd_pcm_close(data
->pcmHandle
);
698 pDevice
->ExtraData
= data
;
704 static void alsa_close_capture(ALCdevice
*pDevice
)
706 alsa_data
*data
= (alsa_data
*)pDevice
->ExtraData
;
711 StopThread(data
->thread
);
712 DestroyRingBuffer(data
->ring
);
714 psnd_pcm_close(data
->pcmHandle
);
717 pDevice
->ExtraData
= NULL
;
720 static void alsa_start_capture(ALCdevice
*pDevice
)
722 alsa_data
*data
= (alsa_data
*)pDevice
->ExtraData
;
726 psnd_pcm_prepare(data
->pcmHandle
);
727 psnd_pcm_start(data
->pcmHandle
);
731 static void alsa_stop_capture(ALCdevice
*pDevice
)
733 alsa_data
*data
= (alsa_data
*)pDevice
->ExtraData
;
736 psnd_pcm_drain(data
->pcmHandle
);
739 static void alsa_capture_samples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCuint lSamples
)
741 alsa_data
*data
= (alsa_data
*)pDevice
->ExtraData
;
742 const snd_pcm_channel_area_t
*areas
= NULL
;
743 snd_pcm_sframes_t frames
, commitres
;
744 snd_pcm_uframes_t size
, offset
;
745 snd_pcm_state_t state
;
750 if(lSamples
<= (ALCuint
)RingBufferSize(data
->ring
))
751 ReadRingBuffer(data
->ring
, pBuffer
, lSamples
);
753 SetALCError(ALC_INVALID_VALUE
);
757 state
= psnd_pcm_state(data
->pcmHandle
);
758 if(state
== SND_PCM_STATE_XRUN
)
760 err
= xrun_recovery(data
->pcmHandle
, -EPIPE
);
763 AL_PRINT("XRUN recovery failed: %s\n", psnd_strerror(err
));
768 frames
= psnd_pcm_avail_update(data
->pcmHandle
);
771 err
= xrun_recovery(data
->pcmHandle
, frames
);
773 AL_PRINT("available update failed: %s\n", psnd_strerror(err
));
775 frames
= psnd_pcm_avail_update(data
->pcmHandle
);
777 if (frames
< (snd_pcm_sframes_t
)lSamples
)
779 SetALCError(ALC_INVALID_VALUE
);
783 // it is possible that contiguous areas are smaller, thus we use a loop
790 err
= psnd_pcm_mmap_begin(data
->pcmHandle
, &areas
, &offset
, &size
);
793 err
= xrun_recovery(data
->pcmHandle
, err
);
796 AL_PRINT("mmap begin error: %s\n", psnd_strerror(err
));
802 Pointer
= (char*)areas
->addr
+ (offset
* areas
->step
/ 8);
803 Count
= psnd_pcm_frames_to_bytes(data
->pcmHandle
, size
);
805 memcpy(pBuffer
, Pointer
, Count
);
806 pBuffer
= (char*)pBuffer
+ Count
;
808 commitres
= psnd_pcm_mmap_commit(data
->pcmHandle
, offset
, size
);
809 if (commitres
< 0 || (commitres
-size
) != 0)
811 AL_PRINT("mmap commit error: %s\n",
812 psnd_strerror(commitres
>= 0 ? -EPIPE
: commitres
));
820 static ALCuint
alsa_available_samples(ALCdevice
*pDevice
)
822 alsa_data
*data
= (alsa_data
*)pDevice
->ExtraData
;
823 snd_pcm_sframes_t frames
;
824 snd_pcm_state_t state
;
828 return RingBufferSize(data
->ring
);
830 state
= psnd_pcm_state(data
->pcmHandle
);
831 if(state
== SND_PCM_STATE_XRUN
)
833 err
= xrun_recovery(data
->pcmHandle
, -EPIPE
);
837 err
= psnd_pcm_start(data
->pcmHandle
);
841 AL_PRINT("XRUN recovery failed: %s\n", psnd_strerror(err
));
846 frames
= psnd_pcm_avail_update(data
->pcmHandle
);
849 err
= xrun_recovery(data
->pcmHandle
, frames
);
851 AL_PRINT("available update failed: %s\n", psnd_strerror(err
));
853 frames
= psnd_pcm_avail_update(data
->pcmHandle
);
855 return max(frames
, 0);
859 BackendFuncs alsa_funcs
= {
866 alsa_capture_samples
,
867 alsa_available_samples
870 void alc_alsa_init(BackendFuncs
*func_list
)
873 int card
, err
, dev
, idx
= 1;
874 snd_ctl_card_info_t
*info
;
875 snd_pcm_info_t
*pcminfo
;
876 snd_pcm_stream_t stream
= SND_PCM_STREAM_PLAYBACK
;
880 *func_list
= alsa_funcs
;
883 alsa_handle
= dlopen("libasound.so.2", RTLD_NOW
);
888 #define LOAD_FUNC(f) do { \
889 p##f = (typeof(f)*)dlsym(alsa_handle, #f); \
890 if((str=dlerror()) != NULL) \
892 dlclose(alsa_handle); \
893 alsa_handle = NULL; \
894 AL_PRINT("Could not load %s from libasound.so.2: %s\n", #f, str); \
900 alsa_handle
= (void*)0xDEADBEEF;
901 #define LOAD_FUNC(f) p##f = f
904 LOAD_FUNC(snd_strerror
);
905 LOAD_FUNC(snd_pcm_open
);
906 LOAD_FUNC(snd_pcm_close
);
907 LOAD_FUNC(snd_pcm_nonblock
);
908 LOAD_FUNC(snd_pcm_frames_to_bytes
);
909 LOAD_FUNC(snd_pcm_hw_params_malloc
);
910 LOAD_FUNC(snd_pcm_hw_params_free
);
911 LOAD_FUNC(snd_pcm_hw_params_any
);
912 LOAD_FUNC(snd_pcm_hw_params_set_access
);
913 LOAD_FUNC(snd_pcm_hw_params_set_format
);
914 LOAD_FUNC(snd_pcm_hw_params_set_channels
);
915 LOAD_FUNC(snd_pcm_hw_params_set_periods_near
);
916 LOAD_FUNC(snd_pcm_hw_params_set_rate_near
);
917 LOAD_FUNC(snd_pcm_hw_params_set_rate
);
918 LOAD_FUNC(snd_pcm_hw_params_set_buffer_size_near
);
919 LOAD_FUNC(snd_pcm_hw_params_set_buffer_size_min
);
920 LOAD_FUNC(snd_pcm_hw_params_get_buffer_size
);
921 LOAD_FUNC(snd_pcm_hw_params_get_period_size
);
922 LOAD_FUNC(snd_pcm_hw_params_get_access
);
923 LOAD_FUNC(snd_pcm_hw_params
);
924 LOAD_FUNC(snd_pcm_prepare
);
925 LOAD_FUNC(snd_pcm_start
);
926 LOAD_FUNC(snd_pcm_resume
);
927 LOAD_FUNC(snd_pcm_wait
);
928 LOAD_FUNC(snd_pcm_state
);
929 LOAD_FUNC(snd_pcm_avail_update
);
930 LOAD_FUNC(snd_pcm_areas_silence
);
931 LOAD_FUNC(snd_pcm_mmap_begin
);
932 LOAD_FUNC(snd_pcm_mmap_commit
);
933 LOAD_FUNC(snd_pcm_readi
);
934 LOAD_FUNC(snd_pcm_writei
);
935 LOAD_FUNC(snd_pcm_drain
);
937 LOAD_FUNC(snd_pcm_info_malloc
);
938 LOAD_FUNC(snd_pcm_info_free
);
939 LOAD_FUNC(snd_pcm_info_set_device
);
940 LOAD_FUNC(snd_pcm_info_set_subdevice
);
941 LOAD_FUNC(snd_pcm_info_set_stream
);
942 LOAD_FUNC(snd_pcm_info_get_name
);
943 LOAD_FUNC(snd_ctl_pcm_next_device
);
944 LOAD_FUNC(snd_ctl_pcm_info
);
945 LOAD_FUNC(snd_ctl_open
);
946 LOAD_FUNC(snd_ctl_close
);
947 LOAD_FUNC(snd_ctl_card_info_malloc
);
948 LOAD_FUNC(snd_ctl_card_info_free
);
949 LOAD_FUNC(snd_ctl_card_info
);
950 LOAD_FUNC(snd_ctl_card_info_get_name
);
951 LOAD_FUNC(snd_card_next
);
955 psnd_ctl_card_info_malloc(&info
);
956 psnd_pcm_info_malloc(&pcminfo
);
959 if(psnd_card_next(&card
) < 0 || card
< 0)
960 AL_PRINT("no playback cards found...\n");
962 alsaDevice
= AppendDeviceList("ALSA Software");
963 allDevNameMap
[0].name
= AppendAllDeviceList("ALSA Software on default");
966 sprintf(name
, "hw:%d", card
);
967 if ((err
= psnd_ctl_open(&handle
, name
, 0)) < 0) {
968 AL_PRINT("control open (%i): %s\n", card
, psnd_strerror(err
));
971 if ((err
= psnd_ctl_card_info(handle
, info
)) < 0) {
972 AL_PRINT("control hardware info (%i): %s\n", card
, psnd_strerror(err
));
973 psnd_ctl_close(handle
);
978 while (idx
< MAX_ALL_DEVICES
) {
979 const char *cname
, *dname
;
981 if (psnd_ctl_pcm_next_device(handle
, &dev
)<0)
982 AL_PRINT("snd_ctl_pcm_next_device failed\n");
986 psnd_pcm_info_set_device(pcminfo
, dev
);
987 psnd_pcm_info_set_subdevice(pcminfo
, 0);
988 psnd_pcm_info_set_stream(pcminfo
, stream
);
989 if ((err
= psnd_ctl_pcm_info(handle
, pcminfo
)) < 0) {
991 AL_PRINT("control digital audio info (%i): %s\n", card
, psnd_strerror(err
));
995 cname
= psnd_ctl_card_info_get_name(info
);
996 dname
= psnd_pcm_info_get_name(pcminfo
);
997 snprintf(name
, sizeof(name
), "ALSA Software on %s [%s] (hw:%d,%d)",
998 cname
, dname
, card
, dev
);
999 allDevNameMap
[idx
].name
= AppendAllDeviceList(name
);
1000 allDevNameMap
[idx
].card
= card
;
1001 allDevNameMap
[idx
].dev
= dev
;
1004 psnd_ctl_close(handle
);
1006 if(psnd_card_next(&card
) < 0) {
1007 AL_PRINT("snd_card_next failed\n");
1013 stream
= SND_PCM_STREAM_CAPTURE
;
1016 if(psnd_card_next(&card
) < 0 || card
< 0) {
1017 AL_PRINT("no capture cards found...\n");
1018 psnd_pcm_info_free(pcminfo
);
1019 psnd_ctl_card_info_free(info
);
1023 allCaptureDevNameMap
[0].name
= AppendCaptureDeviceList("ALSA Capture on default");
1027 sprintf(name
, "hw:%d", card
);
1029 if ((err
= psnd_ctl_open(&handle
, name
, 0)) < 0) {
1030 AL_PRINT("control open (%i): %s\n", card
, psnd_strerror(err
));
1032 if (err
>= 0 && (err
= psnd_ctl_card_info(handle
, info
)) < 0) {
1033 AL_PRINT("control hardware info (%i): %s\n", card
, psnd_strerror(err
));
1038 while (idx
< MAX_ALL_DEVICES
) {
1039 const char *cname
, *dname
;
1041 if (psnd_ctl_pcm_next_device(handle
, &dev
)<0)
1042 AL_PRINT("snd_ctl_pcm_next_device failed\n");
1045 psnd_pcm_info_set_device(pcminfo
, dev
);
1046 psnd_pcm_info_set_subdevice(pcminfo
, 0);
1047 psnd_pcm_info_set_stream(pcminfo
, stream
);
1048 if ((err
= psnd_ctl_pcm_info(handle
, pcminfo
)) < 0) {
1050 AL_PRINT("control digital audio info (%i): %s\n", card
, psnd_strerror(err
));
1054 cname
= psnd_ctl_card_info_get_name(info
);
1055 dname
= psnd_pcm_info_get_name(pcminfo
);
1056 snprintf(name
, sizeof(name
), "ALSA Capture on %s [%s] (hw:%d,%d)",
1057 cname
, dname
, card
, dev
);
1058 allCaptureDevNameMap
[idx
].name
= AppendCaptureDeviceList(name
);
1059 allCaptureDevNameMap
[idx
].card
= card
;
1060 allCaptureDevNameMap
[idx
].dev
= dev
;
1064 if(handle
) psnd_ctl_close(handle
);
1065 if(psnd_card_next(&card
) < 0) {
1066 AL_PRINT("snd_card_next failed\n");
1070 psnd_pcm_info_free(pcminfo
);
1071 psnd_ctl_card_info_free(info
);