src/audio/alsa/SDL_alsa_audio.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 02 Mar 2004 12:49:16 +0000
changeset 865 92615154bb68
parent 769 b8d311d90021
child 939 c7c04f811994
permissions -rw-r--r--
Date: Sun, 29 Feb 2004 15:14:22 +0200
From: Martin_Storsj
Subject: Dynamic loading of ALSA

I recently discovered that SDL can dynamically load ESD and aRts, and
made a patch which adds this same functionality to ALSA.

The update for configure.in isn't too good (it should e.g. look for
libasound.so in other directories than /usr/lib), because I'm not too
good at shellscripting and autoconf.

The reason for using dlfcn.h and dlopen instead of SDL_LoadLibrary and
SDL_LoadFunction is that libasound uses versioned symbols, and it is
necessary to load the correct version using dlvsym. This isn't probably
any real portability issue, because ALSA is linux-only.
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@865
    45
#define __USE_GNU
slouken@865
    46
#include <dlfcn.h>
slouken@865
    47
#include "SDL_name.h"
slouken@865
    48
#include "SDL_loadso.h"
slouken@865
    49
#else
slouken@865
    50
#define SDL_NAME(X)	X
slouken@865
    51
#endif
slouken@865
    52
slouken@865
    53
slouken@0
    54
/* The tag name used by ALSA audio */
slouken@0
    55
#define DRIVER_NAME         "alsa"
slouken@0
    56
slouken@354
    57
/* The default ALSA audio driver */
slouken@765
    58
#define DEFAULT_DEVICE	"default"
slouken@0
    59
slouken@0
    60
/* Audio driver functions */
slouken@354
    61
static int ALSA_OpenAudio(_THIS, SDL_AudioSpec *spec);
slouken@354
    62
static void ALSA_WaitAudio(_THIS);
slouken@354
    63
static void ALSA_PlayAudio(_THIS);
slouken@354
    64
static Uint8 *ALSA_GetAudioBuf(_THIS);
slouken@354
    65
static void ALSA_CloseAudio(_THIS);
slouken@0
    66
slouken@865
    67
#ifdef ALSA_DYNAMIC
slouken@865
    68
slouken@865
    69
static const char *alsa_library = ALSA_DYNAMIC;
slouken@865
    70
static void *alsa_handle = NULL;
slouken@865
    71
static int alsa_loaded = 0;
slouken@865
    72
slouken@865
    73
static int (*SDL_snd_pcm_open)(snd_pcm_t **pcm, const char *name, snd_pcm_stream_t stream, int mode);
slouken@865
    74
static int (*SDL_NAME(snd_pcm_open))(snd_pcm_t **pcm, const char *name, snd_pcm_stream_t stream, int mode);
slouken@865
    75
static int (*SDL_NAME(snd_pcm_close))(snd_pcm_t *pcm);
slouken@865
    76
static snd_pcm_sframes_t (*SDL_NAME(snd_pcm_writei))(snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size);
slouken@865
    77
static int (*SDL_NAME(snd_pcm_resume))(snd_pcm_t *pcm);
slouken@865
    78
static int (*SDL_NAME(snd_pcm_prepare))(snd_pcm_t *pcm);
slouken@865
    79
static int (*SDL_NAME(snd_pcm_drain))(snd_pcm_t *pcm);
slouken@865
    80
static const char *(*SDL_NAME(snd_strerror))(int errnum);
slouken@865
    81
static size_t (*SDL_NAME(snd_pcm_hw_params_sizeof))(void);
slouken@865
    82
static int (*SDL_NAME(snd_pcm_hw_params_any))(snd_pcm_t *pcm, snd_pcm_hw_params_t *params);
slouken@865
    83
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
    84
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
    85
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
    86
static int (*SDL_NAME(snd_pcm_hw_params_get_channels))(const snd_pcm_hw_params_t *params);
slouken@865
    87
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
    88
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
    89
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
    90
static int (*SDL_NAME(snd_pcm_hw_params))(snd_pcm_t *pcm, snd_pcm_hw_params_t *params);
slouken@865
    91
static int (*SDL_NAME(snd_pcm_nonblock))(snd_pcm_t *pcm, int nonblock);
slouken@865
    92
#define snd_pcm_hw_params_sizeof SDL_NAME(snd_pcm_hw_params_sizeof)
slouken@865
    93
