src/audio/alsa/SDL_alsa_audio.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 19 Mar 2006 10:41:49 +0000
changeset 1552 b2462b772cce
parent 1402 d910939febfa
child 1553 63fa37538842
permissions -rw-r--r--
Fixed bug #79

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