Add a basic WaveOut device
[openal-soft.git] / Alc / alsa.c
blobd8d677fd942d07cbadb54a578ea3514e11137c2b
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;
39 ALvoid *buffer;
40 ALsizei size;
42 ALboolean doCapture;
43 RingBuffer *ring;
45 volatile int killNow;
46 ALvoid *thread;
47 } alsa_data;
49 typedef struct {
50 ALCchar *name;
51 int card, dev;
52 } DevMap;
54 static void *alsa_handle;
55 #define MAKE_FUNC(f) static typeof(f) * p##f
56 MAKE_FUNC(snd_strerror);
57 MAKE_FUNC(snd_pcm_open);
58 MAKE_FUNC(snd_pcm_close);
59 MAKE_FUNC(snd_pcm_nonblock);
60 MAKE_FUNC(snd_pcm_frames_to_bytes);
61 MAKE_FUNC(snd_pcm_bytes_to_frames);
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_rate_resample);
72 MAKE_FUNC(snd_pcm_hw_params_set_buffer_time_near);
73 MAKE_FUNC(snd_pcm_hw_params_set_period_time_near);
74 MAKE_FUNC(snd_pcm_hw_params_set_buffer_size_near);
75 MAKE_FUNC(snd_pcm_hw_params_set_period_size_near);
76 MAKE_FUNC(snd_pcm_hw_params_set_buffer_size_min);
77 MAKE_FUNC(snd_pcm_hw_params_get_buffer_size);
78 MAKE_FUNC(snd_pcm_hw_params_get_period_size);
79 MAKE_FUNC(snd_pcm_hw_params_get_access);
80 MAKE_FUNC(snd_pcm_hw_params_get_periods);
81 MAKE_FUNC(snd_pcm_hw_params);
82 MAKE_FUNC(snd_pcm_sw_params_malloc);
83 MAKE_FUNC(snd_pcm_sw_params_current);
84 MAKE_FUNC(snd_pcm_sw_params_set_avail_min);
85 MAKE_FUNC(snd_pcm_sw_params);
86 MAKE_FUNC(snd_pcm_sw_params_free);
87 MAKE_FUNC(snd_pcm_prepare);
88 MAKE_FUNC(snd_pcm_start);
89 MAKE_FUNC(snd_pcm_resume);
90 MAKE_FUNC(snd_pcm_wait);
91 MAKE_FUNC(snd_pcm_state);
92 MAKE_FUNC(snd_pcm_avail_update);
93 MAKE_FUNC(snd_pcm_areas_silence);
94 MAKE_FUNC(snd_pcm_mmap_begin);
95 MAKE_FUNC(snd_pcm_mmap_commit);
96 MAKE_FUNC(snd_pcm_readi);
97 MAKE_FUNC(snd_pcm_writei);
98 MAKE_FUNC(snd_pcm_drain);
99 MAKE_FUNC(snd_pcm_recover);
100 MAKE_FUNC(snd_pcm_info_malloc);
101 MAKE_FUNC(snd_pcm_info_free);
102 MAKE_FUNC(snd_pcm_info_set_device);
103 MAKE_FUNC(snd_pcm_info_set_subdevice);
104 MAKE_FUNC(snd_pcm_info_set_stream);
105 MAKE_FUNC(snd_pcm_info_get_name);
106 MAKE_FUNC(snd_ctl_pcm_next_device);
107 MAKE_FUNC(snd_ctl_pcm_info);
108 MAKE_FUNC(snd_ctl_open);
109 MAKE_FUNC(snd_ctl_close);
110 MAKE_FUNC(snd_ctl_card_info_malloc);
111 MAKE_FUNC(snd_ctl_card_info_free);
112 MAKE_FUNC(snd_ctl_card_info);
113 MAKE_FUNC(snd_ctl_card_info_get_name);
114 MAKE_FUNC(snd_card_next);
115 #undef MAKE_FUNC
118 static const ALCchar alsaDevice[] = "ALSA Default";
119 static DevMap *allDevNameMap;
120 static ALuint numDevNames;
121 static DevMap *allCaptureDevNameMap;
122 static ALuint numCaptureDevNames;
125 void *alsa_load(void)
127 if(!alsa_handle)
129 char *str;
131 #ifdef HAVE_DLFCN_H
132 alsa_handle = dlopen("libasound.so.2", RTLD_NOW);
133 if(!alsa_handle)
134 return NULL;
135 dlerror();
137 #define LOAD_FUNC(f) do { \
138 p##f = dlsym(alsa_handle, #f); \
139 if((str=dlerror()) != NULL) \
141 dlclose(alsa_handle); \
142 alsa_handle = NULL; \
143 AL_PRINT("Could not load %s from libasound.so.2: %s\n", #f, str); \
144 return NULL; \
146 } while(0)
147 #else
148 str = NULL;
149 alsa_handle = (void*)0xDEADBEEF;
150 #define LOAD_FUNC(f) p##f = f
151 #endif
153 LOAD_FUNC(snd_strerror);
154 LOAD_FUNC(snd_pcm_open);
155 LOAD_FUNC(snd_pcm_close);
156 LOAD_FUNC(snd_pcm_nonblock);
157 LOAD_FUNC(snd_pcm_frames_to_bytes);
158 LOAD_FUNC(snd_pcm_bytes_to_frames);
159 LOAD_FUNC(snd_pcm_hw_params_malloc);
160 LOAD_FUNC(snd_pcm_hw_params_free);
161 LOAD_FUNC(snd_pcm_hw_params_any);
162 LOAD_FUNC(snd_pcm_hw_params_set_access);
163 LOAD_FUNC(snd_pcm_hw_params_set_format);
164 LOAD_FUNC(snd_pcm_hw_params_set_channels);
165 LOAD_FUNC(snd_pcm_hw_params_set_periods_near);
166 LOAD_FUNC(snd_pcm_hw_params_set_rate_near);
167 LOAD_FUNC(snd_pcm_hw_params_set_rate);
168 LOAD_FUNC(snd_pcm_hw_params_set_rate_resample);
169 LOAD_FUNC(snd_pcm_hw_params_set_buffer_time_near);
170 LOAD_FUNC(snd_pcm_hw_params_set_period_time_near);
171 LOAD_FUNC(snd_pcm_hw_params_set_buffer_size_near);
172 LOAD_FUNC(snd_pcm_hw_params_set_buffer_size_min);
173 LOAD_FUNC(snd_pcm_hw_params_set_period_size_near);
174 LOAD_FUNC(snd_pcm_hw_params_get_buffer_size);
175 LOAD_FUNC(snd_pcm_hw_params_get_period_size);
176 LOAD_FUNC(snd_pcm_hw_params_get_access);
177 LOAD_FUNC(snd_pcm_hw_params_get_periods);
178 LOAD_FUNC(snd_pcm_hw_params);
179 LOAD_FUNC(snd_pcm_sw_params_malloc);
180 LOAD_FUNC(snd_pcm_sw_params_current);
181 LOAD_FUNC(snd_pcm_sw_params_set_avail_min);
182 LOAD_FUNC(snd_pcm_sw_params);
183 LOAD_FUNC(snd_pcm_sw_params_free);
184 LOAD_FUNC(snd_pcm_prepare);
185 LOAD_FUNC(snd_pcm_start);
186 LOAD_FUNC(snd_pcm_resume);
187 LOAD_FUNC(snd_pcm_wait);
188 LOAD_FUNC(snd_pcm_state);
189 LOAD_FUNC(snd_pcm_avail_update);
190 LOAD_FUNC(snd_pcm_areas_silence);
191 LOAD_FUNC(snd_pcm_mmap_begin);
192 LOAD_FUNC(snd_pcm_mmap_commit);
193 LOAD_FUNC(snd_pcm_readi);
194 LOAD_FUNC(snd_pcm_writei);
195 LOAD_FUNC(snd_pcm_drain);
196 LOAD_FUNC(snd_pcm_recover);
198 LOAD_FUNC(snd_pcm_info_malloc);
199 LOAD_FUNC(snd_pcm_info_free);
200 LOAD_FUNC(snd_pcm_info_set_device);
201 LOAD_FUNC(snd_pcm_info_set_subdevice);
202 LOAD_FUNC(snd_pcm_info_set_stream);
203 LOAD_FUNC(snd_pcm_info_get_name);
204 LOAD_FUNC(snd_ctl_pcm_next_device);
205 LOAD_FUNC(snd_ctl_pcm_info);
206 LOAD_FUNC(snd_ctl_open);
207 LOAD_FUNC(snd_ctl_close);
208 LOAD_FUNC(snd_ctl_card_info_malloc);
209 LOAD_FUNC(snd_ctl_card_info_free);
210 LOAD_FUNC(snd_ctl_card_info);
211 LOAD_FUNC(snd_ctl_card_info_get_name);
212 LOAD_FUNC(snd_card_next);
214 #undef LOAD_FUNC
216 return alsa_handle;
219 static DevMap *probe_devices(snd_pcm_stream_t stream, ALuint *count)
221 snd_ctl_t *handle;
222 int card, err, dev, idx;
223 snd_ctl_card_info_t *info;
224 snd_pcm_info_t *pcminfo;
225 DevMap *DevList;
226 char name[1024];
228 psnd_ctl_card_info_malloc(&info);
229 psnd_pcm_info_malloc(&pcminfo);
231 card = -1;
232 if((err=psnd_card_next(&card)) < 0)
233 AL_PRINT("Failed to find a card: %s\n", psnd_strerror(err));
235 DevList = malloc(sizeof(DevMap) * 1);
236 DevList[0].name = strdup("ALSA Default");
237 idx = 1;
238 while(card >= 0)
240 sprintf(name, "hw:%d", card);
241 if((err = psnd_ctl_open(&handle, name, 0)) < 0)
243 AL_PRINT("control open (%i): %s\n", card, psnd_strerror(err));
244 goto next_card;
246 if((err = psnd_ctl_card_info(handle, info)) < 0)
248 AL_PRINT("control hardware info (%i): %s\n", card, psnd_strerror(err));
249 psnd_ctl_close(handle);
250 goto next_card;
253 dev = -1;
254 while(1)
256 const char *cname, *dname;
257 void *temp;
259 if(psnd_ctl_pcm_next_device(handle, &dev) < 0)
260 AL_PRINT("snd_ctl_pcm_next_device failed\n");
261 if(dev < 0)
262 break;
264 psnd_pcm_info_set_device(pcminfo, dev);
265 psnd_pcm_info_set_subdevice(pcminfo, 0);
266 psnd_pcm_info_set_stream(pcminfo, stream);
267 if((err = psnd_ctl_pcm_info(handle, pcminfo)) < 0) {
268 if(err != -ENOENT)
269 AL_PRINT("control digital audio info (%i): %s\n", card, psnd_strerror(err));
270 continue;
273 temp = realloc(DevList, sizeof(DevMap) * (idx+1));
274 if(temp)
276 DevList = temp;
277 cname = psnd_ctl_card_info_get_name(info);
278 dname = psnd_pcm_info_get_name(pcminfo);
279 snprintf(name, sizeof(name), "%s [%s] (hw:%d,%d) via ALSA",
280 cname, dname, card, dev);
281 DevList[idx].name = strdup(name);
282 DevList[idx].card = card;
283 DevList[idx].dev = dev;
284 idx++;
287 psnd_ctl_close(handle);
288 next_card:
289 if(psnd_card_next(&card) < 0) {
290 AL_PRINT("snd_card_next failed\n");
291 break;
295 psnd_pcm_info_free(pcminfo);
296 psnd_ctl_card_info_free(info);
298 *count = idx;
299 return DevList;
303 static int xrun_recovery(snd_pcm_t *handle, int err)
305 err = psnd_pcm_recover(handle, err, 1);
306 if(err < 0)
307 AL_PRINT("recover failed: %s\n", psnd_strerror(err));
308 return err;
311 static int verify_state(snd_pcm_t *handle)
313 snd_pcm_state_t state = psnd_pcm_state(handle);
314 if(state == SND_PCM_STATE_DISCONNECTED)
315 return -ENODEV;
316 if(state == SND_PCM_STATE_XRUN)
318 int err = xrun_recovery(handle, -EPIPE);
319 if(err < 0) return err;
321 else if(state == SND_PCM_STATE_SUSPENDED)
323 int err = xrun_recovery(handle, -ESTRPIPE);
324 if(err < 0) return err;
327 return state;
331 static ALuint ALSAProc(ALvoid *ptr)
333 ALCdevice *pDevice = (ALCdevice*)ptr;
334 alsa_data *data = (alsa_data*)pDevice->ExtraData;
335 const snd_pcm_channel_area_t *areas = NULL;
336 snd_pcm_sframes_t avail, commitres;
337 snd_pcm_uframes_t offset, frames;
338 char *WritePtr;
339 int err;
341 SetRTPriority();
343 while(!data->killNow)
345 int state = verify_state(data->pcmHandle);
346 if(state < 0)
348 AL_PRINT("Invalid state detected: %s\n", psnd_strerror(state));
349 aluHandleDisconnect(pDevice);
350 break;
353 avail = psnd_pcm_avail_update(data->pcmHandle);
354 if(avail < 0)
356 AL_PRINT("available update failed: %s\n", psnd_strerror(avail));
357 continue;
360 // make sure there's frames to process
361 if((snd_pcm_uframes_t)avail < pDevice->UpdateSize)
363 if(state != SND_PCM_STATE_RUNNING)
365 err = psnd_pcm_start(data->pcmHandle);
366 if(err < 0)
368 AL_PRINT("start failed: %s\n", psnd_strerror(err));
369 continue;
372 if(psnd_pcm_wait(data->pcmHandle, 1000) == 0)
373 AL_PRINT("Wait timeout... buffer size too low?\n");
374 continue;
376 avail -= avail%pDevice->UpdateSize;
378 // it is possible that contiguous areas are smaller, thus we use a loop
379 while(avail > 0)
381 frames = avail;
383 err = psnd_pcm_mmap_begin(data->pcmHandle, &areas, &offset, &frames);
384 if(err < 0)
386 AL_PRINT("mmap begin error: %s\n", psnd_strerror(err));
387 break;
390 WritePtr = (char*)areas->addr + (offset * areas->step / 8);
391 aluMixData(pDevice, WritePtr, frames);
393 commitres = psnd_pcm_mmap_commit(data->pcmHandle, offset, frames);
394 if(commitres < 0 || (commitres-frames) != 0)
396 AL_PRINT("mmap commit error: %s\n",
397 psnd_strerror(commitres >= 0 ? -EPIPE : commitres));
398 break;
401 avail -= frames;
405 return 0;
408 static ALuint ALSANoMMapProc(ALvoid *ptr)
410 ALCdevice *pDevice = (ALCdevice*)ptr;
411 alsa_data *data = (alsa_data*)pDevice->ExtraData;
412 snd_pcm_sframes_t avail;
413 char *WritePtr;
415 SetRTPriority();
417 while(!data->killNow)
419 int state = verify_state(data->pcmHandle);
420 if(state < 0)
422 AL_PRINT("Invalid state detected: %s\n", psnd_strerror(state));
423 aluHandleDisconnect(pDevice);
424 break;
427 WritePtr = data->buffer;
428 avail = data->size / psnd_pcm_frames_to_bytes(data->pcmHandle, 1);
429 aluMixData(pDevice, WritePtr, avail);
431 while(avail > 0)
433 int ret = psnd_pcm_writei(data->pcmHandle, WritePtr, avail);
434 switch (ret)
436 case -EAGAIN:
437 continue;
438 case -ESTRPIPE:
439 case -EPIPE:
440 case -EINTR:
441 ret = psnd_pcm_recover(data->pcmHandle, ret, 1);
442 if(ret < 0)
443 avail = 0;
444 break;
445 default:
446 if (ret >= 0)
448 WritePtr += psnd_pcm_frames_to_bytes(data->pcmHandle, ret);
449 avail -= ret;
451 break;
453 if (ret < 0)
455 ret = psnd_pcm_prepare(data->pcmHandle);
456 if(ret < 0)
457 break;
462 return 0;
465 static ALCboolean alsa_open_playback(ALCdevice *device, const ALCchar *deviceName)
467 alsa_data *data;
468 char driver[64];
469 int i;
471 if(!alsa_load())
472 return ALC_FALSE;
474 strncpy(driver, GetConfigValue("alsa", "device", "default"), sizeof(driver)-1);
475 driver[sizeof(driver)-1] = 0;
477 if(!deviceName)
478 deviceName = alsaDevice;
479 else if(strcmp(deviceName, alsaDevice) != 0)
481 size_t idx;
483 if(!allDevNameMap)
484 allDevNameMap = probe_devices(SND_PCM_STREAM_PLAYBACK, &numDevNames);
486 for(idx = 0;idx < numDevNames;idx++)
488 if(allDevNameMap[idx].name &&
489 strcmp(deviceName, allDevNameMap[idx].name) == 0)
491 if(idx > 0)
492 sprintf(driver, "hw:%d,%d", allDevNameMap[idx].card, allDevNameMap[idx].dev);
493 break;
496 if(idx == numDevNames)
497 return ALC_FALSE;
500 data = (alsa_data*)calloc(1, sizeof(alsa_data));
502 i = psnd_pcm_open(&data->pcmHandle, driver, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
503 if(i >= 0)
505 i = psnd_pcm_nonblock(data->pcmHandle, 0);
506 if(i < 0)
507 psnd_pcm_close(data->pcmHandle);
509 if(i < 0)
511 free(data);
512 AL_PRINT("Could not open playback device '%s': %s\n", driver, psnd_strerror(i));
513 return ALC_FALSE;
516 device->szDeviceName = strdup(deviceName);
517 device->ExtraData = data;
518 return ALC_TRUE;
521 static void alsa_close_playback(ALCdevice *device)
523 alsa_data *data = (alsa_data*)device->ExtraData;
525 psnd_pcm_close(data->pcmHandle);
526 free(data);
527 device->ExtraData = NULL;
530 static ALCboolean alsa_reset_playback(ALCdevice *device)
532 alsa_data *data = (alsa_data*)device->ExtraData;
533 snd_pcm_uframes_t periodSizeInFrames;
534 unsigned int periodLen, bufferLen;
535 snd_pcm_sw_params_t *sp = NULL;
536 snd_pcm_hw_params_t *p = NULL;
537 snd_pcm_access_t access;
538 snd_pcm_format_t format;
539 unsigned int periods;
540 unsigned int rate;
541 int allowmmap;
542 char *err;
543 int i;
546 switch(aluBytesFromFormat(device->Format))
548 case 1:
549 format = SND_PCM_FORMAT_U8;
550 break;
551 case 2:
552 format = SND_PCM_FORMAT_S16;
553 break;
554 case 4:
555 format = SND_PCM_FORMAT_FLOAT;
556 break;
557 default:
558 AL_PRINT("Unknown format: 0x%x\n", device->Format);
559 return ALC_FALSE;
562 allowmmap = GetConfigValueBool("alsa", "mmap", 1);
563 periods = device->NumUpdates;
564 periodLen = (ALuint64)device->UpdateSize * 1000000 / device->Frequency;
565 bufferLen = periodLen * periods;
566 rate = device->Frequency;
568 err = NULL;
569 psnd_pcm_hw_params_malloc(&p);
571 if((i=psnd_pcm_hw_params_any(data->pcmHandle, p)) < 0)
572 err = "any";
573 /* set interleaved access */
574 if(i >= 0 && (!allowmmap || (i=psnd_pcm_hw_params_set_access(data->pcmHandle, p, SND_PCM_ACCESS_MMAP_INTERLEAVED)) < 0))
576 if(periods > 2)
578 periods--;
579 bufferLen = periodLen * periods;
581 if((i=psnd_pcm_hw_params_set_access(data->pcmHandle, p, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
582 err = "set access";
584 /* set format (implicitly sets sample bits) */
585 if(i >= 0 && (i=psnd_pcm_hw_params_set_format(data->pcmHandle, p, format)) < 0)
587 switch(aluChannelsFromFormat(device->Format))
589 case 1: device->Format = AL_FORMAT_MONO_FLOAT32; break;
590 case 2: device->Format = AL_FORMAT_STEREO_FLOAT32; break;
591 case 4: device->Format = AL_FORMAT_QUAD32; break;
592 case 6: device->Format = AL_FORMAT_51CHN32; break;
593 case 7: device->Format = AL_FORMAT_61CHN32; break;
594 case 8: device->Format = AL_FORMAT_71CHN32; break;
596 if(format == SND_PCM_FORMAT_FLOAT ||
597 (i=psnd_pcm_hw_params_set_format(data->pcmHandle, p, SND_PCM_FORMAT_FLOAT)) < 0)
599 switch(aluChannelsFromFormat(device->Format))
601 case 1: device->Format = AL_FORMAT_MONO16; break;
602 case 2: device->Format = AL_FORMAT_STEREO16; break;
603 case 4: device->Format = AL_FORMAT_QUAD16; break;
604 case 6: device->Format = AL_FORMAT_51CHN16; break;
605 case 7: device->Format = AL_FORMAT_61CHN16; break;
606 case 8: device->Format = AL_FORMAT_71CHN16; break;
608 if(format == SND_PCM_FORMAT_S16 ||
609 (i=psnd_pcm_hw_params_set_format(data->pcmHandle, p, SND_PCM_FORMAT_S16)) < 0)
611 switch(aluChannelsFromFormat(device->Format))
613 case 1: device->Format = AL_FORMAT_MONO8; break;
614 case 2: device->Format = AL_FORMAT_STEREO8; break;
615 case 4: device->Format = AL_FORMAT_QUAD8; break;
616 case 6: device->Format = AL_FORMAT_51CHN8; break;
617 case 7: device->Format = AL_FORMAT_61CHN8; break;
618 case 8: device->Format = AL_FORMAT_71CHN8; break;
620 if(format == SND_PCM_FORMAT_U8 ||
621 (i=psnd_pcm_hw_params_set_format(data->pcmHandle, p, SND_PCM_FORMAT_U8)) < 0)
622 err = "set format";
626 /* set channels (implicitly sets frame bits) */
627 if(i >= 0 && (i=psnd_pcm_hw_params_set_channels(data->pcmHandle, p, aluChannelsFromFormat(device->Format))) < 0)
629 switch(aluBytesFromFormat(device->Format))
631 case 1: device->Format = AL_FORMAT_STEREO8; break;
632 case 2: device->Format = AL_FORMAT_STEREO16; break;
633 case 4: device->Format = AL_FORMAT_STEREO_FLOAT32; break;
635 if((i=psnd_pcm_hw_params_set_channels(data->pcmHandle, p, 2)) < 0)
637 switch(aluBytesFromFormat(device->Format))
639 case 1: device->Format = AL_FORMAT_MONO8; break;
640 case 2: device->Format = AL_FORMAT_MONO16; break;
641 case 4: device->Format = AL_FORMAT_MONO_FLOAT32; break;
643 if((i=psnd_pcm_hw_params_set_channels(data->pcmHandle, p, 1)) < 0)
644 err = "set channels";
647 if(i >= 0 && (i=psnd_pcm_hw_params_set_rate_resample(data->pcmHandle, p, 0)) < 0)
649 AL_PRINT("Failed to disable ALSA resampler\n");
650 i = 0;
652 /* set rate (implicitly constrains period/buffer parameters) */
653 if(i >= 0 && (i=psnd_pcm_hw_params_set_rate_near(data->pcmHandle, p, &rate, NULL)) < 0)
654 err = "set rate near";
655 /* set buffer time (implicitly constrains period/buffer parameters) */
656 if(i >= 0 && (i=psnd_pcm_hw_params_set_buffer_time_near(data->pcmHandle, p, &bufferLen, NULL)) < 0)
657 err = "set buffer time near";
658 /* set period time in frame units (implicitly sets buffer size/bytes/time and period size/bytes) */
659 if(i >= 0 && (i=psnd_pcm_hw_params_set_period_time_near(data->pcmHandle, p, &periodLen, NULL)) < 0)
660 err = "set period time near";
661 /* install and prepare hardware configuration */
662 if(i >= 0 && (i=psnd_pcm_hw_params(data->pcmHandle, p)) < 0)
663 err = "set params";
664 if(i >= 0 && (i=psnd_pcm_hw_params_get_access(p, &access)) < 0)
665 err = "get access";
666 if(i >= 0 && (i=psnd_pcm_hw_params_get_period_size(p, &periodSizeInFrames, NULL)) < 0)
667 err = "get period size";
668 if(i >= 0 && (i=psnd_pcm_hw_params_get_periods(p, &periods, NULL)) < 0)
669 err = "get periods";
670 if(i < 0)
672 AL_PRINT("%s failed: %s\n", err, psnd_strerror(i));
673 psnd_pcm_hw_params_free(p);
674 return ALC_FALSE;
677 psnd_pcm_hw_params_free(p);
679 err = NULL;
680 psnd_pcm_sw_params_malloc(&sp);
682 if((i=psnd_pcm_sw_params_current(data->pcmHandle, sp)) != 0)
683 err = "sw current";
684 if(i == 0 && (i=psnd_pcm_sw_params_set_avail_min(data->pcmHandle, sp, periodSizeInFrames)) != 0)
685 err = "sw set avail min";
686 if(i == 0 && (i=psnd_pcm_sw_params(data->pcmHandle, sp)) != 0)
687 err = "sw set params";
688 if(i != 0)
690 AL_PRINT("%s failed: %s\n", err, psnd_strerror(i));
691 psnd_pcm_sw_params_free(sp);
692 return ALC_FALSE;
695 psnd_pcm_sw_params_free(sp);
697 device->Frequency = rate;
699 SetDefaultChannelOrder(device);
701 data->size = psnd_pcm_frames_to_bytes(data->pcmHandle, periodSizeInFrames);
702 if(access == SND_PCM_ACCESS_RW_INTERLEAVED)
704 /* Increase periods by one, since the temp buffer counts as an extra
705 * period */
706 periods++;
707 data->buffer = malloc(data->size);
708 if(!data->buffer)
710 AL_PRINT("buffer malloc failed\n");
711 return ALC_FALSE;
713 device->UpdateSize = periodSizeInFrames;
714 device->NumUpdates = periods;
715 data->thread = StartThread(ALSANoMMapProc, device);
717 else
719 i = psnd_pcm_prepare(data->pcmHandle);
720 if(i < 0)
722 AL_PRINT("prepare error: %s\n", psnd_strerror(i));
723 return ALC_FALSE;
725 device->UpdateSize = periodSizeInFrames;
726 device->NumUpdates = periods;
727 data->thread = StartThread(ALSAProc, device);
729 if(data->thread == NULL)
731 AL_PRINT("Could not create playback thread\n");
732 free(data->buffer);
733 data->buffer = NULL;
734 return ALC_FALSE;
737 return ALC_TRUE;
740 static void alsa_stop_playback(ALCdevice *device)
742 alsa_data *data = (alsa_data*)device->ExtraData;
744 if(data->thread)
746 data->killNow = 1;
747 StopThread(data->thread);
748 data->thread = NULL;
750 data->killNow = 0;
751 free(data->buffer);
752 data->buffer = NULL;
756 static ALCboolean alsa_open_capture(ALCdevice *pDevice, const ALCchar *deviceName)
758 snd_pcm_hw_params_t *p;
759 snd_pcm_uframes_t bufferSizeInFrames;
760 snd_pcm_format_t format;
761 ALuint frameSize;
762 alsa_data *data;
763 char driver[64];
764 char *err;
765 int i;
767 if(!alsa_load())
768 return ALC_FALSE;
770 strncpy(driver, GetConfigValue("alsa", "capture", "default"), sizeof(driver)-1);
771 driver[sizeof(driver)-1] = 0;
773 if(!allCaptureDevNameMap)
774 allCaptureDevNameMap = probe_devices(SND_PCM_STREAM_CAPTURE, &numCaptureDevNames);
776 if(!deviceName)
777 deviceName = allCaptureDevNameMap[0].name;
778 else
780 size_t idx;
782 for(idx = 0;idx < numCaptureDevNames;idx++)
784 if(allCaptureDevNameMap[idx].name &&
785 strcmp(deviceName, allCaptureDevNameMap[idx].name) == 0)
787 if(idx > 0)
788 sprintf(driver, "plughw:%d,%d", allCaptureDevNameMap[idx].card, allCaptureDevNameMap[idx].dev);
789 break;
792 if(idx == numCaptureDevNames)
793 return ALC_FALSE;
796 data = (alsa_data*)calloc(1, sizeof(alsa_data));
798 i = psnd_pcm_open(&data->pcmHandle, driver, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
799 if(i < 0)
801 AL_PRINT("Could not open capture device '%s': %s\n", driver, psnd_strerror(i));
802 free(data);
803 return ALC_FALSE;
806 switch(aluBytesFromFormat(pDevice->Format))
808 case 1:
809 format = SND_PCM_FORMAT_U8;
810 break;
811 case 2:
812 format = SND_PCM_FORMAT_S16;
813 break;
814 case 4:
815 format = SND_PCM_FORMAT_FLOAT;
816 break;
817 default:
818 AL_PRINT("Unknown format: 0x%x\n", pDevice->Format);
819 goto error;
822 err = NULL;
823 bufferSizeInFrames = pDevice->UpdateSize * pDevice->NumUpdates;
824 psnd_pcm_hw_params_malloc(&p);
826 if((i=psnd_pcm_hw_params_any(data->pcmHandle, p)) < 0)
827 err = "any";
828 /* set interleaved access */
829 if(i >= 0 && (i=psnd_pcm_hw_params_set_access(data->pcmHandle, p, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0)
830 err = "set access";
831 /* set format (implicitly sets sample bits) */
832 if(i >= 0 && (i=psnd_pcm_hw_params_set_format(data->pcmHandle, p, format)) < 0)
833 err = "set format";
834 /* set channels (implicitly sets frame bits) */
835 if(i >= 0 && (i=psnd_pcm_hw_params_set_channels(data->pcmHandle, p, aluChannelsFromFormat(pDevice->Format))) < 0)
836 err = "set channels";
837 /* set rate (implicitly constrains period/buffer parameters) */
838 if(i >= 0 && (i=psnd_pcm_hw_params_set_rate(data->pcmHandle, p, pDevice->Frequency, 0)) < 0)
839 err = "set rate near";
840 /* set buffer size in frame units (implicitly sets period size/bytes/time and buffer time/bytes) */
841 if(i >= 0 && (i=psnd_pcm_hw_params_set_buffer_size_near(data->pcmHandle, p, &bufferSizeInFrames)) < 0)
842 err = "set buffer size near";
843 /* install and prepare hardware configuration */
844 if(i >= 0 && (i=psnd_pcm_hw_params(data->pcmHandle, p)) < 0)
845 err = "set params";
846 if(i < 0)
848 AL_PRINT("%s failed: %s\n", err, psnd_strerror(i));
849 psnd_pcm_hw_params_free(p);
850 goto error;
853 if((i=psnd_pcm_hw_params_get_period_size(p, &bufferSizeInFrames, NULL)) < 0)
855 AL_PRINT("get size failed: %s\n", psnd_strerror(i));
856 psnd_pcm_hw_params_free(p);
857 goto error;
860 psnd_pcm_hw_params_free(p);
862 frameSize = aluFrameSizeFromFormat(pDevice->Format);
864 data->ring = CreateRingBuffer(frameSize, pDevice->UpdateSize*pDevice->NumUpdates);
865 if(!data->ring)
867 AL_PRINT("ring buffer create failed\n");
868 goto error;
871 data->size = psnd_pcm_frames_to_bytes(data->pcmHandle, bufferSizeInFrames);
872 data->buffer = malloc(data->size);
873 if(!data->buffer)
875 AL_PRINT("buffer malloc failed\n");
876 goto error;
879 pDevice->szDeviceName = strdup(deviceName);
881 pDevice->ExtraData = data;
882 return ALC_TRUE;
884 error:
885 free(data->buffer);
886 DestroyRingBuffer(data->ring);
887 psnd_pcm_close(data->pcmHandle);
888 free(data);
890 pDevice->ExtraData = NULL;
891 return ALC_FALSE;
894 static void alsa_close_capture(ALCdevice *pDevice)
896 alsa_data *data = (alsa_data*)pDevice->ExtraData;
898 psnd_pcm_close(data->pcmHandle);
899 DestroyRingBuffer(data->ring);
901 free(data->buffer);
902 free(data);
903 pDevice->ExtraData = NULL;
906 static void alsa_start_capture(ALCdevice *Device)
908 alsa_data *data = (alsa_data*)Device->ExtraData;
909 int err;
911 err = psnd_pcm_start(data->pcmHandle);
912 if(err < 0)
914 AL_PRINT("start failed: %s\n", psnd_strerror(err));
915 aluHandleDisconnect(Device);
917 else
918 data->doCapture = AL_TRUE;
921 static void alsa_stop_capture(ALCdevice *Device)
923 alsa_data *data = (alsa_data*)Device->ExtraData;
924 psnd_pcm_drain(data->pcmHandle);
925 data->doCapture = AL_FALSE;
928 static ALCuint alsa_available_samples(ALCdevice *Device)
930 alsa_data *data = (alsa_data*)Device->ExtraData;
931 snd_pcm_sframes_t avail;
933 avail = (Device->Connected ? psnd_pcm_avail_update(data->pcmHandle) : 0);
934 if(avail < 0)
936 AL_PRINT("avail update failed: %s\n", psnd_strerror(avail));
938 if((avail=psnd_pcm_recover(data->pcmHandle, avail, 1)) >= 0)
940 if(data->doCapture)
941 avail = psnd_pcm_start(data->pcmHandle);
942 if(avail >= 0)
943 avail = psnd_pcm_avail_update(data->pcmHandle);
945 if(avail < 0)
947 AL_PRINT("restore error: %s\n", psnd_strerror(avail));
948 aluHandleDisconnect(Device);
951 while(avail > 0)
953 snd_pcm_sframes_t amt;
955 amt = psnd_pcm_bytes_to_frames(data->pcmHandle, data->size);
956 if(avail < amt) amt = avail;
958 amt = psnd_pcm_readi(data->pcmHandle, data->buffer, amt);
959 if(amt < 0)
961 AL_PRINT("read error: %s\n", psnd_strerror(amt));
963 if(amt == -EAGAIN)
964 continue;
965 if((amt=psnd_pcm_recover(data->pcmHandle, amt, 1)) >= 0)
967 if(data->doCapture)
968 amt = psnd_pcm_start(data->pcmHandle);
969 if(amt >= 0)
970 amt = psnd_pcm_avail_update(data->pcmHandle);
972 if(amt < 0)
974 AL_PRINT("restore error: %s\n", psnd_strerror(amt));
975 aluHandleDisconnect(Device);
976 break;
978 avail = amt;
979 continue;
982 WriteRingBuffer(data->ring, data->buffer, amt);
983 avail -= amt;
986 return RingBufferSize(data->ring);
989 static void alsa_capture_samples(ALCdevice *Device, ALCvoid *Buffer, ALCuint Samples)
991 alsa_data *data = (alsa_data*)Device->ExtraData;
993 if(Samples <= alsa_available_samples(Device))
994 ReadRingBuffer(data->ring, Buffer, Samples);
995 else
996 alcSetError(Device, ALC_INVALID_VALUE);
1000 BackendFuncs alsa_funcs = {
1001 alsa_open_playback,
1002 alsa_close_playback,
1003 alsa_reset_playback,
1004 alsa_stop_playback,
1005 alsa_open_capture,
1006 alsa_close_capture,
1007 alsa_start_capture,
1008 alsa_stop_capture,
1009 alsa_capture_samples,
1010 alsa_available_samples
1013 void alc_alsa_init(BackendFuncs *func_list)
1015 *func_list = alsa_funcs;
1018 void alc_alsa_deinit(void)
1020 ALuint i;
1022 for(i = 0;i < numDevNames;++i)
1023 free(allDevNameMap[i].name);
1024 free(allDevNameMap);
1025 allDevNameMap = NULL;
1026 numDevNames = 0;
1028 for(i = 0;i < numCaptureDevNames;++i)
1029 free(allCaptureDevNameMap[i].name);
1030 free(allCaptureDevNameMap);
1031 allCaptureDevNameMap = NULL;
1032 numCaptureDevNames = 0;
1034 if(alsa_handle)
1036 #ifdef HAVE_DLFCN_H
1037 dlclose(alsa_handle);
1038 #endif
1039 alsa_handle = NULL;
1043 void alc_alsa_probe(int type)
1045 ALuint i;
1047 if(!alsa_load())
1048 return;
1050 if(type == DEVICE_PROBE)
1051 AppendDeviceList(alsaDevice);
1052 else if(type == ALL_DEVICE_PROBE)
1054 for(i = 0;i < numDevNames;++i)
1055 free(allDevNameMap[i].name);
1057 free(allDevNameMap);
1058 allDevNameMap = probe_devices(SND_PCM_STREAM_PLAYBACK, &numDevNames);
1060 for(i = 0;i < numDevNames;++i)
1061 AppendAllDeviceList(allDevNameMap[i].name);
1063 else if(type == CAPTURE_DEVICE_PROBE)
1065 for(i = 0;i < numCaptureDevNames;++i)
1066 free(allCaptureDevNameMap[i].name);
1068 free(allCaptureDevNameMap);
1069 allCaptureDevNameMap = probe_devices(SND_PCM_STREAM_CAPTURE, &numCaptureDevNames);
1071 for(i = 0;i < numCaptureDevNames;++i)
1072 AppendCaptureDeviceList(allCaptureDevNameMap[i].name);