slouken@865
    94
static struct {
slouken@865
    95
	const char *name;
slouken@865
    96
	void **func;
slouken@865
    97
} alsa_functions[] = {
slouken@865
    98
	{ "snd_pcm_open",	(void**)&SDL_NAME(snd_pcm_open)		},
slouken@865
    99
	{ "snd_pcm_close",	(void**)&SDL_NAME(snd_pcm_close)	},
slouken@865
   100
	{ "snd_pcm_writei",	(void**)&SDL_NAME(snd_pcm_writei)	},
slouken@865
   101
	{ "snd_pcm_resume",	(void**)&SDL_NAME(snd_pcm_resume)	},
slouken@865
   102
	{ "snd_pcm_prepare",	(void**)&SDL_NAME(snd_pcm_prepare)	},
slouken@865
   103
	{ "snd_pcm_drain",	(void**)&SDL_NAME(snd_pcm_drain)	},
slouken@865
   104
	{ "snd_strerror",	(void**)&SDL_NAME(snd_strerror)		},
slouken@865
   105
	{ "snd_pcm_hw_params_sizeof",		(void**)&SDL_NAME(snd_pcm_hw_params_sizeof)		},
slouken@865
   106
	{ "snd_pcm_hw_params_any",		(void**)&SDL_NAME(snd_pcm_hw_params_any)		},
slouken@865
   107
	{ "snd_pcm_hw_params_set_access",	(void**)&SDL_NAME(snd_pcm_hw_params_set_access)		},
slouken@865
   108
	{ "snd_pcm_hw_params_set_format",	(void**)&SDL_NAME(snd_pcm_hw_params_set_format)		},
slouken@865
   109
	{ "snd_pcm_hw_params_set_channels",	(void**)&SDL_NAME(snd_pcm_hw_params_set_channels)	},
slouken@865
   110
	{ "snd_pcm_hw_params_get_channels",	(void**)&SDL_NAME(snd_pcm_hw_params_get_channels)	},
slouken@865
   111
	{ "snd_pcm_hw_params_set_rate_near",	(void**)&SDL_NAME(snd_pcm_hw_params_set_rate_near)	},
slouken@865
   112
	{ "snd_pcm_hw_params_set_period_size_near",	(void**)&SDL_NAME(snd_pcm_hw_params_set_period_size_near)	},
slouken@865
   113
	{ "snd_pcm_hw_params_set_periods_near",	(void**)&SDL_NAME(snd_pcm_hw_params_set_periods_near)	},
slouken@865
   114
	{ "snd_pcm_hw_params",	(void**)&SDL_NAME(snd_pcm_hw_params)	},
slouken@865
   115
	{ "snd_pcm_nonblock",	(void**)&SDL_NAME(snd_pcm_nonblock)	},
slouken@865
   116
};
slouken@865
   117
slouken@865
   118
static void UnloadALSALibrary(void) {
slouken@865
   119
	if (alsa_loaded) {
slouken@865
   120
/*		SDL_UnloadObject(alsa_handle);*/
slouken@865
   121
		dlclose(alsa_handle);
slouken@865
   122
		alsa_handle = NULL;
slouken@865
   123
		alsa_loaded = 0;
slouken@865
   124
	}
slouken@865
   125
}
slouken@865
   126
slouken@865
   127
static int LoadALSALibrary(void) {
slouken@865
   128
	int i, retval = -1;
slouken@865
   129
slouken@865
   130
/*	alsa_handle = SDL_LoadObject(alsa_library);*/
slouken@865
   131
	alsa_handle = dlopen(alsa_library,RTLD_NOW);
slouken@865
   132
	if (alsa_handle) {
slouken@865
   133
		alsa_loaded = 1;
slouken@865
   134
		retval = 0;
slouken@865
   135
		for (i = 0; i < SDL_TABLESIZE(alsa_functions); i++) {
slouken@865
   136
/*			*alsa_functions[i].func = SDL_LoadFunction(alsa_handle,alsa_functions[i].name);*/
slouken@865
   137
			*alsa_functions[i].func = dlvsym(alsa_handle,alsa_functions[i].name,"ALSA_0.9");
slouken@865
   138
			if (!*alsa_functions[i].func) {
slouken@865
   139
				retval = -1;
slouken@865
   140
				UnloadALSALibrary();
slouken@865
   141
				break;
slouken@865
   142
			}
slouken@865
   143
		}
slouken@865
   144
	}
slouken@865
   145
	return retval;
slouken@865
   146
}
slouken@865
   147
