src/audio/alsa/SDL_alsa_audio.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 20 Oct 2005 06:53:56 +0000
changeset 1161 05d4b93b911e
parent 942 41a59de7f2ed
child 1331 1cbaeee565b1
permissions -rw-r--r--
Placate gcc's strict aliasing rules with an extra cast.
Casts to (char *) will disable strict aliasing when the compiler sees it.
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@0
    22
slouken@0
    23
slouken@0
    24
slouken@0
    25
/* Allow access to a raw mixing buffer */
slouken@0
    26
slouken@0
    27
#include <stdlib.h>
slouken@0
    28
#include <stdio.h>
slouken@0
    29
#include <string.h>
slouken@0
    30
#include <errno.h>
slouken@0
    31
#include <unistd.h>
slouken@0
    32
#include <fcntl.h>
slouken@0
    33
#include <signal.h>
slouken@0
    34
#include <sys/types.h>
slouken@0
    35
#include <sys/time.h>
slouken@0
    36
slouken@0
    37
#include "SDL_audio.h"
slouken@0
    38
#include "SDL_error.h"
slouken@0
    39
#include "SDL_audiomem.h"
slouken@0
    40
#include "SDL_audio_c.h"
slouken@0
    41
#include "SDL_timer.h"
slouken@0
    42
#include "SDL_alsa_audio.h"
slouken@0
    43
slouken@865
    44
#ifdef ALSA_DYNAMIC
slouken@939
    45
#ifdef USE_DLVSYM
slouken@865
    46
#define __USE_GNU
slouken@939
    47
#endif
slouken@865
    48
#include <dlfcn.h>
slouken@865
    49
#include "SDL_name.h"
slouken@865
    50
#include "SDL_loadso.h"
slouken@865
    51
#else
slouken@865
    52
#define SDL_NAME(X)	X
slouken@865
    53
#endif
slouken@865
    54
slouken@865
    55
slouken@0
    56
/* The tag name used by ALSA audio */
slouken@0
    57
#define DRIVER_NAME         "alsa"
slouken@0
    58
slouken@354
    59
/* The default ALSA audio driver */
slouken@765
    60
#define DEFAULT_DEVICE	"default"
slouken@0
    61
slouken@0
    62
/* Audio driver functions */
slouken@354
    63
static int ALSA_OpenAudio(_THIS, SDL_AudioSpec *spec);
slouken@354
    64
static void ALSA_WaitAudio(_THIS);
slouken@354
    65
static void ALSA_PlayAudio(_THIS);
slouken@354
    66
static Uint8 *ALSA_GetAudioBuf(_THIS);
slouken@354
    67
static void ALSA_CloseAudio(_THIS);
slouken@0
    68
slouken@865
    69
#ifdef ALSA_DYNAMIC
slouken@865
    70
slouken@865
    71
static const char *alsa_library = ALSA_DYNAMIC;
slouken@865
    72
static void *alsa_handle = NULL;
slouken@865
    73
static int alsa_loaded = 0;
slouken@865
    74
slouken@865
    75
static int (*SDL_snd_pcm_open)(snd_pcm_t **pcm, const char *name, snd_pcm_stream_t stream, int mode);
slouken@865
    76
static int (*SDL_NAME(snd_pcm_open))(snd_pcm_t **pcm, const char *name, snd_pcm_stream_t stream, int mode);
slouken@865
    77
static int (*SDL_NAME(snd_pcm_close))(snd_pcm_t *pcm);
slouken@865
    78
static snd_pcm_sframes_t (*SDL_NAME(snd_pcm_writei))(snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size);
slouken@865
    79
static int (*SDL_NAME(snd_pcm_resume))(snd_pcm_t *pcm);
slouken@865
    80
static int (*SDL_NAME(snd_pcm_prepare))(snd_pcm_t *pcm);
slouken@865
    81
static int (*SDL_NAME(snd_pcm_drain))(snd_pcm_t *pcm);
slouken@865
    82
static const char *(*SDL_NAME(snd_strerror))(int errnum);
slouken@865
    83
static size_t (*SDL_NAME(snd_pcm_hw_params_sizeof))(void);
slouken@865
    84
static int (*SDL_NAME(snd_pcm_hw_params_any))(snd_pcm_t *pcm, snd_pcm_hw_params_t *params);
slouken@865
    85
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
    86
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
    87
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
    88
