src/audio/alsa/SDL_alsa_audio.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 21 Aug 2004 12:27:02 +0000
changeset 942 41a59de7f2ed
parent 939 c7c04f811994
child 1161 05d4b93b911e
permissions -rw-r--r--
Here are patches for SDL12 and SDL_mixer for 4 or 6 channel
surround sound on Linux using the Alsa driver. To use them, naturally
you need a sound card that will do 4 or 6 channels and probably also a
recent version of the Alsa drivers and library. Since the only SDL
output driver that knows about surround sound is the Alsa driver,
you���ll want to choose it, using:

export SDL_AUDIODRIVER=alsa

There are no syntactic changes to the programming API. No new
library calls, no differences in arguments.

There are two semantic changes:

(1) For library calls with number of channels as an argument, formerly
you could use only 1 or 2 for the number of channels. Now you
can also use 4 or 6.

(2) The two "left" and "right" arguments to Mix_SetPanning, for the
case of 4 or 6 channels, no longer simply control the volumes of
the left and right channels. Now the "left" argument is converted
to an angle and Mix_SetPosition is called, and the "right" argu-
ment is ignored.

With two exceptions, so far as I know, the modified SDL12 and
SDL_mixer work the same way as the original versions, when opened for
1 or 2 channel output. The two exceptions are bugs which I fixed.
Well, the first, anyway, is a bug for sure. When rate conversions up
or down by a factor of two are applied (in src/audio/SDL_audiocvt.c),
streams with different numbers of channels (that is, mono and stereo)
are treated the same way: either each sample is copied or every other
sample is omitted. This is ok for mono, but for stereo, it is frames
that should be copied or omitted, where by "frame" I mean a portion of
the stream containing one sample for each channel. (In the SDL source,
confusingly, sometimes frames are called "samples".) So for these
rate conversions, stereo streams have to be treated differently, and
they are, in my modified version.

The other problem that might be characterized as a bug arises
when SDL_mixer is passed a multichannel chunk which does not have an
integral number of frames. Due to the way the effect_position code
loops over frames, when the chunk ends with a partial frame, memory
outside the chunk buffer will be accessed. In the case of stereo,
it���s possible that because malloc may give more memory than requested,
this potential problem never actually causes a segment fault. I don���t
know. For 6 channel chunks, I do know, and it does cause segment
faults.


If SDL_mixer is passed defective chunks and this causes a segment
fault, arguably, that���s not a bug in SDL_mixer. Still, whether or not
it counts as a bug, it���s easy to protect against, so why not? I added
code in mixer.c to discard any partial frame at the end of a chunk.

Then what about when SDL or SDL_mixer is opened for 4 or 6 chan-
nel output? What happens with the parts of the current library
designed for stereo? I don���t know whether I���ve covered all the bases,
but I���ve tried:

(1) For playing 2 channel waves, or other cases where SDL knows it has
to match up a 2 channel source with a 4 or 6 channel output, I���ve
added code in SDL_audiocvt.c to make the necessary conversions.

(2) For playing midis using timidity, I���ve converted timidity to do 4
or 6 channel output, upon request.

(3) For playing mods using mikmod, I put ad hoc code in music.c to
convert the stereo output that mikmod produces to 4 or 6 chan-
nels. Obviously it would be better to change the mikmod code to
mix down into 4 or 6 channels, but I have a hard time following
the code in mikmod, so I didn���t do that.

(4) For playing mp3s, I put ad hoc code in smpeg to copy channels in
the case when 4 or 6 channel output is needed.

(5) There seems to be no problem with .ogg files - stereo .oggs can be
up converted as .wavs are.

(6) The effect_position code in SDL_mixer is now generalized to in-
clude the cases of 4 and 6 channel streams.

I���ve done a very limited amount of compatibility testing for some
of the games using SDL I happen to have. For details, see the file
TESTS.