slouken@865
   148
#else
slouken@865
   149
slouken@865
   150
static void UnloadALSALibrary(void) {
slouken@865
   151
	return;
slouken@865
   152
}
slouken@865
   153
slouken@865
   154
static int LoadALSALibrary(void) {
slouken@865
   155
	return 0;
slouken@865
   156
}
slouken@865
   157
slouken@865
   158
#endif /* ALSA_DYNAMIC */
slouken@865
   159
slouken@354
   160
static const char *get_audio_device()
slouken@0
   161
{
slouken@354
   162
	const char *device;
slouken@354
   163
	
slouken@354
   164
	device = getenv("AUDIODEV");	/* Is there a standard variable name? */
slouken@354
   165
	if ( device == NULL ) {
slouken@354
   166
		device = DEFAULT_DEVICE;
slouken@354
   167
	}
slouken@354
   168
	return device;
slouken@0
   169
}
slouken@0
   170
slouken@0
   171
/* Audio driver bootstrap functions */
slouken@0
   172
slouken@0
   173
static int Audio_Available(void)
slouken@0
   174
{
slouken@0
   175
	int available;
slouken@354
   176
	int status;
slouken@0
   177
	snd_pcm_t *handle;
slouken@0
   178
slouken@0
   179
	available = 0;
slouken@865
   180
	if (LoadALSALibrary() < 0) {
slouken@865
   181
		return available;
slouken@865
   182
	}
slouken@865
   183
	status = SDL_NAME(snd_pcm_open)(&handle, get_audio_device(), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
slouken@354
   184
	if ( status >= 0 ) {
slouken@354
   185
		available = 1;
slouken@865
   186
        	SDL_NAME(snd_pcm_close)(handle);
slouken@0
   187
	}
slouken@865
   188
	UnloadALSALibrary();
slouken@0
   189
	return(available);
slouken@0
   190
}
slouken@0
   191
slouken@0
   192
static void Audio_DeleteDevice(SDL_AudioDevice *device)
slouken@0
   193
{
slouken@0
   194
	free(device->hidden);
slouken@0
   195
	free(device);
slouken@865
   196
	UnloadALSALibrary();
slouken@0
   197
}
slouken@0
   198
slouken@0
   199
static SDL_AudioDevice *Audio_CreateDevice(int devindex)
slouken@0
   200
{
slouken@0
   201
	SDL_AudioDevice *this;
slouken@0
   202
slouken@0
   203
	/* Initialize all variables that we clean on shutdown */
slouken@865
   204
	LoadALSALibrary();
slouken@0
   205
	this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice));
slouken@0
   206
	if ( this ) {
slouken@0
   207
		memset(this, 0, (sizeof *this));
slouken@0
   208
		this->hidden = (struct SDL_PrivateAudioData *)
slouken@0
   209
				malloc((sizeof *this->hidden));
slouken@0
   210
	}
slouken@0
   211
	if ( (this == NULL) || (this->hidden == NULL) ) {
slouken@0
   212
		SDL_OutOfMemory();
slouken@0
   213
		if ( this ) {
slouken@0
   214
			free(this);
slouken@0
   215
		}
slouken@0
   216
		return(0);
slouken@0
   217
	}
slouken@0
   218
	memset(this->hidden, 0, (sizeof *this->hidden));
slouken@0
   219
slouken@0
   220
	/* Set the function pointers */
slouken@354
   221
	this->OpenAudio = ALSA_OpenAudio;
slouken@354
   222
	this->WaitAudio = ALSA_WaitAudio;
slouken@354
   223
	this->PlayAudio = ALSA_PlayAudio;
slouken@354
   224
	this->GetAudioBuf = ALSA_GetAudioBuf;
slouken@354
   225
	this->CloseAudio = ALSA_CloseAudio;
slouken@0
   226
slouken@0
   227
	this->free = Audio_DeleteDevice;
slouken@0
   228
slouken@0
   229
	return this;
slouken@0
   230
}
slouken@0
   231
slouken@0
   232
AudioBootStrap ALSA_bootstrap = {
slouken@354
   233
	DRIVER_NAME, "ALSA 0.9 PCM audio",
slouken@0
   234
	Audio_Available, Audio_CreateDevice
slouken@0
   235
};
slouken@0
   236