static int (*SDL_NAME(snd_pcm_hw_params_get_channels))(const snd_pcm_hw_params_t *params);
slouken@865
    89
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
    90
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@865
    91
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@865
    92
static int (*SDL_NAME(snd_pcm_hw_params))(snd_pcm_t *pcm, snd_pcm_hw_params_t *params);
slouken@865
    93
static int (*SDL_NAME(snd_pcm_nonblock))(snd_pcm_t *pcm, int nonblock);
slouken@865
    94
#define snd_pcm_hw_params_sizeof SDL_NAME(snd_pcm_hw_params_sizeof)
slouken@865
    95
icculus@1161
    96
/* cast funcs to char* first, to please GCC's strict aliasing rules. */
slouken@865
    97
static struct {
slouken@865
    98
	const char *name;
slouken@865
    99
	void **func;
slouken@865
   100
} alsa_functions[] = {
icculus@1161
   101
	{ "snd_pcm_open",	(void**)(char*)&SDL_NAME(snd_pcm_open)		},
icculus@1161
   102
	{ "snd_pcm_close",	(void**)(char*)&SDL_NAME(snd_pcm_close)	},
icculus@1161
   103
	{ "snd_pcm_writei",	(void**)(char*)&SDL_NAME(snd_pcm_writei)	},
icculus@1161
   104
	{ "snd_pcm_resume",	(void**)(char*)&SDL_NAME(snd_pcm_resume)	},
icculus@1161
   105
	{ "snd_pcm_prepare",	(void**)(char*)&SDL_NAME(snd_pcm_prepare)	},
icculus@1161
   106
	{ "snd_pcm_drain",	(void**)(char*)&SDL_NAME(snd_pcm_drain)	},
icculus@1161
   107
	{ "snd_strerror",	(void**)(char*)&SDL_NAME(snd_strerror)		},
icculus@1161
   108
	{ "snd_pcm_hw_params_sizeof",		(void**)(char*)&SDL_NAME(snd_pcm_hw_params_sizeof)		},
icculus@1161
   109
	{ "snd_pcm_hw_params_any",		(void**)(char*)&SDL_NAME(snd_pcm_hw_params_any)		},
icculus@1161
   110
	{ "snd_pcm_hw_params_set_access",	(void**)(char*)&SDL_NAME(snd_pcm_hw_params_set_access)		},
icculus@1161
   111
	{ "snd_pcm_hw_params_set_format",	(void**)(char*)&SDL_NAME(snd_pcm_hw_params_set_format)		},
icculus@1161
   112
	{ "snd_pcm_hw_params_set_channels",	(void**)(char*)&SDL_NAME(snd_pcm_hw_params_set_channels)	},
icculus@1161
   113
	{ "snd_pcm_hw_params_get_channels",	(void**)(char*)&SDL_NAME(snd_pcm_hw_params_get_channels)	},
icculus@1161
   114
	{ "snd_pcm_hw_params_set_rate_near",	(void**)(char*)&SDL_NAME(snd_pcm_hw_params_set_rate_near)	},
icculus@1161
   115
	{ "snd_pcm_hw_params_set_period_size_near",	(void**)(char*)&SDL_NAME(snd_pcm_hw_params_set_period_size_near)	},
icculus@1161
   116
	{ "snd_pcm_hw_params_set_periods_near",	(void**)(char*)&SDL_NAME(snd_pcm_hw_params_set_periods_near)	},
icculus@1161
   117
	{ "snd_pcm_hw_params",	(void**)(char*)&SDL_NAME(snd_pcm_hw_params)	},
icculus@1161
   118
	{ "snd_pcm_nonblock",	(void**)(char*)&SDL_NAME(snd_pcm_nonblock)	},
slouken@865
   119
};
slouken@865
   120
slouken@865
   121
static void UnloadALSALibrary(void) {
slouken@865
   122
	if (alsa_loaded) {
slouken@865
   123
/*		SDL_UnloadObject(alsa_handle);*/
slouken@865
   124
		dlclose(alsa_handle);
slouken@865
   125
		alsa_handle = NULL;
slouken@865
   126
		alsa_loaded = 0;
slouken@865
   127
	}
slouken@865
   128
}
slouken@865
   129
slouken@865
   130
