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