2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2012 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "SDL_config.h"
24 /* Allow access to a raw mixing buffer */
26 #include <sys/types.h>
27 #include <signal.h> /* For kill() */
29 #include "SDL_timer.h"
30 #include "SDL_audio.h"
31 #include "../SDL_audiomem.h"
32 #include "../SDL_audio_c.h"
33 #include "SDL_alsa_audio.h"
35 #ifdef SDL_AUDIO_DRIVER_ALSA_DYNAMIC
37 #include "SDL_loadso.h"
43 /* The tag name used by ALSA audio */
44 #define DRIVER_NAME "alsa"
46 /* Audio driver functions */
47 static int ALSA_OpenAudio(_THIS, SDL_AudioSpec *spec);
48 static void ALSA_WaitAudio(_THIS);
49 static void ALSA_PlayAudio(_THIS);
50 static Uint8 *ALSA_GetAudioBuf(_THIS);
51 static void ALSA_CloseAudio(_THIS);
53 #ifdef SDL_AUDIO_DRIVER_ALSA_DYNAMIC
55 static const char *alsa_library = SDL_AUDIO_DRIVER_ALSA_DYNAMIC;
56 static void *alsa_handle = NULL;
57 static int alsa_loaded = 0;
59 static int (*SDL_NAME(snd_pcm_open))(snd_pcm_t **pcm, const char *name, snd_pcm_stream_t stream, int mode);
60 static int (*SDL_NAME(snd_pcm_close))(snd_pcm_t *pcm);
61 static snd_pcm_sframes_t (*SDL_NAME(snd_pcm_writei))(snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size);
62 static int (*SDL_NAME(snd_pcm_recover))(snd_pcm_t *pcm, int err, int silent);
63 static int (*SDL_NAME(snd_pcm_prepare))(snd_pcm_t *pcm);
64 static int (*SDL_NAME(snd_pcm_drain))(snd_pcm_t *pcm);
65 static const char *(*SDL_NAME(snd_strerror))(int errnum);
66 static size_t (*SDL_NAME(snd_pcm_hw_params_sizeof))(void);
67 static size_t (*SDL_NAME(snd_pcm_sw_params_sizeof))(void);
68 static void (*SDL_NAME(snd_pcm_hw_params_copy))(snd_pcm_hw_params_t *dst, const snd_pcm_hw_params_t *src);
69 static int (*SDL_NAME(snd_pcm_hw_params_any))(snd_pcm_t *pcm, snd_pcm_hw_params_t *params);
70 static int (*SDL_NAME(snd_pcm_hw_params_set_access))(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_access_t access);
71 static int (*SDL_NAME(snd_pcm_hw_params_set_format))(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_format_t val);
72 static int (*SDL_NAME(snd_pcm_hw_params_set_channels))(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val);
73 static int (*SDL_NAME(snd_pcm_hw_params_get_channels))(const snd_pcm_hw_params_t *params, unsigned int *val);
74 static int (*SDL_NAME(snd_pcm_hw_params_set_rate_near))(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
75 static int (*SDL_NAME(snd_pcm_hw_params_set_period_size_near))(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val, int *dir);
76 static int (*SDL_NAME(snd_pcm_hw_params_get_period_size))(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *frames, int *dir);
77 static int (*SDL_NAME(snd_pcm_hw_params_set_periods_near))(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
78 static int (*SDL_NAME(snd_pcm_hw_params_get_periods))(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
79 static int (*SDL_NAME(snd_pcm_hw_params_set_buffer_size_near))(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val);
80 static int (*SDL_NAME(snd_pcm_hw_params_get_buffer_size))(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val);
81 static int (*SDL_NAME(snd_pcm_hw_params))(snd_pcm_t *pcm, snd_pcm_hw_params_t *params);
84 static int (*SDL_NAME(snd_pcm_sw_params_set_avail_min))(snd_pcm_t *pcm, snd_pcm_sw_params_t *swparams, snd_pcm_uframes_t val);
85 static int (*SDL_NAME(snd_pcm_sw_params_current))(snd_pcm_t *pcm, snd_pcm_sw_params_t *swparams);
86 static int (*SDL_NAME(snd_pcm_sw_params_set_start_threshold))(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val);
87 static int (*SDL_NAME(snd_pcm_sw_params))(snd_pcm_t *pcm, snd_pcm_sw_params_t *params);
88 static int (*SDL_NAME(snd_pcm_nonblock))(snd_pcm_t *pcm, int nonblock);
89 static int (*SDL_NAME(snd_pcm_wait))(snd_pcm_t *pcm, int timeout);
90 #define snd_pcm_hw_params_sizeof SDL_NAME(snd_pcm_hw_params_sizeof)
91 #define snd_pcm_sw_params_sizeof SDL_NAME(snd_pcm_sw_params_sizeof)
93 /* cast funcs to char* first, to please GCC's strict aliasing rules. */
97 } alsa_functions[] = {
98 { "snd_pcm_open", (void**)(char*)&SDL_NAME(snd_pcm_open) },
99 { "snd_pcm_close", (void**)(char*)&SDL_NAME(snd_pcm_close) },
100 { "snd_pcm_writei", (void**)(char*)&SDL_NAME(snd_pcm_writei) },
101 { "snd_pcm_recover", (void**)(char*)&SDL_NAME(snd_pcm_recover) },
102 { "snd_pcm_prepare", (void**)(char*)&SDL_NAME(snd_pcm_prepare) },
103 { "snd_pcm_drain", (void**)(char*)&SDL_NAME(snd_pcm_drain) },
104 { "snd_strerror", (void**)(char*)&SDL_NAME(snd_strerror) },
105 { "snd_pcm_hw_params_sizeof", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_sizeof) },
106 { "snd_pcm_sw_params_sizeof", (void**)(char*)&SDL_NAME(snd_pcm_sw_params_sizeof) },
107 { "snd_pcm_hw_params_copy", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_copy) },
108 { "snd_pcm_hw_params_any", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_any) },
109 { "snd_pcm_hw_params_set_access", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_set_access) },
110 { "snd_pcm_hw_params_set_format", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_set_format) },
111 { "snd_pcm_hw_params_set_channels", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_set_channels) },
112 { "snd_pcm_hw_params_get_channels", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_get_channels) },
113 { "snd_pcm_hw_params_set_rate_near", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_set_rate_near) },
114 { "snd_pcm_hw_params_set_period_size_near", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_set_period_size_near) },
115 { "snd_pcm_hw_params_get_period_size", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_get_period_size) },
116 { "snd_pcm_hw_params_set_periods_near", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_set_periods_near) },
117 { "snd_pcm_hw_params_get_periods", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_get_periods) },
118 { "snd_pcm_hw_params_set_buffer_size_near", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_set_buffer_size_near) },
119 { "snd_pcm_hw_params_get_buffer_size", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_get_buffer_size) },
120 { "snd_pcm_hw_params", (void**)(char*)&SDL_NAME(snd_pcm_hw_params) },
121 { "snd_pcm_sw_params_set_avail_min", (void**)(char*)&SDL_NAME(snd_pcm_sw_params_set_avail_min) },
122 { "snd_pcm_sw_params_current", (void**)(char*)&SDL_NAME(snd_pcm_sw_params_current) },
123 { "snd_pcm_sw_params_set_start_threshold", (void**)(char*)&SDL_NAME(snd_pcm_sw_params_set_start_threshold) },
124 { "snd_pcm_sw_params", (void**)(char*)&SDL_NAME(snd_pcm_sw_params) },
125 { "snd_pcm_nonblock", (void**)(char*)&SDL_NAME(snd_pcm_nonblock) },
126 { "snd_pcm_wait", (void**)(char*)&SDL_NAME(snd_pcm_wait) },
129 static void UnloadALSALibrary(void) {
131 SDL_UnloadObject(alsa_handle);
137 static int LoadALSALibrary(void) {
140 alsa_handle = SDL_LoadObject(alsa_library);
144 for (i = 0; i < SDL_arraysize(alsa_functions); i++) {
145 *alsa_functions[i].func = SDL_LoadFunction(alsa_handle,alsa_functions[i].name);
146 if (!*alsa_functions[i].func) {
158 static void UnloadALSALibrary(void) {
162 static int LoadALSALibrary(void) {
166 #endif /* SDL_AUDIO_DRIVER_ALSA_DYNAMIC */
168 static const char *get_audio_device(int channels)
172 device = SDL_getenv("AUDIODEV"); /* Is there a standard variable name? */
173 if ( device == NULL ) {
176 device = "plug:surround51";
179 device = "plug:surround40";
189 /* Audio driver bootstrap functions */
191 static int Audio_Available(void)
198 if (LoadALSALibrary() < 0) {
201 status = SDL_NAME(snd_pcm_open)(&handle, get_audio_device(2), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
204 SDL_NAME(snd_pcm_close)(handle);
210 static void Audio_DeleteDevice(SDL_AudioDevice *device)
212 SDL_free(device->hidden);
217 static SDL_AudioDevice *Audio_CreateDevice(int devindex)
219 SDL_AudioDevice *this;
221 /* Initialize all variables that we clean on shutdown */
223 this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
225 SDL_memset(this, 0, (sizeof *this));
226 this->hidden = (struct SDL_PrivateAudioData *)
227 SDL_malloc((sizeof *this->hidden));
229 if ( (this == NULL) || (this->hidden == NULL) ) {
236 SDL_memset(this->hidden, 0, (sizeof *this->hidden));
238 /* Set the function pointers */
239 this->OpenAudio = ALSA_OpenAudio;
240 this->WaitAudio = ALSA_WaitAudio;
241 this->PlayAudio = ALSA_PlayAudio;
242 this->GetAudioBuf = ALSA_GetAudioBuf;
243 this->CloseAudio = ALSA_CloseAudio;
245 this->free = Audio_DeleteDevice;
250 AudioBootStrap ALSA_bootstrap = {
251 DRIVER_NAME, "ALSA PCM audio",
252 Audio_Available, Audio_CreateDevice
255 /* This function waits until it is possible to write a full sound buffer */
256 static void ALSA_WaitAudio(_THIS)
258 /* We're in blocking mode, so there's nothing to do here */
263 * http://bugzilla.libsdl.org/show_bug.cgi?id=110
264 * "For Linux ALSA, this is FL-FR-RL-RR-C-LFE
265 * and for Windows DirectX [and CoreAudio], this is FL-FR-C-LFE-RL-RR"
268 T *ptr = (T *) mixbuf; \
270 for (i = 0; i < this->spec.samples; i++, ptr += 6) { \
272 tmp = ptr[2]; ptr[2] = ptr[4]; ptr[4] = tmp; \
273 tmp = ptr[3]; ptr[3] = ptr[5]; ptr[5] = tmp; \
276 static __inline__ void swizzle_alsa_channels_6_64bit(_THIS) { SWIZ6(Uint64); }
277 static __inline__ void swizzle_alsa_channels_6_32bit(_THIS) { SWIZ6(Uint32); }
278 static __inline__ void swizzle_alsa_channels_6_16bit(_THIS) { SWIZ6(Uint16); }
279 static __inline__ void swizzle_alsa_channels_6_8bit(_THIS) { SWIZ6(Uint8); }
285 * Called right before feeding this->mixbuf to the hardware. Swizzle channels
286 * from Windows/Mac order to the format alsalib will want.
288 static __inline__ void swizzle_alsa_channels(_THIS)
290 if (this->spec.channels == 6) {
291 const Uint16 fmtsize = (this->spec.format & 0xFF); /* bits/channel. */
293 swizzle_alsa_channels_6_16bit(this);
294 else if (fmtsize == 8)
295 swizzle_alsa_channels_6_8bit(this);
296 else if (fmtsize == 32)
297 swizzle_alsa_channels_6_32bit(this);
298 else if (fmtsize == 64)
299 swizzle_alsa_channels_6_64bit(this);
302 /* !!! FIXME: update this for 7.1 if needed, later. */
306 static void ALSA_PlayAudio(_THIS)
309 snd_pcm_uframes_t frames_left;
310 const Uint8 *sample_buf = (const Uint8 *) mixbuf;
311 const int frame_size = (((int) (this->spec.format & 0xFF)) / 8) * this->spec.channels;
313 swizzle_alsa_channels(this);
315 frames_left = ((snd_pcm_uframes_t) this->spec.samples);
317 while ( frames_left > 0 && this->enabled ) {
318 /* This works, but needs more testing before going live */
319 /*SDL_NAME(snd_pcm_wait)(pcm_handle, -1);*/
321 status = SDL_NAME(snd_pcm_writei)(pcm_handle, sample_buf, frames_left);
323 if ( status == -EAGAIN ) {
324 /* Apparently snd_pcm_recover() doesn't handle this case - does it assume snd_pcm_wait() above? */
328 status = SDL_NAME(snd_pcm_recover)(pcm_handle, status, 0);
330 /* Hmm, not much we can do - abort */
331 fprintf(stderr, "ALSA write failed (unrecoverable): %s\n", SDL_NAME(snd_strerror)(status));
337 sample_buf += status * frame_size;
338 frames_left -= status;
342 static Uint8 *ALSA_GetAudioBuf(_THIS)
347 static void ALSA_CloseAudio(_THIS)
349 if ( mixbuf != NULL ) {
350 SDL_FreeAudioMem(mixbuf);
354 SDL_NAME(snd_pcm_drain)(pcm_handle);
355 SDL_NAME(snd_pcm_close)(pcm_handle);
360 static int ALSA_finalize_hardware(_THIS, SDL_AudioSpec *spec, snd_pcm_hw_params_t *hwparams, int override)
363 snd_pcm_uframes_t bufsize;
365 /* "set" the hardware with the desired parameters */
366 status = SDL_NAME(snd_pcm_hw_params)(pcm_handle, hwparams);
371 /* Get samples for the actual buffer size */
372 status = SDL_NAME(snd_pcm_hw_params_get_buffer_size)(hwparams, &bufsize);
376 if ( !override && bufsize != spec->samples * 2 ) {
380 /* FIXME: Is this safe to do? */
381 spec->samples = bufsize / 2;
383 /* This is useful for debugging */
384 if ( getenv("SDL_AUDIO_ALSA_DEBUG") ) {
385 snd_pcm_uframes_t persize = 0;
386 unsigned int periods = 0;
388 SDL_NAME(snd_pcm_hw_params_get_period_size)(hwparams, &persize, NULL);
389 SDL_NAME(snd_pcm_hw_params_get_periods)(hwparams, &periods, NULL);
391 fprintf(stderr, "ALSA: period size = %ld, periods = %u, buffer size = %lu\n", persize, periods, bufsize);
396 static int ALSA_set_period_size(_THIS, SDL_AudioSpec *spec, snd_pcm_hw_params_t *params, int override)
400 snd_pcm_hw_params_t *hwparams;
401 snd_pcm_uframes_t frames;
402 unsigned int periods;
404 /* Copy the hardware parameters for this setup */
405 snd_pcm_hw_params_alloca(&hwparams);
406 SDL_NAME(snd_pcm_hw_params_copy)(hwparams, params);
409 env = getenv("SDL_AUDIO_ALSA_SET_PERIOD_SIZE");
411 override = SDL_atoi(env);
412 if ( override == 0 ) {
418 frames = spec->samples;
419 status = SDL_NAME(snd_pcm_hw_params_set_period_size_near)(pcm_handle, hwparams, &frames, NULL);
425 status = SDL_NAME(snd_pcm_hw_params_set_periods_near)(pcm_handle, hwparams, &periods, NULL);
430 return ALSA_finalize_hardware(this, spec, hwparams, override);
433 static int ALSA_set_buffer_size(_THIS, SDL_AudioSpec *spec, snd_pcm_hw_params_t *params, int override)
437 snd_pcm_hw_params_t *hwparams;
438 snd_pcm_uframes_t frames;
440 /* Copy the hardware parameters for this setup */
441 snd_pcm_hw_params_alloca(&hwparams);
442 SDL_NAME(snd_pcm_hw_params_copy)(hwparams, params);
445 env = getenv("SDL_AUDIO_ALSA_SET_BUFFER_SIZE");
447 override = SDL_atoi(env);
448 if ( override == 0 ) {
454 frames = spec->samples * 2;
455 status = SDL_NAME(snd_pcm_hw_params_set_buffer_size_near)(pcm_handle, hwparams, &frames);
460 return ALSA_finalize_hardware(this, spec, hwparams, override);
463 static int ALSA_OpenAudio(_THIS, SDL_AudioSpec *spec)
466 snd_pcm_hw_params_t *hwparams;
467 snd_pcm_sw_params_t *swparams;
468 snd_pcm_format_t format;
470 unsigned int channels;
473 /* Open the audio device */
474 /* Name of device should depend on # channels in spec */
475 status = SDL_NAME(snd_pcm_open)(&pcm_handle, get_audio_device(spec->channels), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
478 SDL_SetError("Couldn't open audio device: %s", SDL_NAME(snd_strerror)(status));
482 /* Figure out what the hardware is capable of */
483 snd_pcm_hw_params_alloca(&hwparams);
484 status = SDL_NAME(snd_pcm_hw_params_any)(pcm_handle, hwparams);
486 SDL_SetError("Couldn't get hardware config: %s", SDL_NAME(snd_strerror)(status));
487 ALSA_CloseAudio(this);
491 /* SDL only uses interleaved sample output */
492 status = SDL_NAME(snd_pcm_hw_params_set_access)(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
494 SDL_SetError("Couldn't set interleaved access: %s", SDL_NAME(snd_strerror)(status));
495 ALSA_CloseAudio(this);
499 /* Try for a closest match on audio format */
501 for ( test_format = SDL_FirstAudioFormat(spec->format);
502 test_format && (status < 0); ) {
503 switch ( test_format ) {
505 format = SND_PCM_FORMAT_U8;
508 format = SND_PCM_FORMAT_S8;
511 format = SND_PCM_FORMAT_S16_LE;
514 format = SND_PCM_FORMAT_S16_BE;
517 format = SND_PCM_FORMAT_U16_LE;
520 format = SND_PCM_FORMAT_U16_BE;
527 status = SDL_NAME(snd_pcm_hw_params_set_format)(pcm_handle, hwparams, format);
530 test_format = SDL_NextAudioFormat();
534 SDL_SetError("Couldn't find any hardware audio formats");
535 ALSA_CloseAudio(this);
538 spec->format = test_format;
540 /* Set the number of channels */
541 status = SDL_NAME(snd_pcm_hw_params_set_channels)(pcm_handle, hwparams, spec->channels);
542 channels = spec->channels;
544 status = SDL_NAME(snd_pcm_hw_params_get_channels)(hwparams, &channels);
546 SDL_SetError("Couldn't set audio channels");
547 ALSA_CloseAudio(this);
550 spec->channels = channels;
553 /* Set the audio rate */
556 status = SDL_NAME(snd_pcm_hw_params_set_rate_near)(pcm_handle, hwparams, &rate, NULL);
558 SDL_SetError("Couldn't set audio frequency: %s", SDL_NAME(snd_strerror)(status));
559 ALSA_CloseAudio(this);
564 /* Set the buffer size, in samples */
565 if ( ALSA_set_period_size(this, spec, hwparams, 0) < 0 &&
566 ALSA_set_buffer_size(this, spec, hwparams, 0) < 0 ) {
567 /* Failed to set desired buffer size, do the best you can... */
568 if ( ALSA_set_period_size(this, spec, hwparams, 1) < 0 ) {
569 SDL_SetError("Couldn't set hardware audio parameters: %s", SDL_NAME(snd_strerror)(status));
570 ALSA_CloseAudio(this);
575 /* Set the software parameters */
576 snd_pcm_sw_params_alloca(&swparams);
577 status = SDL_NAME(snd_pcm_sw_params_current)(pcm_handle, swparams);
579 SDL_SetError("Couldn't get software config: %s", SDL_NAME(snd_strerror)(status));
580 ALSA_CloseAudio(this);
583 status = SDL_NAME(snd_pcm_sw_params_set_avail_min)(pcm_handle, swparams, spec->samples);
585 SDL_SetError("Couldn't set minimum available samples: %s", SDL_NAME(snd_strerror)(status));
586 ALSA_CloseAudio(this);
589 status = SDL_NAME(snd_pcm_sw_params_set_start_threshold)(pcm_handle, swparams, 1);
591 SDL_SetError("Couldn't set start threshold: %s", SDL_NAME(snd_strerror)(status));
592 ALSA_CloseAudio(this);
595 status = SDL_NAME(snd_pcm_sw_params)(pcm_handle, swparams);
597 SDL_SetError("Couldn't set software audio parameters: %s", SDL_NAME(snd_strerror)(status));
598 ALSA_CloseAudio(this);
602 /* Calculate the final parameters for this audio specification */
603 SDL_CalculateAudioSpec(spec);
605 /* Allocate mixing buffer */
607 mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen);
608 if ( mixbuf == NULL ) {
609 ALSA_CloseAudio(this);
612 SDL_memset(mixbuf, spec->silence, spec->size);
614 /* Switch to blocking mode for playback */
615 SDL_NAME(snd_pcm_nonblock)(pcm_handle, 0);
617 /* We're ready to rock and roll. :-) */