src/audio/alsa/SDL_alsa_audio.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 19 Mar 2006 10:48:48 +0000
changeset 1553 63fa37538842
parent 1552 b2462b772cce
child 1662 782fd950bd46
child 1878 d7c9d7f42881
permissions -rw-r--r--
Left the debug code more explicit
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@769
     3
    Copyright (C) 1997-2004 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* Allow access to a raw mixing buffer */
slouken@0
    25
slouken@0
    26
#include <sys/types.h>
slouken@1338
    27
#include <signal.h>	/* For kill() */
slouken@0
    28
slouken@1361
    29
#include "SDL_timer.h"
slouken@0
    30
#include "SDL_audio.h"
slouken@1361
    31
#include "../SDL_audiomem.h"
slouken@1361
    32
#include "../SDL_audio_c.h"
slouken@0
    33
#include "SDL_alsa_audio.h"
slouken@0
    34
slouken@1361
    35
#ifdef SDL_AUDIO_DRIVER_ALSA_DYNAMIC
slouken@865
    36
#include <dlfcn.h>
slouken@865
    37
#include "SDL_name.h"
slouken@865
    38
#include "SDL_loadso.h"
slouken@865
    39
#else
slouken@865
    40
#define SDL_NAME(X)	X
slouken@865
    41
#endif
slouken@865
    42
slouken@865
    43
slouken@0
    44
/* The tag name used by ALSA audio */
slouken@0
    45
#define DRIVER_NAME         "alsa"
slouken@0
    46
slouken@354
    47
/* The default ALSA audio driver */
slouken@765
    48
#define DEFAULT_DEVICE	"default"
slouken@0
    49
slouken@0
    50
/* Audio driver functions */
slouken@354
    51
static int ALSA_OpenAudio(_THIS, SDL_AudioSpec *spec);
slouken@354
    52
static void ALSA_WaitAudio(_THIS);
slouken@354
    53
static void ALSA_PlayAudio(_THIS);
slouken@354
    54
static Uint8 *ALSA_GetAudioBuf(_THIS);
slouken@354
    55
static void ALSA_CloseAudio(_THIS);
slouken@0
    56
slouken@1361
    57
#ifdef SDL_AUDIO_DRIVER_ALSA_DYNAMIC
slouken@865
    58
slouken@1361
    59
static const char *alsa_library = SDL_AUDIO_DRIVER_ALSA_DYNAMIC;
slouken@865
    60
static void *alsa_handle = NULL;
slouken@865
    61
static int alsa_loaded = 0;
slouken@865
    62
slouken@865
    63
static int (*SDL_snd_pcm_open)(snd_pcm_t **pcm, const char *name, snd_pcm_stream_t stream, int mode);
slouken@865
    64
static int (*SDL_NAME(snd_pcm_open))(snd_pcm_t **pcm, const char *name, snd_pcm_stream_t stream, int mode);
slouken@865
    65
static int (*SDL_NAME(snd_pcm_close))(snd_pcm_t *pcm);
slouken@865
    66
static snd_pcm_sframes_t (*SDL_NAME(snd_pcm_writei))(snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size);
slouken@865
    67
static int (*SDL_NAME(snd_pcm_resume))(snd_pcm_t *pcm);
slouken@865
    68
static int (*SDL_NAME(snd_pcm_prepare))(snd_pcm_t *pcm);
slouken@865
    69
static int (*SDL_NAME(snd_pcm_drain))(snd_pcm_t *pcm);
slouken@865
    70
static const char *(*SDL_NAME(snd_strerror))(int errnum);
slouken@865
    71
static size_t (*SDL_NAME(snd_pcm_hw_params_sizeof))(void);
slouken@1552
    72
static size_t (*SDL_NAME(snd_pcm_sw_params_sizeof))(void);
slouken@865
    73
