Add the start of AL_SOFT_source_latency
[openal-soft/openal-hmr.git] / Alc / backends / alsa.c
blob880f7ac92ccf2bc3b5fe4b92afff2f0ef6c0b608
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"
29 #include <alsa/asoundlib.h>
32 static const ALCchar alsaDevice[] = "ALSA Default";
35 #ifdef HAVE_DYNLOAD
36 static void *alsa_handle;
37 #define MAKE_FUNC(f) static typeof(f) * p##f
38 MAKE_FUNC(snd_strerror);
39 MAKE_FUNC(snd_pcm_open);
40 MAKE_FUNC(snd_pcm_close);
41 MAKE_FUNC(snd_pcm_nonblock);
42 MAKE_FUNC(snd_pcm_frames_to_bytes);
43 MAKE_FUNC(snd_pcm_bytes_to_frames);
44 MAKE_FUNC(snd_pcm_hw_params_malloc);
45 MAKE_FUNC(snd_pcm_hw_params_free);
46 MAKE_FUNC(snd_pcm_hw_params_any);
47 MAKE_FUNC(snd_pcm_hw_params_current);
48 MAKE_FUNC(snd_pcm_hw_params_set_access);
49 MAKE_FUNC(snd_pcm_hw_params_set_format);
50 MAKE_FUNC(snd_pcm_hw_params_set_channels);
51 MAKE_FUNC(snd_pcm_hw_params_set_periods_near);
52 MAKE_FUNC(snd_pcm_hw_params_set_rate_near);
53 MAKE_FUNC(snd_pcm_hw_params_set_rate);
54 MAKE_FUNC(snd_pcm_hw_params_set_rate_resample);
55 MAKE_FUNC(snd_pcm_hw_params_set_buffer_time_near);
56 MAKE_FUNC(snd_pcm_hw_params_set_period_time_near);
57 MAKE_FUNC(snd_pcm_hw_params_set_buffer_size_near);
58 MAKE_FUNC(snd_pcm_hw_params_set_period_size_near);
59 MAKE_FUNC(snd_pcm_hw_params_set_buffer_size_min);
60 MAKE_FUNC(snd_pcm_hw_params_get_buffer_time_min);
61 MAKE_FUNC(snd_pcm_hw_params_get_buffer_time_max);
62 MAKE_FUNC(snd_pcm_hw_params_get_period_time_min);
63 MAKE_FUNC(snd_pcm_hw_params_get_period_time_max);
64 MAKE_FUNC(snd_pcm_hw_params_get_buffer_size);
65 MAKE_FUNC(snd_pcm_hw_params_get_period_size);
66 MAKE_FUNC(snd_pcm_hw_params_get_access);
67 MAKE_FUNC(snd_pcm_hw_params_get_periods);
68 MAKE_FUNC(snd_pcm_hw_params_test_format);
69 MAKE_FUNC(snd_pcm_hw_params_test_channels);
70 MAKE_FUNC(snd_pcm_hw_params);
71 MAKE_FUNC(snd_pcm_sw_params_malloc);
72 MAKE_FUNC(snd_pcm_sw_params_current);
73 MAKE_FUNC(snd_pcm_sw_params_set_avail_min);
74 MAKE_FUNC(snd_pcm_sw_params_set_stop_threshold);
75 MAKE_FUNC(snd_pcm_sw_params);
76 MAKE_FUNC(snd_pcm_sw_params_free);
77 MAKE_FUNC(snd_pcm_prepare);
78 MAKE_FUNC(snd_pcm_start);
79 MAKE_FUNC(snd_pcm_resume);
80 MAKE_FUNC(snd_pcm_reset);
81 MAKE_FUNC(snd_pcm_wait);
82 MAKE_FUNC(snd_pcm_delay);
83 MAKE_FUNC(snd_pcm_state);
84 MAKE_FUNC(snd_pcm_avail_update);
85 MAKE_FUNC(snd_pcm_areas_silence);
86 MAKE_FUNC(snd_pcm_mmap_begin);
87 MAKE_FUNC(snd_pcm_mmap_commit);
88 MAKE_FUNC(snd_pcm_readi);
89 MAKE_FUNC(snd_pcm_writei);
90 MAKE_FUNC(snd_pcm_drain);
91 MAKE_FUNC(snd_pcm_drop);
92 MAKE_FUNC(snd_pcm_recover);
93 MAKE_FUNC(snd_pcm_info_malloc);
94 MAKE_FUNC(snd_pcm_info_free);
95 MAKE_FUNC(snd_pcm_info_set_device);
96 MAKE_FUNC(snd_pcm_info_set_subdevice);
97 MAKE_FUNC(snd_pcm_info_set_stream);
98 MAKE_FUNC(snd_pcm_info_get_name);
99 MAKE_FUNC(snd_ctl_pcm_next_device);
100 MAKE_FUNC(snd_ctl_pcm_info);
101 MAKE_FUNC(snd_ctl_open);
102 MAKE_FUNC(snd_ctl_close);
103 MAKE_FUNC(snd_ctl_card_info_malloc);
104 MAKE_FUNC(snd_ctl_card_info_free);
105 MAKE_FUNC(snd_ctl_card_info);
106 MAKE_FUNC(snd_ctl_card_info_get_name);
107 MAKE_FUNC(snd_ctl_card_info_get_id);
108 MAKE_FUNC(snd_card_next);
109 #undef MAKE_FUNC
111 #define snd_strerror psnd_strerror
112 #define snd_pcm_open psnd_pcm_open
113 #define snd_pcm_close psnd_pcm_close
114 #define snd_pcm_nonblock psnd_pcm_nonblock
115 #define snd_pcm_frames_to_bytes psnd_pcm_frames_to_bytes
116 #define snd_pcm_bytes_to_frames psnd_pcm_bytes_to_frames
117 #define snd_pcm_hw_params_malloc psnd_pcm_hw_params_malloc
118 #define snd_pcm_hw_params_free psnd_pcm_hw_params_free
119 #define snd_pcm_hw_params_any psnd_pcm_hw_params_any
120 #define snd_pcm_hw_params_current psnd_pcm_hw_params_current
121 #define snd_pcm_hw_params_set_access psnd_pcm_hw_params_set_access
122 #define snd_pcm_hw_params_set_format psnd_pcm_hw_params_set_format
123 #define snd_pcm_hw_params_set_channels psnd_pcm_hw_params_set_channels
124 #define snd_pcm_hw_params_set_periods_near psnd_pcm_hw_params_set_periods_near
125 #define snd_pcm_hw_params_set_rate_near psnd_pcm_hw_params_set_rate_near
126 #define snd_pcm_hw_params_set_rate psnd_pcm_hw_params_set_rate
127 #define snd_pcm_hw_params_set_rate_resample psnd_pcm_hw_params_set_rate_resample
128 #define snd_pcm_hw_params_set_buffer_time_near psnd_pcm_hw_params_set_buffer_time_near
129 #define snd_pcm_hw_params_set_period_time_near psnd_pcm_hw_params_set_period_time_near
130 #define snd_pcm_hw_params_set_buffer_size_near psnd_pcm_hw_params_set_buffer_size_near
131 #define snd_pcm_hw_params_set_period_size_near psnd_pcm_hw_params_set_period_size_near
132 #define snd_pcm_hw_params_set_buffer_size_min psnd_pcm_hw_params_set_buffer_size_min
133 #define snd_pcm_hw_params_get_buffer_time_min psnd_pcm_hw_params_get_buffer_time_min
134 #define snd_pcm_hw_params_get_buffer_time_max psnd_pcm_hw_params_get_buffer_time_max
135 #define snd_pcm_hw_params_get_period_time_min psnd_pcm_hw_params_get_period_time_min
136 #define snd_pcm_hw_params_get_period_time_max psnd_pcm_hw_params_get_period_time_max
137 #define snd_pcm_hw_params_get_buffer_size psnd_pcm_hw_params_get_buffer_size
138 #define snd_pcm_hw_params_get_period_size psnd_pcm_hw_params_get_period_size
139 #define snd_pcm_hw_params_get_access psnd_pcm_hw_params_get_access
140 #define snd_pcm_hw_params_get_periods psnd_pcm_hw_params_get_periods
141 #define snd_pcm_hw_params_test_format psnd_pcm_hw_params_test_format
142 #define snd_pcm_hw_params_test_channels psnd_pcm_hw_params_test_channels
143 #define snd_pcm_hw_params psnd_pcm_hw_params
144 #define snd_pcm_sw_params_malloc psnd_pcm_sw_params_malloc
145 #define snd_pcm_sw_params_current psnd_pcm_sw_params_current
146 #define snd_pcm_sw_params_set_avail_min psnd_pcm_sw_params_set_avail_min
147 #define snd_pcm_sw_params_set_stop_threshold psnd_pcm_sw_params_set_stop_threshold
148 #define snd_pcm_sw_params psnd_pcm_sw_params
149 #define snd_pcm_sw_params_free psnd_pcm_sw_params_free
150 #define snd_pcm_prepare psnd_pcm_prepare
151 #define snd_pcm_start psnd_pcm_start
152 #define snd_pcm_resume psnd_pcm_resume
153 #define snd_pcm_reset psnd_pcm_reset
154 #define snd_pcm_wait psnd_pcm_wait
155 #define snd_pcm_delay psnd_pcm_delay
156 #define snd_pcm_state psnd_pcm_state
157 #define snd_pcm_avail_update psnd_pcm_avail_update
158 #define snd_pcm_areas_silence psnd_pcm_areas_silence
159 #define snd_pcm_mmap_begin psnd_pcm_mmap_begin
160 #define snd_pcm_mmap_commit psnd_pcm_mmap_commit
161 #define snd_pcm_readi psnd_pcm_readi
162 #define snd_pcm_writei psnd_pcm_writei
163 #define snd_pcm_drain psnd_pcm_drain
164 #define snd_pcm_drop psnd_pcm_drop
165 #define snd_pcm_recover psnd_pcm_recover
166 #define snd_pcm_info_malloc psnd_pcm_info_malloc
167 #define snd_pcm_info_free psnd_pcm_info_free
168 #define snd_pcm_info_set_device psnd_pcm_info_set_device
169 #define snd_pcm_info_set_subdevice psnd_pcm_info_set_subdevice
170 #define snd_pcm_info_set_stream psnd_pcm_info_set_stream
171 #define snd_pcm_info_get_name psnd_pcm_info_get_name
172 #define snd_ctl_pcm_next_device psnd_ctl_pcm_next_device
173 #define snd_ctl_pcm_info psnd_ctl_pcm_info
174 #define snd_ctl_open psnd_ctl_open
175 #define snd_ctl_close psnd_ctl_close
176 #define snd_ctl_card_info_malloc psnd_ctl_card_info_malloc
177 #define snd_ctl_card_info_free psnd_ctl_card_info_free
178 #define snd_ctl_card_info psnd_ctl_card_info
179 #define snd_ctl_card_info_get_name psnd_ctl_card_info_get_name
180 #define snd_ctl_card_info_get_id psnd_ctl_card_info_get_id
181 #define snd_card_next psnd_card_next
182 #endif
185 static ALCboolean alsa_load(void)
187 #ifdef HAVE_DYNLOAD
188 if(!alsa_handle)
190 alsa_handle = LoadLib("libasound.so.2");
191 if(!alsa_handle)
192 return ALC_FALSE;
194 #define LOAD_FUNC(f) do { \
195 p##f = GetSymbol(alsa_handle, #f); \
196 if(p##f == NULL) { \
197 CloseLib(alsa_handle); \
198 alsa_handle = NULL; \
199 return ALC_FALSE; \
201 } while(0)
202 LOAD_FUNC(snd_strerror);
203 LOAD_FUNC(snd_pcm_open);
204 LOAD_FUNC(snd_pcm_close);
205 LOAD_FUNC(snd_pcm_nonblock);
206 LOAD_FUNC(snd_pcm_frames_to_bytes);
207 LOAD_FUNC(snd_pcm_bytes_to_frames);
208 LOAD_FUNC(snd_pcm_hw_params_malloc);
209 LOAD_FUNC(snd_pcm_hw_params_free);
210 LOAD_FUNC(snd_pcm_hw_params_any);
211 LOAD_FUNC(snd_pcm_hw_params_current);
212 LOAD_FUNC(snd_pcm_hw_params_set_access);
213 LOAD_FUNC(snd_pcm_hw_params_set_format);
214 LOAD_FUNC(snd_pcm_hw_params_set_channels);
215 LOAD_FUNC(snd_pcm_hw_params_set_periods_near);
216 LOAD_FUNC(snd_pcm_hw_params_set_rate_near);
217 LOAD_FUNC(snd_pcm_hw_params_set_rate);
218 LOAD_FUNC(snd_pcm_hw_params_set_rate_resample);
219 LOAD_FUNC(snd_pcm_hw_params_set_buffer_time_near);
220 LOAD_FUNC(snd_pcm_hw_params_set_period_time_near);
221 LOAD_FUNC(snd_pcm_hw_params_set_buffer_size_near);
222 LOAD_FUNC(snd_pcm_hw_params_set_buffer_size_min);
223 LOAD_FUNC(snd_pcm_hw_params_set_period_size_near);
224 LOAD_FUNC(snd_pcm_hw_params_get_buffer_time_min);
225 LOAD_FUNC(snd_pcm_hw_params_get_buffer_time_max);
226 LOAD_FUNC(snd_pcm_hw_params_get_period_time_min);
227 LOAD_FUNC(snd_pcm_hw_params_get_period_time_max);
228 LOAD_FUNC(snd_pcm_hw_params_get_buffer_size);
229 LOAD_FUNC(snd_pcm_hw_params_get_period_size);
230 LOAD_FUNC(snd_pcm_hw_params_get_access);
231 LOAD_FUNC(snd_pcm_hw_params_get_periods);
232 LOAD_FUNC(snd_pcm_hw_params_test_format);
233 LOAD_FUNC(snd_pcm_hw_params_test_channels);
234 LOAD_FUNC(snd_pcm_hw_params);
235 LOAD_FUNC(snd_pcm_sw_params_malloc);
236 LOAD_FUNC(snd_pcm_sw_params_current);
237 LOAD_FUNC(snd_pcm_sw_params_set_avail_min);
238 LOAD_FUNC(snd_pcm_sw_params_set_stop_threshold);
239 LOAD_FUNC(snd_pcm_sw_params);
240 LOAD_FUNC(snd_pcm_sw_params_free);
241 LOAD_FUNC(snd_pcm_prepare);
242 LOAD_FUNC(snd_pcm_start);
243 LOAD_FUNC(snd_pcm_resume);
244 LOAD_FUNC(snd_pcm_reset);
245 LOAD_FUNC(snd_pcm_wait);
246 LOAD_FUNC(snd_pcm_delay);
247 LOAD_FUNC(snd_pcm_state);
248 LOAD_FUNC(snd_pcm_avail_update);
249 LOAD_FUNC(snd_pcm_areas_silence);
250 LOAD_FUNC(snd_pcm_mmap_begin);
251 LOAD_FUNC(snd_pcm_mmap_commit);
252 LOAD_FUNC(snd_pcm_readi);
253 LOAD_FUNC(snd_pcm_writei);
254 LOAD_FUNC(snd_pcm_drain);
255 LOAD_FUNC(snd_pcm_drop);
256 LOAD_FUNC(snd_pcm_recover);
257 LOAD_FUNC(snd_pcm_info_malloc);
258 LOAD_FUNC(snd_pcm_info_free);
259 LOAD_FUNC(snd_pcm_info_set_device);
260 LOAD_FUNC(snd_pcm_info_set_subdevice);
261 LOAD_FUNC(snd_pcm_info_set_stream);
262 LOAD_FUNC(snd_pcm_info_get_name);
263 LOAD_FUNC(snd_ctl_pcm_next_device);
264 LOAD_FUNC(snd_ctl_pcm_info);
265 LOAD_FUNC(snd_ctl_open);
266 LOAD_FUNC(snd_ctl_close);
267 LOAD_FUNC(snd_ctl_card_info_malloc);
268 LOAD_FUNC(snd_ctl_card_info_free);
269 LOAD_FUNC(snd_ctl_card_info);
270 LOAD_FUNC(snd_ctl_card_info_get_name);
271 LOAD_FUNC(snd_ctl_card_info_get_id);
272 LOAD_FUNC(snd_card_next);
273 #undef LOAD_FUNC
275 #endif
276 return ALC_TRUE;
280 typedef struct {
281 snd_pcm_t *pcmHandle;
283 ALvoid *buffer;
284 ALsizei size;
286 ALboolean doCapture;
287 RingBuffer *ring;
289 snd_pcm_sframes_t last_avail;
291 volatile int killNow;
292 ALvoid *thread;
293 } alsa_data;
295 typedef struct {
296 ALCchar *name;
297 char *device;
298 } DevMap;
300 static DevMap *allDevNameMap;
301 static ALuint numDevNames;
302 static DevMap *allCaptureDevNameMap;
303 static ALuint numCaptureDevNames;
306 static const char *prefix_name(snd_pcm_stream_t stream)
308 assert(stream == SND_PCM_STREAM_PLAYBACK || stream == SND_PCM_STREAM_CAPTURE);
309 return (stream==SND_PCM_STREAM_PLAYBACK) ? "device-prefix" : "capture-prefix";
312 static DevMap *probe_devices(snd_pcm_stream_t stream, ALuint *count)
314 const char *main_prefix = "plughw:";
315 snd_ctl_t *handle;
316 int card, err, dev, idx;
317 snd_ctl_card_info_t *info;
318 snd_pcm_info_t *pcminfo;
319 DevMap *DevList;
321 snd_ctl_card_info_malloc(&info);
322 snd_pcm_info_malloc(&pcminfo);
324 DevList = malloc(sizeof(DevMap) * 1);
325 DevList[0].name = strdup(alsaDevice);
326 DevList[0].device = strdup(GetConfigValue("alsa", (stream==SND_PCM_STREAM_PLAYBACK) ?
327 "device" : "capture", "default"));
328 idx = 1;
330 card = -1;
331 if((err=snd_card_next(&card)) < 0)
332 ERR("Failed to find a card: %s\n", snd_strerror(err));
333 ConfigValueStr("alsa", prefix_name(stream), &main_prefix);
334 while(card >= 0)
336 const char *card_prefix = main_prefix;
337 const char *cardname, *cardid;
338 char name[256];
340 snprintf(name, sizeof(name), "hw:%d", card);
341 if((err = snd_ctl_open(&handle, name, 0)) < 0)
343 ERR("control open (hw:%d): %s\n", card, snd_strerror(err));
344 goto next_card;
346 if((err = snd_ctl_card_info(handle, info)) < 0)
348 ERR("control hardware info (hw:%d): %s\n", card, snd_strerror(err));
349 snd_ctl_close(handle);
350 goto next_card;
353 cardname = snd_ctl_card_info_get_name(info);
354 cardid = snd_ctl_card_info_get_id(info);
356 snprintf(name, sizeof(name), "%s-%s", prefix_name(stream), cardid);
357 ConfigValueStr("alsa", name, &card_prefix);
359 dev = -1;
360 while(1)
362 const char *devname;
363 void *temp;
365 if(snd_ctl_pcm_next_device(handle, &dev) < 0)
366 ERR("snd_ctl_pcm_next_device failed\n");
367 if(dev < 0)
368 break;
370 snd_pcm_info_set_device(pcminfo, dev);
371 snd_pcm_info_set_subdevice(pcminfo, 0);
372 snd_pcm_info_set_stream(pcminfo, stream);
373 if((err = snd_ctl_pcm_info(handle, pcminfo)) < 0) {
374 if(err != -ENOENT)
375 ERR("control digital audio info (hw:%d): %s\n", card, snd_strerror(err));
376 continue;
379 temp = realloc(DevList, sizeof(DevMap) * (idx+1));
380 if(temp)
382 const char *device_prefix = card_prefix;
383 char device[128];
385 DevList = temp;
386 devname = snd_pcm_info_get_name(pcminfo);
388 snprintf(name, sizeof(name), "%s-%s-%d", prefix_name(stream), cardid, dev);
389 ConfigValueStr("alsa", name, &device_prefix);
391 snprintf(name, sizeof(name), "%s, %s (CARD=%s,DEV=%d)",
392 cardname, devname, cardid, dev);
393 snprintf(device, sizeof(device), "%sCARD=%s,DEV=%d",
394 device_prefix, cardid, dev);
396 TRACE("Got device \"%s\", \"%s\"\n", name, device);
397 DevList[idx].name = strdup(name);
398 DevList[idx].device = strdup(device);
399 idx++;
402 snd_ctl_close(handle);
403 next_card:
404 if(snd_card_next(&card) < 0) {
405 ERR("snd_card_next failed\n");
406 break;
410 snd_pcm_info_free(pcminfo);
411 snd_ctl_card_info_free(info);
413 *count = idx;
414 return DevList;
418 static int verify_state(snd_pcm_t *handle)
420 snd_pcm_state_t state = snd_pcm_state(handle);
421 int err;
423 switch(state)
425 case SND_PCM_STATE_OPEN:
426 case SND_PCM_STATE_SETUP:
427 case SND_PCM_STATE_PREPARED:
428 case SND_PCM_STATE_RUNNING:
429 case SND_PCM_STATE_DRAINING:
430 case SND_PCM_STATE_PAUSED:
431 /* All Okay */
432 break;
434 case SND_PCM_STATE_XRUN:
435 if((err=snd_pcm_recover(handle, -EPIPE, 1)) < 0)
436 return err;
437 break;
438 case SND_PCM_STATE_SUSPENDED:
439 if((err=snd_pcm_recover(handle, -ESTRPIPE, 1)) < 0)
440 return err;
441 break;
442 case SND_PCM_STATE_DISCONNECTED:
443 return -ENODEV;
446 return state;
450 static ALuint ALSAProc(ALvoid *ptr)
452 ALCdevice *Device = (ALCdevice*)ptr;
453 alsa_data *data = (alsa_data*)Device->ExtraData;
454 const snd_pcm_channel_area_t *areas = NULL;
455 snd_pcm_uframes_t update_size, num_updates;
456 snd_pcm_sframes_t avail, commitres;
457 snd_pcm_uframes_t offset, frames;
458 char *WritePtr;
459 int err;
461 SetRTPriority();
463 update_size = Device->UpdateSize;
464 num_updates = Device->NumUpdates;
465 while(!data->killNow)
467 int state = verify_state(data->pcmHandle);
468 if(state < 0)
470 ERR("Invalid state detected: %s\n", snd_strerror(state));
471 aluHandleDisconnect(Device);
472 break;
475 avail = snd_pcm_avail_update(data->pcmHandle);
476 if(avail < 0)
478 ERR("available update failed: %s\n", snd_strerror(avail));
479 continue;
482 if((snd_pcm_uframes_t)avail > update_size*(num_updates+1))
484 WARN("available samples exceeds the buffer size\n");
485 snd_pcm_reset(data->pcmHandle);
486 continue;
489 // make sure there's frames to process
490 if((snd_pcm_uframes_t)avail < update_size)
492 if(state != SND_PCM_STATE_RUNNING)
494 err = snd_pcm_start(data->pcmHandle);
495 if(err < 0)
497 ERR("start failed: %s\n", snd_strerror(err));
498 continue;
501 if(snd_pcm_wait(data->pcmHandle, 1000) == 0)
502 ERR("Wait timeout... buffer size too low?\n");
503 continue;
505 avail -= avail%update_size;
507 // it is possible that contiguous areas are smaller, thus we use a loop
508 while(avail > 0)
510 frames = avail;
512 err = snd_pcm_mmap_begin(data->pcmHandle, &areas, &offset, &frames);
513 if(err < 0)
515 ERR("mmap begin error: %s\n", snd_strerror(err));
516 break;
519 WritePtr = (char*)areas->addr + (offset * areas->step / 8);
520 aluMixData(Device, WritePtr, frames);
522 commitres = snd_pcm_mmap_commit(data->pcmHandle, offset, frames);
523 if(commitres < 0 || (commitres-frames) != 0)
525 ERR("mmap commit error: %s\n",
526 snd_strerror(commitres >= 0 ? -EPIPE : commitres));
527 break;
530 avail -= frames;
534 return 0;
537 static ALuint ALSANoMMapProc(ALvoid *ptr)
539 ALCdevice *Device = (ALCdevice*)ptr;
540 alsa_data *data = (alsa_data*)Device->ExtraData;
541 snd_pcm_sframes_t avail;
542 char *WritePtr;
544 SetRTPriority();
546 while(!data->killNow)
548 int state = verify_state(data->pcmHandle);
549 if(state < 0)
551 ERR("Invalid state detected: %s\n", snd_strerror(state));
552 aluHandleDisconnect(Device);
553 break;
556 WritePtr = data->buffer;
557 avail = data->size / snd_pcm_frames_to_bytes(data->pcmHandle, 1);
558 aluMixData(Device, WritePtr, avail);
560 while(avail > 0)
562 int ret = snd_pcm_writei(data->pcmHandle, WritePtr, avail);
563 switch (ret)
565 case -EAGAIN:
566 continue;
567 case -ESTRPIPE:
568 case -EPIPE:
569 case -EINTR:
570 ret = snd_pcm_recover(data->pcmHandle, ret, 1);
571 if(ret < 0)
572 avail = 0;
573 break;
574 default:
575 if (ret >= 0)
577 WritePtr += snd_pcm_frames_to_bytes(data->pcmHandle, ret);
578 avail -= ret;
580 break;
582 if (ret < 0)
584 ret = snd_pcm_prepare(data->pcmHandle);
585 if(ret < 0)
586 break;
591 return 0;
594 static ALCenum alsa_open_playback(ALCdevice *device, const ALCchar *deviceName)
596 const char *driver = NULL;
597 alsa_data *data;
598 int err;
600 if(deviceName)
602 size_t idx;
604 if(!allDevNameMap)
605 allDevNameMap = probe_devices(SND_PCM_STREAM_PLAYBACK, &numDevNames);
607 for(idx = 0;idx < numDevNames;idx++)
609 if(strcmp(deviceName, allDevNameMap[idx].name) == 0)
611 driver = allDevNameMap[idx].device;
612 break;
615 if(idx == numDevNames)
616 return ALC_INVALID_VALUE;
618 else
620 deviceName = alsaDevice;
621 driver = GetConfigValue("alsa", "device", "default");
624 data = (alsa_data*)calloc(1, sizeof(alsa_data));
626 err = snd_pcm_open(&data->pcmHandle, driver, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
627 if(err >= 0)
629 err = snd_pcm_nonblock(data->pcmHandle, 0);
630 if(err < 0) snd_pcm_close(data->pcmHandle);
632 if(err < 0)
634 free(data);
635 ERR("Could not open playback device '%s': %s\n", driver, snd_strerror(err));
636 return ALC_OUT_OF_MEMORY;
639 device->DeviceName = strdup(deviceName);
640 device->ExtraData = data;
641 return ALC_NO_ERROR;
644 static void alsa_close_playback(ALCdevice *device)
646 alsa_data *data = (alsa_data*)device->ExtraData;
648 snd_pcm_close(data->pcmHandle);
649 free(data);
650 device->ExtraData = NULL;
653 static ALCboolean alsa_reset_playback(ALCdevice *device)
655 alsa_data *data = (alsa_data*)device->ExtraData;
656 snd_pcm_uframes_t periodSizeInFrames;
657 unsigned int periodLen, bufferLen;
658 snd_pcm_sw_params_t *sp = NULL;
659 snd_pcm_hw_params_t *hp = NULL;
660 snd_pcm_access_t access;
661 snd_pcm_format_t format;
662 unsigned int periods;
663 unsigned int rate;
664 const char *funcerr;
665 int allowmmap;
666 int err;
668 format = -1;
669 switch(device->FmtType)
671 case DevFmtByte:
672 format = SND_PCM_FORMAT_S8;
673 break;
674 case DevFmtUByte:
675 format = SND_PCM_FORMAT_U8;
676 break;
677 case DevFmtShort:
678 format = SND_PCM_FORMAT_S16;
679 break;
680 case DevFmtUShort:
681 format = SND_PCM_FORMAT_U16;
682 break;
683 case DevFmtInt:
684 format = SND_PCM_FORMAT_S32;
685 break;
686 case DevFmtUInt:
687 format = SND_PCM_FORMAT_U32;
688 break;
689 case DevFmtFloat:
690 format = SND_PCM_FORMAT_FLOAT;
691 break;
694 allowmmap = GetConfigValueBool("alsa", "mmap", 1);
695 periods = device->NumUpdates;
696 periodLen = (ALuint64)device->UpdateSize * 1000000 / device->Frequency;
697 bufferLen = periodLen * periods;
698 rate = device->Frequency;
700 snd_pcm_hw_params_malloc(&hp);
701 #define CHECK(x) if((funcerr=#x),(err=(x)) < 0) goto error
702 CHECK(snd_pcm_hw_params_any(data->pcmHandle, hp));
703 /* set interleaved access */
704 if(!allowmmap || snd_pcm_hw_params_set_access(data->pcmHandle, hp, SND_PCM_ACCESS_MMAP_INTERLEAVED) < 0)
706 if(periods > 2)
708 periods--;
709 bufferLen = periodLen * periods;
711 CHECK(snd_pcm_hw_params_set_access(data->pcmHandle, hp, SND_PCM_ACCESS_RW_INTERLEAVED));
713 /* test and set format (implicitly sets sample bits) */
714 if(snd_pcm_hw_params_test_format(data->pcmHandle, hp, format) < 0)
716 static const struct {
717 snd_pcm_format_t format;
718 enum DevFmtType fmttype;
719 } formatlist[] = {
720 { SND_PCM_FORMAT_FLOAT, DevFmtFloat },
721 { SND_PCM_FORMAT_S32, DevFmtInt },
722 { SND_PCM_FORMAT_U32, DevFmtUInt },
723 { SND_PCM_FORMAT_S16, DevFmtShort },
724 { SND_PCM_FORMAT_U16, DevFmtUShort },
725 { SND_PCM_FORMAT_S8, DevFmtByte },
726 { SND_PCM_FORMAT_U8, DevFmtUByte },
728 size_t k;
730 for(k = 0;k < COUNTOF(formatlist);k++)
732 format = formatlist[k].format;
733 if(snd_pcm_hw_params_test_format(data->pcmHandle, hp, format) >= 0)
735 device->FmtType = formatlist[k].fmttype;
736 break;
740 CHECK(snd_pcm_hw_params_set_format(data->pcmHandle, hp, format));
741 /* test and set channels (implicitly sets frame bits) */
742 if(snd_pcm_hw_params_test_channels(data->pcmHandle, hp, ChannelsFromDevFmt(device->FmtChans)) < 0)
744 static const enum DevFmtChannels channellist[] = {
745 DevFmtStereo,
746 DevFmtQuad,
747 DevFmtX51,
748 DevFmtX71,
749 DevFmtMono,
751 size_t k;
753 for(k = 0;k < COUNTOF(channellist);k++)
755 if(snd_pcm_hw_params_test_channels(data->pcmHandle, hp, ChannelsFromDevFmt(channellist[k])) >= 0)
757 device->FmtChans = channellist[k];
758 break;
762 CHECK(snd_pcm_hw_params_set_channels(data->pcmHandle, hp, ChannelsFromDevFmt(device->FmtChans)));
763 /* set rate (implicitly constrains period/buffer parameters) */
764 if(snd_pcm_hw_params_set_rate_resample(data->pcmHandle, hp, 0) < 0)
765 ERR("Failed to disable ALSA resampler\n");
766 CHECK(snd_pcm_hw_params_set_rate_near(data->pcmHandle, hp, &rate, NULL));
767 /* set buffer time (implicitly constrains period/buffer parameters) */
768 if(snd_pcm_hw_params_set_buffer_time_near(data->pcmHandle, hp, &bufferLen, NULL) < 0)
769 ERR("snd_pcm_hw_params_set_buffer_time_near failed: %s\n", snd_strerror(err));
770 /* set period time (implicitly sets buffer size/bytes/time and period size/bytes) */
771 if(snd_pcm_hw_params_set_period_time_near(data->pcmHandle, hp, &periodLen, NULL) < 0)
772 ERR("snd_pcm_hw_params_set_period_time_near failed: %s\n", snd_strerror(err));
773 /* install and prepare hardware configuration */
774 CHECK(snd_pcm_hw_params(data->pcmHandle, hp));
775 /* retrieve configuration info */
776 CHECK(snd_pcm_hw_params_get_access(hp, &access));
777 CHECK(snd_pcm_hw_params_get_period_size(hp, &periodSizeInFrames, NULL));
778 CHECK(snd_pcm_hw_params_get_periods(hp, &periods, NULL));
780 snd_pcm_hw_params_free(hp);
781 hp = NULL;
782 snd_pcm_sw_params_malloc(&sp);
784 CHECK(snd_pcm_sw_params_current(data->pcmHandle, sp));
785 CHECK(snd_pcm_sw_params_set_avail_min(data->pcmHandle, sp, periodSizeInFrames));
786 CHECK(snd_pcm_sw_params_set_stop_threshold(data->pcmHandle, sp, periodSizeInFrames*periods));
787 CHECK(snd_pcm_sw_params(data->pcmHandle, sp));
788 #undef CHECK
789 snd_pcm_sw_params_free(sp);
790 sp = NULL;
792 /* Increase periods by one, since the temp buffer counts as an extra
793 * period */
794 if(access == SND_PCM_ACCESS_RW_INTERLEAVED)
795 device->NumUpdates = periods+1;
796 else
797 device->NumUpdates = periods;
798 device->UpdateSize = periodSizeInFrames;
799 device->Frequency = rate;
801 SetDefaultChannelOrder(device);
803 return ALC_TRUE;
805 error:
806 ERR("%s failed: %s\n", funcerr, snd_strerror(err));
807 if(hp) snd_pcm_hw_params_free(hp);
808 if(sp) snd_pcm_sw_params_free(sp);
809 return ALC_FALSE;
812 static ALCboolean alsa_start_playback(ALCdevice *device)
814 alsa_data *data = (alsa_data*)device->ExtraData;
815 snd_pcm_hw_params_t *hp = NULL;
816 snd_pcm_access_t access;
817 const char *funcerr;
818 int err;
820 snd_pcm_hw_params_malloc(&hp);
821 #define CHECK(x) if((funcerr=#x),(err=(x)) < 0) goto error
822 CHECK(snd_pcm_hw_params_current(data->pcmHandle, hp));
823 /* retrieve configuration info */
824 CHECK(snd_pcm_hw_params_get_access(hp, &access));
825 #undef CHECK
826 snd_pcm_hw_params_free(hp);
827 hp = NULL;
829 data->size = snd_pcm_frames_to_bytes(data->pcmHandle, device->UpdateSize);
830 if(access == SND_PCM_ACCESS_RW_INTERLEAVED)
832 data->buffer = malloc(data->size);
833 if(!data->buffer)
835 ERR("buffer malloc failed\n");
836 return ALC_FALSE;
838 data->thread = StartThread(ALSANoMMapProc, device);
840 else
842 err = snd_pcm_prepare(data->pcmHandle);
843 if(err < 0)
845 ERR("snd_pcm_prepare(data->pcmHandle) failed: %s\n", snd_strerror(err));
846 return ALC_FALSE;
848 data->thread = StartThread(ALSAProc, device);
850 if(data->thread == NULL)
852 ERR("Could not create playback thread\n");
853 free(data->buffer);
854 data->buffer = NULL;
855 return ALC_FALSE;
858 return ALC_TRUE;
860 error:
861 ERR("%s failed: %s\n", funcerr, snd_strerror(err));
862 if(hp) snd_pcm_hw_params_free(hp);
863 return ALC_FALSE;
866 static void alsa_stop_playback(ALCdevice *device)
868 alsa_data *data = (alsa_data*)device->ExtraData;
870 if(data->thread)
872 data->killNow = 1;
873 StopThread(data->thread);
874 data->thread = NULL;
876 data->killNow = 0;
877 free(data->buffer);
878 data->buffer = NULL;
882 static ALCenum alsa_open_capture(ALCdevice *Device, const ALCchar *deviceName)
884 const char *driver = NULL;
885 snd_pcm_hw_params_t *hp;
886 snd_pcm_uframes_t bufferSizeInFrames;
887 snd_pcm_uframes_t periodSizeInFrames;
888 ALboolean needring = AL_FALSE;
889 snd_pcm_format_t format;
890 const char *funcerr;
891 alsa_data *data;
892 int err;
894 if(deviceName)
896 size_t idx;
898 if(!allCaptureDevNameMap)
899 allCaptureDevNameMap = probe_devices(SND_PCM_STREAM_CAPTURE, &numCaptureDevNames);
901 for(idx = 0;idx < numCaptureDevNames;idx++)
903 if(strcmp(deviceName, allCaptureDevNameMap[idx].name) == 0)
905 driver = allCaptureDevNameMap[idx].device;
906 break;
909 if(idx == numCaptureDevNames)
910 return ALC_INVALID_VALUE;
912 else
914 deviceName = alsaDevice;
915 driver = GetConfigValue("alsa", "capture", "default");
918 data = (alsa_data*)calloc(1, sizeof(alsa_data));
920 err = snd_pcm_open(&data->pcmHandle, driver, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
921 if(err < 0)
923 ERR("Could not open capture device '%s': %s\n", driver, snd_strerror(err));
924 free(data);
925 return ALC_INVALID_VALUE;
928 format = -1;
929 switch(Device->FmtType)
931 case DevFmtByte:
932 format = SND_PCM_FORMAT_S8;
933 break;
934 case DevFmtUByte:
935 format = SND_PCM_FORMAT_U8;
936 break;
937 case DevFmtShort:
938 format = SND_PCM_FORMAT_S16;
939 break;
940 case DevFmtUShort:
941 format = SND_PCM_FORMAT_U16;
942 break;
943 case DevFmtInt:
944 format = SND_PCM_FORMAT_S32;
945 break;
946 case DevFmtUInt:
947 format = SND_PCM_FORMAT_U32;
948 break;
949 case DevFmtFloat:
950 format = SND_PCM_FORMAT_FLOAT;
951 break;
954 funcerr = NULL;
955 bufferSizeInFrames = maxu(Device->UpdateSize*Device->NumUpdates,
956 100*Device->Frequency/1000);
957 periodSizeInFrames = minu(bufferSizeInFrames, 25*Device->Frequency/1000);
959 snd_pcm_hw_params_malloc(&hp);
960 #define CHECK(x) if((funcerr=#x),(err=(x)) < 0) goto error
961 CHECK(snd_pcm_hw_params_any(data->pcmHandle, hp));
962 /* set interleaved access */
963 CHECK(snd_pcm_hw_params_set_access(data->pcmHandle, hp, SND_PCM_ACCESS_RW_INTERLEAVED));
964 /* set format (implicitly sets sample bits) */
965 CHECK(snd_pcm_hw_params_set_format(data->pcmHandle, hp, format));
966 /* set channels (implicitly sets frame bits) */
967 CHECK(snd_pcm_hw_params_set_channels(data->pcmHandle, hp, ChannelsFromDevFmt(Device->FmtChans)));
968 /* set rate (implicitly constrains period/buffer parameters) */
969 CHECK(snd_pcm_hw_params_set_rate(data->pcmHandle, hp, Device->Frequency, 0));
970 /* set buffer size in frame units (implicitly sets period size/bytes/time and buffer time/bytes) */
971 if(snd_pcm_hw_params_set_buffer_size_min(data->pcmHandle, hp, &bufferSizeInFrames) < 0)
973 TRACE("Buffer too large, using intermediate ring buffer\n");
974 needring = AL_TRUE;
975 CHECK(snd_pcm_hw_params_set_buffer_size_near(data->pcmHandle, hp, &bufferSizeInFrames));
977 /* set buffer size in frame units (implicitly sets period size/bytes/time and buffer time/bytes) */
978 CHECK(snd_pcm_hw_params_set_period_size_near(data->pcmHandle, hp, &periodSizeInFrames, NULL));
979 /* install and prepare hardware configuration */
980 CHECK(snd_pcm_hw_params(data->pcmHandle, hp));
981 /* retrieve configuration info */
982 CHECK(snd_pcm_hw_params_get_period_size(hp, &periodSizeInFrames, NULL));
983 #undef CHECK
984 snd_pcm_hw_params_free(hp);
985 hp = NULL;
987 if(needring)
989 data->ring = CreateRingBuffer(FrameSizeFromDevFmt(Device->FmtChans, Device->FmtType),
990 Device->UpdateSize*Device->NumUpdates);
991 if(!data->ring)
993 ERR("ring buffer create failed\n");
994 goto error2;
997 data->size = snd_pcm_frames_to_bytes(data->pcmHandle, periodSizeInFrames);
998 data->buffer = malloc(data->size);
999 if(!data->buffer)
1001 ERR("buffer malloc failed\n");
1002 goto error2;
1006 Device->DeviceName = strdup(deviceName);
1008 Device->ExtraData = data;
1009 return ALC_NO_ERROR;
1011 error:
1012 ERR("%s failed: %s\n", funcerr, snd_strerror(err));
1013 if(hp) snd_pcm_hw_params_free(hp);
1015 error2:
1016 free(data->buffer);
1017 DestroyRingBuffer(data->ring);
1018 snd_pcm_close(data->pcmHandle);
1019 free(data);
1021 Device->ExtraData = NULL;
1022 return ALC_INVALID_VALUE;
1025 static void alsa_close_capture(ALCdevice *Device)
1027 alsa_data *data = (alsa_data*)Device->ExtraData;
1029 snd_pcm_close(data->pcmHandle);
1030 DestroyRingBuffer(data->ring);
1032 free(data->buffer);
1033 free(data);
1034 Device->ExtraData = NULL;
1037 static void alsa_start_capture(ALCdevice *Device)
1039 alsa_data *data = (alsa_data*)Device->ExtraData;
1040 int err;
1042 err = snd_pcm_start(data->pcmHandle);
1043 if(err < 0)
1045 ERR("start failed: %s\n", snd_strerror(err));
1046 aluHandleDisconnect(Device);
1048 else
1049 data->doCapture = AL_TRUE;
1052 static ALCenum alsa_capture_samples(ALCdevice *Device, ALCvoid *Buffer, ALCuint Samples)
1054 alsa_data *data = (alsa_data*)Device->ExtraData;
1056 if(data->ring)
1058 ReadRingBuffer(data->ring, Buffer, Samples);
1059 return ALC_NO_ERROR;
1062 data->last_avail -= Samples;
1063 while(Device->Connected && Samples > 0)
1065 snd_pcm_sframes_t amt = 0;
1067 if(data->size > 0)
1069 /* First get any data stored from the last stop */
1070 amt = snd_pcm_bytes_to_frames(data->pcmHandle, data->size);
1071 if((snd_pcm_uframes_t)amt > Samples) amt = Samples;
1073 amt = snd_pcm_frames_to_bytes(data->pcmHandle, amt);
1074 memmove(Buffer, data->buffer, amt);
1076 if(data->size > amt)
1078 memmove(data->buffer, data->buffer+amt, data->size - amt);
1079 data->size -= amt;
1081 else
1083 free(data->buffer);
1084 data->buffer = NULL;
1085 data->size = 0;
1087 amt = snd_pcm_bytes_to_frames(data->pcmHandle, amt);
1089 else if(data->doCapture)
1090 amt = snd_pcm_readi(data->pcmHandle, Buffer, Samples);
1091 if(amt < 0)
1093 ERR("read error: %s\n", snd_strerror(amt));
1095 if(amt == -EAGAIN)
1096 continue;
1097 if((amt=snd_pcm_recover(data->pcmHandle, amt, 1)) >= 0)
1099 amt = snd_pcm_start(data->pcmHandle);
1100 if(amt >= 0)
1101 amt = snd_pcm_avail_update(data->pcmHandle);
1103 if(amt < 0)
1105 ERR("restore error: %s\n", snd_strerror(amt));
1106 aluHandleDisconnect(Device);
1107 break;
1109 /* If the amount available is less than what's asked, we lost it
1110 * during recovery. So just give silence instead. */
1111 if((snd_pcm_uframes_t)amt < Samples)
1112 break;
1113 continue;
1116 Buffer = (ALbyte*)Buffer + amt;
1117 Samples -= amt;
1119 if(Samples > 0)
1120 memset(Buffer, ((Device->FmtType == DevFmtUByte) ? 0x80 : 0),
1121 snd_pcm_frames_to_bytes(data->pcmHandle, Samples));
1123 return ALC_NO_ERROR;
1126 static ALCuint alsa_available_samples(ALCdevice *Device)
1128 alsa_data *data = (alsa_data*)Device->ExtraData;
1129 snd_pcm_sframes_t avail = 0;
1131 if(Device->Connected && data->doCapture)
1132 avail = snd_pcm_avail_update(data->pcmHandle);
1133 if(avail < 0)
1135 ERR("avail update failed: %s\n", snd_strerror(avail));
1137 if((avail=snd_pcm_recover(data->pcmHandle, avail, 1)) >= 0)
1139 if(data->doCapture)
1140 avail = snd_pcm_start(data->pcmHandle);
1141 if(avail >= 0)
1142 avail = snd_pcm_avail_update(data->pcmHandle);
1144 if(avail < 0)
1146 ERR("restore error: %s\n", snd_strerror(avail));
1147 aluHandleDisconnect(Device);
1151 if(!data->ring)
1153 if(avail < 0) avail = 0;
1154 avail += snd_pcm_bytes_to_frames(data->pcmHandle, data->size);
1155 if(avail > data->last_avail) data->last_avail = avail;
1156 return data->last_avail;
1159 while(avail > 0)
1161 snd_pcm_sframes_t amt;
1163 amt = snd_pcm_bytes_to_frames(data->pcmHandle, data->size);
1164 if(avail < amt) amt = avail;
1166 amt = snd_pcm_readi(data->pcmHandle, data->buffer, amt);
1167 if(amt < 0)
1169 ERR("read error: %s\n", snd_strerror(amt));
1171 if(amt == -EAGAIN)
1172 continue;
1173 if((amt=snd_pcm_recover(data->pcmHandle, amt, 1)) >= 0)
1175 if(data->doCapture)
1176 amt = snd_pcm_start(data->pcmHandle);
1177 if(amt >= 0)
1178 amt = snd_pcm_avail_update(data->pcmHandle);
1180 if(amt < 0)
1182 ERR("restore error: %s\n", snd_strerror(amt));
1183 aluHandleDisconnect(Device);
1184 break;
1186 avail = amt;
1187 continue;
1190 WriteRingBuffer(data->ring, data->buffer, amt);
1191 avail -= amt;
1194 return RingBufferSize(data->ring);
1197 static void alsa_stop_capture(ALCdevice *Device)
1199 alsa_data *data = (alsa_data*)Device->ExtraData;
1200 ALCuint avail;
1201 int err;
1203 /* OpenAL requires access to unread audio after stopping, but ALSA's
1204 * snd_pcm_drain is unreliable and snd_pcm_drop drops it. Capture what's
1205 * available now so it'll be available later after the drop. */
1206 avail = alsa_available_samples(Device);
1207 if(!data->ring && avail > 0)
1209 /* The ring buffer implicitly captures when checking availability.
1210 * Direct access needs to explicitly capture it into temp storage. */
1211 ALsizei size;
1212 void *ptr;
1214 size = snd_pcm_frames_to_bytes(data->pcmHandle, avail);
1215 ptr = realloc(data->buffer, size);
1216 if(ptr)
1218 data->buffer = ptr;
1219 alsa_capture_samples(Device, data->buffer, avail);
1220 data->size = size;
1223 err = snd_pcm_drop(data->pcmHandle);
1224 if(err < 0)
1225 ERR("drop failed: %s\n", snd_strerror(err));
1226 data->doCapture = AL_FALSE;
1230 static ALint64 alsa_get_latency(ALCdevice *device)
1232 alsa_data *data = (alsa_data*)device->ExtraData;
1233 snd_pcm_sframes_t delay = 0;
1234 int err;
1236 if((err=snd_pcm_delay(data->pcmHandle, &delay)) < 0)
1238 ERR("Failed to get latency!\n");
1239 return 0;
1241 return maxi64((ALint64)delay*1000000000/device->Frequency, 0);
1245 static const BackendFuncs alsa_funcs = {
1246 alsa_open_playback,
1247 alsa_close_playback,
1248 alsa_reset_playback,
1249 alsa_start_playback,
1250 alsa_stop_playback,
1251 alsa_open_capture,
1252 alsa_close_capture,
1253 alsa_start_capture,
1254 alsa_stop_capture,
1255 alsa_capture_samples,
1256 alsa_available_samples,
1257 alsa_get_latency
1260 ALCboolean alc_alsa_init(BackendFuncs *func_list)
1262 if(!alsa_load())
1263 return ALC_FALSE;
1264 *func_list = alsa_funcs;
1265 return ALC_TRUE;
1268 void alc_alsa_deinit(void)
1270 ALuint i;
1272 for(i = 0;i < numDevNames;++i)
1274 free(allDevNameMap[i].name);
1275 free(allDevNameMap[i].device);
1277 free(allDevNameMap);
1278 allDevNameMap = NULL;
1279 numDevNames = 0;
1281 for(i = 0;i < numCaptureDevNames;++i)
1283 free(allCaptureDevNameMap[i].name);
1284 free(allCaptureDevNameMap[i].device);
1286 free(allCaptureDevNameMap);
1287 allCaptureDevNameMap = NULL;
1288 numCaptureDevNames = 0;
1290 #ifdef HAVE_DYNLOAD
1291 if(alsa_handle)
1292 CloseLib(alsa_handle);
1293 alsa_handle = NULL;
1294 #endif
1297 void alc_alsa_probe(enum DevProbe type)
1299 ALuint i;
1301 switch(type)
1303 case ALL_DEVICE_PROBE:
1304 for(i = 0;i < numDevNames;++i)
1306 free(allDevNameMap[i].name);
1307 free(allDevNameMap[i].device);
1310 free(allDevNameMap);
1311 allDevNameMap = probe_devices(SND_PCM_STREAM_PLAYBACK, &numDevNames);
1313 for(i = 0;i < numDevNames;++i)
1314 AppendAllDevicesList(allDevNameMap[i].name);
1315 break;
1317 case CAPTURE_DEVICE_PROBE:
1318 for(i = 0;i < numCaptureDevNames;++i)
1320 free(allCaptureDevNameMap[i].name);
1321 free(allCaptureDevNameMap[i].device);
1324 free(allCaptureDevNameMap);
1325 allCaptureDevNameMap = probe_devices(SND_PCM_STREAM_CAPTURE, &numCaptureDevNames);
1327 for(i = 0;i < numCaptureDevNames;++i)
1328 AppendCaptureDeviceList(allCaptureDevNameMap[i].name);
1329 break;