I���ve put into a separate archive, Surround-SDL-testfiles.tgz, a
couple of 6 channel wave files for testing and a 6 channel ogg file.
If you have the right hardware and version of Alsa, you should be able
to play the wave files with the Alsa utility aplay (and hear all
channels, except maybe lfe, for chan-id.wav, since it���s rather faint).
Don���t expect aplay to give good sound, though. There���s something
wrong with the current version of aplay.

The canyon.ogg file is to test loading of 6 channel oggs. After
patching and compiling, you can play it with playmus. (My version of
ogg123 will not play it, and I had to patch mplayer to get it to play
6 channel oggs.)

Greg Lee <greg@ling.lll.hawaii.edu>
Thus, July 1, 2004
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@942
   166
static const char *get_audio_device(int channels)
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@942
   172
		if (channels == 6) device = "surround51";
slouken@942
   173
		else if (channels == 4) device = "surround40";
slouken@942
   174
		else device = DEFAULT_DEVICE;
slouken@354
   175
	}
slouken@354
   176
	return device;
slouken@0
   177
}
slouken@0
   178
slouken@0
   179
/* Audio driver bootstrap functions */
slouken@0
   180
slouken@0
   181
static int Audio_Available(void)
slouken@0
   182
{
slouken@0
   183
	int available;
slouken@354
   184
	int status;
slouken@0
   185
	snd_pcm_t *handle;
slouken@0
   186
slouken@0
   187
	available = 0;
slouken@865
   188
	if (LoadALSALibrary() < 0) {
slouken@865
   189
		return available;
slouken@865
   190
	}
slouken@942
   191
	status = SDL_NAME(snd_pcm_open)(&handle, get_audio_device(2), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
slouken@354
   192
	if ( status >= 0 ) {
slouken@354
   193
		available = 1;
slouken@865
   194
        	SDL_NAME(snd_pcm_close)(handle);
slouken@0
   195
	}
slouken@865
   196
	UnloadALSALibrary();
slouken@0
   197
	return(available);
slouken@0
   198
}
slouken@0
   199
slouken@0
   200
static void Audio_DeleteDevice(SDL_AudioDevice *device)
slouken@0
   201
{
slouken@0
   202
	free(device->hidden);
slouken@0
   203
	free(device);
slouken@865
   204
	UnloadALSALibrary();
slouken@0
   205
}
slouken@0
   206
slouken@0
   207
static SDL_AudioDevice *Audio_CreateDevice(int devindex)
slouken@0
   208
{
slouken@0
   209
	SDL_AudioDevice *this;
slouken@0
   210
slouken@0
   211
	/* Initialize all variables that we clean on shutdown */
slouken@865
   212
	LoadALSALibrary();
slouken@0
   213
	this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice));
slouken@0
   214
	if ( this ) {
slouken@0
   215
		memset(this, 0, (sizeof *this));
slouken@0
   216
		this->hidden = (struct SDL_PrivateAudioData *)
slouken@0
   217
				malloc((sizeof *this->hidden));
slouken@0
   218
	}
slouken@0
   219
	if ( (this == NULL) || (this->hidden == NULL) ) {
slouken@0
   220
		SDL_OutOfMemory();
slouken@0
   221
		if ( this ) {
slouken@0
   222
			free(this);
slouken@0
   223
		}
slouken@0
   224
		return(0);
slouken@0
   225
	}
slouken@0
   226
	memset(this->hidden, 0, (sizeof *this->hidden));
slouken@0
   227
slouken@0
   228
	/* Set the function pointers */
slouken@354
   229
	this->OpenAudio = ALSA_OpenAudio;
slouken@354
   230
	this->WaitAudio = ALSA_WaitAudio;
slouken@354
   231
	this->PlayAudio = ALSA_PlayAudio;
slouken@354
   232
	this->GetAudioBuf = ALSA_GetAudioBuf;
slouken@354
   233
	this->CloseAudio = ALSA_CloseAudio;
slouken@0
   234
slouken@0
   235
	this->free = Audio_DeleteDevice;