static int (*SDL_NAME(snd_pcm_hw_params_any))(snd_pcm_t *pcm, snd_pcm_hw_params_t *params);
slouken@865
    74
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);
slouken@865
    75
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);
slouken@865
    76
static int (*SDL_NAME(snd_pcm_hw_params_set_channels))(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val);
slouken@865
    77
static int (*SDL_NAME(snd_pcm_hw_params_get_channels))(const snd_pcm_hw_params_t *params);
slouken@865
    78
static unsigned 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);
slouken@865
    79
static snd_pcm_uframes_t (*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);
slouken@1553
    80
static snd_pcm_sframes_t (*SDL_NAME(snd_pcm_hw_params_get_period_size))(const snd_pcm_hw_params_t *params);
slouken@865
    81
static unsigned 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);
slouken@1553
    82
static int (*SDL_NAME(snd_pcm_hw_params_get_periods))(snd_pcm_hw_params_t *params);
slouken@865
    83
static int (*SDL_NAME(snd_pcm_hw_params))(snd_pcm_t *pcm, snd_pcm_hw_params_t *params);
slouken@1552
    84
/*
slouken@1552
    85
*/
slouken@1552
    86
static int (*SDL_NAME(snd_pcm_sw_params_current))(snd_pcm_t *pcm, snd_pcm_sw_params_t *swparams);
slouken@1552
    87
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);
slouken@1552
    88
static int (*SDL_NAME(snd_pcm_sw_params_set_avail_min))(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val);
slouken@1552
    89
static int (*SDL_NAME(snd_pcm_sw_params))(snd_pcm_t *pcm, snd_pcm_sw_params_t *params);
slouken@1553
    90
static int (*SDL_NAME(snd_pcm_nonblock))(snd_pcm_t *pcm, int nonblock);
slouken@865
    91
#define snd_pcm_hw_params_sizeof SDL_NAME(snd_pcm_hw_params_sizeof)
slouken@1552
    92
#define snd_pcm_sw_params_sizeof SDL_NAME(snd_pcm_sw_params_sizeof)
slouken@865
    93
icculus@1161
    94
/* cast funcs to char* first, to please GCC's strict aliasing rules. */
slouken@865
    95
