src/audio/alsa/SDL_alsa_audio.c
author Sam Lantinga
Thu, 15 Oct 2009 02:52:11 +0000
branchSDL-1.2
changeset 4337 4eb6b4be1dbc
parent 4336 77b20871fd44
child 4339 819270e2f893
permissions -rw-r--r--
Fixed bug #728

I am using a headset on my monitor as primary sound output device. From time to
time it happens that I switch off my monitor when I leave my computer and
switch it on when I come back.
When doing so while wesnoth is running I get no sound back when I switch on my
monitor. There is probably good reason for that. Therefore I wanted to exit and
restart wesnoth. Unfortunately, wesnoth always hangs in this situation. It
looks like wesnoth can not be exited when the primary sound device vanished.
I created a backtrace of this situation (please see attachment).
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@4159
     3
    Copyright (C) 1997-2009 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 "SDL_name.h"
slouken@865
    37
#include "SDL_loadso.h"
slouken@865
    38
#else
slouken@865
    39
#define SDL_NAME(X)	X
slouken@865
    40
#endif
slouken@865
    41
slouken@865
    42
slouken@0
    43
/* The tag name used by ALSA audio */
slouken@0
    44
#define DRIVER_NAME         "alsa"
slouken@0
    45
slouken@4333
    46
/* Whether we should set the buffer size or the period size */
slouken@4333
    47
/*#define SET_PERIOD_SIZE*/
slouken@4333
    48
/*#define DEBUG_PERIOD_SIZE*/
slouken@4333
    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_NAME(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_close))(snd_pcm_t *pcm);
slouken@865
    65
static snd_pcm_sframes_t (*SDL_NAME(snd_pcm_writei))(snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size);
slouken@865
    66
static int (*SDL_NAME(snd_pcm_resume))(snd_pcm_t *pcm);
slouken@865
    67
static int (*SDL_NAME(snd_pcm_prepare))(snd_pcm_t *pcm);
slouken@865
    68
static int (*SDL_NAME(snd_pcm_drain))(snd_pcm_t *pcm);
slouken@865
    69
static const char *(*SDL_NAME(snd_strerror))(int errnum);
slouken@865
    70
static size_t (*SDL_NAME(snd_pcm_hw_params_sizeof))(void);
slouken@1552
    71