static int LoadALSALibrary(void) {
slouken@865
   131
	int i, retval = -1;
slouken@865
   132
slouken@865
   133
/*	alsa_handle = SDL_LoadObject(alsa_library);*/
slouken@865
   134
	alsa_handle = dlopen(alsa_library,RTLD_NOW);
slouken@865
   135
	if (alsa_handle) {
slouken@865
   136
		alsa_loaded = 1;
slouken@865
   137
		retval = 0;
slouken@865
   138
		for (i = 0; i < SDL_TABLESIZE(alsa_functions); i++) {
slouken@865
   139
/*			*alsa_functions[i].func = SDL_LoadFunction(alsa_handle,alsa_functions[i].name);*/
slouken@939
   140
#ifdef USE_DLVSYM
slouken@865
   141
			*alsa_functions[i].func = dlvsym(alsa_handle,alsa_functions[i].name,"ALSA_0.9");
slouken@939
   142
			if (!*alsa_functions[i].func)
slouken@939
   143
#endif
slouken@939
   144
				*alsa_functions[i].func = dlsym(alsa_handle,alsa_functions[i].name);
slouken@865
   145
			if (!*alsa_functions[i].func) {
slouken@865
   146
				retval = -1;
slouken@865
   147
				UnloadALSALibrary();
slouken@865
   148
				break;
slouken@865
   149
			}
slouken@865
   150
		}
slouken@865
   151
	}
slouken@865
   152
	return retval;
slouken@865
   153
}
slouken@865
   154
slouken@865
   155
#else
slouken@865
   156
slouken@865
   157
static void UnloadALSALibrary(void) {
slouken@865
   158
	return;
slouken@865
   159
}
slouken@865
   160
slouken@865
   161
static int LoadALSALibrary(void) {
slouken@865
   162
	return 0;
slouken@865
   163
}
slouken@865
   164
slouken@865
   165
#endif /* ALSA_DYNAMIC */
slouken@865
   166
slouken@942
   167
static const char *get_audio_device(int channels)
slouken@0
   168
{
slouken@354
   169
	const char *device;
slouken@354
   170
	
slouken@354
   171
	device = getenv("AUDIODEV");	/* Is there a standard variable name? */
slouken@354
   172
	if ( device == NULL ) {
slouken@942
   173
		if (channels == 6) device = "surround51";
slouken@942
   174
		else if (channels == 4) device = "surround40";
slouken@942
   175
		else device = DEFAULT_DEVICE;
slouken@354
   176
	}
slouken@354
   177
	return device;
slouken@0
   178
}
slouken@0
   179
slouken@0
   180
/* Audio driver bootstrap functions */
slouken@0
   181
slouken@0
   182
static int Audio_Available(void)
slouken@0
   183
{
slouken@0
   184
	int available;
slouken@354
   185
	int status;
slouken@0
   186
	snd_pcm_t *handle;
slouken@0
   187
slouken@0
   188
	available = 0;
slouken@865
   189
	if (LoadALSALibrary() < 0) {
slouken@865
   190
		return available;
slouken@865
   191
	}
slouken@942
   192
	status = SDL_NAME(snd_pcm_open)(&handle, get_audio_device(2), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
slouken@354
   193
	if ( status >= 0 ) {
slouken@354
   194
		available = 1;
slouken@865
   195
        	SDL_NAME(snd_pcm_close)(handle);
slouken@0
   196
	}
slouken@865
   197
	UnloadALSALibrary();
slouken@0
   198
	return(available);
slouken@0
   199
}
slouken@0
   200
slouken@0
   201
static void Audio_DeleteDevice(SDL_AudioDevice *device)
slouken@0
   202
{
slouken@0
   203
	free(device->hidden);
slouken@0
   204
	free(device);
slouken@865
   205
	UnloadALSALibrary();
slouken@0
   206
}
slouken@0
   207
slouken@0
   208
static SDL_AudioDevice *Audio_CreateDevice(int devindex)
slouken@0
   209
{
slouken@0
   210
	SDL_AudioDevice *this;
slouken@0
   211
slouken@0
   212
	/* Initialize all variables that we clean on shutdown */
slouken@865
   213
	LoadALSALibrary();
slouken@0
   214
	this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice));