static struct {
slouken@865
    96
	const char *name;
slouken@865
    97
	void **func;
slouken@865
    98
} alsa_functions[] = {
icculus@1161
    99
	{ "snd_pcm_open",	(void**)(char*)&SDL_NAME(snd_pcm_open)		},
icculus@1161
   100
	{ "snd_pcm_close",	(void**)(char*)&SDL_NAME(snd_pcm_close)	},
icculus@1161
   101
	{ "snd_pcm_writei",	(void**)(char*)&SDL_NAME(snd_pcm_writei)	},
icculus@1161
   102
	{ "snd_pcm_resume",	(void**)(char*)&SDL_NAME(snd_pcm_resume)	},
icculus@1161
   103
	{ "snd_pcm_prepare",	(void**)(char*)&SDL_NAME(snd_pcm_prepare)	},
icculus@1161
   104
	{ "snd_pcm_drain",	(void**)(char*)&SDL_NAME(snd_pcm_drain)	},
icculus@1161
   105
	{ "snd_strerror",	(void**)(char*)&SDL_NAME(snd_strerror)		},
icculus@1161
   106
	{ "snd_pcm_hw_params_sizeof",		(void**)(char*)&SDL_NAME(snd_pcm_hw_params_sizeof)		},
slouken@1552
   107
	{ "snd_pcm_sw_params_sizeof",		(void**)(char*)&SDL_NAME(snd_pcm_sw_params_sizeof)		},
icculus@1161
   108
	{ "snd_pcm_hw_params_any",		(void**)(char*)&SDL_NAME(snd_pcm_hw_params_any)		},
icculus@1161
   109
	{ "snd_pcm_hw_params_set_access",	(void**)(char*)&SDL_NAME(snd_pcm_hw_params_set_access)		},
icculus@1161
   110
	{ "snd_pcm_hw_params_set_format",	(void**)(char*)&SDL_NAME(snd_pcm_hw_params_set_format)		},
icculus@1161
   111
	{ "snd_pcm_hw_params_set_channels",	(void**)(char*)&SDL_NAME(snd_pcm_hw_params_set_channels)	},
icculus@1161
   112
	{ "snd_pcm_hw_params_get_channels",	(void**)(char*)&SDL_NAME(snd_pcm_hw_params_get_channels)	},
icculus@1161
   113
	{ "snd_pcm_hw_params_set_rate_near",	(void**)(char*)&SDL_NAME(snd_pcm_hw_params_set_rate_near)	},
icculus@1161
   114
	{ "snd_pcm_hw_params_set_period_size_near",	(void**)(char*)&SDL_NAME(snd_pcm_hw_params_set_period_size_near)	},
slouken@1553
   115
	{ "snd_pcm_hw_params_get_period_size",	(void**)(char*)&SDL_NAME(snd_pcm_hw_params_get_period_size)	},
icculus@1161
   116
	{ "snd_pcm_hw_params_set_periods_near",	(void**)(char*)&SDL_NAME(snd_pcm_hw_params_set_periods_near)	},
slouken@1553
   117
	{ "snd_pcm_hw_params_get_periods",	(void**)(char*)&SDL_NAME(snd_pcm_hw_params_get_periods)	},
icculus@1161
   118
	{ "snd_pcm_hw_params",	(void**)(char*)&SDL_NAME(snd_pcm_hw_params)	},
slouken@1552
   119
	{ "snd_pcm_sw_params_current",	(void**)(char*)&SDL_NAME(snd_pcm_sw_params_current)	},
slouken@1552
   120
	{ "snd_pcm_sw_params_set_start_threshold",	(void**)(char*)&SDL_NAME(snd_pcm_sw_params_set_start_threshold)	},
slouken@1552
   121
	{ "snd_pcm_sw_params_set_avail_min",	(void**)(char*)&SDL_NAME(snd_pcm_sw_params_set_avail_min)	},
slouken@1552
   122
	{ "snd_pcm_sw_params",	(void**)(char*)&SDL_NAME(snd_pcm_sw_params)	},
slouken@1553
   123
	{ "snd_pcm_nonblock",	(void**)(char*)&SDL_NAME(snd_pcm_nonblock)	},
slouken@865
   124
};
slouken@865
   125
slouken@865
   126
static void UnloadALSALibrary(void) {
slouken@865
   127
	if (alsa_loaded) {
slouken@865
   128
/*		SDL_UnloadObject(alsa_handle);*/
slouken@865
   129
		dlclose(alsa_handle);
slouken@865
   130
		alsa_handle = NULL;
slouken@865
   131
		alsa_loaded = 0;
slouken@865
   132
	}
slouken@865
   133
}
slouken@865
   134
slouken@865
   135
static int LoadALSALibrary(void) {
slouken@865
   136
	int i, retval = -1;
slouken@865
   137
slouken@865
   138
/*	alsa_handle = SDL_LoadObject(alsa_library);*/
slouken@865
   139
	alsa_handle = dlopen(alsa_library,RTLD_NOW);
slouken@865
   140
	if (alsa_handle) {
slouken@865
   141
		alsa_loaded = 1;
slouken@865
   142
		retval = 0;
slouken@1379
   143
		for (i = 0; i < SDL_arraysize(alsa_functions); i++) {
slouken@865
   144
/*			*alsa_functions[i].func = SDL_LoadFunction(alsa_handle,alsa_functions[i].name);*/
slouken@1361
   145
#if HAVE_DLVSYM
slouken@865
   146
			*alsa_functions[i].func = dlvsym(alsa_handle,alsa_functions[i].name,"ALSA_0.9");
slouken@939
   147
			if (!*alsa_functions[i].func)
slouken@939
   148
#endif
slouken@939
   149
				*alsa_functions[i].func = dlsym(alsa_handle,alsa_functions[i].name);
slouken@865
   150
			if (!*alsa_functions[i].func) {
slouken@865
   151
				retval = -1;
slouken@865
   152
				UnloadALSALibrary();
slouken@865
   153
				break;
slouken@865
   154
			}
slouken@865
   155
		}
slouken@865
   156
	}
slouken@865
   157
	return retval;
slouken@865
   158
}
slouken@865
   159
