src/audio/alsa/SDL_alsa_audio.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 21 Aug 2004 04:20:00 +0000
changeset 939 c7c04f811994
parent 865 92615154bb68
child 942 41a59de7f2ed
permissions -rw-r--r--
Date: Tue, 01 Jun 2004 15:27:44 +0300
From: Martin_Storsj
Subject: Update for dynamic loading of ALSA

I sent you a patch a few months ago which enables SDL to load ALSA
dynamically. Now I've finally got time to tweak this yet some more. I've
added code from alsa.m4 (from alsa's dev package) to acinclude.m4, and
made the detection of the alsa library name a bit better. I've also
fixed up the loading versioned symbols with dlvsym, so that it falls
back to dlsym.

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