Don't include alu.h in alMain.h
[openal-soft.git] / Alc / backends / alsa.c
blob035d38e947f25ed1bd76a05299bad84b82753d80
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>
27 #include "alMain.h"
28 #include "alu.h"
30 #include <alsa/asoundlib.h>
33 static const ALCchar alsaDevice[] = "ALSA Default";
36 #ifdef HAVE_DYNLOAD
37 static void *alsa_handle;
38 #define MAKE_FUNC(f) static typeof(f) * p##f
39 MAKE_FUNC(snd_strerror);
40 MAKE_FUNC(snd_pcm_open);
41 MAKE_FUNC(snd_pcm_close);
42 MAKE_FUNC(snd_pcm_nonblock);
43 MAKE_FUNC(snd_pcm_frames_to_bytes);
44 MAKE_FUNC(snd_pcm_bytes_to_frames);
45 MAKE_FUNC(snd_pcm_hw_params_malloc);
46 MAKE_FUNC(snd_pcm_hw_params_free);
47 MAKE_FUNC(snd_pcm_hw_params_any);
48 MAKE_FUNC(snd_pcm_hw_params_current);
49 MAKE_FUNC(snd_pcm_hw_params_set_access);
50 MAKE_FUNC(snd_pcm_hw_params_set_format);
51 MAKE_FUNC(snd_pcm_hw_params_set_channels);
52 MAKE_FUNC(snd_pcm_hw_params_set_periods_near);
53 MAKE_FUNC(snd_pcm_hw_params_set_rate_near);
54 MAKE_FUNC(snd_pcm_hw_params_set_rate);
55 MAKE_FUNC(snd_pcm_hw_params_set_rate_resample);
56 MAKE_FUNC(snd_pcm_hw_params_set_buffer_time_near);
57 MAKE_FUNC(snd_pcm_hw_params_set_period_time_near);
58 MAKE_FUNC(snd_pcm_hw_params_set_buffer_size_near);
59 MAKE_FUNC(snd_pcm_hw_params_set_period_size_near);
60 MAKE_FUNC(snd_pcm_hw_params_set_buffer_size_min);
61 MAKE_FUNC(snd_pcm_hw_params_get_buffer_time_min);
62 MAKE_FUNC(snd_pcm_hw_params_get_buffer_time_max);
63 MAKE_FUNC(snd_pcm_hw_params_get_period_time_min);
64 MAKE_FUNC(snd_pcm_hw_params_get_period_time_max);
65 MAKE_FUNC(snd_pcm_hw_params_get_buffer_size);
66 MAKE_FUNC(snd_pcm_hw_params_get_period_size);
67 MAKE_FUNC(snd_pcm_hw_params_get_access);
68 MAKE_FUNC(snd_pcm_hw_params_get_periods);
69 MAKE_FUNC(snd_pcm_hw_params_test_format);
70 MAKE_FUNC(snd_pcm_hw_params_test_channels);
71 MAKE_FUNC(snd_pcm_hw_params);
72 MAKE_FUNC(snd_pcm_sw_params_malloc);
73 MAKE_FUNC(snd_pcm_sw_params_current);
74 MAKE_FUNC(snd_pcm_sw_params_set_avail_min);
75 MAKE_FUNC(snd_pcm_sw_params_set_stop_threshold);
76 MAKE_FUNC(snd_pcm_sw_params);
77 MAKE_FUNC(snd_pcm_sw_params_free);
78 MAKE_FUNC(snd_pcm_prepare);
79 MAKE_FUNC(snd_pcm_start);
80 MAKE_FUNC(snd_pcm_resume);
81 MAKE_FUNC(snd_pcm_reset);
82 MAKE_FUNC(snd_pcm_wait);
83 MAKE_FUNC(snd_pcm_delay);
84 MAKE_FUNC(snd_pcm_state);
85 MAKE_FUNC(snd_pcm_avail_update);
86 MAKE_FUNC(snd_pcm_areas_silence);
87 MAKE_FUNC(snd_pcm_mmap_begin);
88 MAKE_FUNC(snd_pcm_mmap_commit);
89 MAKE_FUNC(snd_pcm_readi);
90 MAKE_FUNC(snd_pcm_writei);
91 MAKE_FUNC(snd_pcm_drain);
92 MAKE_FUNC(snd_pcm_drop);
93 MAKE_FUNC(snd_pcm_recover);
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_ctl_card_info_get_id);
109 MAKE_FUNC(snd_card_next);
110 MAKE_FUNC(snd_config_update_free_global);
111 #undef MAKE_FUNC
113 #define snd_strerror psnd_strerror
114 #define snd_pcm_open psnd_pcm_open
115 #define snd_pcm_close psnd_pcm_close
116 #define snd_pcm_nonblock psnd_pcm_nonblock
117 #define snd_pcm_frames_to_bytes psnd_pcm_frames_to_bytes
118 #define snd_pcm_bytes_to_frames psnd_pcm_bytes_to_frames
119 #define snd_pcm_hw_params_malloc psnd_pcm_hw_params_malloc
120 #define snd_pcm_hw_params_free psnd_pcm_hw_params_free
121 #define snd_pcm_hw_params_any psnd_pcm_hw_params_any
122 #define snd_pcm_hw_params_current psnd_pcm_hw_params_current
123 #define snd_pcm_hw_params_set_access psnd_pcm_hw_params_set_access
124 #define snd_pcm_hw_params_set_format psnd_pcm_hw_params_set_format
125 #define snd_pcm_hw_params_set_channels psnd_pcm_hw_params_set_channels
126 #define snd_pcm_hw_params_set_periods_near psnd_pcm_hw_params_set_periods_near
127 #define snd_pcm_hw_params_set_rate_near psnd_pcm_hw_params_set_rate_near
128 #define snd_pcm_hw_params_set_rate psnd_pcm_hw_params_set_rate
129 #define snd_pcm_hw_params_set_rate_resample psnd_pcm_hw_params_set_rate_resample
130 #define snd_pcm_hw_params_set_buffer_time_near psnd_pcm_hw_params_set_buffer_time_near
131 #define snd_pcm_hw_params_set_period_time_near psnd_pcm_hw_params_set_period_time_near
132 #define snd_pcm_hw_params_set_buffer_size_near psnd_pcm_hw_params_set_buffer_size_near
133 #define snd_pcm_hw_params_set_period_size_near psnd_pcm_hw_params_set_period_size_near
134 #define snd_pcm_hw_params_set_buffer_size_min psnd_pcm_hw_params_set_buffer_size_min
135 #define snd_pcm_hw_params_get_buffer_time_min psnd_pcm_hw_params_get_buffer_time_min
136 #define snd_pcm_hw_params_get_buffer_time_max psnd_pcm_hw_params_get_buffer_time_max
137 #define snd_pcm_hw_params_get_period_time_min psnd_pcm_hw_params_get_period_time_min
138 #define snd_pcm_hw_params_get_period_time_max psnd_pcm_hw_params_get_period_time_max
139 #define snd_pcm_hw_params_get_buffer_size psnd_pcm_hw_params_get_buffer_size
140 #define snd_pcm_hw_params_get_period_size psnd_pcm_hw_params_get_period_size
141 #define snd_pcm_hw_params_get_access psnd_pcm_hw_params_get_access
142 #define snd_pcm_hw_params_get_periods psnd_pcm_hw_params_get_periods
143 #define snd_pcm_hw_params_test_format psnd_pcm_hw_params_test_format
144 #define snd_pcm_hw_params_test_channels psnd_pcm_hw_params_test_channels
145 #define snd_pcm_hw_params psnd_pcm_hw_params
146 #define snd_pcm_sw_params_malloc psnd_pcm_sw_params_malloc
147 #define snd_pcm_sw_params_current psnd_pcm_sw_params_current
148 #define snd_pcm_sw_params_set_avail_min psnd_pcm_sw_params_set_avail_min
149 #define snd_pcm_sw_params_set_stop_threshold psnd_pcm_sw_params_set_stop_threshold
150 #define snd_pcm_sw_params psnd_pcm_sw_params
151 #define snd_pcm_sw_params_free psnd_pcm_sw_params_free
152 #define snd_pcm_prepare psnd_pcm_prepare
153 #define snd_pcm_start psnd_pcm_start
154 #define snd_pcm_resume psnd_pcm_resume
155 #define snd_pcm_reset psnd_pcm_reset
156 #define snd_pcm_wait psnd_pcm_wait
157 #define snd_pcm_delay psnd_pcm_delay
158 #define snd_pcm_state psnd_pcm_state
159 #define snd_pcm_avail_update psnd_pcm_avail_update
160 #define snd_pcm_areas_silence psnd_pcm_areas_silence
161 #define snd_pcm_mmap_begin psnd_pcm_mmap_begin
162 #define snd_pcm_mmap_commit psnd_pcm_mmap_commit
163 #define snd_pcm_readi psnd_pcm_readi
164 #define snd_pcm_writei psnd_pcm_writei
165 #define snd_pcm_drain psnd_pcm_drain
166 #define snd_pcm_drop psnd_pcm_drop
167 #define snd_pcm_recover psnd_pcm_recover
168 #define snd_pcm_info_malloc psnd_pcm_info_malloc
169 #define snd_pcm_info_free psnd_pcm_info_free
170 #define snd_pcm_info_set_device psnd_pcm_info_set_device
171 #define snd_pcm_info_set_subdevice psnd_pcm_info_set_subdevice
172 #define snd_pcm_info_set_stream psnd_pcm_info_set_stream
173 #define snd_pcm_info_get_name psnd_pcm_info_get_name
174 #define snd_ctl_pcm_next_device psnd_ctl_pcm_next_device
175 #define snd_ctl_pcm_info psnd_ctl_pcm_info
176 #define snd_ctl_open psnd_ctl_open
177 #define snd_ctl_close psnd_ctl_close
178 #define snd_ctl_card_info_malloc psnd_ctl_card_info_malloc
179 #define snd_ctl_card_info_free psnd_ctl_card_info_free
180 #define snd_ctl_card_info psnd_ctl_card_info
181 #define snd_ctl_card_info_get_name psnd_ctl_card_info_get_name
182 #define snd_ctl_card_info_get_id psnd_ctl_card_info_get_id
183 #define snd_card_next psnd_card_next
184 #define snd_config_update_free_global psnd_config_update_free_global
185 #endif
188 static ALCboolean alsa_load(void)
190 #ifdef HAVE_DYNLOAD
191 if(!alsa_handle)
193 alsa_handle = LoadLib("libasound.so.2");
194 if(!alsa_handle)
195 return ALC_FALSE;
197 #define LOAD_FUNC(f) do { \
198 p##f = GetSymbol(alsa_handle, #f); \
199 if(p##f == NULL) { \
200 CloseLib(alsa_handle); \
201 alsa_handle = NULL; \
202 return ALC_FALSE; \
204 } while(0)
205 LOAD_FUNC(snd_strerror);
206 LOAD_FUNC(snd_pcm_open);
207 LOAD_FUNC(snd_pcm_close);
208 LOAD_FUNC(snd_pcm_nonblock);
209 LOAD_FUNC(snd_pcm_frames_to_bytes);
210 LOAD_FUNC(snd_pcm_bytes_to_frames);
211 LOAD_FUNC(snd_pcm_hw_params_malloc);
212 LOAD_FUNC(snd_pcm_hw_params_free);
213 LOAD_FUNC(snd_pcm_hw_params_any);
214 LOAD_FUNC(snd_pcm_hw_params_current);
215 LOAD_FUNC(snd_pcm_hw_params_set_access);
216 LOAD_FUNC(snd_pcm_hw_params_set_format);
217 LOAD_FUNC(snd_pcm_hw_params_set_channels);
218 LOAD_FUNC(snd_pcm_hw_params_set_periods_near);
219 LOAD_FUNC(snd_pcm_hw_params_set_rate_near);
220 LOAD_FUNC(snd_pcm_hw_params_set_rate);
221 LOAD_FUNC(snd_pcm_hw_params_set_rate_resample);
222 LOAD_FUNC(snd_pcm_hw_params_set_buffer_time_near);
223 LOAD_FUNC(snd_pcm_hw_params_set_period_time_near);
224 LOAD_FUNC(snd_pcm_hw_params_set_buffer_size_near);
225 LOAD_FUNC(snd_pcm_hw_params_set_buffer_size_min);
226 LOAD_FUNC(snd_pcm_hw_params_set_period_size_near);
227 LOAD_FUNC(snd_pcm_hw_params_get_buffer_time_min);
228 LOAD_FUNC(snd_pcm_hw_params_get_buffer_time_max);
229 LOAD_FUNC(snd_pcm_hw_params_get_period_time_min);
230 LOAD_FUNC(snd_pcm_hw_params_get_period_time_max);
231 LOAD_FUNC(snd_pcm_hw_params_get_buffer_size);
232 LOAD_FUNC(snd_pcm_hw_params_get_period_size);
233 LOAD_FUNC(snd_pcm_hw_params_get_access);
234 LOAD_FUNC(snd_pcm_hw_params_get_periods);
235 LOAD_FUNC(snd_pcm_hw_params_test_format);
236 LOAD_FUNC(snd_pcm_hw_params_test_channels);
237 LOAD_FUNC(snd_pcm_hw_params);
238 LOAD_FUNC(snd_pcm_sw_params_malloc);
239 LOAD_FUNC(snd_pcm_sw_params_current);
240 LOAD_FUNC(snd_pcm_sw_params_set_avail_min);
241 LOAD_FUNC(snd_pcm_sw_params_set_stop_threshold);
242 LOAD_FUNC(snd_pcm_sw_params);
243 LOAD_FUNC(snd_pcm_sw_params_free);
244 LOAD_FUNC(snd_pcm_prepare);
245 LOAD_FUNC(snd_pcm_start);
246 LOAD_FUNC(snd_pcm_resume);
247 LOAD_FUNC(snd_pcm_reset);
248 LOAD_FUNC(snd_pcm_wait);
249 LOAD_FUNC(snd_pcm_delay);
250 LOAD_FUNC(snd_pcm_state);
251 LOAD_FUNC(snd_pcm_avail_update);
252 LOAD_FUNC(snd_pcm_areas_silence);
253 LOAD_FUNC(snd_pcm_mmap_begin);
254 LOAD_FUNC(snd_pcm_mmap_commit);
255 LOAD_FUNC(snd_pcm_readi);
256 LOAD_FUNC(snd_pcm_writei);
257 LOAD_FUNC(snd_pcm_drain);
258 LOAD_FUNC(snd_pcm_drop);
259 LOAD_FUNC(snd_pcm_recover);
260 LOAD_FUNC(snd_pcm_info_malloc);
261 LOAD_FUNC(snd_pcm_info_free);
262 LOAD_FUNC(snd_pcm_info_set_device);
263 LOAD_FUNC(snd_pcm_info_set_subdevice);
264 LOAD_FUNC(snd_pcm_info_set_stream);
265 LOAD_FUNC(snd_pcm_info_get_name);
266 LOAD_FUNC(snd_ctl_pcm_next_device);
267 LOAD_FUNC(snd_ctl_pcm_info);
268 LOAD_FUNC(snd_ctl_open);
269 LOAD_FUNC(snd_ctl_close);
270 LOAD_FUNC(snd_ctl_card_info_malloc);
271 LOAD_FUNC(snd_ctl_card_info_free);
272 LOAD_FUNC(snd_ctl_card_info);
273 LOAD_FUNC(snd_ctl_card_info_get_name);
274 LOAD_FUNC(snd_ctl_card_info_get_id);
275 LOAD_FUNC(snd_card_next);
276 LOAD_FUNC(snd_config_update_free_global);
277 #undef LOAD_FUNC
279 #endif
280 return ALC_TRUE;
284 typedef struct {
285 snd_pcm_t *pcmHandle;
287 ALvoid *buffer;
288 ALsizei size;
290 ALboolean doCapture;
291 RingBuffer *ring;
293 snd_pcm_sframes_t last_avail;
295 volatile int killNow;
296 ALvoid *thread;
297 } alsa_data;
299 typedef struct {
300 ALCchar *name;
301 char *device;
302 } DevMap;
304 static DevMap *allDevNameMap;
305 static ALuint numDevNames;
306 static DevMap *allCaptureDevNameMap;
307 static ALuint numCaptureDevNames;
310 static const char *prefix_name(snd_pcm_stream_t stream)
312 assert(stream == SND_PCM_STREAM_PLAYBACK || stream == SND_PCM_STREAM_CAPTURE);
313 return (stream==SND_PCM_STREAM_PLAYBACK) ? "device-prefix" : "capture-prefix";
316 static DevMap *probe_devices(snd_pcm_stream_t stream, ALuint *count)
318 const char *main_prefix = "plughw:";
319 snd_ctl_t *handle;
320 int card, err, dev, idx;
321 snd_ctl_card_info_t *info;
322 snd_pcm_info_t *pcminfo;
323 DevMap *DevList;
325 snd_ctl_card_info_malloc(&info);
326 snd_pcm_info_malloc(&pcminfo);
328 DevList = malloc(sizeof(DevMap) * 1);
329 DevList[0].name = strdup(alsaDevice);
330 DevList[0].device = strdup(GetConfigValue("alsa", (stream==SND_PCM_STREAM_PLAYBACK) ?
331 "device" : "capture", "default"));
332 idx = 1;
334 card = -1;
335 if((err=snd_card_next(&card)) < 0)
336 ERR("Failed to find a card: %s\n", snd_strerror(err));
337 ConfigValueStr("alsa", prefix_name(stream), &main_prefix);
338 while(card >= 0)
340 const char *card_prefix = main_prefix;
341 const char *cardname, *cardid;
342 char name[256];
344 snprintf(name, sizeof(name), "hw:%d", card);
345 if((err = snd_ctl_open(&handle, name, 0)) < 0)
347 ERR("control open (hw:%d): %s\n", card, snd_strerror(err));
348 goto next_card;
350 if((err = snd_ctl_card_info(handle, info)) < 0)
352 ERR("control hardware info (hw:%d): %s\n", card, snd_strerror(err));
353 snd_ctl_close(handle);
354 goto next_card;
357 cardname = snd_ctl_card_info_get_name(info);
358 cardid = snd_ctl_card_info_get_id(info);
360 snprintf(name, sizeof(name), "%s-%s", prefix_name(stream), cardid);
361 ConfigValueStr("alsa", name, &card_prefix);
363 dev = -1;
364 while(1)
366 const char *devname;
367 void *temp;
369 if(snd_ctl_pcm_next_device(handle, &dev) < 0)
370 ERR("snd_ctl_pcm_next_device failed\n");
371 if(dev < 0)
372 break;
374 snd_pcm_info_set_device(pcminfo, dev);
375 snd_pcm_info_set_subdevice(pcminfo, 0);
376 snd_pcm_info_set_stream(pcminfo, stream);
377 if((err = snd_ctl_pcm_info(handle, pcminfo)) < 0) {
378 if(err != -ENOENT)
379 ERR("control digital audio info (hw:%d): %s\n", card, snd_strerror(err));
380 continue;
383 temp = realloc(DevList, sizeof(DevMap) * (idx+1));
384 if(temp)
386 const char *device_prefix = card_prefix;
387 char device[128];
389 DevList = temp;
390 devname = snd_pcm_info_get_name(pcminfo);
392 snprintf(name, sizeof(name), "%s-%s-%d", prefix_name(stream), cardid, dev);
393 ConfigValueStr("alsa", name, &device_prefix);
395 snprintf(name, sizeof(name), "%s, %s (CARD=%s,DEV=%d)",
396 cardname, devname, cardid, dev);
397 snprintf(device, sizeof(device), "%sCARD=%s,DEV=%d",
398 device_prefix, cardid, dev);
400 TRACE("Got device \"%s\", \"%s\"\n", name, device);
401 DevList[idx].name = strdup(name);
402 DevList[idx].device = strdup(device);
403 idx++;
406 snd_ctl_close(handle);
407 next_card:
408 if(snd_card_next(&card) < 0) {
409 ERR("snd_card_next failed\n");
410 break;
414 snd_pcm_info_free(pcminfo);
415 snd_ctl_card_info_free(info);
417 *count = idx;
418 return DevList;
422 static int verify_state(snd_pcm_t *handle)
424 snd_pcm_state_t state = snd_pcm_state(handle);
425 int err;
427 switch(state)
429 case SND_PCM_STATE_OPEN:
430 case SND_PCM_STATE_SETUP:
431 case SND_PCM_STATE_PREPARED:
432 case SND_PCM_STATE_RUNNING:
433 case SND_PCM_STATE_DRAINING:
434 case SND_PCM_STATE_PAUSED:
435 /* All Okay */
436 break;
438 case SND_PCM_STATE_XRUN:
439 if((err=snd_pcm_recover(handle, -EPIPE, 1)) < 0)
440 return err;
441 break;
442 case SND_PCM_STATE_SUSPENDED:
443 if((err=snd_pcm_recover(handle, -ESTRPIPE, 1)) < 0)
444 return err;
445 break;
446 case SND_PCM_STATE_DISCONNECTED:
447 return -ENODEV;
450 return state;
454 static ALuint ALSAProc(ALvoid *ptr)
456 ALCdevice *Device = (ALCdevice*)ptr;
457 alsa_data *data = (alsa_data*)Device->ExtraData;
458 const snd_pcm_channel_area_t *areas = NULL;
459 snd_pcm_uframes_t update_size, num_updates;
460 snd_pcm_sframes_t avail, commitres;
461 snd_pcm_uframes_t offset, frames;
462 char *WritePtr;
463 int err;
465 SetRTPriority();
467 update_size = Device->UpdateSize;
468 num_updates = Device->NumUpdates;
469 while(!data->killNow)
471 int state = verify_state(data->pcmHandle);
472 if(state < 0)
474 ERR("Invalid state detected: %s\n", snd_strerror(state));
475 aluHandleDisconnect(Device);
476 break;
479 avail = snd_pcm_avail_update(data->pcmHandle);
480 if(avail < 0)
482 ERR("available update failed: %s\n", snd_strerror(avail));
483 continue;
486 if((snd_pcm_uframes_t)avail > update_size*(num_updates+1))
488 WARN("available samples exceeds the buffer size\n");
489 snd_pcm_reset(data->pcmHandle);
490 continue;
493 // make sure there's frames to process
494 if((snd_pcm_uframes_t)avail < update_size)
496 if(state != SND_PCM_STATE_RUNNING)
498 err = snd_pcm_start(data->pcmHandle);
499 if(err < 0)
501 ERR("start failed: %s\n", snd_strerror(err));
502 continue;
505 if(snd_pcm_wait(data->pcmHandle, 1000) == 0)
506 ERR("Wait timeout... buffer size too low?\n");
507 continue;
509 avail -= avail%update_size;
511 // it is possible that contiguous areas are smaller, thus we use a loop
512 ALCdevice_Lock(Device);
513 while(avail > 0)
515 frames = avail;
517 err = snd_pcm_mmap_begin(data->pcmHandle, &areas, &offset, &frames);
518 if(err < 0)
520 ERR("mmap begin error: %s\n", snd_strerror(err));
521 break;
524 WritePtr = (char*)areas->addr + (offset * areas->step / 8);
525 aluMixData(Device, WritePtr, frames);
527 commitres = snd_pcm_mmap_commit(data->pcmHandle, offset, frames);
528 if(commitres < 0 || (commitres-frames) != 0)
530 ERR("mmap commit error: %s\n",
531 snd_strerror(commitres >= 0 ? -EPIPE : commitres));
532 break;
535 avail -= frames;
537 ALCdevice_Unlock(Device);
540 return 0;
543 static ALuint ALSANoMMapProc(ALvoid *ptr)
545 ALCdevice *Device = (ALCdevice*)ptr;
546 alsa_data *data = (alsa_data*)Device->ExtraData;
547 snd_pcm_uframes_t update_size, num_updates;
548 snd_pcm_sframes_t avail;
549 char *WritePtr;
550 int err;
552 SetRTPriority();
554 update_size = Device->UpdateSize;
555 num_updates = Device->NumUpdates;
556 while(!data->killNow)
558 int state = verify_state(data->pcmHandle);
559 if(state < 0)
561 ERR("Invalid state detected: %s\n", snd_strerror(state));
562 aluHandleDisconnect(Device);
563 break;
566 avail = snd_pcm_avail_update(data->pcmHandle);
567 if(avail < 0)
569 ERR("available update failed: %s\n", snd_strerror(avail));
570 continue;
573 if((snd_pcm_uframes_t)avail > update_size*num_updates)
575 WARN("available samples exceeds the buffer size\n");
576 snd_pcm_reset(data->pcmHandle);
577 continue;
580 if((snd_pcm_uframes_t)avail < update_size)
582 if(state != SND_PCM_STATE_RUNNING)
584 err = snd_pcm_start(data->pcmHandle);
585 if(err < 0)
587 ERR("start failed: %s\n", snd_strerror(err));
588 continue;
591 if(snd_pcm_wait(data->pcmHandle, 1000) == 0)
592 ERR("Wait timeout... buffer size too low?\n");
593 continue;
596 ALCdevice_Lock(Device);
597 WritePtr = data->buffer;
598 avail = snd_pcm_bytes_to_frames(data->pcmHandle, data->size);
599 aluMixData(Device, WritePtr, avail);
601 while(avail > 0)
603 int ret = snd_pcm_writei(data->pcmHandle, WritePtr, avail);
604 switch (ret)
606 case -EAGAIN:
607 continue;
608 case -ESTRPIPE:
609 case -EPIPE:
610 case -EINTR:
611 ret = snd_pcm_recover(data->pcmHandle, ret, 1);
612 if(ret < 0)
613 avail = 0;
614 break;
615 default:
616 if (ret >= 0)
618 WritePtr += snd_pcm_frames_to_bytes(data->pcmHandle, ret);
619 avail -= ret;
621 break;
623 if (ret < 0)
625 ret = snd_pcm_prepare(data->pcmHandle);
626 if(ret < 0)
627 break;
630 ALCdevice_Unlock(Device);
633 return 0;
636 static ALCenum alsa_open_playback(ALCdevice *device, const ALCchar *deviceName)
638 const char *driver = NULL;
639 alsa_data *data;
640 int err;
642 if(deviceName)
644 size_t idx;
646 if(!allDevNameMap)
647 allDevNameMap = probe_devices(SND_PCM_STREAM_PLAYBACK, &numDevNames);
649 for(idx = 0;idx < numDevNames;idx++)
651 if(strcmp(deviceName, allDevNameMap[idx].name) == 0)
653 driver = allDevNameMap[idx].device;
654 break;
657 if(idx == numDevNames)
658 return ALC_INVALID_VALUE;
660 else
662 deviceName = alsaDevice;
663 driver = GetConfigValue("alsa", "device", "default");
666 data = (alsa_data*)calloc(1, sizeof(alsa_data));
668 err = snd_pcm_open(&data->pcmHandle, driver, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
669 if(err < 0)
671 free(data);
672 ERR("Could not open playback device '%s': %s\n", driver, snd_strerror(err));
673 return ALC_OUT_OF_MEMORY;
676 /* Free alsa's global config tree. Otherwise valgrind reports a ton of leaks. */
677 snd_config_update_free_global();
679 device->DeviceName = strdup(deviceName);
680 device->ExtraData = data;
681 return ALC_NO_ERROR;
684 static void alsa_close_playback(ALCdevice *device)
686 alsa_data *data = (alsa_data*)device->ExtraData;
688 snd_pcm_close(data->pcmHandle);
689 free(data);
690 device->ExtraData = NULL;
693 static ALCboolean alsa_reset_playback(ALCdevice *device)
695 alsa_data *data = (alsa_data*)device->ExtraData;
696 snd_pcm_uframes_t periodSizeInFrames;
697 unsigned int periodLen, bufferLen;
698 snd_pcm_sw_params_t *sp = NULL;
699 snd_pcm_hw_params_t *hp = NULL;
700 snd_pcm_access_t access;
701 snd_pcm_format_t format;
702 unsigned int periods;
703 unsigned int rate;
704 const char *funcerr;
705 int allowmmap;
706 int err;
708 format = -1;
709 switch(device->FmtType)
711 case DevFmtByte:
712 format = SND_PCM_FORMAT_S8;
713 break;
714 case DevFmtUByte:
715 format = SND_PCM_FORMAT_U8;
716 break;
717 case DevFmtShort:
718 format = SND_PCM_FORMAT_S16;
719 break;
720 case DevFmtUShort:
721 format = SND_PCM_FORMAT_U16;
722 break;
723 case DevFmtInt:
724 format = SND_PCM_FORMAT_S32;
725 break;
726 case DevFmtUInt:
727 format = SND_PCM_FORMAT_U32;
728 break;
729 case DevFmtFloat:
730 format = SND_PCM_FORMAT_FLOAT;
731 break;
734 allowmmap = GetConfigValueBool("alsa", "mmap", 1);
735 periods = device->NumUpdates;
736 periodLen = (ALuint64)device->UpdateSize * 1000000 / device->Frequency;
737 bufferLen = periodLen * periods;
738 rate = device->Frequency;
740 snd_pcm_hw_params_malloc(&hp);
741 #define CHECK(x) if((funcerr=#x),(err=(x)) < 0) goto error
742 CHECK(snd_pcm_hw_params_any(data->pcmHandle, hp));
743 /* set interleaved access */
744 if(!allowmmap || snd_pcm_hw_params_set_access(data->pcmHandle, hp, SND_PCM_ACCESS_MMAP_INTERLEAVED) < 0)
746 /* No mmap */
747 CHECK(snd_pcm_hw_params_set_access(data->pcmHandle, hp, SND_PCM_ACCESS_RW_INTERLEAVED));
749 /* test and set format (implicitly sets sample bits) */
750 if(snd_pcm_hw_params_test_format(data->pcmHandle, hp, format) < 0)
752 static const struct {
753 snd_pcm_format_t format;
754 enum DevFmtType fmttype;
755 } formatlist[] = {
756 { SND_PCM_FORMAT_FLOAT, DevFmtFloat },
757 { SND_PCM_FORMAT_S32, DevFmtInt },
758 { SND_PCM_FORMAT_U32, DevFmtUInt },
759 { SND_PCM_FORMAT_S16, DevFmtShort },
760 { SND_PCM_FORMAT_U16, DevFmtUShort },
761 { SND_PCM_FORMAT_S8, DevFmtByte },
762 { SND_PCM_FORMAT_U8, DevFmtUByte },
764 size_t k;
766 for(k = 0;k < COUNTOF(formatlist);k++)
768 format = formatlist[k].format;
769 if(snd_pcm_hw_params_test_format(data->pcmHandle, hp, format) >= 0)
771 device->FmtType = formatlist[k].fmttype;
772 break;
776 CHECK(snd_pcm_hw_params_set_format(data->pcmHandle, hp, format));
777 /* test and set channels (implicitly sets frame bits) */
778 if(snd_pcm_hw_params_test_channels(data->pcmHandle, hp, ChannelsFromDevFmt(device->FmtChans)) < 0)
780 static const enum DevFmtChannels channellist[] = {
781 DevFmtStereo,
782 DevFmtQuad,
783 DevFmtX51,
784 DevFmtX71,
785 DevFmtMono,
787 size_t k;
789 for(k = 0;k < COUNTOF(channellist);k++)
791 if(snd_pcm_hw_params_test_channels(data->pcmHandle, hp, ChannelsFromDevFmt(channellist[k])) >= 0)
793 device->FmtChans = channellist[k];
794 break;
798 CHECK(snd_pcm_hw_params_set_channels(data->pcmHandle, hp, ChannelsFromDevFmt(device->FmtChans)));
799 /* set rate (implicitly constrains period/buffer parameters) */
800 if(snd_pcm_hw_params_set_rate_resample(data->pcmHandle, hp, 0) < 0)
801 ERR("Failed to disable ALSA resampler\n");
802 CHECK(snd_pcm_hw_params_set_rate_near(data->pcmHandle, hp, &rate, NULL));
803 /* set buffer time (implicitly constrains period/buffer parameters) */
804 if(snd_pcm_hw_params_set_buffer_time_near(data->pcmHandle, hp, &bufferLen, NULL) < 0)
805 ERR("snd_pcm_hw_params_set_buffer_time_near failed: %s\n", snd_strerror(err));
806 /* set period time (implicitly sets buffer size/bytes/time and period size/bytes) */
807 if(snd_pcm_hw_params_set_period_time_near(data->pcmHandle, hp, &periodLen, NULL) < 0)
808 ERR("snd_pcm_hw_params_set_period_time_near failed: %s\n", snd_strerror(err));
809 /* install and prepare hardware configuration */
810 CHECK(snd_pcm_hw_params(data->pcmHandle, hp));
811 /* retrieve configuration info */
812 CHECK(snd_pcm_hw_params_get_access(hp, &access));
813 CHECK(snd_pcm_hw_params_get_period_size(hp, &periodSizeInFrames, NULL));
814 CHECK(snd_pcm_hw_params_get_periods(hp, &periods, NULL));
816 snd_pcm_hw_params_free(hp);
817 hp = NULL;
818 snd_pcm_sw_params_malloc(&sp);
820 CHECK(snd_pcm_sw_params_current(data->pcmHandle, sp));
821 CHECK(snd_pcm_sw_params_set_avail_min(data->pcmHandle, sp, periodSizeInFrames));
822 CHECK(snd_pcm_sw_params_set_stop_threshold(data->pcmHandle, sp, periodSizeInFrames*periods));
823 CHECK(snd_pcm_sw_params(data->pcmHandle, sp));
824 #undef CHECK
825 snd_pcm_sw_params_free(sp);
826 sp = NULL;
828 device->NumUpdates = periods;
829 device->UpdateSize = periodSizeInFrames;
830 device->Frequency = rate;
832 SetDefaultChannelOrder(device);
834 return ALC_TRUE;
836 error:
837 ERR("%s failed: %s\n", funcerr, snd_strerror(err));
838 if(hp) snd_pcm_hw_params_free(hp);
839 if(sp) snd_pcm_sw_params_free(sp);
840 return ALC_FALSE;
843 static ALCboolean alsa_start_playback(ALCdevice *device)
845 alsa_data *data = (alsa_data*)device->ExtraData;
846 snd_pcm_hw_params_t *hp = NULL;
847 snd_pcm_access_t access;
848 const char *funcerr;
849 int err;
851 snd_pcm_hw_params_malloc(&hp);
852 #define CHECK(x) if((funcerr=#x),(err=(x)) < 0) goto error
853 CHECK(snd_pcm_hw_params_current(data->pcmHandle, hp));
854 /* retrieve configuration info */
855 CHECK(snd_pcm_hw_params_get_access(hp, &access));
856 #undef CHECK
857 snd_pcm_hw_params_free(hp);
858 hp = NULL;
860 data->size = snd_pcm_frames_to_bytes(data->pcmHandle, device->UpdateSize);
861 if(access == SND_PCM_ACCESS_RW_INTERLEAVED)
863 data->buffer = malloc(data->size);
864 if(!data->buffer)
866 ERR("buffer malloc failed\n");
867 return ALC_FALSE;
869 data->thread = StartThread(ALSANoMMapProc, device);
871 else
873 err = snd_pcm_prepare(data->pcmHandle);
874 if(err < 0)
876 ERR("snd_pcm_prepare(data->pcmHandle) failed: %s\n", snd_strerror(err));
877 return ALC_FALSE;
879 data->thread = StartThread(ALSAProc, device);
881 if(data->thread == NULL)
883 ERR("Could not create playback thread\n");
884 free(data->buffer);
885 data->buffer = NULL;
886 return ALC_FALSE;
889 return ALC_TRUE;
891 error:
892 ERR("%s failed: %s\n", funcerr, snd_strerror(err));
893 if(hp) snd_pcm_hw_params_free(hp);
894 return ALC_FALSE;
897 static void alsa_stop_playback(ALCdevice *device)
899 alsa_data *data = (alsa_data*)device->ExtraData;
901 if(data->thread)
903 data->killNow = 1;
904 StopThread(data->thread);
905 data->thread = NULL;
907 data->killNow = 0;
908 free(data->buffer);
909 data->buffer = NULL;
913 static ALCenum alsa_open_capture(ALCdevice *Device, const ALCchar *deviceName)
915 const char *driver = NULL;
916 snd_pcm_hw_params_t *hp;
917 snd_pcm_uframes_t bufferSizeInFrames;
918 snd_pcm_uframes_t periodSizeInFrames;
919 ALboolean needring = AL_FALSE;
920 snd_pcm_format_t format;
921 const char *funcerr;
922 alsa_data *data;
923 int err;
925 if(deviceName)
927 size_t idx;
929 if(!allCaptureDevNameMap)
930 allCaptureDevNameMap = probe_devices(SND_PCM_STREAM_CAPTURE, &numCaptureDevNames);
932 for(idx = 0;idx < numCaptureDevNames;idx++)
934 if(strcmp(deviceName, allCaptureDevNameMap[idx].name) == 0)
936 driver = allCaptureDevNameMap[idx].device;
937 break;
940 if(idx == numCaptureDevNames)
941 return ALC_INVALID_VALUE;
943 else
945 deviceName = alsaDevice;
946 driver = GetConfigValue("alsa", "capture", "default");
949 data = (alsa_data*)calloc(1, sizeof(alsa_data));
951 err = snd_pcm_open(&data->pcmHandle, driver, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
952 if(err < 0)
954 ERR("Could not open capture device '%s': %s\n", driver, snd_strerror(err));
955 free(data);
956 return ALC_INVALID_VALUE;
959 /* Free alsa's global config tree. Otherwise valgrind reports a ton of leaks. */
960 snd_config_update_free_global();
962 format = -1;
963 switch(Device->FmtType)
965 case DevFmtByte:
966 format = SND_PCM_FORMAT_S8;
967 break;
968 case DevFmtUByte:
969 format = SND_PCM_FORMAT_U8;
970 break;
971 case DevFmtShort:
972 format = SND_PCM_FORMAT_S16;
973 break;
974 case DevFmtUShort:
975 format = SND_PCM_FORMAT_U16;
976 break;
977 case DevFmtInt:
978 format = SND_PCM_FORMAT_S32;
979 break;
980 case DevFmtUInt:
981 format = SND_PCM_FORMAT_U32;
982 break;
983 case DevFmtFloat:
984 format = SND_PCM_FORMAT_FLOAT;
985 break;
988 funcerr = NULL;
989 bufferSizeInFrames = maxu(Device->UpdateSize*Device->NumUpdates,
990 100*Device->Frequency/1000);
991 periodSizeInFrames = minu(bufferSizeInFrames, 25*Device->Frequency/1000);
993 snd_pcm_hw_params_malloc(&hp);
994 #define CHECK(x) if((funcerr=#x),(err=(x)) < 0) goto error
995 CHECK(snd_pcm_hw_params_any(data->pcmHandle, hp));
996 /* set interleaved access */
997 CHECK(snd_pcm_hw_params_set_access(data->pcmHandle, hp, SND_PCM_ACCESS_RW_INTERLEAVED));
998 /* set format (implicitly sets sample bits) */
999 CHECK(snd_pcm_hw_params_set_format(data->pcmHandle, hp, format));
1000 /* set channels (implicitly sets frame bits) */
1001 CHECK(snd_pcm_hw_params_set_channels(data->pcmHandle, hp, ChannelsFromDevFmt(Device->FmtChans)));
1002 /* set rate (implicitly constrains period/buffer parameters) */
1003 CHECK(snd_pcm_hw_params_set_rate(data->pcmHandle, hp, Device->Frequency, 0));
1004 /* set buffer size in frame units (implicitly sets period size/bytes/time and buffer time/bytes) */
1005 if(snd_pcm_hw_params_set_buffer_size_min(data->pcmHandle, hp, &bufferSizeInFrames) < 0)
1007 TRACE("Buffer too large, using intermediate ring buffer\n");
1008 needring = AL_TRUE;
1009 CHECK(snd_pcm_hw_params_set_buffer_size_near(data->pcmHandle, hp, &bufferSizeInFrames));
1011 /* set buffer size in frame units (implicitly sets period size/bytes/time and buffer time/bytes) */
1012 CHECK(snd_pcm_hw_params_set_period_size_near(data->pcmHandle, hp, &periodSizeInFrames, NULL));
1013 /* install and prepare hardware configuration */
1014 CHECK(snd_pcm_hw_params(data->pcmHandle, hp));
1015 /* retrieve configuration info */
1016 CHECK(snd_pcm_hw_params_get_period_size(hp, &periodSizeInFrames, NULL));
1017 #undef CHECK
1018 snd_pcm_hw_params_free(hp);
1019 hp = NULL;
1021 if(needring)
1023 data->ring = CreateRingBuffer(FrameSizeFromDevFmt(Device->FmtChans, Device->FmtType),
1024 Device->UpdateSize*Device->NumUpdates);
1025 if(!data->ring)
1027 ERR("ring buffer create failed\n");
1028 goto error2;
1031 data->size = snd_pcm_frames_to_bytes(data->pcmHandle, periodSizeInFrames);
1032 data->buffer = malloc(data->size);
1033 if(!data->buffer)
1035 ERR("buffer malloc failed\n");
1036 goto error2;
1040 Device->DeviceName = strdup(deviceName);
1042 Device->ExtraData = data;
1043 return ALC_NO_ERROR;
1045 error:
1046 ERR("%s failed: %s\n", funcerr, snd_strerror(err));
1047 if(hp) snd_pcm_hw_params_free(hp);
1049 error2:
1050 free(data->buffer);
1051 DestroyRingBuffer(data->ring);
1052 snd_pcm_close(data->pcmHandle);
1053 free(data);
1055 Device->ExtraData = NULL;
1056 return ALC_INVALID_VALUE;
1059 static void alsa_close_capture(ALCdevice *Device)
1061 alsa_data *data = (alsa_data*)Device->ExtraData;
1063 snd_pcm_close(data->pcmHandle);
1064 DestroyRingBuffer(data->ring);
1066 free(data->buffer);
1067 free(data);
1068 Device->ExtraData = NULL;
1071 static void alsa_start_capture(ALCdevice *Device)
1073 alsa_data *data = (alsa_data*)Device->ExtraData;
1074 int err;
1076 err = snd_pcm_start(data->pcmHandle);
1077 if(err < 0)
1079 ERR("start failed: %s\n", snd_strerror(err));
1080 aluHandleDisconnect(Device);
1082 else
1083 data->doCapture = AL_TRUE;
1086 static ALCenum alsa_capture_samples(ALCdevice *Device, ALCvoid *Buffer, ALCuint Samples)
1088 alsa_data *data = (alsa_data*)Device->ExtraData;
1090 if(data->ring)
1092 ReadRingBuffer(data->ring, Buffer, Samples);
1093 return ALC_NO_ERROR;
1096 data->last_avail -= Samples;
1097 while(Device->Connected && Samples > 0)
1099 snd_pcm_sframes_t amt = 0;
1101 if(data->size > 0)
1103 /* First get any data stored from the last stop */
1104 amt = snd_pcm_bytes_to_frames(data->pcmHandle, data->size);
1105 if((snd_pcm_uframes_t)amt > Samples) amt = Samples;
1107 amt = snd_pcm_frames_to_bytes(data->pcmHandle, amt);
1108 memmove(Buffer, data->buffer, amt);
1110 if(data->size > amt)
1112 memmove(data->buffer, data->buffer+amt, data->size - amt);
1113 data->size -= amt;
1115 else
1117 free(data->buffer);
1118 data->buffer = NULL;
1119 data->size = 0;
1121 amt = snd_pcm_bytes_to_frames(data->pcmHandle, amt);
1123 else if(data->doCapture)
1124 amt = snd_pcm_readi(data->pcmHandle, Buffer, Samples);
1125 if(amt < 0)
1127 ERR("read error: %s\n", snd_strerror(amt));
1129 if(amt == -EAGAIN)
1130 continue;
1131 if((amt=snd_pcm_recover(data->pcmHandle, amt, 1)) >= 0)
1133 amt = snd_pcm_start(data->pcmHandle);
1134 if(amt >= 0)
1135 amt = snd_pcm_avail_update(data->pcmHandle);
1137 if(amt < 0)
1139 ERR("restore error: %s\n", snd_strerror(amt));
1140 aluHandleDisconnect(Device);
1141 break;
1143 /* If the amount available is less than what's asked, we lost it
1144 * during recovery. So just give silence instead. */
1145 if((snd_pcm_uframes_t)amt < Samples)
1146 break;
1147 continue;
1150 Buffer = (ALbyte*)Buffer + amt;
1151 Samples -= amt;
1153 if(Samples > 0)
1154 memset(Buffer, ((Device->FmtType == DevFmtUByte) ? 0x80 : 0),
1155 snd_pcm_frames_to_bytes(data->pcmHandle, Samples));
1157 return ALC_NO_ERROR;
1160 static ALCuint alsa_available_samples(ALCdevice *Device)
1162 alsa_data *data = (alsa_data*)Device->ExtraData;
1163 snd_pcm_sframes_t avail = 0;
1165 if(Device->Connected && data->doCapture)
1166 avail = snd_pcm_avail_update(data->pcmHandle);
1167 if(avail < 0)
1169 ERR("avail update failed: %s\n", snd_strerror(avail));
1171 if((avail=snd_pcm_recover(data->pcmHandle, avail, 1)) >= 0)
1173 if(data->doCapture)
1174 avail = snd_pcm_start(data->pcmHandle);
1175 if(avail >= 0)
1176 avail = snd_pcm_avail_update(data->pcmHandle);
1178 if(avail < 0)
1180 ERR("restore error: %s\n", snd_strerror(avail));
1181 aluHandleDisconnect(Device);
1185 if(!data->ring)
1187 if(avail < 0) avail = 0;
1188 avail += snd_pcm_bytes_to_frames(data->pcmHandle, data->size);
1189 if(avail > data->last_avail) data->last_avail = avail;
1190 return data->last_avail;
1193 while(avail > 0)
1195 snd_pcm_sframes_t amt;
1197 amt = snd_pcm_bytes_to_frames(data->pcmHandle, data->size);
1198 if(avail < amt) amt = avail;
1200 amt = snd_pcm_readi(data->pcmHandle, data->buffer, amt);
1201 if(amt < 0)
1203 ERR("read error: %s\n", snd_strerror(amt));
1205 if(amt == -EAGAIN)
1206 continue;
1207 if((amt=snd_pcm_recover(data->pcmHandle, amt, 1)) >= 0)
1209 if(data->doCapture)
1210 amt = snd_pcm_start(data->pcmHandle);
1211 if(amt >= 0)
1212 amt = snd_pcm_avail_update(data->pcmHandle);
1214 if(amt < 0)
1216 ERR("restore error: %s\n", snd_strerror(amt));
1217 aluHandleDisconnect(Device);
1218 break;
1220 avail = amt;
1221 continue;
1224 WriteRingBuffer(data->ring, data->buffer, amt);
1225 avail -= amt;
1228 return RingBufferSize(data->ring);
1231 static void alsa_stop_capture(ALCdevice *Device)
1233 alsa_data *data = (alsa_data*)Device->ExtraData;
1234 ALCuint avail;
1235 int err;
1237 /* OpenAL requires access to unread audio after stopping, but ALSA's
1238 * snd_pcm_drain is unreliable and snd_pcm_drop drops it. Capture what's
1239 * available now so it'll be available later after the drop. */
1240 avail = alsa_available_samples(Device);
1241 if(!data->ring && avail > 0)
1243 /* The ring buffer implicitly captures when checking availability.
1244 * Direct access needs to explicitly capture it into temp storage. */
1245 ALsizei size;
1246 void *ptr;
1248 size = snd_pcm_frames_to_bytes(data->pcmHandle, avail);
1249 ptr = realloc(data->buffer, size);
1250 if(ptr)
1252 data->buffer = ptr;
1253 alsa_capture_samples(Device, data->buffer, avail);
1254 data->size = size;
1257 err = snd_pcm_drop(data->pcmHandle);
1258 if(err < 0)
1259 ERR("drop failed: %s\n", snd_strerror(err));
1260 data->doCapture = AL_FALSE;
1264 static ALint64 alsa_get_latency(ALCdevice *device)
1266 alsa_data *data = (alsa_data*)device->ExtraData;
1267 snd_pcm_sframes_t delay = 0;
1268 int err;
1270 if((err=snd_pcm_delay(data->pcmHandle, &delay)) < 0)
1272 ERR("Failed to get pcm delay: %s\n", snd_strerror(err));
1273 return 0;
1275 return maxi64((ALint64)delay*1000000000/device->Frequency, 0);
1279 static const BackendFuncs alsa_funcs = {
1280 alsa_open_playback,
1281 alsa_close_playback,
1282 alsa_reset_playback,
1283 alsa_start_playback,
1284 alsa_stop_playback,
1285 alsa_open_capture,
1286 alsa_close_capture,
1287 alsa_start_capture,
1288 alsa_stop_capture,
1289 alsa_capture_samples,
1290 alsa_available_samples,
1291 ALCdevice_LockDefault,
1292 ALCdevice_UnlockDefault,
1293 alsa_get_latency
1296 ALCboolean alc_alsa_init(BackendFuncs *func_list)
1298 if(!alsa_load())
1299 return ALC_FALSE;
1300 *func_list = alsa_funcs;
1301 return ALC_TRUE;
1304 void alc_alsa_deinit(void)
1306 ALuint i;
1308 for(i = 0;i < numDevNames;++i)
1310 free(allDevNameMap[i].name);
1311 free(allDevNameMap[i].device);
1313 free(allDevNameMap);
1314 allDevNameMap = NULL;
1315 numDevNames = 0;
1317 for(i = 0;i < numCaptureDevNames;++i)
1319 free(allCaptureDevNameMap[i].name);
1320 free(allCaptureDevNameMap[i].device);
1322 free(allCaptureDevNameMap);
1323 allCaptureDevNameMap = NULL;
1324 numCaptureDevNames = 0;
1326 #ifdef HAVE_DYNLOAD
1327 if(alsa_handle)
1328 CloseLib(alsa_handle);
1329 alsa_handle = NULL;
1330 #endif
1333 void alc_alsa_probe(enum DevProbe type)
1335 ALuint i;
1337 switch(type)
1339 case ALL_DEVICE_PROBE:
1340 for(i = 0;i < numDevNames;++i)
1342 free(allDevNameMap[i].name);
1343 free(allDevNameMap[i].device);
1346 free(allDevNameMap);
1347 allDevNameMap = probe_devices(SND_PCM_STREAM_PLAYBACK, &numDevNames);
1349 for(i = 0;i < numDevNames;++i)
1350 AppendAllDevicesList(allDevNameMap[i].name);
1351 break;
1353 case CAPTURE_DEVICE_PROBE:
1354 for(i = 0;i < numCaptureDevNames;++i)
1356 free(allCaptureDevNameMap[i].name);
1357 free(allCaptureDevNameMap[i].device);
1360 free(allCaptureDevNameMap);
1361 allCaptureDevNameMap = probe_devices(SND_PCM_STREAM_CAPTURE, &numCaptureDevNames);
1363 for(i = 0;i < numCaptureDevNames;++i)
1364 AppendCaptureDeviceList(allCaptureDevNameMap[i].name);
1365 break;