slouken@0
   215
	if ( this ) {
slouken@0
   216
		memset(this, 0, (sizeof *this));
slouken@0
   217
		this->hidden = (struct SDL_PrivateAudioData *)
slouken@0
   218
				malloc((sizeof *this->hidden));
slouken@0
   219
	}
slouken@0
   220
	if ( (this == NULL) || (this->hidden == NULL) ) {
slouken@0
   221
		SDL_OutOfMemory();
slouken@0
   222
		if ( this ) {
slouken@0
   223
			free(this);
slouken@0
   224
		}
slouken@0
   225
		return(0);
slouken@0
   226
	}
slouken@0
   227
	memset(this->hidden, 0, (sizeof *this->hidden));
slouken@0
   228
slouken@0
   229
	/* Set the function pointers */
slouken@354
   230
	this->OpenAudio = ALSA_OpenAudio;
slouken@354
   231
	this->WaitAudio = ALSA_WaitAudio;
slouken@354
   232
	this->PlayAudio = ALSA_PlayAudio;
slouken@354
   233
	this->GetAudioBuf = ALSA_GetAudioBuf;
slouken@354
   234
	this->CloseAudio = ALSA_CloseAudio;
slouken@0
   235
slouken@0
   236
	this->free = Audio_DeleteDevice;
slouken@0
   237
slouken@0
   238
	return this;
slouken@0
   239
}
slouken@0
   240
slouken@0
   241
AudioBootStrap ALSA_bootstrap = {
slouken@354
   242
	DRIVER_NAME, "ALSA 0.9 PCM audio",
slouken@0
   243
	Audio_Available, Audio_CreateDevice
slouken@0
   244
};
slouken@0
   245
slouken@0
   246
/* This function waits until it is possible to write a full sound buffer */
slouken@354
   247
static void ALSA_WaitAudio(_THIS)
slouken@0
   248
{
slouken@0
   249
	/* Check to see if the thread-parent process is still alive */
slouken@0
   250
	{ static int cnt = 0;
slouken@0
   251
		/* Note that this only works with thread implementations 
slouken@0
   252
		   that use a different process id for each thread.
slouken@0
   253
		*/
slouken@0
   254
		if (parent && (((++cnt)%10) == 0)) { /* Check every 10 loops */
slouken@0
   255
			if ( kill(parent, 0) < 0 ) {
slouken@0
   256
				this->enabled = 0;
slouken@0
   257
			}
slouken@0
   258
		}
slouken@0
   259
	}
slouken@0
   260
}
slouken@0
   261
slouken@354
   262
static void ALSA_PlayAudio(_THIS)
slouken@0
   263
{
slouken@354
   264
	int           status;
slouken@354
   265
	int           sample_len;
slouken@354
   266
	signed short *sample_buf;
slouken@765
   267
slouken@354
   268
	sample_len = this->spec.samples;
slouken@354
   269
	sample_buf = (signed short *)mixbuf;
slouken@354
   270
	while ( sample_len > 0 ) {
slouken@865
   271
		status = SDL_NAME(snd_pcm_writei)(pcm_handle, sample_buf, sample_len);
slouken@354
   272
		if ( status < 0 ) {
slouken@354
   273
			if ( status == -EAGAIN ) {
slouken@765
   274
				SDL_Delay(1);
slouken@354
   275
				continue;
slouken@354
   276
			}
slouken@354
   277
			if ( status == -ESTRPIPE ) {
slouken@354
   278
				do {
slouken@765
   279
					SDL_Delay(1);
slouken@865
   280
					status = SDL_NAME(snd_pcm_resume)(pcm_handle);
slouken@354
   281
				} while ( status == -EAGAIN );
slouken@354
   282
			}
slouken@354
   283
			if ( status < 0 ) {
slouken@865
   284
				status = SDL_NAME(snd_pcm_prepare)(pcm_handle);
slouken@354
   285
			}
slouken@354
   286
			if ( status < 0 ) {
slouken@354
   287
				/* Hmm, not much we can do - abort */
slouken@354
   288
				this->enabled = 0;
slouken@354
   289
				return;
slouken@0
   290
			}
slouken@356
   291
			continue;
slouken@0
   292
		}
slouken@354
   293
		sample_buf += status * this->spec.channels;
slouken@354
   294
		sample_len -= status;
slouken@0
   295
	}
slouken@0
   296
}
slouken@0
   297
slouken@354
   298
