Update queued buffer states when source looping is toggled
[openal-soft.git] / Alc / alsa.c
blobf194b130d396b0e15b88662d49b05cc802b8a799
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include "config.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <memory.h>
26 #ifdef HAVE_DLFCN_H
27 #include <dlfcn.h>
28 #endif
29 #include "alMain.h"
30 #include "AL/al.h"
31 #include "AL/alc.h"
33 #include <alsa/asoundlib.h>
36 typedef struct {
37 snd_pcm_t *pcmHandle;
38 snd_pcm_format_t format;
40 ALvoid *buffer;
41 ALsizei size;
43 RingBuffer *ring;
44 int doCapture;
46 volatile int killNow;
47 ALvoid *thread;
48 } alsa_data;
50 typedef struct {
51 ALCchar *name;
52 int card, dev;
53 } DevMap;
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);
109 #undef MAKE_FUNC
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)
119 if (err == -EPIPE)
120 { /* under-run */
121 err = psnd_pcm_prepare(handle);
122 if (err < 0)
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 */
129 if (err < 0)
131 err = psnd_pcm_prepare(handle);
132 if (err < 0)
133 AL_PRINT("prepare failed: %s\n", psnd_strerror(err));
136 return 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;
147 char *WritePtr;
148 int WriteCnt;
149 int err;
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);
157 if (err < 0)
159 AL_PRINT("XRUN recovery failed: %s\n", psnd_strerror(err));
160 break;
163 else if (state == SND_PCM_STATE_SUSPENDED)
165 err = xrun_recovery(data->pcmHandle, -ESTRPIPE);
166 if (err < 0)
168 AL_PRINT("SUSPEND recovery failed: %s\n", psnd_strerror(err));
169 break;
173 avail = psnd_pcm_avail_update(data->pcmHandle);
174 if(avail < 0)
176 err = xrun_recovery(data->pcmHandle, avail);
177 if (err < 0)
179 AL_PRINT("available update failed: %s\n", psnd_strerror(err));
180 break;
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);
190 if(err < 0)
191 err = xrun_recovery(data->pcmHandle, err);
192 if(err < 0)
194 AL_PRINT("start failed: %s\n", psnd_strerror(err));
195 break;
198 else if(psnd_pcm_wait(data->pcmHandle, 1000) == 0)
199 AL_PRINT("Wait timeout... buffer size too low?\n");
200 continue;
202 avail = pDevice->UpdateSize;
204 // it is possible that contiguous areas are smaller, thus we use a loop
205 while (avail > 0)
207 frames = avail;
209 SuspendContext(NULL);
210 err = psnd_pcm_mmap_begin(data->pcmHandle, &areas, &offset, &frames);
211 if (err < 0)
213 err = xrun_recovery(data->pcmHandle, err);
214 if (err < 0)
215 AL_PRINT("mmap begin error: %s\n", psnd_strerror(err));
216 ProcessContext(NULL);
217 break;
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);
230 break;
232 ProcessContext(NULL);
234 avail -= frames;
238 return 0;
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;
246 char *WritePtr;
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);
256 while(avail > 0)
258 int ret = psnd_pcm_writei(data->pcmHandle, WritePtr, avail);
259 switch (ret)
261 case -EAGAIN:
262 continue;
263 case -ESTRPIPE:
264 while((ret=psnd_pcm_resume(data->pcmHandle)) == -EAGAIN)
265 Sleep(1);
266 break;
267 case -EPIPE:
268 break;
269 default:
270 if (ret >= 0)
272 WritePtr += psnd_pcm_frames_to_bytes(data->pcmHandle, ret);
273 avail -= ret;
275 break;
277 if (ret < 0)
279 ret = psnd_pcm_prepare(data->pcmHandle);
280 if(ret < 0)
281 break;
286 return 0;
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);
299 switch(avail)
301 case -EAGAIN:
302 continue;
303 case -ESTRPIPE:
304 while((avail=psnd_pcm_resume(data->pcmHandle)) == -EAGAIN)
305 Sleep(1);
306 break;
307 case -EPIPE:
308 break;
309 default:
310 if (avail >= 0 && data->doCapture)
311 WriteRingBuffer(data->ring, data->buffer, avail);
312 break;
314 if(avail < 0)
316 avail = psnd_pcm_prepare(data->pcmHandle);
317 if(avail < 0)
318 AL_PRINT("prepare error: %s\n", psnd_strerror(avail));
322 return 0;
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;
332 unsigned int rate;
333 alsa_data *data;
334 char driver[64];
335 const char *str;
336 int allowmmap;
337 char *err;
338 int i;
340 if(!alsa_handle)
341 return ALC_FALSE;
343 strncpy(driver, GetConfigValue("alsa", "device", "default"), sizeof(driver)-1);
344 driver[sizeof(driver)-1] = 0;
345 if(deviceName)
347 size_t idx;
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;
355 if(idx > 0)
356 sprintf(driver, "hw:%d,%d", allDevNameMap[idx].card, allDevNameMap[idx].dev);
357 goto open_alsa;
360 if(strcmp(deviceName, alsaDevice) == 0)
362 device->szDeviceName = alsaDevice;
363 goto open_alsa;
365 return ALC_FALSE;
367 else
368 device->szDeviceName = alsaDevice;
370 open_alsa:
371 data = (alsa_data*)calloc(1, sizeof(alsa_data));
373 i = psnd_pcm_open(&data->pcmHandle, driver, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
374 if(i < 0)
376 Sleep(200);
377 i = psnd_pcm_open(&data->pcmHandle, driver, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
379 if(i >= 0)
381 i = psnd_pcm_nonblock(data->pcmHandle, 0);
382 if(i < 0)
383 psnd_pcm_close(data->pcmHandle);
385 if(i < 0)
387 free(data);
388 AL_PRINT("Could not open playback device '%s': %s\n", driver, psnd_strerror(i));
389 return ALC_FALSE;
392 switch(aluBytesFromFormat(device->Format))
394 case 1:
395 data->format = SND_PCM_FORMAT_U8;
396 break;
397 case 2:
398 data->format = SND_PCM_FORMAT_S16;
399 break;
400 default:
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 ||
413 atoi(str) != 0);
415 err = NULL;
416 psnd_pcm_hw_params_malloc(&p);
418 if((i=psnd_pcm_hw_params_any(data->pcmHandle, p)) < 0)
419 err = "any";
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)
424 err = "set access";
426 /* set format (implicitly sets sample bits) */
427 if(err == NULL && (i=psnd_pcm_hw_params_set_format(data->pcmHandle, p, data->format)) < 0)
428 err = "set format";
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)
443 err = "set params";
444 if(err == NULL && (i=psnd_pcm_hw_params_get_access(p, &access)) < 0)
445 err = "get access";
446 if(err == NULL && (i=psnd_pcm_hw_params_get_period_size(p, &bufferSizeInFrames, NULL)) < 0)
447 err = "get period size";
448 if(err != NULL)
450 AL_PRINT("%s failed: %s\n", err, psnd_strerror(i));
451 psnd_pcm_hw_params_free(p);
452 psnd_pcm_close(data->pcmHandle);
453 free(data);
454 return ALC_FALSE;
457 psnd_pcm_hw_params_free(p);
459 err = NULL;
460 psnd_pcm_sw_params_malloc(&sp);
462 if((i=psnd_pcm_sw_params_current(data->pcmHandle, sp)) != 0)
463 err = "sw current";
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";
468 if(err != NULL)
470 AL_PRINT("%s failed: %s\n", err, psnd_strerror(i));
471 psnd_pcm_sw_params_free(sp);
472 psnd_pcm_close(data->pcmHandle);
473 free(data);
474 return ALC_FALSE;
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);
483 if(!data->buffer)
485 AL_PRINT("buffer malloc failed\n");
486 psnd_pcm_close(data->pcmHandle);
487 free(data);
488 return ALC_FALSE;
491 else
493 i = psnd_pcm_prepare(data->pcmHandle);
494 if(i < 0)
496 AL_PRINT("prepare error: %s\n", psnd_strerror(i));
497 psnd_pcm_close(data->pcmHandle);
498 free(data->buffer);
499 free(data);
500 return ALC_FALSE;
504 device->ExtraData = data;
505 if(access == SND_PCM_ACCESS_RW_INTERLEAVED)
506 data->thread = StartThread(ALSANoMMapProc, device);
507 else
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;
514 free(data->buffer);
515 free(data);
516 return ALC_FALSE;
519 device->UpdateSize = bufferSizeInFrames;
520 device->Frequency = rate;
522 return ALC_TRUE;
525 static void alsa_close_playback(ALCdevice *device)
527 alsa_data *data = (alsa_data*)device->ExtraData;
528 data->killNow = 1;
529 StopThread(data->thread);
530 psnd_pcm_close(data->pcmHandle);
532 free(data->buffer);
533 free(data);
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;
543 const char *str;
544 alsa_data *data;
545 char driver[64];
546 int allowmmap;
547 char *err;
548 int i;
550 if(!alsa_handle)
551 return ALC_FALSE;
553 strncpy(driver, GetConfigValue("alsa", "capture", "default"), sizeof(driver)-1);
554 driver[sizeof(driver)-1] = 0;
555 if(deviceName)
557 size_t idx;
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;
565 if(idx > 0)
566 sprintf(driver, "plughw:%d,%d", allCaptureDevNameMap[idx].card, allCaptureDevNameMap[idx].dev);
567 goto open_alsa;
570 return ALC_FALSE;
572 else
573 pDevice->szDeviceName = allCaptureDevNameMap[0].name;
575 open_alsa:
576 data = (alsa_data*)calloc(1, sizeof(alsa_data));
578 i = psnd_pcm_open(&data->pcmHandle, driver, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
579 if(i < 0)
581 Sleep(200);
582 i = psnd_pcm_open(&data->pcmHandle, driver, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
584 if(i >= 0)
586 i = psnd_pcm_nonblock(data->pcmHandle, 0);
587 if(i < 0)
588 psnd_pcm_close(data->pcmHandle);
590 if(i < 0)
592 free(data);
593 AL_PRINT("Could not open capture device '%s': %s\n", driver, psnd_strerror(i));
594 return ALC_FALSE;
597 switch(aluBytesFromFormat(format))
599 case 1:
600 data->format = SND_PCM_FORMAT_U8;
601 break;
602 case 2:
603 data->format = SND_PCM_FORMAT_S16;
604 break;
605 default:
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 ||
614 atoi(str) != 0);
616 err = NULL;
617 bufferSizeInFrames = SampleSize;
618 psnd_pcm_hw_params_malloc(&p);
620 if(!allowmmap)
621 err = "no mmap";
622 if((i=psnd_pcm_hw_params_any(data->pcmHandle, p)) < 0)
623 err = "any";
624 /* set interleaved access */
625 if(err == NULL && (i=psnd_pcm_hw_params_set_access(data->pcmHandle, p, SND_PCM_ACCESS_MMAP_INTERLEAVED)) < 0)
627 err = "set access";
628 allowmmap = 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)
632 err = "set format";
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";
643 allowmmap = 0;
645 /* install and prepare hardware configuration */
646 if(err == NULL && (i=psnd_pcm_hw_params(data->pcmHandle, p)) < 0)
647 err = "set params";
648 if(!allowmmap)
650 err = NULL;
651 if((i=psnd_pcm_hw_params_any(data->pcmHandle, p)) < 0)
652 err = "any";
653 /* set interleaved access */
654 if(err == NULL && (i=psnd_pcm_hw_params_set_access(data->pcmHandle, p, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
655 err = "set access";
656 /* set format (implicitly sets sample bits) */
657 if(err == NULL && (i=psnd_pcm_hw_params_set_format(data->pcmHandle, p, data->format)) < 0)
658 err = "set format";
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)
670 err = "set params";
672 if(err != NULL)
674 AL_PRINT("%s failed: %s\n", err, psnd_strerror(i));
675 psnd_pcm_hw_params_free(p);
676 psnd_pcm_close(data->pcmHandle);
677 free(data);
678 return ALC_FALSE;
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);
686 free(data);
687 return ALC_FALSE;
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);
694 free(data);
695 return ALC_FALSE;
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);
706 if(!data->ring)
708 AL_PRINT("ring buffer create failed\n");
709 psnd_pcm_close(data->pcmHandle);
710 free(data);
711 return ALC_FALSE;
714 data->size = psnd_pcm_frames_to_bytes(data->pcmHandle, bufferSizeInFrames);
715 data->buffer = malloc(data->size);
716 if(!data->buffer)
718 AL_PRINT("buffer malloc failed\n");
719 psnd_pcm_close(data->pcmHandle);
720 DestroyRingBuffer(data->ring);
721 free(data);
722 return ALC_FALSE;
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);
733 free(data->buffer);
734 free(data);
735 return ALC_FALSE;
738 else
740 i = psnd_pcm_prepare(data->pcmHandle);
741 if(i < 0)
743 AL_PRINT("prepare error: %s\n", psnd_strerror(i));
744 psnd_pcm_close(data->pcmHandle);
745 free(data);
746 return ALC_FALSE;
748 pDevice->ExtraData = data;
751 return ALC_TRUE;
754 static void alsa_close_capture(ALCdevice *pDevice)
756 alsa_data *data = (alsa_data*)pDevice->ExtraData;
758 if(data->thread)
760 data->killNow = 1;
761 StopThread(data->thread);
762 DestroyRingBuffer(data->ring);
764 psnd_pcm_close(data->pcmHandle);
766 free(data);
767 pDevice->ExtraData = NULL;
770 static void alsa_start_capture(ALCdevice *pDevice)
772 alsa_data *data = (alsa_data*)pDevice->ExtraData;
773 data->doCapture = 1;
774 if(!data->thread)
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;
784 data->doCapture = 0;
785 if(!data->thread)
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;
796 int err;
798 if(data->thread)
800 if(lSamples <= (ALCuint)RingBufferSize(data->ring))
801 ReadRingBuffer(data->ring, pBuffer, lSamples);
802 else
803 SetALCError(ALC_INVALID_VALUE);
804 return;
807 state = psnd_pcm_state(data->pcmHandle);
808 if(state == SND_PCM_STATE_XRUN)
810 err = xrun_recovery(data->pcmHandle, -EPIPE);
811 if(err < 0)
813 AL_PRINT("XRUN recovery failed: %s\n", psnd_strerror(err));
814 return;
818 frames = psnd_pcm_avail_update(data->pcmHandle);
819 if(frames < 0)
821 err = xrun_recovery(data->pcmHandle, frames);
822 if (err < 0)
823 AL_PRINT("available update failed: %s\n", psnd_strerror(err));
824 else
825 frames = psnd_pcm_avail_update(data->pcmHandle);
827 if (frames < (snd_pcm_sframes_t)lSamples)
829 SetALCError(ALC_INVALID_VALUE);
830 return;
833 // it is possible that contiguous areas are smaller, thus we use a loop
834 while (lSamples > 0)
836 char *Pointer;
837 int Count;
839 size = lSamples;
840 err = psnd_pcm_mmap_begin(data->pcmHandle, &areas, &offset, &size);
841 if (err < 0)
843 err = xrun_recovery(data->pcmHandle, err);
844 if (err < 0)
846 AL_PRINT("mmap begin error: %s\n", psnd_strerror(err));
847 break;
849 continue;
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));
863 break;
866 lSamples -= size;
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;
875 int err;
877 if(data->thread)
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);
884 if(err >= 0)
886 if(data->doCapture)
887 err = psnd_pcm_start(data->pcmHandle);
889 if (err < 0)
891 AL_PRINT("XRUN recovery failed: %s\n", psnd_strerror(err));
892 return 0;
896 frames = psnd_pcm_avail_update(data->pcmHandle);
897 if(frames < 0)
899 err = xrun_recovery(data->pcmHandle, frames);
900 if (err < 0)
901 AL_PRINT("available update failed: %s\n", psnd_strerror(err));
902 else
903 frames = psnd_pcm_avail_update(data->pcmHandle);
905 return max(frames, 0);
909 BackendFuncs alsa_funcs = {
910 alsa_open_playback,
911 alsa_close_playback,
912 alsa_open_capture,
913 alsa_close_capture,
914 alsa_start_capture,
915 alsa_stop_capture,
916 alsa_capture_samples,
917 alsa_available_samples
920 void alc_alsa_init(BackendFuncs *func_list)
922 snd_ctl_t *handle;
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;
927 char name[128];
928 char *str;
930 *func_list = alsa_funcs;
932 #ifdef HAVE_DLFCN_H
933 alsa_handle = dlopen("libasound.so.2", RTLD_NOW);
934 if(!alsa_handle)
935 return;
936 dlerror();
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); \
945 return; \
947 } while(0)
948 #else
949 str = NULL;
950 alsa_handle = (void*)0xDEADBEEF;
951 #define LOAD_FUNC(f) p##f = f
952 #endif
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);
1008 #undef LOAD_FUNC
1010 psnd_ctl_card_info_malloc(&info);
1011 psnd_pcm_info_malloc(&pcminfo);
1013 card = -1;
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");
1020 while (card >= 0) {
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));
1024 goto next_card;
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);
1029 goto next_card;
1032 dev = -1;
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");
1038 if (dev < 0)
1039 break;
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) {
1045 if (err != -ENOENT)
1046 AL_PRINT("control digital audio info (%i): %s\n", card, psnd_strerror(err));
1047 continue;
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;
1057 idx++;
1059 psnd_ctl_close(handle);
1060 next_card:
1061 if(psnd_card_next(&card) < 0) {
1062 AL_PRINT("snd_card_next failed\n");
1063 break;
1068 stream = SND_PCM_STREAM_CAPTURE;
1070 card = -1;
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);
1075 return;
1078 allCaptureDevNameMap[0].name = AppendCaptureDeviceList("ALSA Capture on default");
1079 idx = 1;
1081 while (card >= 0) {
1082 sprintf(name, "hw:%d", card);
1083 handle = NULL;
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));
1090 else if (err >= 0)
1092 dev = -1;
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");
1098 if (dev < 0)
1099 break;
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) {
1104 if (err != -ENOENT)
1105 AL_PRINT("control digital audio info (%i): %s\n", card, psnd_strerror(err));
1106 continue;
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;
1116 idx++;
1119 if(handle) psnd_ctl_close(handle);
1120 if(psnd_card_next(&card) < 0) {
1121 AL_PRINT("snd_card_next failed\n");
1122 break;
1125 psnd_pcm_info_free(pcminfo);
1126 psnd_ctl_card_info_free(info);