Fix comment regarding the 3rd echo tap
[openal-soft.git] / Alc / alsa.c
blobc0e688c246be4276e6605004d0835eb918c01087
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_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);
104 #undef MAKE_FUNC
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)
114 if (err == -EPIPE)
115 { /* under-run */
116 err = psnd_pcm_prepare(handle);
117 if (err < 0)
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 */
124 if (err < 0)
126 err = psnd_pcm_prepare(handle);
127 if (err < 0)
128 AL_PRINT("prepare failed: %s\n", psnd_strerror(err));
131 return 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;
142 char *WritePtr;
143 int WriteCnt;
144 int err;
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);
152 if (err < 0)
154 AL_PRINT("XRUN recovery failed: %s\n", psnd_strerror(err));
155 break;
158 else if (state == SND_PCM_STATE_SUSPENDED)
160 err = xrun_recovery(data->pcmHandle, -ESTRPIPE);
161 if (err < 0)
163 AL_PRINT("SUSPEND recovery failed: %s\n", psnd_strerror(err));
164 break;
168 avail = psnd_pcm_avail_update(data->pcmHandle);
169 if(avail < 0)
171 err = xrun_recovery(data->pcmHandle, avail);
172 if (err < 0)
174 AL_PRINT("available update failed: %s\n", psnd_strerror(err));
175 break;
179 // make sure there's frames to process
180 if(avail == 0)
182 if(state != SND_PCM_STATE_RUNNING)
184 err = psnd_pcm_start(data->pcmHandle);
185 if(err < 0)
186 err = xrun_recovery(data->pcmHandle, err);
187 if(err < 0)
189 AL_PRINT("start failed: %s\n", psnd_strerror(err));
190 break;
193 else if(psnd_pcm_wait(data->pcmHandle, 1000) == 0)
194 AL_PRINT("Wait timeout... buffer size too low?\n");
195 continue;
198 // it is possible that contiguous areas are smaller, thus we use a loop
199 while (avail > 0)
201 frames = avail;
203 err = psnd_pcm_mmap_begin(data->pcmHandle, &areas, &offset, &frames);
204 if (err < 0)
206 err = xrun_recovery(data->pcmHandle, err);
207 if (err < 0)
208 AL_PRINT("mmap begin error: %s\n", psnd_strerror(err));
209 break;
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));
223 break;
226 avail -= frames;
230 return 0;
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;
238 char *WritePtr;
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);
248 while(avail > 0)
250 int ret = psnd_pcm_writei(data->pcmHandle, WritePtr, avail);
251 switch (ret)
253 case -EAGAIN:
254 continue;
255 case -ESTRPIPE:
256 while((ret=psnd_pcm_resume(data->pcmHandle)) == -EAGAIN)
257 Sleep(1);
258 break;
259 case -EPIPE:
260 break;
261 default:
262 if (ret >= 0)
264 WritePtr += psnd_pcm_frames_to_bytes(data->pcmHandle, ret);
265 avail -= ret;
267 break;
269 if (ret < 0)
271 ret = psnd_pcm_prepare(data->pcmHandle);
272 if(ret < 0)
273 break;
278 return 0;
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);
291 switch(avail)
293 case -EAGAIN:
294 continue;
295 case -ESTRPIPE:
296 while((avail=psnd_pcm_resume(data->pcmHandle)) == -EAGAIN)
297 Sleep(1);
298 break;
299 case -EPIPE:
300 break;
301 default:
302 if (avail >= 0 && data->doCapture)
303 WriteRingBuffer(data->ring, data->buffer, avail);
304 break;
306 if(avail < 0)
308 avail = psnd_pcm_prepare(data->pcmHandle);
309 if(avail < 0)
310 AL_PRINT("prepare error: %s\n", psnd_strerror(avail));
314 return 0;
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;
323 unsigned int rate;
324 alsa_data *data;
325 char driver[64];
326 const char *str;
327 int allowmmap;
328 char *err;
329 int i;
331 if(!alsa_handle)
332 return ALC_FALSE;
334 strncpy(driver, GetConfigValue("alsa", "device", "default"), sizeof(driver)-1);
335 driver[sizeof(driver)-1] = 0;
336 if(deviceName)
338 size_t idx;
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;
346 if(idx > 0)
347 sprintf(driver, "hw:%d,%d", allDevNameMap[idx].card, allDevNameMap[idx].dev);
348 goto open_alsa;
351 if(strcmp(deviceName, alsaDevice) == 0)
353 device->szDeviceName = alsaDevice;
354 goto open_alsa;
356 return ALC_FALSE;
358 else
359 device->szDeviceName = alsaDevice;
361 open_alsa:
362 data = (alsa_data*)calloc(1, sizeof(alsa_data));
364 i = psnd_pcm_open(&data->pcmHandle, driver, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
365 if(i < 0)
367 Sleep(200);
368 i = psnd_pcm_open(&data->pcmHandle, driver, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
370 if(i >= 0)
372 i = psnd_pcm_nonblock(data->pcmHandle, 0);
373 if(i < 0)
374 psnd_pcm_close(data->pcmHandle);
376 if(i < 0)
378 free(data);
379 AL_PRINT("Could not open playback device '%s': %s\n", driver, psnd_strerror(i));
380 return ALC_FALSE;
383 switch(aluBytesFromFormat(device->Format))
385 case 1:
386 data->format = SND_PCM_FORMAT_U8;
387 break;
388 case 2:
389 data->format = SND_PCM_FORMAT_S16;
390 break;
391 default:
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 ||
404 atoi(str) != 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);
429 free(data);
430 return ALC_FALSE;
432 #undef ok
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);
439 free(data);
440 return ALC_FALSE;
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);
448 free(data);
449 return ALC_FALSE;
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);
458 if(!data->buffer)
460 AL_PRINT("buffer malloc failed\n");
461 psnd_pcm_close(data->pcmHandle);
462 free(data);
463 return ALC_FALSE;
466 else
468 i = psnd_pcm_prepare(data->pcmHandle);
469 if(i < 0)
471 AL_PRINT("prepare error: %s\n", psnd_strerror(i));
472 psnd_pcm_close(data->pcmHandle);
473 free(data->buffer);
474 free(data);
475 return ALC_FALSE;
479 device->ExtraData = data;
480 if(access == SND_PCM_ACCESS_RW_INTERLEAVED)
481 data->thread = StartThread(ALSANoMMapProc, device);
482 else
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;
489 free(data->buffer);
490 free(data);
491 return ALC_FALSE;
494 device->UpdateSize = bufferSizeInFrames;
495 device->Frequency = rate;
497 return ALC_TRUE;
500 static void alsa_close_playback(ALCdevice *device)
502 alsa_data *data = (alsa_data*)device->ExtraData;
503 data->killNow = 1;
504 StopThread(data->thread);
505 psnd_pcm_close(data->pcmHandle);
507 free(data->buffer);
508 free(data);
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;
519 const char *str;
520 alsa_data *data;
521 char driver[64];
522 int allowmmap;
523 char *err;
524 int i;
526 if(!alsa_handle)
527 return ALC_FALSE;
529 strncpy(driver, GetConfigValue("alsa", "capture", "default"), sizeof(driver)-1);
530 driver[sizeof(driver)-1] = 0;
531 if(deviceName)
533 size_t idx;
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;
541 if(idx > 0)
542 sprintf(driver, "plughw:%d,%d", allCaptureDevNameMap[idx].card, allCaptureDevNameMap[idx].dev);
543 goto open_alsa;
546 return ALC_FALSE;
548 else
549 pDevice->szDeviceName = allCaptureDevNameMap[0].name;
551 open_alsa:
552 data = (alsa_data*)calloc(1, sizeof(alsa_data));
554 i = psnd_pcm_open(&data->pcmHandle, driver, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
555 if(i < 0)
557 Sleep(200);
558 i = psnd_pcm_open(&data->pcmHandle, driver, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
560 if(i >= 0)
562 i = psnd_pcm_nonblock(data->pcmHandle, 0);
563 if(i < 0)
564 psnd_pcm_close(data->pcmHandle);
566 if(i < 0)
568 free(data);
569 AL_PRINT("Could not open capture device '%s': %s\n", driver, psnd_strerror(i));
570 return ALC_FALSE;
573 switch(aluBytesFromFormat(format))
575 case 1:
576 alsaFormat = SND_PCM_FORMAT_U8;
577 break;
578 case 2:
579 alsaFormat = SND_PCM_FORMAT_S16;
580 break;
581 default:
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 ||
590 atoi(str) != 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 */
596 if(!(allowmmap &&
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")))
611 if(i < 0)
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);
625 free(data);
626 return ALC_FALSE;
629 #undef ok
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);
636 free(data);
637 return ALC_FALSE;
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);
644 free(data);
645 return ALC_FALSE;
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);
656 if(!data->ring)
658 AL_PRINT("ring buffer create failed\n");
659 psnd_pcm_close(data->pcmHandle);
660 free(data);
661 return ALC_FALSE;
664 data->size = psnd_pcm_frames_to_bytes(data->pcmHandle, bufferSizeInFrames);
665 data->buffer = malloc(data->size);
666 if(!data->buffer)
668 AL_PRINT("buffer malloc failed\n");
669 psnd_pcm_close(data->pcmHandle);
670 DestroyRingBuffer(data->ring);
671 free(data);
672 return ALC_FALSE;
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);
683 free(data->buffer);
684 free(data);
685 return ALC_FALSE;
688 else
690 i = psnd_pcm_prepare(data->pcmHandle);
691 if(i < 0)
693 AL_PRINT("prepare error: %s\n", psnd_strerror(i));
694 psnd_pcm_close(data->pcmHandle);
695 free(data);
696 return ALC_FALSE;
698 pDevice->ExtraData = data;
701 return ALC_TRUE;
704 static void alsa_close_capture(ALCdevice *pDevice)
706 alsa_data *data = (alsa_data*)pDevice->ExtraData;
708 if(data->thread)
710 data->killNow = 1;
711 StopThread(data->thread);
712 DestroyRingBuffer(data->ring);
714 psnd_pcm_close(data->pcmHandle);
716 free(data);
717 pDevice->ExtraData = NULL;
720 static void alsa_start_capture(ALCdevice *pDevice)
722 alsa_data *data = (alsa_data*)pDevice->ExtraData;
723 data->doCapture = 1;
724 if(!data->thread)
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;
734 data->doCapture = 0;
735 if(!data->thread)
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;
746 int err;
748 if(data->thread)
750 if(lSamples <= (ALCuint)RingBufferSize(data->ring))
751 ReadRingBuffer(data->ring, pBuffer, lSamples);
752 else
753 SetALCError(ALC_INVALID_VALUE);
754 return;
757 state = psnd_pcm_state(data->pcmHandle);
758 if(state == SND_PCM_STATE_XRUN)
760 err = xrun_recovery(data->pcmHandle, -EPIPE);
761 if(err < 0)
763 AL_PRINT("XRUN recovery failed: %s\n", psnd_strerror(err));
764 return;
768 frames = psnd_pcm_avail_update(data->pcmHandle);
769 if(frames < 0)
771 err = xrun_recovery(data->pcmHandle, frames);
772 if (err < 0)
773 AL_PRINT("available update failed: %s\n", psnd_strerror(err));
774 else
775 frames = psnd_pcm_avail_update(data->pcmHandle);
777 if (frames < (snd_pcm_sframes_t)lSamples)
779 SetALCError(ALC_INVALID_VALUE);
780 return;
783 // it is possible that contiguous areas are smaller, thus we use a loop
784 while (lSamples > 0)
786 char *Pointer;
787 int Count;
789 size = lSamples;
790 err = psnd_pcm_mmap_begin(data->pcmHandle, &areas, &offset, &size);
791 if (err < 0)
793 err = xrun_recovery(data->pcmHandle, err);
794 if (err < 0)
796 AL_PRINT("mmap begin error: %s\n", psnd_strerror(err));
797 break;
799 continue;
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));
813 break;
816 lSamples -= size;
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;
825 int err;
827 if(data->thread)
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);
834 if(err >= 0)
836 if(data->doCapture)
837 err = psnd_pcm_start(data->pcmHandle);
839 if (err < 0)
841 AL_PRINT("XRUN recovery failed: %s\n", psnd_strerror(err));
842 return 0;
846 frames = psnd_pcm_avail_update(data->pcmHandle);
847 if(frames < 0)
849 err = xrun_recovery(data->pcmHandle, frames);
850 if (err < 0)
851 AL_PRINT("available update failed: %s\n", psnd_strerror(err));
852 else
853 frames = psnd_pcm_avail_update(data->pcmHandle);
855 return max(frames, 0);
859 BackendFuncs alsa_funcs = {
860 alsa_open_playback,
861 alsa_close_playback,
862 alsa_open_capture,
863 alsa_close_capture,
864 alsa_start_capture,
865 alsa_stop_capture,
866 alsa_capture_samples,
867 alsa_available_samples
870 void alc_alsa_init(BackendFuncs *func_list)
872 snd_ctl_t *handle;
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;
877 char name[128];
878 char *str;
880 *func_list = alsa_funcs;
882 #ifdef HAVE_DLFCN_H
883 alsa_handle = dlopen("libasound.so.2", RTLD_NOW);
884 if(!alsa_handle)
885 return;
886 dlerror();
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); \
895 return; \
897 } while(0)
898 #else
899 str = NULL;
900 alsa_handle = (void*)0xDEADBEEF;
901 #define LOAD_FUNC(f) p##f = f
902 #endif
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);
953 #undef LOAD_FUNC
955 psnd_ctl_card_info_malloc(&info);
956 psnd_pcm_info_malloc(&pcminfo);
958 card = -1;
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");
965 while (card >= 0) {
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));
969 goto next_card;
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);
974 goto next_card;
977 dev = -1;
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");
983 if (dev < 0)
984 break;
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) {
990 if (err != -ENOENT)
991 AL_PRINT("control digital audio info (%i): %s\n", card, psnd_strerror(err));
992 continue;
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;
1002 idx++;
1004 psnd_ctl_close(handle);
1005 next_card:
1006 if(psnd_card_next(&card) < 0) {
1007 AL_PRINT("snd_card_next failed\n");
1008 break;
1013 stream = SND_PCM_STREAM_CAPTURE;
1015 card = -1;
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);
1020 return;
1023 allCaptureDevNameMap[0].name = AppendCaptureDeviceList("ALSA Capture on default");
1024 idx = 1;
1026 while (card >= 0) {
1027 sprintf(name, "hw:%d", card);
1028 handle = NULL;
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));
1035 else if (err >= 0)
1037 dev = -1;
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");
1043 if (dev < 0)
1044 break;
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) {
1049 if (err != -ENOENT)
1050 AL_PRINT("control digital audio info (%i): %s\n", card, psnd_strerror(err));
1051 continue;
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;
1061 idx++;
1064 if(handle) psnd_ctl_close(handle);
1065 if(psnd_card_next(&card) < 0) {
1066 AL_PRINT("snd_card_next failed\n");
1067 break;
1070 psnd_pcm_info_free(pcminfo);
1071 psnd_ctl_card_info_free(info);