static Uint8 *ALSA_GetAudioBuf(_THIS)
slouken@0
   299
{
slouken@354
   300
	return(mixbuf);
slouken@354
   301
}
slouken@0
   302
slouken@354
   303
static void ALSA_CloseAudio(_THIS)
slouken@354
   304
{
slouken@354
   305
	if ( mixbuf != NULL ) {
slouken@354
   306
		SDL_FreeAudioMem(mixbuf);
slouken@354
   307
		mixbuf = NULL;
slouken@354
   308
	}
slouken@354
   309
	if ( pcm_handle ) {
slouken@865
   310
		SDL_NAME(snd_pcm_drain)(pcm_handle);
slouken@865
   311
		SDL_NAME(snd_pcm_close)(pcm_handle);
slouken@354
   312
		pcm_handle = NULL;
slouken@354
   313
	}
slouken@354
   314
}
slouken@0
   315
slouken@354
   316
static int ALSA_OpenAudio(_THIS, SDL_AudioSpec *spec)
slouken@354
   317
{
slouken@354
   318
	int                  status;
slouken@354
   319
	snd_pcm_hw_params_t *params;
slouken@354
   320
	snd_pcm_format_t     format;
slouken@354
   321
	snd_pcm_uframes_t    frames;
slouken@354
   322
	Uint16               test_format;
slouken@0
   323
slouken@0
   324
	/* Open the audio device */
slouken@942
   325
	/* Name of device should depend on # channels in spec */
slouken@942
   326
	status = SDL_NAME(snd_pcm_open)(&pcm_handle, get_audio_device(spec->channels), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
slouken@942
   327
slouken@354
   328
	if ( status < 0 ) {
slouken@865
   329
		SDL_SetError("Couldn't open audio device: %s", SDL_NAME(snd_strerror)(status));
slouken@0
   330
		return(-1);
slouken@0
   331
	}
slouken@0
   332
slouken@354
   333
	/* Figure out what the hardware is capable of */
slouken@354
   334
	snd_pcm_hw_params_alloca(&params);
slouken@865
   335
	status = SDL_NAME(snd_pcm_hw_params_any)(pcm_handle, params);
slouken@354
   336
	if ( status < 0 ) {
slouken@865
   337
		SDL_SetError("Couldn't get hardware config: %s", SDL_NAME(snd_strerror)(status));
slouken@354
   338
		ALSA_CloseAudio(this);
slouken@354
   339
		return(-1);
slouken@354
   340
	}
slouken@0
   341
slouken@354
   342
	/* SDL only uses interleaved sample output */
slouken@865
   343
	status = SDL_NAME(snd_pcm_hw_params_set_access)(pcm_handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
slouken@354
   344
	if ( status < 0 ) {
slouken@865
   345
		SDL_SetError("Couldn't set interleaved access: %s", SDL_NAME(snd_strerror)(status));
slouken@354
   346
		ALSA_CloseAudio(this);
slouken@354
   347
		return(-1);
slouken@354
   348
	}
slouken@0
   349
slouken@0
   350
	/* Try for a closest match on audio format */
slouken@354
   351
	status = -1;
slouken@0
   352
	for ( test_format = SDL_FirstAudioFormat(spec->format);
slouken@354
   353
	      test_format && (status < 0); ) {
slouken@354
   354
		switch ( test_format ) {
slouken@0
   355
			case AUDIO_U8:
slouken@354
   356
				format = SND_PCM_FORMAT_U8;
slouken@0
   357
				break;
slouken@0
   358
			case AUDIO_S8:
slouken@354
   359
				format = SND_PCM_FORMAT_S8;
slouken@0
   360
				break;
slouken@0
   361
			case AUDIO_S16LSB:
slouken@354
   362
				format = SND_PCM_FORMAT_S16_LE;
slouken@0
   363
				break;
slouken@0
   364
			case AUDIO_S16MSB:
slouken@354
   365
				format = SND_PCM_FORMAT_S16_BE;
slouken@0
   366
				break;
slouken@0
   367
			case AUDIO_U16LSB:
slouken@354
   368
				format = SND_PCM_FORMAT_U16_LE;
slouken@0
   369
				break;
slouken@0
   370
			case AUDIO_U16MSB:
slouken@354
   371
				format = SND_PCM_FORMAT_U16_BE;
slouken@0
   372
				break;
slouken@0
   373
			default:
slouken@354
   374
				format = 0;
slouken@0
   375
				break;
slouken@0
   376
		}
slouken@354
   377
		if ( format != 0 ) {
slouken@865
   378
			status = SDL_NAME(snd_pcm_hw_params_set_format)(pcm_handle, params, format);
slouken@354
   379
		}
slouken@354
   380
		if ( status < 0 ) {
slouken@0
   381
			test_format = SDL_NextAudioFormat();
slouken@0
   382
		}
slouken@0
   383
	}
slouken@354
   384
	if ( status < 0 ) {
slouken@0
   385
		SDL_SetError("Couldn't find any hardware audio formats");
slouken@354
   386
		ALSA_CloseAudio(this);
slouken@0
   387
		return(-1);
slouken@0
   388
	}
slouken@0
   389
	spec->format = test_format;
slouken@0
   390
slouken@354
   391
	/* Set the number of channels */
slouken@865
   392
	status = SDL_NAME(snd_pcm_hw_params_set_channels)(pcm_handle, params, spec->channels);
slouken@354
   393
	if ( status < 0 ) {
slouken@865
   394
		status = SDL_NAME(snd_pcm_hw_params_get_channels)(params);
slouken@354
   395
		if ( (status <= 0) || (status > 2) ) {
slouken@354
   396
			SDL_SetError("Couldn't set audio channels");
slouken@354
   397
			ALSA_CloseAudio(this);
slouken@354
   398
			return(-1);
slouken@354
   399
		}
slouken@354
   400
		spec->channels = status;
slouken@354
   401
	}
slouken@0
   402
slouken@354
   403
	/* Set the audio rate */
slouken@865
   404
	status = SDL_NAME(snd_pcm_hw_params_set_rate_near)(pcm_handle, params, spec->freq, NULL);
slouken@354
   405
	if ( status < 0 ) {
slouken@865
   406
		SDL_SetError("Couldn't set audio frequency: %s", SDL_NAME(snd_strerror)(status));
slouken@354
   407
		ALSA_CloseAudio(this);
slouken@354
   408
		return(-1);
slouken@354
   409
	}
slouken@354
   410
	spec->freq = status;
slouken@0
   411
slouken@354
   412
	/* Set the buffer size, in samples */
slouken@354
   413
	frames = spec->samples;
slouken@865
   414
	frames = SDL_NAME(snd_pcm_hw_params_set_period_size_near)(pcm_handle, params, frames, NULL);
slouken@354
   415
	spec->samples = frames;
slouken@865
   416
	SDL_NAME(snd_pcm_hw_params_set_periods_near)(pcm_handle, params, 2, NULL);
slouken@354
   417
slouken@354
   418
	/* "set" the hardware with the desired parameters */
slouken@865
   419
	status = SDL_NAME(snd_pcm_hw_params)(pcm_handle, params);
slouken@354
   420
	if ( status < 0 ) {
slouken@865
   421
		SDL_SetError("Couldn't set audio parameters: %s", SDL_NAME(snd_strerror)(status));
slouken@354
   422
		ALSA_CloseAudio(this);
slouken@0
   423
		return(-1);
slouken@0
   424
	}
slouken@0
   425
slouken@354
   426
	/* Calculate the final parameters for this audio specification */
slouken@354
   427
	SDL_CalculateAudioSpec(spec);
slouken@0
   428
slouken@354
   429
	/* Allocate mixing buffer */
slouken@354
   430
	mixlen = spec->size;
slouken@354
   431
	mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen);
slouken@354
   432
	if ( mixbuf == NULL ) {
slouken@354
   433
		ALSA_CloseAudio(this);
slouken@354
   434
		return(-1);
slouken@0
   435
	}
slouken@354
   436
	memset(mixbuf, spec->silence, spec->size);
slouken@0
   437
slouken@0
   438
	/* Get the parent process id (we're the parent of the audio thread) */
slouken@0
   439
	parent = getpid();
slouken@0
   440
slouken@765
   441
	/* Switch to blocking mode for playback */
slouken@865
   442
	SDL_NAME(snd_pcm_nonblock)(pcm_handle, 0);
slouken@765
   443
slouken@0
   444
	/* We're ready to rock and roll. :-) */
slouken@0
   445
	return(0);
slouken@0
   446
}