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