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
30 #include <alsa/asoundlib.h>
33 static const ALCchar alsaDevice
[] = "ALSA Default";
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
);
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
188 static ALCboolean
alsa_load(void)
193 alsa_handle
= LoadLib("libasound.so.2");
197 #define LOAD_FUNC(f) do { \
198 p##f = GetSymbol(alsa_handle, #f); \
200 CloseLib(alsa_handle); \
201 alsa_handle = NULL; \
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
);
285 snd_pcm_t
*pcmHandle
;
293 snd_pcm_sframes_t last_avail
;
295 volatile int killNow
;
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:";
320 int card
, err
, dev
, idx
;
321 snd_ctl_card_info_t
*info
;
322 snd_pcm_info_t
*pcminfo
;
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"));
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
);
340 const char *card_prefix
= main_prefix
;
341 const char *cardname
, *cardid
;
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
));
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
);
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
);
369 if(snd_ctl_pcm_next_device(handle
, &dev
) < 0)
370 ERR("snd_ctl_pcm_next_device failed\n");
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) {
379 ERR("control digital audio info (hw:%d): %s\n", card
, snd_strerror(err
));
383 temp
= realloc(DevList
, sizeof(DevMap
) * (idx
+1));
386 const char *device_prefix
= card_prefix
;
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
);
406 snd_ctl_close(handle
);
408 if(snd_card_next(&card
) < 0) {
409 ERR("snd_card_next failed\n");
414 snd_pcm_info_free(pcminfo
);
415 snd_ctl_card_info_free(info
);
422 static int verify_state(snd_pcm_t
*handle
)
424 snd_pcm_state_t state
= snd_pcm_state(handle
);
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
:
438 case SND_PCM_STATE_XRUN
:
439 if((err
=snd_pcm_recover(handle
, -EPIPE
, 1)) < 0)
442 case SND_PCM_STATE_SUSPENDED
:
443 if((err
=snd_pcm_recover(handle
, -ESTRPIPE
, 1)) < 0)
446 case SND_PCM_STATE_DISCONNECTED
:
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
;
467 update_size
= Device
->UpdateSize
;
468 num_updates
= Device
->NumUpdates
;
469 while(!data
->killNow
)
471 int state
= verify_state(data
->pcmHandle
);
474 ERR("Invalid state detected: %s\n", snd_strerror(state
));
475 aluHandleDisconnect(Device
);
479 avail
= snd_pcm_avail_update(data
->pcmHandle
);
482 ERR("available update failed: %s\n", snd_strerror(avail
));
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
);
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
);
501 ERR("start failed: %s\n", snd_strerror(err
));
505 if(snd_pcm_wait(data
->pcmHandle
, 1000) == 0)
506 ERR("Wait timeout... buffer size too low?\n");
509 avail
-= avail
%update_size
;
511 // it is possible that contiguous areas are smaller, thus we use a loop
512 ALCdevice_Lock(Device
);
517 err
= snd_pcm_mmap_begin(data
->pcmHandle
, &areas
, &offset
, &frames
);
520 ERR("mmap begin error: %s\n", snd_strerror(err
));
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
));
537 ALCdevice_Unlock(Device
);
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
;
554 update_size
= Device
->UpdateSize
;
555 num_updates
= Device
->NumUpdates
;
556 while(!data
->killNow
)
558 int state
= verify_state(data
->pcmHandle
);
561 ERR("Invalid state detected: %s\n", snd_strerror(state
));
562 aluHandleDisconnect(Device
);
566 avail
= snd_pcm_avail_update(data
->pcmHandle
);
569 ERR("available update failed: %s\n", snd_strerror(avail
));
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
);
580 if((snd_pcm_uframes_t
)avail
< update_size
)
582 if(state
!= SND_PCM_STATE_RUNNING
)
584 err
= snd_pcm_start(data
->pcmHandle
);
587 ERR("start failed: %s\n", snd_strerror(err
));
591 if(snd_pcm_wait(data
->pcmHandle
, 1000) == 0)
592 ERR("Wait timeout... buffer size too low?\n");
596 ALCdevice_Lock(Device
);
597 WritePtr
= data
->buffer
;
598 avail
= snd_pcm_bytes_to_frames(data
->pcmHandle
, data
->size
);
599 aluMixData(Device
, WritePtr
, avail
);
603 int ret
= snd_pcm_writei(data
->pcmHandle
, WritePtr
, avail
);
611 ret
= snd_pcm_recover(data
->pcmHandle
, ret
, 1);
618 WritePtr
+= snd_pcm_frames_to_bytes(data
->pcmHandle
, ret
);
625 ret
= snd_pcm_prepare(data
->pcmHandle
);
630 ALCdevice_Unlock(Device
);
636 static ALCenum
alsa_open_playback(ALCdevice
*device
, const ALCchar
*deviceName
)
638 const char *driver
= NULL
;
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
;
657 if(idx
== numDevNames
)
658 return ALC_INVALID_VALUE
;
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
);
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
;
684 static void alsa_close_playback(ALCdevice
*device
)
686 alsa_data
*data
= (alsa_data
*)device
->ExtraData
;
688 snd_pcm_close(data
->pcmHandle
);
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
;
709 switch(device
->FmtType
)
712 format
= SND_PCM_FORMAT_S8
;
715 format
= SND_PCM_FORMAT_U8
;
718 format
= SND_PCM_FORMAT_S16
;
721 format
= SND_PCM_FORMAT_U16
;
724 format
= SND_PCM_FORMAT_S32
;
727 format
= SND_PCM_FORMAT_U32
;
730 format
= SND_PCM_FORMAT_FLOAT
;
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)
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
;
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
},
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
;
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
[] = {
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
];
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
);
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
));
825 snd_pcm_sw_params_free(sp
);
828 device
->NumUpdates
= periods
;
829 device
->UpdateSize
= periodSizeInFrames
;
830 device
->Frequency
= rate
;
832 SetDefaultChannelOrder(device
);
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
);
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
;
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
));
857 snd_pcm_hw_params_free(hp
);
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
);
866 ERR("buffer malloc failed\n");
869 data
->thread
= StartThread(ALSANoMMapProc
, device
);
873 err
= snd_pcm_prepare(data
->pcmHandle
);
876 ERR("snd_pcm_prepare(data->pcmHandle) failed: %s\n", snd_strerror(err
));
879 data
->thread
= StartThread(ALSAProc
, device
);
881 if(data
->thread
== NULL
)
883 ERR("Could not create playback thread\n");
892 ERR("%s failed: %s\n", funcerr
, snd_strerror(err
));
893 if(hp
) snd_pcm_hw_params_free(hp
);
897 static void alsa_stop_playback(ALCdevice
*device
)
899 alsa_data
*data
= (alsa_data
*)device
->ExtraData
;
904 StopThread(data
->thread
);
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
;
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
;
940 if(idx
== numCaptureDevNames
)
941 return ALC_INVALID_VALUE
;
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
);
954 ERR("Could not open capture device '%s': %s\n", driver
, snd_strerror(err
));
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();
963 switch(Device
->FmtType
)
966 format
= SND_PCM_FORMAT_S8
;
969 format
= SND_PCM_FORMAT_U8
;
972 format
= SND_PCM_FORMAT_S16
;
975 format
= SND_PCM_FORMAT_U16
;
978 format
= SND_PCM_FORMAT_S32
;
981 format
= SND_PCM_FORMAT_U32
;
984 format
= SND_PCM_FORMAT_FLOAT
;
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");
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
));
1018 snd_pcm_hw_params_free(hp
);
1023 data
->ring
= CreateRingBuffer(FrameSizeFromDevFmt(Device
->FmtChans
, Device
->FmtType
),
1024 Device
->UpdateSize
*Device
->NumUpdates
);
1027 ERR("ring buffer create failed\n");
1031 data
->size
= snd_pcm_frames_to_bytes(data
->pcmHandle
, periodSizeInFrames
);
1032 data
->buffer
= malloc(data
->size
);
1035 ERR("buffer malloc failed\n");
1040 Device
->DeviceName
= strdup(deviceName
);
1042 Device
->ExtraData
= data
;
1043 return ALC_NO_ERROR
;
1046 ERR("%s failed: %s\n", funcerr
, snd_strerror(err
));
1047 if(hp
) snd_pcm_hw_params_free(hp
);
1051 DestroyRingBuffer(data
->ring
);
1052 snd_pcm_close(data
->pcmHandle
);
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
);
1068 Device
->ExtraData
= NULL
;
1071 static void alsa_start_capture(ALCdevice
*Device
)
1073 alsa_data
*data
= (alsa_data
*)Device
->ExtraData
;
1076 err
= snd_pcm_start(data
->pcmHandle
);
1079 ERR("start failed: %s\n", snd_strerror(err
));
1080 aluHandleDisconnect(Device
);
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
;
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;
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
);
1118 data
->buffer
= NULL
;
1121 amt
= snd_pcm_bytes_to_frames(data
->pcmHandle
, amt
);
1123 else if(data
->doCapture
)
1124 amt
= snd_pcm_readi(data
->pcmHandle
, Buffer
, Samples
);
1127 ERR("read error: %s\n", snd_strerror(amt
));
1131 if((amt
=snd_pcm_recover(data
->pcmHandle
, amt
, 1)) >= 0)
1133 amt
= snd_pcm_start(data
->pcmHandle
);
1135 amt
= snd_pcm_avail_update(data
->pcmHandle
);
1139 ERR("restore error: %s\n", snd_strerror(amt
));
1140 aluHandleDisconnect(Device
);
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
)
1150 Buffer
= (ALbyte
*)Buffer
+ amt
;
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
);
1169 ERR("avail update failed: %s\n", snd_strerror(avail
));
1171 if((avail
=snd_pcm_recover(data
->pcmHandle
, avail
, 1)) >= 0)
1174 avail
= snd_pcm_start(data
->pcmHandle
);
1176 avail
= snd_pcm_avail_update(data
->pcmHandle
);
1180 ERR("restore error: %s\n", snd_strerror(avail
));
1181 aluHandleDisconnect(Device
);
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
;
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
);
1203 ERR("read error: %s\n", snd_strerror(amt
));
1207 if((amt
=snd_pcm_recover(data
->pcmHandle
, amt
, 1)) >= 0)
1210 amt
= snd_pcm_start(data
->pcmHandle
);
1212 amt
= snd_pcm_avail_update(data
->pcmHandle
);
1216 ERR("restore error: %s\n", snd_strerror(amt
));
1217 aluHandleDisconnect(Device
);
1224 WriteRingBuffer(data
->ring
, data
->buffer
, amt
);
1228 return RingBufferSize(data
->ring
);
1231 static void alsa_stop_capture(ALCdevice
*Device
)
1233 alsa_data
*data
= (alsa_data
*)Device
->ExtraData
;
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. */
1248 size
= snd_pcm_frames_to_bytes(data
->pcmHandle
, avail
);
1249 ptr
= realloc(data
->buffer
, size
);
1253 alsa_capture_samples(Device
, data
->buffer
, avail
);
1257 err
= snd_pcm_drop(data
->pcmHandle
);
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;
1270 if((err
=snd_pcm_delay(data
->pcmHandle
, &delay
)) < 0)
1272 ERR("Failed to get pcm delay: %s\n", snd_strerror(err
));
1275 return maxi64((ALint64
)delay
*1000000000/device
->Frequency
, 0);
1279 static const BackendFuncs alsa_funcs
= {
1281 alsa_close_playback
,
1282 alsa_reset_playback
,
1283 alsa_start_playback
,
1289 alsa_capture_samples
,
1290 alsa_available_samples
,
1291 ALCdevice_LockDefault
,
1292 ALCdevice_UnlockDefault
,
1296 ALCboolean
alc_alsa_init(BackendFuncs
*func_list
)
1300 *func_list
= alsa_funcs
;
1304 void alc_alsa_deinit(void)
1308 for(i
= 0;i
< numDevNames
;++i
)
1310 free(allDevNameMap
[i
].name
);
1311 free(allDevNameMap
[i
].device
);
1313 free(allDevNameMap
);
1314 allDevNameMap
= NULL
;
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;
1328 CloseLib(alsa_handle
);
1333 void alc_alsa_probe(enum DevProbe 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
);
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
);