slouken@0
   236
slouken@0
   237
	return this;
slouken@0
   238
}
slouken@0
   239
slouken@0
   240
AudioBootStrap ALSA_bootstrap = {
slouken@354
   241
	DRIVER_NAME, "ALSA 0.9 PCM audio",
slouken@0
   242
	Audio_Available, Audio_CreateDevice
slouken@0
   243
};
slouken@0
   244
slouken@0
   245
/* This function waits until it is possible to write a full sound buffer */
slouken@354
   246
static void ALSA_WaitAudio(_THIS)
slouken@0
   247
{
slouken@0
   248
	/* Check to see if the thread-parent process is still alive */
slouken@0
   249
	{ static int cnt = 0;
slouken@0
   250
		/* Note that this only works with thread implementations 
slouken@0
   251
		   that use a different process id for each thread.
slouken@0
   252
		*/
slouken@0
   253
		if (parent && (((++cnt)%10) == 0)) { /* Check every 10 loops */
slouken@0
   254
			if ( kill(parent, 0) < 0 ) {
slouken@0
   255
				this->enabled = 0;
slouken@0
   256
			}
slouken@0
   257
		}
slouken@0
   258
	}
slouken@0
   259
}
slouken@0
   260
slouken@354
   261
static void ALSA_PlayAudio(_THIS)
slouken@0
   262
{
slouken@354
   263
	int           status;
slouken@354
   264
	int           sample_len;
slouken@354
   265
	signed short *sample_buf;
slouken@765
   266
slouken@354
   267
	sample_len = this->spec.samples;
slouken@354
   268
	sample_buf = (signed short *)mixbuf;
slouken@354
   269
	while ( sample_len > 0 ) {
slouken@865
   270
		status = SDL_NAME(snd_pcm_writei)(pcm_handle, sample_buf, sample_len);
slouken@354
   271
		if ( status < 0 ) {
slouken@354
   272
			if ( status == -EAGAIN ) {
slouken@765
   273
				SDL_Delay(1);
slouken@354
   274
				continue;
slouken@354
   275
			}
slouken@354
   276
			if ( status == -ESTRPIPE ) {
slouken@354
   277
				do {
slouken@765
   278
					SDL_Delay(1);
slouken@865
   279
					status = SDL_NAME(snd_pcm_resume)(pcm_handle);
slouken@354
   280
				} while ( status == -EAGAIN );
slouken@354
   281
			}
slouken@354
   282
			if ( status < 0 ) {
slouken@865
   283
				status = SDL_NAME(snd_pcm_prepare)(pcm_handle);
slouken@354
   284
			}
slouken@354
   285
			if ( status < 0 ) {
slouken@354
   286
				/* Hmm, not much we can do - abort */
slouken@354
   287
				this->enabled = 0;
slouken@354
   288
				return;
slouken@0
   289
			}
slouken@356
   290
			continue;
slouken@0
   291
		}
slouken@354
   292
		sample_buf += status * this->spec.channels;
slouken@354
   293
		sample_len -= status;
slouken@0
   294
	}
slouken@0
   295
}
slouken@0
   296
slouken@354
   297
static Uint8 *ALSA_GetAudioBuf(_THIS)
slouken@0
   298
{
slouken@354
   299
	return(mixbuf);
slouken@354
   300
}
slouken@0
   301
slouken@354
   302
static void ALSA_CloseAudio(_THIS)
slouken@354
   303
{
slouken@354
   304
	if ( mixbuf != NULL ) {
slouken@354
   305
		SDL_FreeAudioMem(mixbuf);
slouken@354
   306
		mixbuf = NULL;
slouken@354
   307
	}
slouken@354
   308
	if ( pcm_handle ) {
slouken@865
   309
		SDL_NAME(snd_pcm_drain)(pcm_handle);
slouken@865
   310
		SDL_NAME(snd_pcm_close)(pcm_handle);
slouken@354
   311
		pcm_handle = NULL;
slouken@354
   312
	}
slouken@354
   313
}
slouken@0
   314