slouken@0
   237
/* This function waits until it is possible to write a full sound buffer */
slouken@354
   238
static void ALSA_WaitAudio(_THIS)
slouken@0
   239
{
slouken@0
   240
	/* Check to see if the thread-parent process is still alive */
slouken@0
   241
	{ static int cnt = 0;
slouken@0
   242
		/* Note that this only works with thread implementations 
slouken@0
   243
		   that use a different process id for each thread.
slouken@0
   244
		*/
slouken@0
   245
		if (parent && (((++cnt)%10) == 0)) { /* Check every 10 loops */
slouken@0
   246
			if ( kill(parent, 0) < 0 ) {
slouken@0
   247
				this->enabled = 0;
slouken@0
   248
			}
slouken@0
   249
		}
slouken@0
   250
	}
slouken@0
   251
}
slouken@0
   252
slouken@354
   253
static void ALSA_PlayAudio(_THIS)
slouken@0
   254
{
slouken@354
   255
	int           status;
slouken@354
   256
	int           sample_len;
slouken@354
   257
	signed short *sample_buf;
slouken@765
   258
slouken@354
   259
	sample_len = this->spec.samples;
slouken@354
   260
	sample_buf = (signed short *)mixbuf;
slouken@354
   261
	while ( sample_len > 0 ) {
slouken@865
   262
		status = SDL_NAME(snd_pcm_writei)(pcm_handle, sample_buf, sample_len);
slouken@354
   263
		if ( status < 0 ) {
slouken@354
   264
			if ( status == -EAGAIN ) {
slouken@765
   265
				SDL_Delay(1);
slouken@354
   266
				continue;
slouken@354
   267
			}
slouken@354
   268
			if ( status == -ESTRPIPE ) {
slouken@354
   269
				do {
slouken@765
   270
					SDL_Delay(1);
slouken@865
   271
					status = SDL_NAME(snd_pcm_resume)(pcm_handle);
slouken@354
   272
				} while ( status == -EAGAIN );
slouken@354
   273
			}
slouken@354
   274
			if ( status < 0 ) {
slouken@865
   275
				status = SDL_NAME(snd_pcm_prepare)(pcm_handle);
slouken@354
   276
			}
slouken@354
   277
			if ( status < 0 ) {
slouken@354
   278
				/* Hmm, not much we can do - abort */
slouken@354
   279
				this->enabled = 0;
slouken@354
   280
				return;
slouken@0
   281
			}
slouken@356
   282
			continue;
slouken@0
   283
		}
slouken@354
   284
		sample_buf += status * this->spec.channels;
slouken@354
   285
		sample_len -= status;
slouken@0
   286
	}
slouken@0
   287
}
slouken@0
   288
slouken@354
   289
static Uint8 *ALSA_GetAudioBuf(_THIS)
slouken@0
   290
{
slouken@354
   291
	return(mixbuf);
slouken@354
   292
}
slouken@0
   293
slouken@354
   294
static void ALSA_CloseAudio(_THIS)
slouken@354
   295
{
slouken@354
   296
	if ( mixbuf != NULL ) {
slouken@354
   297
		SDL_FreeAudioMem(mixbuf);
slouken@354
   298
		mixbuf = NULL;
slouken@354
   299
	}
slouken@354
   300
	if ( pcm_handle ) {
slouken@865
   301
		SDL_NAME(snd_pcm_drain)(pcm_handle);
slouken@865
   302
		SDL_NAME(snd_pcm_close)(pcm_handle);
slouken@354
   303
		pcm_handle = NULL;
slouken@354
   304
	}
slouken@354
   305
}
slouken@0
   306
slouken@354
   307
static int ALSA_OpenAudio(_THIS, SDL_AudioSpec *spec)
slouken@354
   308
{
slouken@354
   309
	int                  status;
slouken@354
   310
	snd_pcm_hw_params_t *params;
slouken@354
   311
	snd_pcm_format_t     format;
slouken@354
   312
	snd_pcm_uframes_t    frames;
slouken@354
   313
	Uint16               test_format;
slouken@0
   314
slouken@0
   315
	/* Open the audio device */
slouken@865
   316
	status = SDL_NAME(snd_pcm_open)(&pcm_handle, get_audio_device(), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
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@354
   425
	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
}