Updated ALSA audio support for ALSA 0.9
authorSam Lantinga <slouken@libsdl.org>
Mon, 15 Apr 2002 07:38:54 +0000
changeset 35430935e76acb5
parent 353 d47637068e52
child 355 b68a34dee49d
Updated ALSA audio support for ALSA 0.9
configure.in
src/audio/alsa/SDL_alsa_audio.c
src/audio/alsa/SDL_alsa_audio.h
src/audio/dma/SDL_dmaaudio.c
src/audio/dsp/SDL_dspaudio.c
     1.1 --- a/configure.in	Mon Apr 15 04:53:41 2002 +0000
     1.2 +++ b/configure.in	Mon Apr 15 07:38:54 2002 +0000
     1.3 @@ -269,14 +269,25 @@
     1.4  [  --enable-alsa           support the ALSA audio API [default=yes]],
     1.5                    , enable_alsa=yes)
     1.6      if test x$enable_audio = xyes -a x$enable_alsa = xyes; then
     1.7 +        AC_MSG_CHECKING(for ALSA audio support)
     1.8          have_alsa=no
     1.9 -        AC_CHECK_HEADER(sys/asoundlib.h, have_alsa_hdr=yes)
    1.10 -        AC_CHECK_LIB(asound, snd_pcm_open, have_alsa_lib=yes)
    1.11 -        if test x$have_alsa_hdr = xyes -a x$have_alsa_lib = xyes; then
    1.12 +        AC_TRY_COMPILE([
    1.13 +          #include <alsa/asoundlib.h>
    1.14 +        ],[
    1.15 +          #if SND_LIB_VERSION < ((0<<16)|(9<<8)|0)
    1.16 +          #error Your version of ALSA is too old
    1.17 +          #endif
    1.18 +          snd_pcm_t *pcm_handle;
    1.19 +        ],[
    1.20 +        AC_CHECK_LIB(asound, snd_pcm_open, have_alsa=yes)
    1.21 +        ])
    1.22 +        if test x$have_alsa = xyes; then
    1.23              CFLAGS="$CFLAGS -DALSA_SUPPORT"
    1.24              SYSTEM_LIBS="$SYSTEM_LIBS -lasound"
    1.25              AUDIO_SUBDIRS="$AUDIO_SUBDIRS alsa"
    1.26              AUDIO_DRIVERS="$AUDIO_DRIVERS alsa/libaudio_alsa.la"
    1.27 +        else
    1.28 +            AC_MSG_RESULT($have_alsa)
    1.29          fi
    1.30      fi
    1.31  }
     2.1 --- a/src/audio/alsa/SDL_alsa_audio.c	Mon Apr 15 04:53:41 2002 +0000
     2.2 +++ b/src/audio/alsa/SDL_alsa_audio.c	Mon Apr 15 07:38:54 2002 +0000
     2.3 @@ -44,96 +44,41 @@
     2.4  /* The tag name used by ALSA audio */
     2.5  #define DRIVER_NAME         "alsa"
     2.6  
     2.7 -/* default card and device numbers as listed in dev/snd */
     2.8 -static int card_no = 0;
     2.9 -static int device_no = 0;
    2.10 -
    2.11 -/* default channel communication parameters */
    2.12 -#define DEFAULT_CPARAMS_RATE 22050
    2.13 -#define DEFAULT_CPARAMS_VOICES 1
    2.14 -#define DEFAULT_CPARAMS_FRAG_SIZE 512
    2.15 -#define DEFAULT_CPARAMS_FRAGS_MIN 1
    2.16 -#define DEFAULT_CPARAMS_FRAGS_MAX -1
    2.17 -
    2.18 -/* Open the audio device for playback, and don't block if busy */
    2.19 -#define OPEN_FLAGS	(SND_PCM_OPEN_PLAYBACK|SND_PCM_OPEN_NONBLOCK)
    2.20 +/* The default ALSA audio driver */
    2.21 +#define DEFAULT_DEVICE	"plughw:0,0"
    2.22  
    2.23  /* Audio driver functions */
    2.24 -static int PCM_OpenAudio(_THIS, SDL_AudioSpec *spec);
    2.25 -static void PCM_WaitAudio(_THIS);
    2.26 -static void PCM_PlayAudio(_THIS);
    2.27 -static Uint8 *PCM_GetAudioBuf(_THIS);
    2.28 -static void PCM_CloseAudio(_THIS);
    2.29 +static int ALSA_OpenAudio(_THIS, SDL_AudioSpec *spec);
    2.30 +static void ALSA_WaitAudio(_THIS);
    2.31 +static void ALSA_PlayAudio(_THIS);
    2.32 +static Uint8 *ALSA_GetAudioBuf(_THIS);
    2.33 +static void ALSA_CloseAudio(_THIS);
    2.34  
    2.35 -/* PCM transfer channel parameters initialize function */
    2.36 -static void init_pcm_cparams(snd_pcm_channel_params_t* cparams)
    2.37 +static const char *get_audio_device()
    2.38  {
    2.39 -	memset(cparams,0,sizeof(snd_pcm_channel_params_t));
    2.40 -
    2.41 -	cparams->channel = SND_PCM_CHANNEL_PLAYBACK;
    2.42 -	cparams->mode = SND_PCM_MODE_BLOCK;
    2.43 -	cparams->start_mode = SND_PCM_START_DATA; //_FULL
    2.44 -	cparams->stop_mode  = SND_PCM_STOP_STOP;
    2.45 -	cparams->format.format = SND_PCM_SFMT_S16_LE;
    2.46 -	cparams->format.interleave = 1;
    2.47 -	cparams->format.rate = DEFAULT_CPARAMS_RATE;
    2.48 -	cparams->format.voices = DEFAULT_CPARAMS_VOICES;
    2.49 -	cparams->buf.block.frag_size = DEFAULT_CPARAMS_FRAG_SIZE;
    2.50 -	cparams->buf.block.frags_min = DEFAULT_CPARAMS_FRAGS_MIN;
    2.51 -	cparams->buf.block.frags_max = DEFAULT_CPARAMS_FRAGS_MAX;
    2.52 +	const char *device;
    2.53 +	
    2.54 +	device = getenv("AUDIODEV");	/* Is there a standard variable name? */
    2.55 +	if ( device == NULL ) {
    2.56 +		device = DEFAULT_DEVICE;
    2.57 +	}
    2.58 +	return device;
    2.59  }
    2.60  
    2.61  /* Audio driver bootstrap functions */
    2.62  
    2.63  static int Audio_Available(void)
    2.64 -/*
    2.65 -	See if we can open a nonblocking channel.
    2.66 -	Return value '1' means we can.
    2.67 -	Return value '0' means we cannot.
    2.68 -*/
    2.69  {
    2.70  	int available;
    2.71 -	int rval;
    2.72 +	int status;
    2.73  	snd_pcm_t *handle;
    2.74 -	snd_pcm_channel_params_t cparams;
    2.75 -#ifdef DEBUG_AUDIO
    2.76 -	snd_pcm_channel_status_t cstatus;
    2.77 -#endif
    2.78  
    2.79  	available = 0;
    2.80 -	handle = NULL;
    2.81 -
    2.82 -	init_pcm_cparams(&cparams);
    2.83 -	
    2.84 -	rval = snd_pcm_open(&handle, card_no, device_no, OPEN_FLAGS);
    2.85 -	if (rval >= 0)
    2.86 -	{
    2.87 -		rval = snd_pcm_plugin_params(handle, &cparams);
    2.88 -
    2.89 -#ifdef DEBUG_AUDIO
    2.90 -		snd_pcm_plugin_status(handle, &cstatus);
    2.91 -		printf("status after snd_pcm_plugin_params call = %d\n",cstatus.status);
    2.92 -#endif
    2.93 -		if (rval >= 0)
    2.94 -		{
    2.95 -			available = 1;
    2.96 -		}
    2.97 -		else
    2.98 -		{
    2.99 -	        	SDL_SetError("snd_pcm_channel_params failed: %s\n", snd_strerror (rval));
   2.100 -		}
   2.101 -
   2.102 -        if ((rval = snd_pcm_close(handle)) < 0)
   2.103 -        {
   2.104 -            SDL_SetError("snd_pcm_close failed: %s\n",snd_strerror(rval));
   2.105 -			available = 0;
   2.106 -        }
   2.107 +	status = snd_pcm_open(&handle, get_audio_device(), SND_PCM_STREAM_PLAYBACK, 0);
   2.108 +	if ( status >= 0 ) {
   2.109 +		available = 1;
   2.110 +        	snd_pcm_close(handle);
   2.111  	}
   2.112 -	else
   2.113 -	{
   2.114 -       SDL_SetError("snd_pcm_open failed: %s\n", snd_strerror(rval));
   2.115 -	}
   2.116 -
   2.117  	return(available);
   2.118  }
   2.119  
   2.120 @@ -162,14 +107,13 @@
   2.121  		return(0);
   2.122  	}
   2.123  	memset(this->hidden, 0, (sizeof *this->hidden));
   2.124 -	audio_handle = NULL;
   2.125  
   2.126  	/* Set the function pointers */
   2.127 -	this->OpenAudio = PCM_OpenAudio;
   2.128 -	this->WaitAudio = PCM_WaitAudio;
   2.129 -	this->PlayAudio = PCM_PlayAudio;
   2.130 -	this->GetAudioBuf = PCM_GetAudioBuf;
   2.131 -	this->CloseAudio = PCM_CloseAudio;
   2.132 +	this->OpenAudio = ALSA_OpenAudio;
   2.133 +	this->WaitAudio = ALSA_WaitAudio;
   2.134 +	this->PlayAudio = ALSA_PlayAudio;
   2.135 +	this->GetAudioBuf = ALSA_GetAudioBuf;
   2.136 +	this->CloseAudio = ALSA_CloseAudio;
   2.137  
   2.138  	this->free = Audio_DeleteDevice;
   2.139  
   2.140 @@ -177,14 +121,13 @@
   2.141  }
   2.142  
   2.143  AudioBootStrap ALSA_bootstrap = {
   2.144 -	DRIVER_NAME, "ALSA PCM audio",
   2.145 +	DRIVER_NAME, "ALSA 0.9 PCM audio",
   2.146  	Audio_Available, Audio_CreateDevice
   2.147  };
   2.148  
   2.149  /* This function waits until it is possible to write a full sound buffer */
   2.150 -static void PCM_WaitAudio(_THIS)
   2.151 +static void ALSA_WaitAudio(_THIS)
   2.152  {
   2.153 -
   2.154  	/* Check to see if the thread-parent process is still alive */
   2.155  	{ static int cnt = 0;
   2.156  		/* Note that this only works with thread implementations 
   2.157 @@ -196,322 +139,177 @@
   2.158  			}
   2.159  		}
   2.160  	}
   2.161 -
   2.162 -	/* See if we need to use timed audio synchronization */
   2.163 -	if ( frame_ticks ) 
   2.164 -	{
   2.165 -		/* Use timer for general audio synchronization */
   2.166 -		Sint32 ticks;
   2.167 -
   2.168 -		ticks = ((Sint32)(next_frame - SDL_GetTicks()))-FUDGE_TICKS;
   2.169 -		if ( ticks > 0 ) 
   2.170 -		{
   2.171 -			SDL_Delay(ticks);
   2.172 -		}
   2.173 -	}
   2.174 -    else 
   2.175 -	{
   2.176 -    	/* Use select() for audio synchronization */
   2.177 -		fd_set fdset;
   2.178 -	    struct timeval timeout;
   2.179 -    	FD_ZERO(&fdset);
   2.180 -	    FD_SET(audio_fd, &fdset);
   2.181 -    	timeout.tv_sec = 10;
   2.182 -	    timeout.tv_usec = 0;
   2.183 -#ifdef DEBUG_AUDIO
   2.184 -    	fprintf(stderr, "Waiting for audio to get ready\n");
   2.185 -#endif
   2.186 -	    if ( select(audio_fd+1, NULL, &fdset, NULL, &timeout) <= 0 ) 
   2.187 -		{
   2.188 -            const char *message =
   2.189 -            "Audio timeout - buggy audio driver? (disabled)";
   2.190 -	        /* In general we should never print to the screen,
   2.191 -       	       but in this case we have no other way of letting
   2.192 -               the user know what happened.
   2.193 -            */
   2.194 -       	    fprintf(stderr, "SDL: %s\n", message);
   2.195 -   	        this->enabled = 0;
   2.196 -            /* Don't try to close - may hang */
   2.197 -            audio_fd = -1;
   2.198 -#ifdef DEBUG_AUDIO
   2.199 -       	    fprintf(stderr, "Done disabling audio\n");
   2.200 -#endif
   2.201 -    	    }
   2.202 -#ifdef DEBUG_AUDIO
   2.203 -        fprintf(stderr, "Ready!\n");
   2.204 -#endif
   2.205 -    }
   2.206  }
   2.207  
   2.208 -static snd_pcm_channel_status_t cstatus;
   2.209 -
   2.210 -static void PCM_PlayAudio(_THIS)
   2.211 +static void ALSA_PlayAudio(_THIS)
   2.212  {
   2.213 -    int written, rval;
   2.214 -
   2.215 -    /* Write the audio data, checking for EAGAIN (buffer full) and underrun */
   2.216 -    do {
   2.217 -		written = snd_pcm_plugin_write(audio_handle, pcm_buf, pcm_len);
   2.218 -#ifdef DEBUG_AUDIO
   2.219 -		fprintf(stderr, "written = %d pcm_len = %d\n",written,pcm_len);
   2.220 -#endif
   2.221 -		if (written != pcm_len)
   2.222 -		{
   2.223 -	        if (errno == EAGAIN) 
   2.224 -			{
   2.225 -            	SDL_Delay(1);   /* Let a little CPU time go by and try to write again */
   2.226 -#ifdef DEBUG_AUDIO
   2.227 -				fprintf(stderr, "errno == EAGAIN\n");
   2.228 -#endif
   2.229 -        	}
   2.230 -			else
   2.231 -			{
   2.232 -		        if( (rval = snd_pcm_plugin_status(audio_handle, &cstatus)) < 0 )
   2.233 -        		{
   2.234 -		            SDL_SetError("snd_pcm_plugin_status failed: %s\n", snd_strerror(rval));
   2.235 -        		    return;
   2.236 -		        }
   2.237 -				if ( (cstatus.status == SND_PCM_STATUS_UNDERRUN)
   2.238 -					||(cstatus.status == SND_PCM_STATUS_READY) )
   2.239 -				{
   2.240 -#ifdef DEBUG_AUDIO
   2.241 -					fprintf(stderr, "buffer underrun\n");
   2.242 -#endif
   2.243 -					if ( (rval = snd_pcm_plugin_prepare (audio_handle,SND_PCM_CHANNEL_PLAYBACK)) < 0 )
   2.244 -					{
   2.245 -						SDL_SetError("snd_pcm_plugin_prepare failed: %s\n",snd_strerror(rval) );
   2.246 -						return;
   2.247 -					}
   2.248 -					/* if we reach here, try to write again */
   2.249 -				}
   2.250 +	int           status;
   2.251 +	int           sample_len;
   2.252 +	signed short *sample_buf;
   2.253 +       
   2.254 +	sample_len = this->spec.samples;
   2.255 +	sample_buf = (signed short *)mixbuf;
   2.256 +	while ( sample_len > 0 ) {
   2.257 +		status = snd_pcm_writei(pcm_handle, sample_buf, sample_len);
   2.258 +		if ( status < 0 ) {
   2.259 +			if ( status == -EAGAIN ) {
   2.260 +				continue;
   2.261 +			}
   2.262 +			if ( status == -ESTRPIPE ) {
   2.263 +				do {
   2.264 +					status = snd_pcm_resume(pcm_handle);
   2.265 +				} while ( status == -EAGAIN );
   2.266 +			}
   2.267 +			if ( status < 0 ) {
   2.268 +				status = snd_pcm_prepare(pcm_handle);
   2.269 +			}
   2.270 +			if ( status < 0 ) {
   2.271 +				/* Hmm, not much we can do - abort */
   2.272 +				this->enabled = 0;
   2.273 +				return;
   2.274  			}
   2.275  		}
   2.276 -    } while ( (written < 0) && ((errno == 0) || (errno == EAGAIN)) );
   2.277 -
   2.278 -    /* Set the next write frame */
   2.279 -   if ( frame_ticks ) {
   2.280 -	    next_frame += frame_ticks;
   2.281 -	}
   2.282 -
   2.283 -    /* If we couldn't write, assume fatal error for now */
   2.284 -    if ( written < 0 ) {
   2.285 -        this->enabled = 0;
   2.286 -    }
   2.287 -	return;
   2.288 -}
   2.289 -
   2.290 -static Uint8 *PCM_GetAudioBuf(_THIS)
   2.291 -{
   2.292 -	return(pcm_buf);
   2.293 -}
   2.294 -
   2.295 -static void PCM_CloseAudio(_THIS)
   2.296 -{
   2.297 -	int rval;
   2.298 -
   2.299 -	if ( pcm_buf != NULL ) {
   2.300 -		free(pcm_buf);
   2.301 -		pcm_buf = NULL;
   2.302 -	}
   2.303 -	if ( audio_handle != NULL ) {
   2.304 -		if ((rval = snd_pcm_plugin_flush(audio_handle,SND_PCM_CHANNEL_PLAYBACK)) < 0)
   2.305 -		{
   2.306 -        	SDL_SetError("snd_pcm_plugin_flush failed: %s\n",snd_strerror(rval));
   2.307 -			return;
   2.308 -		}
   2.309 -		if ((rval = snd_pcm_close(audio_handle)) < 0)
   2.310 -		{
   2.311 -			SDL_SetError("snd_pcm_close failed: %s\n",snd_strerror(rval));
   2.312 -			return;
   2.313 -		}
   2.314 -		audio_handle = NULL;
   2.315 +		sample_buf += status * this->spec.channels;
   2.316 +		sample_len -= status;
   2.317  	}
   2.318  }
   2.319  
   2.320 -static int PCM_OpenAudio(_THIS, SDL_AudioSpec *spec)
   2.321 +static Uint8 *ALSA_GetAudioBuf(_THIS)
   2.322  {
   2.323 -	int rval;
   2.324 -	snd_pcm_channel_params_t cparams;
   2.325 -	snd_pcm_channel_setup_t  csetup;
   2.326 -	int format;
   2.327 -	Uint16 test_format;
   2.328 -	int twidth;
   2.329 +	return(mixbuf);
   2.330 +}
   2.331  
   2.332 -	/* initialize channel transfer parameters to default */
   2.333 -	init_pcm_cparams(&cparams);
   2.334 +static void ALSA_CloseAudio(_THIS)
   2.335 +{
   2.336 +	if ( mixbuf != NULL ) {
   2.337 +		SDL_FreeAudioMem(mixbuf);
   2.338 +		mixbuf = NULL;
   2.339 +	}
   2.340 +	if ( pcm_handle ) {
   2.341 +		snd_pcm_close(pcm_handle);
   2.342 +		pcm_handle = NULL;
   2.343 +	}
   2.344 +}
   2.345  
   2.346 -	/* Reset the timer synchronization flag */
   2.347 -	frame_ticks = 0.0;
   2.348 +static int ALSA_OpenAudio(_THIS, SDL_AudioSpec *spec)
   2.349 +{
   2.350 +	int                  status;
   2.351 +	snd_pcm_hw_params_t *params;
   2.352 +	snd_pcm_format_t     format;
   2.353 +	snd_pcm_uframes_t    frames;
   2.354 +	Uint16               test_format;
   2.355  
   2.356  	/* Open the audio device */
   2.357 -	
   2.358 -	rval = snd_pcm_open(&audio_handle, card_no, device_no, OPEN_FLAGS);
   2.359 -	if ( rval < 0 ) {
   2.360 -		SDL_SetError("snd_pcm_open failed: %s\n", snd_strerror(rval));
   2.361 +	status = snd_pcm_open(&pcm_handle, get_audio_device(), SND_PCM_STREAM_PLAYBACK, 0);
   2.362 +	if ( status < 0 ) {
   2.363 +		SDL_SetError("Couldn't open audio device: %s", snd_strerror(status));
   2.364  		return(-1);
   2.365  	}
   2.366  
   2.367 -#ifdef PLUGIN_DISABLE_MMAP /* This is gone in newer versions of ALSA? */
   2.368 -    /* disable count status parameter */
   2.369 -    if ((rval = snd_plugin_set_disable(audio_handle, PLUGIN_DISABLE_MMAP))<0)
   2.370 -    {
   2.371 -        SDL_SetError("snd_plugin_set_disable failed: %s\n", snd_strerror(rval));
   2.372 -        return(-1);
   2.373 -    }
   2.374 -#endif
   2.375 +	/* Figure out what the hardware is capable of */
   2.376 +	snd_pcm_hw_params_alloca(&params);
   2.377 +	status = snd_pcm_hw_params_any(pcm_handle, params);
   2.378 +	if ( status < 0 ) {
   2.379 +		SDL_SetError("Couldn't get hardware config: %s", snd_strerror(status));
   2.380 +		ALSA_CloseAudio(this);
   2.381 +		return(-1);
   2.382 +	}
   2.383  
   2.384 -	pcm_buf = NULL;
   2.385 +	/* SDL only uses interleaved sample output */
   2.386 +	status = snd_pcm_hw_params_set_access(pcm_handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
   2.387 +	if ( status < 0 ) {
   2.388 +		SDL_SetError("Couldn't set interleaved access: %s", snd_strerror(status));
   2.389 +		ALSA_CloseAudio(this);
   2.390 +		return(-1);
   2.391 +	}
   2.392  
   2.393  	/* Try for a closest match on audio format */
   2.394 -	format = 0;
   2.395 +	status = -1;
   2.396  	for ( test_format = SDL_FirstAudioFormat(spec->format);
   2.397 -						! format && test_format; ) 
   2.398 -	{
   2.399 -#ifdef DEBUG_AUDIO
   2.400 -		fprintf(stderr, "Trying format 0x%4.4x spec->samples %d\n", test_format,spec->samples);
   2.401 -#endif
   2.402 -			/* if match found set format to equivalent ALSA format */
   2.403 -        switch ( test_format ) {
   2.404 +	      test_format && (status < 0); ) {
   2.405 +		switch ( test_format ) {
   2.406  			case AUDIO_U8:
   2.407 -				format = SND_PCM_SFMT_U8;
   2.408 -				cparams.buf.block.frag_size = spec->samples * spec->channels;
   2.409 +				format = SND_PCM_FORMAT_U8;
   2.410  				break;
   2.411  			case AUDIO_S8:
   2.412 -				format = SND_PCM_SFMT_S8;
   2.413 -				cparams.buf.block.frag_size = spec->samples * spec->channels;
   2.414 +				format = SND_PCM_FORMAT_S8;
   2.415  				break;
   2.416  			case AUDIO_S16LSB:
   2.417 -				format = SND_PCM_SFMT_S16_LE;
   2.418 -				cparams.buf.block.frag_size = spec->samples*2 * spec->channels;
   2.419 +				format = SND_PCM_FORMAT_S16_LE;
   2.420  				break;
   2.421  			case AUDIO_S16MSB:
   2.422 -				format = SND_PCM_SFMT_S16_BE;
   2.423 -				cparams.buf.block.frag_size = spec->samples*2 * spec->channels;
   2.424 +				format = SND_PCM_FORMAT_S16_BE;
   2.425  				break;
   2.426  			case AUDIO_U16LSB:
   2.427 -				format = SND_PCM_SFMT_U16_LE;
   2.428 -				cparams.buf.block.frag_size = spec->samples*2 * spec->channels;
   2.429 +				format = SND_PCM_FORMAT_U16_LE;
   2.430  				break;
   2.431  			case AUDIO_U16MSB:
   2.432 -				format = SND_PCM_SFMT_U16_BE;
   2.433 -				cparams.buf.block.frag_size = spec->samples*2 * spec->channels;
   2.434 +				format = SND_PCM_FORMAT_U16_BE;
   2.435  				break;
   2.436  			default:
   2.437 +				format = 0;
   2.438  				break;
   2.439  		}
   2.440 -		if ( ! format ) {
   2.441 +		if ( format != 0 ) {
   2.442 +			status = snd_pcm_hw_params_set_format(pcm_handle, params, format);
   2.443 +		}
   2.444 +		if ( status < 0 ) {
   2.445  			test_format = SDL_NextAudioFormat();
   2.446  		}
   2.447  	}
   2.448 -	if ( format == 0 ) {
   2.449 +	if ( status < 0 ) {
   2.450  		SDL_SetError("Couldn't find any hardware audio formats");
   2.451 +		ALSA_CloseAudio(this);
   2.452  		return(-1);
   2.453  	}
   2.454  	spec->format = test_format;
   2.455  
   2.456 -	/* Set the audio format */
   2.457 -	cparams.format.format = format;
   2.458 +	/* Set the number of channels */
   2.459 +	status = snd_pcm_hw_params_set_channels(pcm_handle, params, spec->channels);
   2.460 +	if ( status < 0 ) {
   2.461 +		status = snd_pcm_hw_params_get_channels(params);
   2.462 +		if ( (status <= 0) || (status > 2) ) {
   2.463 +			SDL_SetError("Couldn't set audio channels");
   2.464 +			ALSA_CloseAudio(this);
   2.465 +			return(-1);
   2.466 +		}
   2.467 +		spec->channels = status;
   2.468 +	}
   2.469  
   2.470 -	/* Set mono or stereo audio (currently only two channels supported) */
   2.471 -	cparams.format.voices = spec->channels;
   2.472 -	
   2.473 -	#ifdef DEBUG_AUDIO
   2.474 -	printf("intializing channels %d\n", cparams.format.voices);
   2.475 -	#endif
   2.476 -	
   2.477 -	/* Set rate */
   2.478 -	cparams.format.rate = spec->freq ;
   2.479 +	/* Set the audio rate */
   2.480 +	status = snd_pcm_hw_params_set_rate_near(pcm_handle, params, spec->freq, NULL);
   2.481 +	if ( status < 0 ) {
   2.482 +		SDL_SetError("Couldn't set audio frequency: %s", snd_strerror(status));
   2.483 +		ALSA_CloseAudio(this);
   2.484 +		return(-1);
   2.485 +	}
   2.486 +	spec->freq = status;
   2.487  
   2.488 -	/* Setup the transfer parameters according to cparams */
   2.489 -	rval = snd_pcm_plugin_params(audio_handle, &cparams);
   2.490 -	if (rval < 0) {
   2.491 -		SDL_SetError("snd_pcm_channel_params failed: %s\n", snd_strerror (rval));
   2.492 +	/* Set the buffer size, in samples */
   2.493 +	frames = spec->samples;
   2.494 +	frames = snd_pcm_hw_params_set_period_size_near(pcm_handle, params, frames, NULL);
   2.495 +	spec->samples = frames;
   2.496 +	snd_pcm_hw_params_set_periods_near(pcm_handle, params, 2, NULL);
   2.497 +
   2.498 +	/* "set" the hardware with the desired parameters */
   2.499 +	status = snd_pcm_hw_params(pcm_handle, params);
   2.500 +	if ( status < 0 ) {
   2.501 +		SDL_SetError("Couldn't set audio parameters: %s", snd_strerror(status));
   2.502 +		ALSA_CloseAudio(this);
   2.503  		return(-1);
   2.504  	}
   2.505  
   2.506 -    /*  Make sure channel is setup right one last time */
   2.507 -    memset( &csetup, 0, sizeof( csetup ) );
   2.508 -    csetup.channel = SND_PCM_CHANNEL_PLAYBACK;
   2.509 -    if ( snd_pcm_plugin_setup( audio_handle, &csetup ) < 0 )
   2.510 -    {
   2.511 -        SDL_SetError("Unable to setup playback channel\n" );
   2.512 -        return(-1);
   2.513 -    }
   2.514 +	/* Calculate the final parameters for this audio specification */
   2.515 +	SDL_CalculateAudioSpec(spec);
   2.516  
   2.517 -#ifdef DEBUG_AUDIO
   2.518 -    else
   2.519 -    {
   2.520 -        fprintf(stderr,"requested format: %d\n",cparams.format.format);
   2.521 -        fprintf(stderr,"requested frag size: %d\n",cparams.buf.block.frag_size);
   2.522 -        fprintf(stderr,"requested max frags: %d\n\n",cparams.buf.block.frags_max);
   2.523 -
   2.524 -        fprintf(stderr,"real format: %d\n", csetup.format.format );
   2.525 -        fprintf(stderr,"real frag size : %d\n", csetup.buf.block.frag_size );
   2.526 -		fprintf(stderr,"real max frags : %d\n", csetup.buf.block.frags_max );
   2.527 -    }
   2.528 -#endif // DEBUG_AUDIO
   2.529 -
   2.530 -    /*  Allocate memory to the audio buffer and initialize with silence
   2.531 -        (Note that buffer size must be a multiple of fragment size, so find closest multiple)
   2.532 -    */
   2.533 -    
   2.534 -    twidth = snd_pcm_format_width(format);
   2.535 -    if (twidth < 0) {
   2.536 -        printf("snd_pcm_format_width failed\n");
   2.537 -        twidth = 0;
   2.538 -    }
   2.539 -#ifdef DEBUG_AUDIO
   2.540 -    printf("format is %d bits wide\n",twidth);
   2.541 -#endif      
   2.542 -    
   2.543 -    pcm_len = csetup.buf.block.frag_size * (twidth/8) * csetup.format.voices ;
   2.544 -    
   2.545 -#ifdef DEBUG_AUDIO    
   2.546 -    printf("pcm_len set to %d\n", pcm_len);
   2.547 -#endif
   2.548 -    
   2.549 -    if (pcm_len == 0)
   2.550 -    {
   2.551 -        pcm_len = csetup.buf.block.frag_size;
   2.552 -    }
   2.553 -    
   2.554 -    pcm_buf = (Uint8*)malloc(pcm_len);
   2.555 -    if (pcm_buf == NULL) {
   2.556 -        SDL_SetError("pcm_buf malloc failed\n");
   2.557 -        return(-1);
   2.558 -    }
   2.559 -    memset(pcm_buf,spec->silence,pcm_len);
   2.560 -
   2.561 -#ifdef DEBUG_AUDIO
   2.562 -	fprintf(stderr,"pcm_buf malloced and silenced.\n");
   2.563 -#endif
   2.564 -
   2.565 -    /* get the file descriptor */
   2.566 -    if( (audio_fd = snd_pcm_file_descriptor(audio_handle, device_no)) < 0)
   2.567 -    {
   2.568 -       fprintf(stderr, "snd_pcm_file_descriptor failed with error code: %d\n", audio_fd);
   2.569 -    }
   2.570 -
   2.571 -	/* Trigger audio playback */
   2.572 -	rval = snd_pcm_plugin_prepare( audio_handle, SND_PCM_CHANNEL_PLAYBACK);
   2.573 -	if (rval < 0) {
   2.574 -       SDL_SetError("snd_pcm_plugin_prepare failed: %s\n", snd_strerror (rval));
   2.575 -       return(-1);
   2.576 +	/* Allocate mixing buffer */
   2.577 +	mixlen = spec->size;
   2.578 +	mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen);
   2.579 +	if ( mixbuf == NULL ) {
   2.580 +		ALSA_CloseAudio(this);
   2.581 +		return(-1);
   2.582  	}
   2.583 -	rval =  snd_pcm_playback_go(audio_handle);
   2.584 -    if (rval < 0) {
   2.585 -       SDL_SetError("snd_pcm_playback_go failed: %s\n", snd_strerror (rval));
   2.586 -       return(-1);
   2.587 -    }
   2.588 -
   2.589 -    /* Check to see if we need to use select() workaround */
   2.590 -    { char *workaround;
   2.591 -        workaround = getenv("SDL_DSP_NOSELECT");
   2.592 -        if ( workaround ) {
   2.593 -            frame_ticks = (float)(spec->samples*1000)/spec->freq;
   2.594 -            next_frame = SDL_GetTicks()+frame_ticks;
   2.595 -        }
   2.596 -    }
   2.597 +	memset(mixbuf, spec->silence, spec->size);
   2.598  
   2.599  	/* Get the parent process id (we're the parent of the audio thread) */
   2.600  	parent = getpid();
     3.1 --- a/src/audio/alsa/SDL_alsa_audio.h	Mon Apr 15 04:53:41 2002 +0000
     3.2 +++ b/src/audio/alsa/SDL_alsa_audio.h	Mon Apr 15 07:38:54 2002 +0000
     3.3 @@ -24,38 +24,27 @@
     3.4  #define _ALSA_PCM_audio_h
     3.5  
     3.6  #include "SDL_sysaudio.h"
     3.7 -#include <sys/asoundlib.h>
     3.8 +#include <alsa/asoundlib.h>
     3.9  
    3.10  /* Hidden "this" pointer for the video functions */
    3.11  #define _THIS	SDL_AudioDevice *this
    3.12  
    3.13  struct SDL_PrivateAudioData {
    3.14  	/* The audio device handle */
    3.15 -	 snd_pcm_t *audio_handle;
    3.16 -
    3.17 -	/* The audio file descriptor */
    3.18 -	int audio_fd;
    3.19 +	snd_pcm_t *pcm_handle;
    3.20  
    3.21  	/* The parent process id, to detect when application quits */
    3.22  	pid_t parent;
    3.23  
    3.24  	/* Raw mixing buffer */
    3.25 -	Uint8 *pcm_buf;
    3.26 -	int    pcm_len;
    3.27 -
    3.28 -	/* Support for audio timing using a timer, in addition to select() */
    3.29 -	float frame_ticks;
    3.30 -	float next_frame;
    3.31 +	Uint8 *mixbuf;
    3.32 +	int    mixlen;
    3.33  };
    3.34 -#define FUDGE_TICKS	10	/* The scheduler overhead ticks per frame */
    3.35  
    3.36  /* Old variable names */
    3.37 -#define audio_handle	(this->hidden->audio_handle)
    3.38 -#define audio_fd		(this->hidden->audio_fd)
    3.39 +#define pcm_handle		(this->hidden->pcm_handle)
    3.40  #define parent			(this->hidden->parent)
    3.41 -#define pcm_buf			(this->hidden->pcm_buf)
    3.42 -#define pcm_len			(this->hidden->pcm_len)
    3.43 -#define frame_ticks		(this->hidden->frame_ticks)
    3.44 -#define next_frame		(this->hidden->next_frame)
    3.45 +#define mixbuf			(this->hidden->mixbuf)
    3.46 +#define mixlen			(this->hidden->mixlen)
    3.47  
    3.48  #endif /* _ALSA_PCM_audio_h */
     4.1 --- a/src/audio/dma/SDL_dmaaudio.c	Mon Apr 15 04:53:41 2002 +0000
     4.2 +++ b/src/audio/dma/SDL_dmaaudio.c	Mon Apr 15 07:38:54 2002 +0000
     4.3 @@ -377,6 +377,7 @@
     4.4  				}
     4.5  				break;
     4.6  			default:
     4.7 +				format = 0;
     4.8  				break;
     4.9  		}
    4.10  		if ( ! format ) {
     5.1 --- a/src/audio/dsp/SDL_dspaudio.c	Mon Apr 15 04:53:41 2002 +0000
     5.2 +++ b/src/audio/dsp/SDL_dspaudio.c	Mon Apr 15 07:38:54 2002 +0000
     5.3 @@ -394,6 +394,7 @@
     5.4  				}
     5.5  				break;
     5.6  			default:
     5.7 +				format = 0;
     5.8  				break;
     5.9  		}
    5.10  		if ( ! format ) {