slouken@354
   315
static int ALSA_OpenAudio(_THIS, SDL_AudioSpec *spec)
slouken@354
   316
{
slouken@354
   317
	int                  status;
slouken@354
   318
	snd_pcm_hw_params_t *params;
slouken@354
   319
	snd_pcm_format_t     format;
slouken@354
   320
	snd_pcm_uframes_t    frames;
slouken@354
   321
	Uint16               test_format;
slouken@0
   322
slouken@0
   323
	/* Open the audio device */
slouken@942
   324
	/* Name of device should depend on # channels in spec */
slouken@942
   325
	status = SDL_NAME(snd_pcm_open)(&pcm_handle, get_audio_device(spec->channels), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
slouken@942
   326
slouken@354
   327
	if ( status < 0 ) {
slouken@865
   328
		SDL_SetError("Couldn't open audio device: %s", SDL_NAME(snd_strerror)(status));
slouken@0
   329
		return(-1);
slouken@0
   330
	}
slouken@0
   331
slouken@354
   332
	/* Figure out what the hardware is capable of */
slouken@354
   333
	snd_pcm_hw_params_alloca(&params);
slouken@865
   334
	status = SDL_NAME(snd_pcm_hw_params_any)(pcm_handle, params);
slouken@354
   335
	if ( status < 0 ) {
slouken@865
   336
		SDL_SetError("Couldn't get hardware config: %s", SDL_NAME(snd_strerror)(status));
slouken@354
   337
		ALSA_CloseAudio(this);
slouken@354
   338
		return(-1);
slouken@354
   339
	}
slouken@0
   340
slouken@354
   341
	/* SDL only uses interleaved sample output */
slouken@865
   342
	status = SDL_NAME(snd_pcm_hw_params_set_access)(pcm_handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
slouken@354
   343
	if ( status < 0 ) {
slouken@865
   344
		SDL_SetError("Couldn't set interleaved access: %s", SDL_NAME(snd_strerror)(status));
slouken@354
   345
		ALSA_CloseAudio(this);
slouken@354
   346
		return(-1);
slouken@354
   347
	}
slouken@0
   348
slouken@0
   349
	/* Try for a closest match on audio format */
slouken@354
   350
	status = -1;
slouken@0
   351
	for ( test_format = SDL_FirstAudioFormat(spec->format);
slouken@354
   352
	      test_format && (status < 0); ) {
slouken@354
   353
		switch ( test_format ) {
slouken@0
   354
			case AUDIO_U8:
slouken@354
   355
				format = SND_PCM_FORMAT_U8;
slouken@0
   356
				break;
slouken@0
   357
			case AUDIO_S8:
slouken@354
   358
				format = SND_PCM_FORMAT_S8;
slouken@0
   359
				break;
slouken@0
   360
			case AUDIO_S16LSB:
slouken@354
   361
				format = SND_PCM_FORMAT_S16_LE;
slouken@0
   362
				break;
slouken@0
   363
			case AUDIO_S16MSB:
slouken@354
   364
				format = SND_PCM_FORMAT_S16_BE;
slouken@0
   365
				break;
slouken@0
   366
			case AUDIO_U16LSB:
slouken@354
   367
				format = SND_PCM_FORMAT_U16_LE;
slouken@0
   368
				break;
slouken@0
   369
			case AUDIO_U16MSB:
slouken@354
   370
				format = SND_PCM_FORMAT_U16_BE;
slouken@0
   371
				break;
slouken@0
   372
			default:
slouken@354
   373
				format = 0;
slouken@0
   374
				break;
slouken@0
   375
		}
slouken@354
   376
		if ( format != 0 ) {
slouken@865
   377
			status = SDL_NAME(snd_pcm_hw_params_set_format)(pcm_handle, params, format);
slouken@354
   378
		}
slouken@354
   379
		if ( status < 0 ) {
slouken@0
   380
			test_format = SDL_NextAudioFormat();
slouken@0
   381
		}
slouken@0
   382
	}
slouken@354
   383
	if ( status < 0 ) {
slouken@0
   384
		SDL_SetError("Couldn't find any hardware audio formats");
slouken@354
   385
		ALSA_CloseAudio(this);
slouken@0
   386
		return(-1);
slouken@0
   387
	}
slouken@0
   388
	spec->format = test_format;
slouken@0
   389
slouken@354
   390
	/* Set the number of channels */
slouken@865
   391
	status = SDL_NAME(snd_pcm_hw_params_set_channels)(pcm_handle, params, spec->channels);
slouken@354
   392
	if ( status < 0 ) {
slouken@865
   393
		status = SDL_NAME(snd_pcm_hw_params_get_channels)(params);
slouken@354
   394
		if ( (status <= 0) || (status > 2) ) {
slouken@354
   395
			SDL_SetError("Couldn't set audio channels");
slouken@354
   396
			ALSA_CloseAudio(this);
slouken@354
   397
			return(-1);
slouken@354
   398
		}
slouken@354
   399
		spec->channels = status;
slouken@354
   400
	}
slouken@0
   401
slouken@354
   402
	/* Set the audio rate */
slouken@865
   403
	status = SDL_NAME(snd_pcm_hw_params_set_rate_near)(pcm_handle, params, spec->freq, NULL);
slouken@354
   404
	if ( status < 0 ) {
slouken@865
   405
		SDL_SetError("Couldn't set audio frequency: %s", SDL_NAME(snd_strerror)(status));
slouken@354
   406
		ALSA_CloseAudio(this);
slouken@354
   407
		return(-1);
slouken@354
   408
	}
slouken@354
   409
	spec->freq = status;
slouken@0
   410
slouken@354
   411
	/* Set the buffer size, in samples */
slouken@354
   412
	frames = spec->samples;
slouken@865
   413
	frames = SDL_NAME(snd_pcm_hw_params_set_period_size_near)(pcm_handle, params, frames, NULL);
slouken@354
   414
	spec->samples = frames;
slouken@865
   415
	SDL_NAME(snd_pcm_hw_params_set_periods_near)(pcm_handle, params, 2, NULL);
slouken@354
   416
slouken@354
   417
	/* "set" the hardware with the desired parameters */
slouken@865
   418
	status = SDL_NAME(snd_pcm_hw_params)(pcm_handle, params);
slouken@354
   419
	if ( status < 0 ) {
slouken@865
   420
		SDL_SetError("Couldn't set audio parameters: %s", SDL_NAME(snd_strerror)(status));
slouken@354
   421
		ALSA_CloseAudio(this);
slouken@0
   422
		return(-1);
slouken@0
   423
	}
slouken@0
   424
slouken@354
   425
	/* Calculate the final parameters for this audio specification */
slouken@354
   426
	SDL_CalculateAudioSpec(spec);
slouken@0
   427
slouken@354
   428
	/* Allocate mixing buffer */
slouken@354
   429
	mixlen = spec->size;
slouken@354
   430
	mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen);
slouken@354
   431
	if ( mixbuf == NULL ) {
slouken@354
   432
		ALSA_CloseAudio(this);
slouken@354
   433
		return(-1);
slouken@0
   434
	}
slouken@354
   435
	memset(mixbuf, spec->silence, spec->size);
slouken@0
   436
slouken@0
   437
	/* Get the parent process id (we're the parent of the audio thread) */
slouken@0
   438
	parent = getpid();
slouken@0
   439
slouken@765
   440
	/* Switch to blocking mode for playback */
slouken@865
   441
	SDL_NAME(snd_pcm_nonblock)(pcm_handle, 0);
slouken@765
   442
slouken@0
   443
	/* We're ready to rock and roll. :-) */
slouken@0
   444
	return(0);
slouken@0
   445
}