2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 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_current))(snd_pcm_t *pcm, snd_pcm_sw_params_t *swparams);
85 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);
86 static int (*SDL_NAME(snd_pcm_sw_params))(snd_pcm_t *pcm, snd_pcm_sw_params_t *params);
87 static int (*SDL_NAME(snd_pcm_nonblock))(snd_pcm_t *pcm, int nonblock);
88 static int (*SDL_NAME(snd_pcm_wait))(snd_pcm_t *pcm, int timeout);
89 #define snd_pcm_hw_params_sizeof SDL_NAME(snd_pcm_hw_params_sizeof)
90 #define snd_pcm_sw_params_sizeof SDL_NAME(snd_pcm_sw_params_sizeof)
92 /* cast funcs to char* first, to please GCC's strict aliasing rules. */
96 } alsa_functions[] = {
97 { "snd_pcm_open", (void**)(char*)&SDL_NAME(snd_pcm_open) },
98 { "snd_pcm_close", (void**)(char*)&SDL_NAME(snd_pcm_close) },
99 { "snd_pcm_writei", (void**)(char*)&SDL_NAME(snd_pcm_writei) },
100 { "snd_pcm_recover", (void**)(char*)&SDL_NAME(snd_pcm_recover) },
101 { "snd_pcm_prepare", (void**)(char*)&SDL_NAME(snd_pcm_prepare) },
102 { "snd_pcm_drain", (void**)(char*)&SDL_NAME(snd_pcm_drain) },
103 { "snd_strerror", (void**)(char*)&SDL_NAME(snd_strerror) },
104 { "snd_pcm_hw_params_sizeof", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_sizeof) },
105 { "snd_pcm_sw_params_sizeof", (void**)(char*)&SDL_NAME(snd_pcm_sw_params_sizeof) },
106 { "snd_pcm_hw_params_copy", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_copy) },
107 { "snd_pcm_hw_params_any", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_any) },
108 { "snd_pcm_hw_params_set_access", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_set_access) },
109 { "snd_pcm_hw_params_set_format", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_set_format) },
110 { "snd_pcm_hw_params_set_channels", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_set_channels) },
111 { "snd_pcm_hw_params_get_channels", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_get_channels) },
112 { "snd_pcm_hw_params_set_rate_near", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_set_rate_near) },
113 { "snd_pcm_hw_params_set_period_size_near", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_set_period_size_near) },
114 { "snd_pcm_hw_params_get_period_size", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_get_period_size) },
115 { "snd_pcm_hw_params_set_periods_near", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_set_periods_near) },
116 { "snd_pcm_hw_params_get_periods", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_get_periods) },
117 { "snd_pcm_hw_params_set_buffer_size_near", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_set_buffer_size_near) },
118 { "snd_pcm_hw_params_get_buffer_size", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_get_buffer_size) },
119 { "snd_pcm_hw_params", (void**)(char*)&SDL_NAME(snd_pcm_hw_params) },
120 { "snd_pcm_sw_params_current", (void**)(char*)&SDL_NAME(snd_pcm_sw_params_current) },
121 { "snd_pcm_sw_params_set_start_threshold", (void**)(char*)&SDL_NAME(snd_pcm_sw_params_set_start_threshold) },
122 { "snd_pcm_sw_params", (void**)(char*)&SDL_NAME(snd_pcm_sw_params) },
123 { "snd_pcm_nonblock", (void**)(char*)&SDL_NAME(snd_pcm_nonblock) },
124 { "snd_pcm_wait", (void**)(char*)&SDL_NAME(snd_pcm_wait) },
127 static void UnloadALSALibrary(void) {
129 SDL_UnloadObject(alsa_handle);
135 static int LoadALSALibrary(void) {
138 alsa_handle = SDL_LoadObject(alsa_library);
142 for (i = 0; i < SDL_arraysize(alsa_functions); i++) {
143 *alsa_functions[i].func = SDL_LoadFunction(alsa_handle,alsa_functions[i].name);
144 if (!*alsa_functions[i].func) {
156 static void UnloadALSALibrary(void) {
160 static int LoadALSALibrary(void) {
164 #endif /* SDL_AUDIO_DRIVER_ALSA_DYNAMIC */
166 static const char *get_audio_device(int channels)
170 device = SDL_getenv("AUDIODEV"); /* Is there a standard variable name? */
171 if ( device == NULL ) {
174 device = "plug:surround51";
177 device = "plug:surround40";
187 /* Audio driver bootstrap functions */
189 static int Audio_Available(void)
196 if (LoadALSALibrary() < 0) {
199 status = SDL_NAME(snd_pcm_open)(&handle, get_audio_device(2), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
202 SDL_NAME(snd_pcm_close)(handle);
208 static void Audio_DeleteDevice(SDL_AudioDevice *device)
210 SDL_free(device->hidden);
215 static SDL_AudioDevice *Audio_CreateDevice(int devindex)
217 SDL_AudioDevice *this;
219 /* Initialize all variables that we clean on shutdown */
221 this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
223 SDL_memset(this, 0, (sizeof *this));
224 this->hidden = (struct SDL_PrivateAudioData *)
225 SDL_malloc((sizeof *this->hidden));
227 if ( (this == NULL) || (this->hidden == NULL) ) {
234 SDL_memset(this->hidden, 0, (sizeof *this->hidden));
236 /* Set the function pointers */
237 this->OpenAudio = ALSA_OpenAudio;
238 this->WaitAudio = ALSA_WaitAudio;
239 this->PlayAudio = ALSA_PlayAudio;
240 this->GetAudioBuf = ALSA_GetAudioBuf;
241 this->CloseAudio = ALSA_CloseAudio;
243 this->free = Audio_DeleteDevice;
248 AudioBootStrap ALSA_bootstrap = {
249 DRIVER_NAME, "ALSA PCM audio",
250 Audio_Available, Audio_CreateDevice
253 /* This function waits until it is possible to write a full sound buffer */
254 static void ALSA_WaitAudio(_THIS)
256 /* We're in blocking mode, so there's nothing to do here */
261 * http://bugzilla.libsdl.org/show_bug.cgi?id=110
262 * "For Linux ALSA, this is FL-FR-RL-RR-C-LFE
263 * and for Windows DirectX [and CoreAudio], this is FL-FR-C-LFE-RL-RR"
266 T *ptr = (T *) mixbuf; \
267 const Uint32 count = (this->spec.samples / 6); \
269 for (i = 0; i < count; i++, ptr += 6) { \
271 tmp = ptr[2]; ptr[2] = ptr[4]; ptr[4] = tmp; \
272 tmp = ptr[3]; ptr[3] = ptr[5]; ptr[5] = tmp; \
275 static __inline__ void swizzle_alsa_channels_6_64bit(_THIS) { SWIZ6(Uint64); }
276 static __inline__ void swizzle_alsa_channels_6_32bit(_THIS) { SWIZ6(Uint32); }
277 static __inline__ void swizzle_alsa_channels_6_16bit(_THIS) { SWIZ6(Uint16); }
278 static __inline__ void swizzle_alsa_channels_6_8bit(_THIS) { SWIZ6(Uint8); }
284 * Called right before feeding this->mixbuf to the hardware. Swizzle channels
285 * from Windows/Mac order to the format alsalib will want.
287 static __inline__ void swizzle_alsa_channels(_THIS)
289 if (this->spec.channels == 6) {
290 const Uint16 fmtsize = (this->spec.format & 0xFF); /* bits/channel. */
292 swizzle_alsa_channels_6_16bit(this);
293 else if (fmtsize == 8)
294 swizzle_alsa_channels_6_8bit(this);
295 else if (fmtsize == 32)
296 swizzle_alsa_channels_6_32bit(this);
297 else if (fmtsize == 64)
298 swizzle_alsa_channels_6_64bit(this);
301 /* !!! FIXME: update this for 7.1 if needed, later. */
305 static void ALSA_PlayAudio(_THIS)
308 snd_pcm_uframes_t frames_left;
309 const Uint8 *sample_buf = (const Uint8 *) mixbuf;
310 const int frame_size = (((int) (this->spec.format & 0xFF)) / 8) * this->spec.channels;
312 swizzle_alsa_channels(this);
314 frames_left = ((snd_pcm_uframes_t) this->spec.samples);
316 while ( frames_left > 0 && this->enabled ) {
317 /* This works, but needs more testing before going live */
318 /*SDL_NAME(snd_pcm_wait)(pcm_handle, -1);*/
320 status = SDL_NAME(snd_pcm_writei)(pcm_handle, sample_buf, frames_left);
322 if ( status == -EAGAIN ) {
323 /* Apparently snd_pcm_recover() doesn't handle this case - does it assume snd_pcm_wait() above? */
327 status = SDL_NAME(snd_pcm_recover)(pcm_handle, status, 0);
329 /* Hmm, not much we can do - abort */
330 fprintf(stderr, "ALSA write failed (unrecoverable): %s\n", SDL_NAME(snd_strerror)(status));
336 sample_buf += status * frame_size;
337 frames_left -= status;
341 static Uint8 *ALSA_GetAudioBuf(_THIS)
346 static void ALSA_CloseAudio(_THIS)
348 if ( mixbuf != NULL ) {
349 SDL_FreeAudioMem(mixbuf);
353 SDL_NAME(snd_pcm_drain)(pcm_handle);
354 SDL_NAME(snd_pcm_close)(pcm_handle);
359 static int ALSA_finalize_hardware(_THIS, SDL_AudioSpec *spec, snd_pcm_hw_params_t *hwparams, int override)
362 snd_pcm_uframes_t bufsize;
364 /* "set" the hardware with the desired parameters */
365 status = SDL_NAME(snd_pcm_hw_params)(pcm_handle, hwparams);
370 /* Get samples for the actual buffer size */
371 status = SDL_NAME(snd_pcm_hw_params_get_buffer_size)(hwparams, &bufsize);
375 if ( !override && bufsize != spec->samples * 2 ) {
379 /* FIXME: Is this safe to do? */
380 spec->samples = bufsize / 2;
382 /* This is useful for debugging */
383 if ( getenv("SDL_AUDIO_ALSA_DEBUG") ) {
384 snd_pcm_uframes_t persize = 0;
385 unsigned int periods = 0;
387 SDL_NAME(snd_pcm_hw_params_get_period_size)(hwparams, &persize, NULL);
388 SDL_NAME(snd_pcm_hw_params_get_periods)(hwparams, &periods, NULL);
390 fprintf(stderr, "ALSA: period size = %ld, periods = %u, buffer size = %lu\n", persize, periods, bufsize);
395 static int ALSA_set_period_size(_THIS, SDL_AudioSpec *spec, snd_pcm_hw_params_t *params, int override)
399 snd_pcm_hw_params_t *hwparams;
400 snd_pcm_uframes_t frames;
401 unsigned int periods;
403 /* Copy the hardware parameters for this setup */
404 snd_pcm_hw_params_alloca(&hwparams);
405 SDL_NAME(snd_pcm_hw_params_copy)(hwparams, params);
408 env = getenv("SDL_AUDIO_ALSA_SET_PERIOD_SIZE");
410 override = SDL_atoi(env);
411 if ( override == 0 ) {
417 frames = spec->samples;
418 status = SDL_NAME(snd_pcm_hw_params_set_period_size_near)(pcm_handle, hwparams, &frames, NULL);
424 status = SDL_NAME(snd_pcm_hw_params_set_periods_near)(pcm_handle, hwparams, &periods, NULL);
429 return ALSA_finalize_hardware(this, spec, hwparams, override);
432 static int ALSA_set_buffer_size(_THIS, SDL_AudioSpec *spec, snd_pcm_hw_params_t *params, int override)
436 snd_pcm_hw_params_t *hwparams;
437 snd_pcm_uframes_t frames;
439 /* Copy the hardware parameters for this setup */
440 snd_pcm_hw_params_alloca(&hwparams);
441 SDL_NAME(snd_pcm_hw_params_copy)(hwparams, params);
444 env = getenv("SDL_AUDIO_ALSA_SET_BUFFER_SIZE");
446 override = SDL_atoi(env);
447 if ( override == 0 ) {
453 frames = spec->samples * 2;
454 status = SDL_NAME(snd_pcm_hw_params_set_buffer_size_near)(pcm_handle, hwparams, &frames);
459 return ALSA_finalize_hardware(this, spec, hwparams, override);
462 static int ALSA_OpenAudio(_THIS, SDL_AudioSpec *spec)
465 snd_pcm_hw_params_t *hwparams;
466 snd_pcm_sw_params_t *swparams;
467 snd_pcm_format_t format;
469 unsigned int channels;
472 /* Open the audio device */
473 /* Name of device should depend on # channels in spec */
474 status = SDL_NAME(snd_pcm_open)(&pcm_handle, get_audio_device(spec->channels), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
477 SDL_SetError("Couldn't open audio device: %s", SDL_NAME(snd_strerror)(status));
481 /* Figure out what the hardware is capable of */
482 snd_pcm_hw_params_alloca(&hwparams);
483 status = SDL_NAME(snd_pcm_hw_params_any)(pcm_handle, hwparams);
485 SDL_SetError("Couldn't get hardware config: %s", SDL_NAME(snd_strerror)(status));
486 ALSA_CloseAudio(this);
490 /* SDL only uses interleaved sample output */
491 status = SDL_NAME(snd_pcm_hw_params_set_access)(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
493 SDL_SetError("Couldn't set interleaved access: %s", SDL_NAME(snd_strerror)(status));
494 ALSA_CloseAudio(this);
498 /* Try for a closest match on audio format */
500 for ( test_format = SDL_FirstAudioFormat(spec->format);
501 test_format && (status < 0); ) {
502 switch ( test_format ) {
504 format = SND_PCM_FORMAT_U8;
507 format = SND_PCM_FORMAT_S8;
510 format = SND_PCM_FORMAT_S16_LE;
513 format = SND_PCM_FORMAT_S16_BE;
516 format = SND_PCM_FORMAT_U16_LE;
519 format = SND_PCM_FORMAT_U16_BE;
526 status = SDL_NAME(snd_pcm_hw_params_set_format)(pcm_handle, hwparams, format);
529 test_format = SDL_NextAudioFormat();
533 SDL_SetError("Couldn't find any hardware audio formats");
534 ALSA_CloseAudio(this);
537 spec->format = test_format;
539 /* Set the number of channels */
540 status = SDL_NAME(snd_pcm_hw_params_set_channels)(pcm_handle, hwparams, spec->channels);
541 channels = spec->channels;
543 status = SDL_NAME(snd_pcm_hw_params_get_channels)(hwparams, &channels);
545 SDL_SetError("Couldn't set audio channels");
546 ALSA_CloseAudio(this);
549 spec->channels = channels;
552 /* Set the audio rate */
555 status = SDL_NAME(snd_pcm_hw_params_set_rate_near)(pcm_handle, hwparams, &rate, NULL);
557 SDL_SetError("Couldn't set audio frequency: %s", SDL_NAME(snd_strerror)(status));
558 ALSA_CloseAudio(this);
563 /* Set the buffer size, in samples */
564 if ( ALSA_set_period_size(this, spec, hwparams, 0) < 0 &&
565 ALSA_set_buffer_size(this, spec, hwparams, 0) < 0 ) {
566 /* Failed to set desired buffer size, do the best you can... */
567 if ( ALSA_set_period_size(this, spec, hwparams, 1) < 0 ) {
568 SDL_SetError("Couldn't set hardware audio parameters: %s", SDL_NAME(snd_strerror)(status));
569 ALSA_CloseAudio(this);
574 /* Set the software parameters */
575 snd_pcm_sw_params_alloca(&swparams);
576 status = SDL_NAME(snd_pcm_sw_params_current)(pcm_handle, swparams);
578 SDL_SetError("Couldn't get software config: %s", SDL_NAME(snd_strerror)(status));
579 ALSA_CloseAudio(this);
582 status = SDL_NAME(snd_pcm_sw_params_set_start_threshold)(pcm_handle, swparams, 1);
584 SDL_SetError("Couldn't set start threshold: %s", SDL_NAME(snd_strerror)(status));
585 ALSA_CloseAudio(this);
588 status = SDL_NAME(snd_pcm_sw_params)(pcm_handle, swparams);
590 SDL_SetError("Couldn't set software audio parameters: %s", SDL_NAME(snd_strerror)(status));
591 ALSA_CloseAudio(this);
595 /* Calculate the final parameters for this audio specification */
596 SDL_CalculateAudioSpec(spec);
598 /* Allocate mixing buffer */
600 mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen);
601 if ( mixbuf == NULL ) {
602 ALSA_CloseAudio(this);
605 SDL_memset(mixbuf, spec->silence, spec->size);
607 /* Switch to blocking mode for playback */
608 SDL_NAME(snd_pcm_nonblock)(pcm_handle, 0);
610 /* We're ready to rock and roll. :-) */