slouken@865
   160
#else
slouken@865
   161
slouken@865
   162
static void UnloadALSALibrary(void) {
slouken@865
   163
	return;
slouken@865
   164
}
slouken@865
   165
slouken@865
   166
static int LoadALSALibrary(void) {
slouken@865
   167
	return 0;
slouken@865
   168
}
slouken@865
   169
slouken@1361
   170
#endif /* SDL_AUDIO_DRIVER_ALSA_DYNAMIC */
slouken@865
   171
slouken@942
   172
static const char *get_audio_device(int channels)
slouken@0
   173
{
slouken@354
   174
	const char *device;
slouken@354
   175
	
slouken@1336
   176
	device = SDL_getenv("AUDIODEV");	/* Is there a standard variable name? */
slouken@354
   177
	if ( device == NULL ) {
slouken@942
   178
		if (channels == 6) device = "surround51";
slouken@942
   179
		else if (channels == 4) device = "surround40";
slouken@942
   180
		else device = DEFAULT_DEVICE;
slouken@354
   181
	}
slouken@354
   182
	return device;
slouken@0
   183
}
slouken@0
   184
slouken@0
   185
/* Audio driver bootstrap functions */
slouken@0
   186
slouken@0
   187
static int Audio_Available(void)
slouken@0
   188
{
slouken@0
   189
	int available;
slouken@354
   190
	int status;
slouken@0
   191
	snd_pcm_t *handle;
slouken@0
   192
slouken@0
   193
	available = 0;
slouken@865
   194
	if (LoadALSALibrary() < 0) {
slouken@865
   195
		return available;
slouken@865
   196
	}
slouken@942
   197
	status = SDL_NAME(snd_pcm_open)(&handle, get_audio_device(2), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
slouken@354
   198
	if ( status >= 0 ) {
slouken@354
   199
		available = 1;
slouken@865
   200
        	SDL_NAME(snd_pcm_close)(handle);
slouken@0
   201
	}
slouken@865
   202
	UnloadALSALibrary();
slouken@0
   203
	return(available);
slouken@0
   204
}
slouken@0
   205
slouken@0
   206
static void Audio_DeleteDevice(SDL_AudioDevice *device)
slouken@0
   207
{
slouken@1336
   208
	SDL_free(device->hidden);
slouken@1336
   209
	SDL_free(device);
slouken@865
   210
	UnloadALSALibrary();
slouken@0
   211
}
slouken@0
   212
slouken@0
   213
static SDL_AudioDevice *Audio_CreateDevice(int devindex)
slouken@0
   214
{
slouken@0
   215
	SDL_AudioDevice *this;
slouken@0
   216
slouken@0
   217
	/* Initialize all variables that we clean on shutdown */
slouken@865
   218
	LoadALSALibrary();
slouken@1336
   219
	this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
slouken@0
   220
	if ( this ) {
slouken@1336
   221
		SDL_memset(this, 0, (sizeof *this));
slouken@0
   222
		this->hidden = (struct SDL_PrivateAudioData *)
slouken@1336
   223
				SDL_malloc((sizeof *this->hidden));
slouken@0
   224
	}
slouken@0
   225
	if ( (this == NULL) || (this->hidden == NULL) ) {
slouken@0
   226
		SDL_OutOfMemory();
slouken@0
   227
		if ( this ) {
slouken@1336
   228
			SDL_free(this);
slouken@0
   229
		}
slouken@0
   230
		return(0);
slouken@0
   231
	}
slouken@1336
   232
	SDL_memset(this->hidden, 0, (sizeof *this->hidden));
slouken@0
   233
slouken@0
   234
	/* Set the function pointers */
slouken@354
   235
	this->OpenAudio = ALSA_OpenAudio;
slouken@354
   236
	this->WaitAudio = ALSA_WaitAudio;
slouken@354
   237
	this->PlayAudio = ALSA_PlayAudio;
slouken@354
   238
	this->GetAudioBuf = ALSA_GetAudioBuf;
slouken@354
   239
	this->CloseAudio = ALSA_CloseAudio;
slouken@0
   240
slouken@0
   241
	this->free = Audio_DeleteDevice;
slouken@0
   242
slouken@0
   243
	return this;
slouken@0
   244
}
slouken@0
   245
slouken@0
   246
AudioBootStrap ALSA_bootstrap = {
slouken@354
   247
	DRIVER_NAME, "ALSA 0.9 PCM audio",
slouken@0
   248
	Audio_Available, Audio_CreateDevice
slouken@0
   249
};
slouken@0
   250
slouken@0
   251
/* This function waits until it is possible to write a full sound buffer */
slouken@354
   252
static void ALSA_WaitAudio(_THIS)
slouken@0
   253
{
slouken@0
   254
	/* Check to see if the thread-parent process is still alive */
slouken@0
   255
	{ static int cnt = 0;
slouken@0
   256
		/* Note that this only works with thread implementations 
slouken@0
   257
		   that use a different process id for each thread.
slouken@0
   258
		*/
slouken@0
   259
		if (parent && (((++cnt)%10) == 0)) { /* Check every 10 loops */
slouken@0
   260
			if ( kill(parent, 0) < 0 ) {
slouken@0
   261
				this->enabled = 0;
slouken@0
   262
			}
slouken@0
   263
		}
slouken@0
   264
	}
slouken@0
   265
}
slouken@0
   266
slouken@354
   267
static void ALSA_PlayAudio(_THIS)
slouken@0
   268
{
slouken@354
   269
	int           status;
slouken@354
   270
	int           sample_len;
slouken@354
   271
	signed short *sample_buf;
slouken@765
   272
slouken@354
   273
	sample_len = this->spec.samples;
slouken@354
   274
	sample_buf = (signed short *)mixbuf;
slouken@354
   275
	while ( sample_len > 0 ) {
slouken@865
   276
		status = SDL_NAME(snd_pcm_writei)(pcm_handle, sample_buf, sample_len);
slouken@354
   277
		if ( status < 0 ) {
slouken@354
   278
			if ( status == -EAGAIN ) {
slouken@765
   279
				SDL_Delay(1);
slouken@354
   280
				continue;
slouken@354
   281
			}
slouken@354
   282
			if ( status == -ESTRPIPE ) {
slouken@354
   283
				do {
slouken@765
   284
					SDL_Delay(1);
slouken@865
   285
					status = SDL_NAME(snd_pcm_resume)(pcm_handle);
slouken@354
   286
				} while ( status == -EAGAIN );
slouken@354
   287
			}
slouken@354
   288
			if ( status < 0 ) {
slouken@865
   289
				status = SDL_NAME(snd_pcm_prepare)(pcm_handle);
slouken@354
   290
			}
slouken@354
   291
			if ( status < 0 ) {
slouken@354
   292
				/* Hmm, not much we can do - abort */
slouken@354
   293
				this->enabled = 0;
slouken@354
   294
				return;
slouken@0
   295
			}
slouken@356
   296
			continue;
slouken@0
   297
		}
slouken@354
   298
		sample_buf += status * this->spec.channels;
slouken@354
   299
		sample_len -= status;
slouken@0
   300
	}
slouken@0
   301
}
slouken@0
   302
slouken@354
   303
static Uint8 *ALSA_GetAudioBuf(_THIS)
slouken@0
   304
{
slouken@354
   305
	return(mixbuf);
slouken@354
   306
}
slouken@0
   307
slouken@354
   308
static void ALSA_CloseAudio(_THIS)
slouken@354
   309
{
slouken@354
   310
	if ( mixbuf != NULL ) {
slouken@354
   311
		SDL_FreeAudioMem(mixbuf);
slouken@354
   312
		mixbuf = NULL;
slouken@354
   313
	}
slouken@354
   314
	if ( pcm_handle ) {
slouken@865
   315
		SDL_NAME(snd_pcm_drain)(pcm_handle);
slouken@865
   316
		SDL_NAME(snd_pcm_close)(pcm_handle);
slouken@354
   317
		pcm_handle = NULL;
slouken@354
   318
	}
slouken@354
   319
}
slouken@0
   320
slouken@354
   321
static int ALSA_OpenAudio(_THIS, SDL_AudioSpec *spec)
slouken@354
   322
{
slouken@354
   323
	int                  status;
slouken@1552
   324
	snd_pcm_hw_params_t *hwparams;
slouken@1552
   325
	snd_pcm_sw_params_t *swparams;
slouken@354
   326
	snd_pcm_format_t     format;
slouken@354
   327
	snd_pcm_uframes_t    frames;
slouken@354
   328
	Uint16               test_format;
slouken@0
   329
slouken@0
   330
	/* Open the audio device */
slouken@942
   331
	/* Name of device should depend on # channels in spec */
slouken@942
   332
	status = SDL_NAME(snd_pcm_open)(&pcm_handle, get_audio_device(spec->channels), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
slouken@942
   333
slouken@354
   334
	if ( status < 0 ) {
slouken@865
   335
		SDL_SetError("Couldn't open audio device: %s", SDL_NAME(snd_strerror)(status));
slouken@0
   336
		return(-1);
slouken@0
   337
	}
slouken@0
   338
slouken@354
   339
	/* Figure out what the hardware is capable of */
slouken@1552
   340
	snd_pcm_hw_params_alloca(&hwparams);
slouken@1552
   341
	status = SDL_NAME(snd_pcm_hw_params_any)(pcm_handle, hwparams);
slouken@354
   342
	if ( status < 0 ) {
slouken@865
   343
		SDL_SetError("Couldn't get hardware config: %s", SDL_NAME(snd_strerror)(status));
slouken@354
   344
		ALSA_CloseAudio(this);
slouken@354
   345
		return(-1);
slouken@354
   346
	}
slouken@0
   347
slouken@354
   348
	/* SDL only uses interleaved sample output */
slouken@1552
   349
	status = SDL_NAME(snd_pcm_hw_params_set_access)(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
slouken@354
   350
	if ( status < 0 ) {
slouken@865
   351
		SDL_SetError("Couldn't set interleaved access: %s", SDL_NAME(snd_strerror)(status));
slouken@354
   352
		ALSA_CloseAudio(this);
slouken@354
   353
		return(-1);
slouken@354
   354
	}
slouken@0
   355
slouken@0
   356
	/* Try for a closest match on audio format */
slouken@354
   357
	status = -1;
slouken@0
   358
	for ( test_format = SDL_FirstAudioFormat(spec->format);
slouken@354
   359
	      test_format && (status < 0); ) {
slouken@354
   360
		switch ( test_format ) {
slouken@0
   361
			case AUDIO_U8:
slouken@354
   362
				format = SND_PCM_FORMAT_U8;
slouken@0
   363
				break;
slouken@0
   364
			case AUDIO_S8:
slouken@354
   365
				format = SND_PCM_FORMAT_S8;
slouken@0
   366
				break;
slouken@0
   367
			case AUDIO_S16LSB:
slouken@354
   368
				format = SND_PCM_FORMAT_S16_LE;
slouken@0
   369
				break;
slouken@0
   370
			case AUDIO_S16MSB:
slouken@354
   371
				format = SND_PCM_FORMAT_S16_BE;
slouken@0
   372
				break;
slouken@0
   373
			case AUDIO_U16LSB:
slouken@354
   374
				format = SND_PCM_FORMAT_U16_LE;
slouken@0
   375
				break;
slouken@0
   376
			case AUDIO_U16MSB:
slouken@354
   377
				format = SND_PCM_FORMAT_U16_BE;
slouken@0
   378
				break;
slouken@0
   379
			default:
slouken@354
   380
				format = 0;
slouken@0
   381
				break;
slouken@0
   382
		}
slouken@354
   383
		if ( format != 0 ) {
slouken@1552
   384
			status = SDL_NAME(snd_pcm_hw_params_set_format)(pcm_handle, hwparams, format);
slouken@354
   385
		}
slouken@354
   386
		if ( status < 0 ) {
slouken@0
   387
			test_format = SDL_NextAudioFormat();
slouken@0
   388
		}
slouken@0
   389
	}
slouken@354
   390
	if ( status < 0 ) {
slouken@0
   391
		SDL_SetError("Couldn't find any hardware audio formats");
slouken@354
   392
		ALSA_CloseAudio(this);
slouken@0
   393
		return(-1);
slouken@0
   394
	}
slouken@0
   395
	spec->format = test_format;
slouken@0
   396
slouken@354
   397
	/* Set the number of channels */
slouken@1552
   398
	status = SDL_NAME(snd_pcm_hw_params_set_channels)(pcm_handle, hwparams, spec->channels);
slouken@354
   399
	if ( status < 0 ) {
slouken@1552
   400
		status = SDL_NAME(snd_pcm_hw_params_get_channels)(hwparams);
slouken@354
   401
		if ( (status <= 0) || (status > 2) ) {
slouken@354
   402
			SDL_SetError("Couldn't set audio channels");
slouken@354
   403
			ALSA_CloseAudio(this);
slouken@354
   404
			return(-1);
slouken@354
   405
		}
slouken@354
   406
		spec->channels = status;
slouken@354
   407
	}
slouken@0
   408
slouken@354
   409
	/* Set the audio rate */
slouken@1552
   410
	status = SDL_NAME(snd_pcm_hw_params_set_rate_near)(pcm_handle, hwparams, spec->freq, NULL);
slouken@354
   411
	if ( status < 0 ) {
slouken@865
   412
		SDL_SetError("Couldn't set audio frequency: %s", SDL_NAME(snd_strerror)(status));
slouken@354
   413
		ALSA_CloseAudio(this);
slouken@354
   414
		return(-1);
slouken@354
   415
	}
slouken@354
   416
	spec->freq = status;
slouken@0
   417
slouken@354
   418
	/* Set the buffer size, in samples */
slouken@354
   419
	frames = spec->samples;
slouken@1552
   420
	frames = SDL_NAME(snd_pcm_hw_params_set_period_size_near)(pcm_handle, hwparams, frames, NULL);
slouken@354
   421
	spec->samples = frames;
slouken@1552
   422
	SDL_NAME(snd_pcm_hw_params_set_periods_near)(pcm_handle, hwparams, 2, NULL);
slouken@354
   423
slouken@354
   424
	/* "set" the hardware with the desired parameters */
slouken@1552
   425
	status = SDL_NAME(snd_pcm_hw_params)(pcm_handle, hwparams);
slouken@354
   426
	if ( status < 0 ) {
slouken@1552
   427
		SDL_SetError("Couldn't set hardware audio parameters: %s", SDL_NAME(snd_strerror)(status));
slouken@1552
   428
		ALSA_CloseAudio(this);
slouken@1552
   429
		return(-1);
slouken@1552
   430
	}
slouken@1552
   431
slouken@1553
   432
/* This is useful for debugging... */
slouken@1552
   433
/*
slouken@1553
   434
{ snd_pcm_sframes_t bufsize; int fragments;
slouken@1552
   435
   bufsize = SDL_NAME(snd_pcm_hw_params_get_period_size)(hwparams);
slouken@1552
   436
   fragments = SDL_NAME(snd_pcm_hw_params_get_periods)(hwparams);
slouken@1552
   437
slouken@1552
   438
   fprintf(stderr, "ALSA: bufsize = %ld, fragments = %d\n", bufsize, fragments);
slouken@1552
   439
}
slouken@1552
   440
*/
slouken@1552
   441
slouken@1552
   442
	/* Set the software parameters */
slouken@1552
   443
	snd_pcm_sw_params_alloca(&swparams);
slouken@1552
   444
	status = SDL_NAME(snd_pcm_sw_params_current)(pcm_handle, swparams);
slouken@1552
   445
	if ( status < 0 ) {
slouken@1552
   446
		SDL_SetError("Couldn't get software config: %s", SDL_NAME(snd_strerror)(status));
slouken@1552
   447
		ALSA_CloseAudio(this);
slouken@1552
   448
		return(-1);
slouken@1552
   449
	}
slouken@1552
   450
	status = SDL_NAME(snd_pcm_sw_params_set_start_threshold)(pcm_handle, swparams, 0);
slouken@1552
   451
	if ( status < 0 ) {
slouken@1552
   452
		SDL_SetError("Couldn't set start threshold: %s", SDL_NAME(snd_strerror)(status));
slouken@1552
   453
		ALSA_CloseAudio(this);
slouken@1552
   454
		return(-1);
slouken@1552
   455
	}
slouken@1552
   456
	status = SDL_NAME(snd_pcm_sw_params_set_avail_min)(pcm_handle, swparams, frames);
slouken@1552
   457
	if ( status < 0 ) {
slouken@1552
   458
		SDL_SetError("Couldn't set avail min: %s", SDL_NAME(snd_strerror)(status));
slouken@1552
   459
		ALSA_CloseAudio(this);
slouken@1552
   460
		return(-1);
slouken@1552
   461
	}
slouken@1552
   462
	status = SDL_NAME(snd_pcm_sw_params)(pcm_handle, swparams);
slouken@1552
   463
	if ( status < 0 ) {
slouken@1552
   464
		SDL_SetError("Couldn't set software audio parameters: %s", SDL_NAME(snd_strerror)(status));
slouken@354
   465
		ALSA_CloseAudio(this);
slouken@0
   466
		return(-1);
slouken@0
   467
	}
slouken@0
   468
slouken@354
   469
	/* Calculate the final parameters for this audio specification */
slouken@354
   470
	SDL_CalculateAudioSpec(spec);
slouken@0
   471
slouken@354
   472
	/* Allocate mixing buffer */
slouken@354
   473
	mixlen = spec->size;
slouken@354
   474
	mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen);
slouken@354
   475
	if ( mixbuf == NULL ) {
slouken@354
   476
		ALSA_CloseAudio(this);
slouken@354
   477
		return(-1);
slouken@0
   478
	}
slouken@1336
   479
	SDL_memset(mixbuf, spec->silence, spec->size);
slouken@0
   480
slouken@0
   481
	/* Get the parent process id (we're the parent of the audio thread) */
slouken@0
   482
	parent = getpid();
slouken@0
   483
slouken@765
   484
	/* Switch to blocking mode for playback */
slouken@865
   485
	SDL_NAME(snd_pcm_nonblock)(pcm_handle, 0);
slouken@765
   486
slouken@0
   487
	/* We're ready to rock and roll. :-) */
slouken@0
   488
	return(0);
slouken@0
   489
}