static size_t (*SDL_NAME(snd_pcm_sw_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@4331
    76
static int (*SDL_NAME(snd_pcm_hw_params_get_channels))(const snd_pcm_hw_params_t *params, unsigned int *val);
icculus@4292
    77
static 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);
icculus@4292
    78
static int (*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@4331
    79
static int (*SDL_NAME(snd_pcm_hw_params_get_period_size))(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *frames, int *dir);
icculus@4292
    80
static 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@4331
    81
static int (*SDL_NAME(snd_pcm_hw_params_get_periods))(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
slouken@4333
    82
static int (*SDL_NAME(snd_pcm_hw_params_set_buffer_size_near))(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val);
slouken@4331
    83
static int (*SDL_NAME(snd_pcm_hw_params_get_buffer_size))(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val);
slouken@865
    84
static int (*SDL_NAME(snd_pcm_hw_params))(snd_pcm_t *pcm, snd_pcm_hw_params_t *params);
slouken@1552
    85
/*
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))(snd_pcm_t *pcm, snd_pcm_sw_params_t *params);
slouken@1553
    90
static int (*SDL_NAME(snd_pcm_nonblock))(snd_pcm_t *pcm, int nonblock);
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)	},
slouken@1553
   115
	{ "snd_pcm_hw_params_get_period_size",	(void**)(char*)&SDL_NAME(snd_pcm_hw_params_get_period_size)	},
icculus@1161
   116
	{ "snd_pcm_hw_params_set_periods_near",	(void**)(char*)&SDL_NAME(snd_pcm_hw_params_set_periods_near)	},
slouken@1553
   117
	{ "snd_pcm_hw_params_get_periods",	(void**)(char*)&SDL_NAME(snd_pcm_hw_params_get_periods)	},
slouken@4333
   118
	{ "snd_pcm_hw_params_set_buffer_size_near",	(void**)(char*)&SDL_NAME(snd_pcm_hw_params_set_buffer_size_near) },
slouken@4331
   119
	{ "snd_pcm_hw_params_get_buffer_size",	(void**)(char*)&SDL_NAME(snd_pcm_hw_params_get_buffer_size) },
icculus@1161
   120
	{ "snd_pcm_hw_params",	(void**)(char*)&SDL_NAME(snd_pcm_hw_params)	},
slouken@1552
   121
	{ "snd_pcm_sw_params_current",	(void**)(char*)&SDL_NAME(snd_pcm_sw_params_current)	},
slouken@1552
   122
	{ "snd_pcm_sw_params_set_start_threshold",	(void**)(char*)&SDL_NAME(snd_pcm_sw_params_set_start_threshold)	},
slouken@1552
   123
	{ "snd_pcm_sw_params",	(void**)(char*)&SDL_NAME(snd_pcm_sw_params)	},
slouken@1553
   124
	{ "snd_pcm_nonblock",	(void**)(char*)&SDL_NAME(snd_pcm_nonblock)	},
slouken@865
   125
};
slouken@865
   126
slouken@865
   127
static void UnloadALSALibrary(void) {
slouken@865
   128
	if (alsa_loaded) {
icculus@4292
   129
		SDL_UnloadObject(alsa_handle);
slouken@865
   130
		alsa_handle = NULL;
slouken@865
   131
		alsa_loaded = 0;
slouken@865
   132
	}
slouken@865
   133
}
slouken@865
   134
slouken@865
   135
static int LoadALSALibrary(void) {
slouken@865
   136
	int i, retval = -1;
slouken@865
   137
icculus@4292
   138
	alsa_handle = SDL_LoadObject(alsa_library);
slouken@865
   139
	if (alsa_handle) {
slouken@865
   140
		alsa_loaded = 1;
slouken@865
   141
		retval = 0;
slouken@1379
   142
		for (i = 0; i < SDL_arraysize(alsa_functions); i++) {
icculus@4292
   143
			*alsa_functions[i].func = SDL_LoadFunction(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@1361
   164
#endif /* SDL_AUDIO_DRIVER_ALSA_DYNAMIC */
slouken@865
   165
slouken@942
   166
static const char *get_audio_device(int channels)
slouken@354
   167
{
slouken@354
   168
	const char *device;
slouken@354
   169
	
slouken@1336
   170
	device = SDL_getenv("AUDIODEV");	/* Is there a standard variable name? */
slouken@354
   171
	if ( device == NULL ) {
slouken@4334
   172
		switch (channels) {
slouken@4334
   173
		case 6:
slouken@4334
   174
			device = "plug:surround51";
slouken@4334
   175
			break;
slouken@4334
   176
		case 4:
slouken@4334
   177
			device = "plug:surround40";
slouken@4334
   178
			break;
slouken@4334
   179
		default:
slouken@4334
   180
			device = "default";
slouken@4334
   181
			break;
slouken@4334
   182
		}
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
icculus@1878
   269
icculus@1878
   270
/*
icculus@1878
   271
 * http://bugzilla.libsdl.org/show_bug.cgi?id=110
icculus@1878
   272
 * "For Linux ALSA, this is FL-FR-RL-RR-C-LFE
icculus@1878
   273
 *  and for Windows DirectX [and CoreAudio], this is FL-FR-C-LFE-RL-RR"
icculus@1878
   274
 */
icculus@1878
   275
#define SWIZ6(T) \
icculus@1878
   276
    T *ptr = (T *) mixbuf; \
icculus@1878
   277
    const Uint32 count = (this->spec.samples / 6); \
icculus@1878
   278
    Uint32 i; \
icculus@1878
   279
    for (i = 0; i < count; i++, ptr += 6) { \
icculus@1878
   280
        T tmp; \
icculus@1878
   281
        tmp = ptr[2]; ptr[2] = ptr[4]; ptr[4] = tmp; \
icculus@1878
   282
        tmp = ptr[3]; ptr[3] = ptr[5]; ptr[5] = tmp; \
icculus@1878
   283
    }
icculus@1878
   284
icculus@1878
   285
static __inline__ void swizzle_alsa_channels_6_64bit(_THIS) { SWIZ6(Uint64); }
icculus@1878
   286
static __inline__ void swizzle_alsa_channels_6_32bit(_THIS) { SWIZ6(Uint32); }
icculus@1878
   287
static __inline__ void swizzle_alsa_channels_6_16bit(_THIS) { SWIZ6(Uint16); }
icculus@1878
   288
static __inline__ void swizzle_alsa_channels_6_8bit(_THIS) { SWIZ6(Uint8); }
icculus@1878
   289
icculus@1878
   290
#undef SWIZ6
icculus@1878
   291
icculus@1878
   292
icculus@1878
   293
/*
icculus@1878
   294
 * Called right before feeding this->mixbuf to the hardware. Swizzle channels
icculus@1878
   295
 *  from Windows/Mac order to the format alsalib will want.
icculus@1878
   296
 */
icculus@1878
   297
static __inline__ void swizzle_alsa_channels(_THIS)
icculus@1878
   298
{
icculus@1878
   299
    if (this->spec.channels == 6) {
icculus@1878
   300
        const Uint16 fmtsize = (this->spec.format & 0xFF); /* bits/channel. */
icculus@1878
   301
        if (fmtsize == 16)
icculus@1878
   302
            swizzle_alsa_channels_6_16bit(this);
icculus@1878
   303
        else if (fmtsize == 8)
icculus@1878
   304
            swizzle_alsa_channels_6_8bit(this);
icculus@1878
   305
        else if (fmtsize == 32)
icculus@1878
   306
            swizzle_alsa_channels_6_32bit(this);
icculus@1878
   307
        else if (fmtsize == 64)
icculus@1878
   308
            swizzle_alsa_channels_6_64bit(this);
icculus@1878
   309
    }
icculus@1878
   310
icculus@1878
   311
    /* !!! FIXME: update this for 7.1 if needed, later. */
icculus@1878
   312
}
icculus@1878
   313
icculus@1878
   314
slouken@354
   315
static void ALSA_PlayAudio(_THIS)
slouken@0
   316
{
icculus@4320
   317
	int status;
slouken@4332
   318
	snd_pcm_uframes_t frames_left;
icculus@4320
   319
	const Uint8 *sample_buf = (const Uint8 *) mixbuf;
slouken@4336
   320
	const int frame_size = (((int) (this->spec.format & 0xFF)) / 8) * this->spec.channels;
slouken@765
   321
icculus@1878
   322
	swizzle_alsa_channels(this);
icculus@1878
   323
slouken@4332
   324
	frames_left = ((snd_pcm_uframes_t) this->spec.samples);
icculus@1878
   325
slouken@4337
   326
	while ( frames_left > 0 && this->enabled ) {
slouken@4332
   327
		status = SDL_NAME(snd_pcm_writei)(pcm_handle, sample_buf, frames_left);
slouken@354
   328
		if ( status < 0 ) {
slouken@354
   329
			if ( status == -EAGAIN ) {
slouken@765
   330
				SDL_Delay(1);
slouken@354
   331
				continue;
slouken@354
   332
			}
slouken@354
   333
			if ( status == -ESTRPIPE ) {
slouken@354
   334
				do {
slouken@765
   335
					SDL_Delay(1);
slouken@865
   336
					status = SDL_NAME(snd_pcm_resume)(pcm_handle);
slouken@354
   337
				} while ( status == -EAGAIN );
slouken@354
   338
			}
slouken@354
   339
			if ( status < 0 ) {
slouken@865
   340
				status = SDL_NAME(snd_pcm_prepare)(pcm_handle);
slouken@354
   341
			}
slouken@354
   342
			if ( status < 0 ) {
slouken@354
   343
				/* Hmm, not much we can do - abort */
slouken@354
   344
				this->enabled = 0;
slouken@354
   345
				return;
slouken@0
   346
			}
slouken@356
   347
			continue;
slouken@0
   348
		}
slouken@4336
   349
		sample_buf += status * frame_size;
slouken@4332
   350
		frames_left -= status;
slouken@0
   351
	}
slouken@0
   352
}
slouken@0
   353
slouken@354
   354
static Uint8 *ALSA_GetAudioBuf(_THIS)
slouken@0
   355
{
slouken@354
   356
	return(mixbuf);
slouken@0
   357
}
slouken@0
   358
slouken@354
   359
static void ALSA_CloseAudio(_THIS)
slouken@0
   360
{
slouken@354
   361
	if ( mixbuf != NULL ) {
slouken@354
   362
		SDL_FreeAudioMem(mixbuf);
slouken@354
   363
		mixbuf = NULL;
slouken@0
   364
	}
slouken@354
   365
	if ( pcm_handle ) {
slouken@865
   366
		SDL_NAME(snd_pcm_drain)(pcm_handle);
slouken@865
   367
		SDL_NAME(snd_pcm_close)(pcm_handle);
slouken@354
   368
		pcm_handle = NULL;
slouken@0
   369
	}
slouken@0
   370
}
slouken@0
   371
slouken@354
   372
static int ALSA_OpenAudio(_THIS, SDL_AudioSpec *spec)
slouken@0
   373
{
slouken@354
   374
	int                  status;
slouken@1552
   375
	snd_pcm_hw_params_t *hwparams;
slouken@1552
   376
	snd_pcm_sw_params_t *swparams;
slouken@354
   377
	snd_pcm_format_t     format;
slouken@354
   378
	snd_pcm_uframes_t    frames;
icculus@4292
   379
	unsigned int         rate;
slouken@4333
   380
#ifdef SET_PERIOD_SIZE
icculus@4292
   381
	unsigned int         periods;
slouken@4333
   382
#endif
slouken@4333
   383
	unsigned int 	     channels;
slouken@354
   384
	Uint16               test_format;
slouken@0
   385
slouken@0
   386
	/* Open the audio device */
slouken@942
   387
	/* Name of device should depend on # channels in spec */
slouken@942
   388
	status = SDL_NAME(snd_pcm_open)(&pcm_handle, get_audio_device(spec->channels), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
slouken@942
   389
slouken@354
   390
	if ( status < 0 ) {
slouken@865
   391
		SDL_SetError("Couldn't open audio device: %s", SDL_NAME(snd_strerror)(status));
slouken@354
   392
		return(-1);
slouken@354
   393
	}
slouken@354
   394
slouken@354
   395
	/* Figure out what the hardware is capable of */
slouken@1552
   396
	snd_pcm_hw_params_alloca(&hwparams);
slouken@1552
   397
	status = SDL_NAME(snd_pcm_hw_params_any)(pcm_handle, hwparams);
slouken@354
   398
	if ( status < 0 ) {
slouken@865
   399
		SDL_SetError("Couldn't get hardware config: %s", SDL_NAME(snd_strerror)(status));
slouken@354
   400
		ALSA_CloseAudio(this);
slouken@354
   401
		return(-1);
slouken@354
   402
	}
slouken@354
   403
slouken@354
   404
	/* SDL only uses interleaved sample output */
slouken@1552
   405
	status = SDL_NAME(snd_pcm_hw_params_set_access)(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
slouken@354
   406
	if ( status < 0 ) {
slouken@865
   407
		SDL_SetError("Couldn't set interleaved access: %s", SDL_NAME(snd_strerror)(status));
slouken@354
   408
		ALSA_CloseAudio(this);
slouken@0
   409
		return(-1);
slouken@0
   410
	}
slouken@0
   411
slouken@0
   412
	/* Try for a closest match on audio format */
slouken@354
   413
	status = -1;
slouken@0
   414
	for ( test_format = SDL_FirstAudioFormat(spec->format);
slouken@354
   415
	      test_format && (status < 0); ) {
slouken@354
   416
		switch ( test_format ) {
slouken@0
   417
			case AUDIO_U8:
slouken@354
   418
				format = SND_PCM_FORMAT_U8;
slouken@0
   419
				break;
slouken@0
   420
			case AUDIO_S8:
slouken@354
   421
				format = SND_PCM_FORMAT_S8;
slouken@0
   422
				break;
slouken@0
   423
			case AUDIO_S16LSB:
slouken@354
   424
				format = SND_PCM_FORMAT_S16_LE;
slouken@0
   425
				break;
slouken@0
   426
			case AUDIO_S16MSB:
slouken@354
   427
				format = SND_PCM_FORMAT_S16_BE;
slouken@0
   428
				break;
slouken@0
   429
			case AUDIO_U16LSB:
slouken@354
   430
				format = SND_PCM_FORMAT_U16_LE;
slouken@0
   431
				break;
slouken@0
   432
			case AUDIO_U16MSB:
slouken@354
   433
				format = SND_PCM_FORMAT_U16_BE;
slouken@0
   434
				break;
slouken@0
   435
			default:
slouken@354
   436
				format = 0;
slouken@0
   437
				break;
slouken@0
   438
		}
slouken@354
   439
		if ( format != 0 ) {
slouken@1552
   440
			status = SDL_NAME(snd_pcm_hw_params_set_format)(pcm_handle, hwparams, format);
slouken@354
   441
		}
slouken@354
   442
		if ( status < 0 ) {
slouken@0
   443
			test_format = SDL_NextAudioFormat();
slouken@0
   444
		}
slouken@0
   445
	}
slouken@354
   446
	if ( status < 0 ) {
slouken@0
   447
		SDL_SetError("Couldn't find any hardware audio formats");
slouken@354
   448
		ALSA_CloseAudio(this);
slouken@0
   449
		return(-1);
slouken@0
   450
	}
slouken@0
   451
	spec->format = test_format;
slouken@0
   452
slouken@354
   453
	/* Set the number of channels */
slouken@1552
   454
	status = SDL_NAME(snd_pcm_hw_params_set_channels)(pcm_handle, hwparams, spec->channels);
icculus@4292
   455
	channels = spec->channels;
slouken@354
   456
	if ( status < 0 ) {
icculus@4292
   457
		status = SDL_NAME(snd_pcm_hw_params_get_channels)(hwparams, &channels);
icculus@4292
   458
		if ( status < 0 ) {
slouken@354
   459
			SDL_SetError("Couldn't set audio channels");
slouken@354
   460
			ALSA_CloseAudio(this);
slouken@354
   461
			return(-1);
slouken@354
   462
		}
icculus@4292
   463
		spec->channels = channels;
slouken@354
   464
	}
slouken@0
   465
slouken@354
   466
	/* Set the audio rate */
icculus@4292
   467
	rate = spec->freq;
icculus@4292
   468
icculus@4292
   469
	status = SDL_NAME(snd_pcm_hw_params_set_rate_near)(pcm_handle, hwparams, &rate, NULL);
slouken@354
   470
	if ( status < 0 ) {
slouken@865
   471
		SDL_SetError("Couldn't set audio frequency: %s", SDL_NAME(snd_strerror)(status));
slouken@354
   472
		ALSA_CloseAudio(this);
slouken@354
   473
		return(-1);
slouken@354
   474
	}
icculus@4292
   475
	spec->freq = rate;
slouken@0
   476
slouken@354
   477
	/* Set the buffer size, in samples */
slouken@4333
   478
#ifdef SET_PERIOD_SIZE
slouken@354
   479
	frames = spec->samples;
icculus@4292
   480
	status = SDL_NAME(snd_pcm_hw_params_set_period_size_near)(pcm_handle, hwparams, &frames, NULL);
icculus@4292
   481
	if ( status < 0 ) {
slouken@4333
   482
		SDL_SetError("Couldn't set period size: %s", SDL_NAME(snd_strerror)(status));
icculus@4292
   483
		ALSA_CloseAudio(this);
icculus@4292
   484
		return(-1);
icculus@4292
   485
	}
icculus@4292
   486
slouken@354
   487
	spec->samples = frames;
icculus@4292
   488
icculus@4292
   489
	periods = 2;
slouken@4333
   490
	status = SDL_NAME(snd_pcm_hw_params_set_periods_near)(pcm_handle, hwparams, &periods, NULL);
slouken@4333
   491
	if ( status < 0 ) {
slouken@4333
   492
		SDL_SetError("Couldn't set period count: %s", SDL_NAME(snd_strerror)(status));
slouken@4333
   493
		ALSA_CloseAudio(this);
slouken@4333
   494
		return(-1);
slouken@4333
   495
	}
slouken@4333
   496
#else
slouken@4333
   497
	frames = spec->samples * 2;
slouken@4333
   498
	status = SDL_NAME(snd_pcm_hw_params_set_buffer_size_near)(pcm_handle, hwparams, &frames);
slouken@4333
   499
#endif
slouken@354
   500
slouken@354
   501
	/* "set" the hardware with the desired parameters */
slouken@1552
   502
	status = SDL_NAME(snd_pcm_hw_params)(pcm_handle, hwparams);
slouken@1552
   503
	if ( status < 0 ) {
slouken@1552
   504
		SDL_SetError("Couldn't set hardware audio parameters: %s", SDL_NAME(snd_strerror)(status));
slouken@1552
   505
		ALSA_CloseAudio(this);
slouken@1552
   506
		return(-1);
slouken@1552
   507
	}
slouken@1552
   508
slouken@1553
   509
/* This is useful for debugging... */
slouken@4333
   510
#ifdef DEBUG_PERIOD_SIZE
slouken@4331
   511
{ snd_pcm_uframes_t bufsize; snd_pcm_sframes_t persize; unsigned int periods; int dir;
slouken@4331
   512
   SDL_NAME(snd_pcm_hw_params_get_buffer_size)(hwparams, &bufsize);
slouken@4331
   513
   SDL_NAME(snd_pcm_hw_params_get_period_size)(hwparams, &persize, &dir);
slouken@4331
   514
   SDL_NAME(snd_pcm_hw_params_get_periods)(hwparams, &periods, &dir);
slouken@1552
   515
slouken@4331
   516
   fprintf(stderr, "ALSA: period size = %ld, periods = %u, buffer size = %lu\n", persize, periods, bufsize);
slouken@1552
   517
}
slouken@4331
   518
#endif
slouken@1552
   519
slouken@1552
   520
	/* Set the software parameters */
slouken@1552
   521
	snd_pcm_sw_params_alloca(&swparams);
slouken@1552
   522
	status = SDL_NAME(snd_pcm_sw_params_current)(pcm_handle, swparams);
slouken@354
   523
	if ( status < 0 ) {
slouken@1552
   524
		SDL_SetError("Couldn't get software config: %s", SDL_NAME(snd_strerror)(status));
slouken@1552
   525
		ALSA_CloseAudio(this);
slouken@1552
   526
		return(-1);
slouken@1552
   527
	}
slouken@4331
   528
	status = SDL_NAME(snd_pcm_sw_params_set_start_threshold)(pcm_handle, swparams, 1);
slouken@1552
   529
	if ( status < 0 ) {
slouken@1552
   530
		SDL_SetError("Couldn't set start threshold: %s", SDL_NAME(snd_strerror)(status));
slouken@1552
   531
		ALSA_CloseAudio(this);
slouken@1552
   532
		return(-1);
slouken@1552
   533
	}
slouken@1552
   534
	status = SDL_NAME(snd_pcm_sw_params)(pcm_handle, swparams);
slouken@1552
   535
	if ( status < 0 ) {
slouken@1552
   536
		SDL_SetError("Couldn't set software audio parameters: %s", SDL_NAME(snd_strerror)(status));
slouken@354
   537
		ALSA_CloseAudio(this);
slouken@0
   538
		return(-1);
slouken@0
   539
	}
slouken@0
   540
slouken@354
   541
	/* Calculate the final parameters for this audio specification */
slouken@354
   542
	SDL_CalculateAudioSpec(spec);
slouken@0
   543
slouken@354
   544
	/* Allocate mixing buffer */
slouken@354
   545
	mixlen = spec->size;
slouken@354
   546
	mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen);
slouken@354
   547
	if ( mixbuf == NULL ) {
slouken@354
   548
		ALSA_CloseAudio(this);
slouken@354
   549
		return(-1);
slouken@0
   550
	}
slouken@1336
   551
	SDL_memset(mixbuf, spec->silence, spec->size);
slouken@0
   552
slouken@0
   553
	/* Get the parent process id (we're the parent of the audio thread) */
slouken@0
   554
	parent = getpid();
slouken@0
   555
slouken@765
   556
	/* Switch to blocking mode for playback */
slouken@865
   557
	SDL_NAME(snd_pcm_nonblock)(pcm_handle, 0);
slouken@765
   558
slouken@0
   559
	/* We're ready to rock and roll. :-) */
slouken@0
   560
	return(0);
slouken@0
   561
}