Updated the QNX audio code for QNX 6.2 (thanks Travis!)
authorSam Lantinga <slouken@libsdl.org>
Sun, 16 Jun 2002 04:17:57 +0000
changeset 418337f3ec4c385
parent 417 04ec6995f75d
child 419 3d8ad40ef561
Updated the QNX audio code for QNX 6.2 (thanks Travis!)
src/audio/nto/SDL_nto_audio.c
     1.1 --- a/src/audio/nto/SDL_nto_audio.c	Thu Jun 13 23:59:20 2002 +0000
     1.2 +++ b/src/audio/nto/SDL_nto_audio.c	Sun Jun 16 04:17:57 2002 +0000
     1.3 @@ -1,3 +1,8 @@
     1.4 +Date: Sun, 16 Jun 2002 00:59:13 -0300
     1.5 +From: Travis <smallfri@bigfoot.com>
     1.6 +To: slouken@libsdl.org
     1.7 +Subject: NTO Audio Fixes
     1.8 +
     1.9  /*
    1.10      SDL - Simple DirectMedia Layer
    1.11      Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  Sam Lantinga
    1.12 @@ -20,10 +25,6 @@
    1.13      slouken@libsdl.org
    1.14  */
    1.15  
    1.16 -
    1.17 -
    1.18 -/* Allow access to a raw mixing buffer */
    1.19 -
    1.20  #include <stdlib.h>
    1.21  #include <stdio.h>
    1.22  #include <string.h>
    1.23 @@ -35,6 +36,7 @@
    1.24  #include <sys/time.h>
    1.25  #include <sched.h>
    1.26  #include <sys/asoundlib.h>
    1.27 +#include <sys/select.h>
    1.28  
    1.29  #include "SDL_audio.h"
    1.30  #include "SDL_error.h"
    1.31 @@ -46,19 +48,15 @@
    1.32  /* The tag name used by NTO audio */
    1.33  #define DRIVER_NAME         "nto"
    1.34  
    1.35 -/* default card and device numbers as listed in dev/snd */
    1.36 -static int card_no = 0;
    1.37 -static int device_no = 0;
    1.38 -
    1.39  /* default channel communication parameters */
    1.40  #define DEFAULT_CPARAMS_RATE 22050
    1.41  #define DEFAULT_CPARAMS_VOICES 1
    1.42 -#define DEFAULT_CPARAMS_FRAG_SIZE 4096  //was 512
    1.43 +#define DEFAULT_CPARAMS_FRAG_SIZE 4096
    1.44  #define DEFAULT_CPARAMS_FRAGS_MIN 1
    1.45  #define DEFAULT_CPARAMS_FRAGS_MAX 1
    1.46  
    1.47  /* Open the audio device for playback, and don't block if busy */
    1.48 -#define OPEN_FLAGS	SND_PCM_OPEN_PLAYBACK
    1.49 +#define OPEN_FLAGS SND_PCM_OPEN_PLAYBACK
    1.50  
    1.51  /* Audio driver functions */
    1.52  static int NTO_OpenAudio(_THIS, SDL_AudioSpec *spec);
    1.53 @@ -68,8 +66,8 @@
    1.54  static void NTO_CloseAudio(_THIS);
    1.55  
    1.56  static snd_pcm_channel_status_t cstatus;
    1.57 -static	snd_pcm_channel_params_t cparams;
    1.58 -static	snd_pcm_channel_setup_t  csetup;
    1.59 +static snd_pcm_channel_params_t cparams;
    1.60 +static snd_pcm_channel_setup_t  csetup;
    1.61  
    1.62  /* PCM transfer channel parameters initialize function */
    1.63  static void init_pcm_cparams(snd_pcm_channel_params_t* cparams)
    1.64 @@ -78,7 +76,7 @@
    1.65  
    1.66  	cparams->channel = SND_PCM_CHANNEL_PLAYBACK;
    1.67  	cparams->mode = SND_PCM_MODE_BLOCK;
    1.68 -	cparams->start_mode = SND_PCM_START_DATA; //_FULL
    1.69 +	cparams->start_mode = SND_PCM_START_DATA;
    1.70  	cparams->stop_mode  = SND_PCM_STOP_STOP;
    1.71  	cparams->format.format = SND_PCM_SFMT_S16_LE;
    1.72  	cparams->format.interleave = 1;
    1.73 @@ -89,54 +87,48 @@
    1.74  	cparams->buf.block.frags_max = DEFAULT_CPARAMS_FRAGS_MAX;
    1.75  }
    1.76  
    1.77 -/* Audio driver bootstrap functions */
    1.78 +static int Audio_Available(void)
    1.79 +{
    1.80 +	/*
    1.81 +		See if we can open a nonblocking channel.
    1.82 +		Return value '1' means we can.
    1.83 +		Return value '0' means we cannot.
    1.84 +	*/
    1.85  
    1.86 -static int Audio_Available(void)
    1.87 -/*
    1.88 -	See if we can open a nonblocking channel.
    1.89 -	Return value '1' means we can.
    1.90 -	Return value '0' means we cannot.
    1.91 -*/
    1.92 -{
    1.93  	int available;
    1.94  	int rval;
    1.95  	snd_pcm_t *handle;
    1.96  
    1.97  	available = 0;
    1.98  	handle = NULL;
    1.99 -	
   1.100 -	//JB modified to take advantage of software mixer
   1.101 -	rval = snd_pcm_open_preferred(&handle, &card_no, &device_no, OPEN_FLAGS);
   1.102  
   1.103 -	if (rval >= 0)
   1.104 -	{
   1.105 -			available = 1;
   1.106 +	rval = snd_pcm_open_preferred(&handle, NULL, NULL, OPEN_FLAGS);
   1.107  
   1.108 -        if ((rval = snd_pcm_close(handle)) < 0)
   1.109 -        {
   1.110 -            SDL_SetError("snd_pcm_close failed: %s\n",snd_strerror(rval));
   1.111 +	if (rval >= 0){
   1.112 +		available = 1;
   1.113 +		
   1.114 +		if ((rval = snd_pcm_close(handle)) < 0){
   1.115 +			SDL_SetError("snd_pcm_close failed: %s\n",snd_strerror(rval));
   1.116  			available = 0;
   1.117 -        }
   1.118 +		}
   1.119  	}
   1.120 -	else
   1.121 -	{
   1.122 -	
   1.123 -       SDL_SetError("snd_pcm_open failed: %s\n", snd_strerror(rval));
   1.124 +	else{
   1.125 +		SDL_SetError("snd_pcm_open failed: %s\n", snd_strerror(rval));
   1.126  	}
   1.127  	
   1.128 -#ifdef DEBUG_AUDIO
   1.129 +	#ifdef DEBUG_AUDIO
   1.130  	fprintf(stderr,"AudioAvailable rtns %d\n", available);
   1.131 -#endif
   1.132 +	#endif
   1.133 +	
   1.134  	return(available);
   1.135  }
   1.136  
   1.137  static void Audio_DeleteDevice(SDL_AudioDevice *device)
   1.138  {
   1.139 -#ifdef DEBUG_AUDIO
   1.140 +	#ifdef DEBUG_AUDIO
   1.141  	fprintf(stderr,"Audio_DeleteDevice\n");
   1.142 -#endif
   1.143 +	#endif
   1.144  
   1.145 -	
   1.146  	free(device->hidden);
   1.147  	free(device);
   1.148  }
   1.149 @@ -144,15 +136,15 @@
   1.150  static SDL_AudioDevice *Audio_CreateDevice(int devindex)
   1.151  {
   1.152  	SDL_AudioDevice *this;
   1.153 -#ifdef DEBUG_AUDIO
   1.154 +	#ifdef DEBUG_AUDIO
   1.155  	fprintf(stderr,"Audio_CreateDevice\n");
   1.156 -#endif
   1.157 +	#endif
   1.158  	/* Initialize all variables that we clean on shutdown */
   1.159  	this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice));
   1.160  	if ( this ) {
   1.161  		memset(this, 0, (sizeof *this));
   1.162  		this->hidden = (struct SDL_PrivateAudioData *)
   1.163 -				malloc((sizeof *this->hidden));
   1.164 +		malloc((sizeof *this->hidden));
   1.165  	}
   1.166  	if ( (this == NULL) || (this->hidden == NULL) ) {
   1.167  		SDL_OutOfMemory();
   1.168 @@ -185,117 +177,92 @@
   1.169  /* This function waits until it is possible to write a full sound buffer */
   1.170  static void NTO_WaitAudio(_THIS)
   1.171  {
   1.172 -	int rval;
   1.173 -	int totalbytes,roomavail;
   1.174 -	/*we consider a full sound buffer to be of size pcm_len bytes */
   1.175 -	
   1.176 -#ifdef DEBUG_AUDIO
   1.177 -	fprintf(stderr,"NTO_WaitAudio\n");
   1.178 -#endif
   1.179 +	fd_set wfds;
   1.180  
   1.181 -	while(1)
   1.182 +	FD_SET( audio_fd, &wfds );
   1.183 +	switch( select( audio_fd + 1, NULL, &wfds, NULL, NULL ) )
   1.184  	{
   1.185 -	memset(&cstatus, 0, sizeof(cstatus));
   1.186 -	 if( (rval = snd_pcm_plugin_status(audio_handle, &cstatus)) < 0 )
   1.187 -    {
   1.188 -		 SDL_SetError("snd_pcm_plugin_status failed: %s\n", snd_strerror(rval));
   1.189 -   	    return;
   1.190 -	}	
   1.191 -	
   1.192 -	totalbytes = csetup.buf.block.frag_size *csetup.buf.block.frags;
   1.193 -	roomavail = totalbytes - cstatus.count;
   1.194 -
   1.195 -#ifdef DEBUG_AUDIO
   1.196 -	fprintf(stderr,"NTO_WaitAudio roomavail %d pcm_len %d\n",roomavail,pcm_len);
   1.197 -#endif
   1.198 -	
   1.199 -	if ((roomavail >= pcm_len) || (roomavail < 0))
   1.200 -		return;
   1.201 -		
   1.202 -		SDL_Delay(10);	
   1.203 -	}	
   1.204 -      
   1.205 +		case -1:
   1.206 +		case 0:
   1.207 +			/* Error */
   1.208 +			SDL_SetError("select() in NTO_WaitAudio failed: %s\n", strerror(errno));
   1.209 +			break;
   1.210 +		default:
   1.211 +			if(FD_ISSET(audio_fd, &wfds))
   1.212 +				return;
   1.213 +	}
   1.214  }
   1.215  
   1.216 -
   1.217 -
   1.218  static void NTO_PlayAudio(_THIS)
   1.219  {
   1.220 -    int written, rval;
   1.221 -    int towrite;
   1.222 +	int written, rval;
   1.223 +	int towrite;
   1.224  
   1.225 -#ifdef DEBUG_AUDIO
   1.226 -		fprintf(stderr, "NTO_PlayAudio\n");
   1.227 -#endif
   1.228 +	#ifdef DEBUG_AUDIO
   1.229 +	fprintf(stderr, "NTO_PlayAudio\n");
   1.230 +	#endif
   1.231  
   1.232 -	if( !this->enabled)
   1.233 -	   return;
   1.234 +	if( !this->enabled){
   1.235 +		return;
   1.236 +	}
   1.237  
   1.238  	towrite = pcm_len;
   1.239  	
   1.240 -
   1.241 -    /* Write the audio data, checking for EAGAIN (buffer full) and underrun */
   1.242 -    do {
   1.243 +	/* Write the audio data, checking for EAGAIN (buffer full) and underrun */
   1.244 +	do {
   1.245  		written = snd_pcm_plugin_write(audio_handle, pcm_buf, towrite);
   1.246 -#ifdef DEBUG_AUDIO
   1.247 +		#ifdef DEBUG_AUDIO
   1.248  		fprintf(stderr, "NTO_PlayAudio: written = %d towrite = %d\n",written,towrite);
   1.249 -#endif
   1.250 -		if (written != towrite)
   1.251 -		{
   1.252 -	        if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
   1.253 -			{
   1.254 -            	SDL_Delay(1);   /* Let a little CPU time go by and try to write again */
   1.255 -#ifdef DEBUG_AUDIO
   1.256 +		#endif
   1.257 +		if (written != towrite){
   1.258 +			if ((errno == EAGAIN) || (errno == EWOULDBLOCK)){
   1.259 +				SDL_Delay(1);   /* Let a little CPU time go by and try to write again */
   1.260 +				#ifdef DEBUG_AUDIO
   1.261  				fprintf(stderr, "errno == EAGAIN written %d\n", written);
   1.262 +				#endif
   1.263  				towrite -= written; //we wrote some data
   1.264 -#endif
   1.265  				continue;
   1.266 -        	}		
   1.267 -			else if((errno == EINVAL) || (errno == EIO))
   1.268 -			{
   1.269 -#ifdef DEBUG_AUDIO
   1.270 -					if(errno == EIO)
   1.271 -						fprintf(stderr,"snd_pcm_plugin_write failed EIO: %s\n", snd_strerror(written));	
   1.272 -					if(errno == EINVAL)
   1.273 -						fprintf(stderr,"snd_pcm_plugin_write failed EINVAL: %s\n", snd_strerror(written));	
   1.274 -								
   1.275 -#endif			
   1.276 -			
   1.277 -				  memset(&cstatus, 0, sizeof(cstatus));			
   1.278 -		         if( (rval = snd_pcm_plugin_status(audio_handle, &cstatus)) < 0 )
   1.279 -        		 {
   1.280 -#ifdef DEBUG_AUDIO
   1.281 +			}		
   1.282 +			else if((errno == EINVAL) || (errno == EIO)){
   1.283 +				if(errno == EIO){
   1.284 +					#ifdef DEBUG_AUDIO
   1.285 +					fprintf(stderr,"snd_pcm_plugin_write failed EIO: %s\n", snd_strerror(written));
   1.286 +					#endif
   1.287 +				}
   1.288 +				if(errno == EINVAL){
   1.289 +					#ifdef DEBUG_AUDIO
   1.290 +					fprintf(stderr,"snd_pcm_plugin_write failed EINVAL: %s\n", snd_strerror(written));	
   1.291 +					#endif
   1.292 +				}
   1.293 +				
   1.294 +				memset(&cstatus, 0, sizeof(cstatus));
   1.295 +				if( (rval = snd_pcm_plugin_status(audio_handle, &cstatus)) < 0 ){
   1.296 +					#ifdef DEBUG_AUDIO
   1.297  					fprintf(stderr, "snd_pcm_plugin_status failed %s\n",snd_strerror(rval));
   1.298 -#endif
   1.299 -		            SDL_SetError("snd_pcm_plugin_status failed: %s\n", snd_strerror(rval));
   1.300 -        		   return;
   1.301 -		        }	
   1.302 +					#endif
   1.303 +					SDL_SetError("snd_pcm_plugin_status failed: %s\n", snd_strerror(rval));
   1.304 +					return;
   1.305 +				}	
   1.306  		        
   1.307 -				if ( (cstatus.status == SND_PCM_STATUS_UNDERRUN) ||
   1.308 -					(cstatus.status == SND_PCM_STATUS_READY) )
   1.309 -				{
   1.310 -#ifdef DEBUG_AUDIO
   1.311 +				if ( (cstatus.status == SND_PCM_STATUS_UNDERRUN) || (cstatus.status == SND_PCM_STATUS_READY) ){
   1.312 +					#ifdef DEBUG_AUDIO
   1.313  					fprintf(stderr, "buffer underrun\n");
   1.314 -#endif
   1.315 -					if ( (rval = snd_pcm_plugin_prepare (audio_handle,SND_PCM_CHANNEL_PLAYBACK)) < 0 )
   1.316 -					{
   1.317 -#ifdef DEBUG_AUDIO
   1.318 +					#endif
   1.319 +					if ( (rval = snd_pcm_plugin_prepare (audio_handle,SND_PCM_CHANNEL_PLAYBACK)) < 0 ){
   1.320 +						#ifdef DEBUG_AUDIO
   1.321  						fprintf(stderr, "NTO_PlayAudio: prepare failed %s\n",snd_strerror(rval));
   1.322 -#endif
   1.323 +						#endif
   1.324  						SDL_SetError("snd_pcm_plugin_prepare failed: %s\n",snd_strerror(rval) );
   1.325  						return;
   1.326  					}
   1.327 -					
   1.328  				}		        		
   1.329   				continue;
   1.330  			}
   1.331 -			else
   1.332 -			{
   1.333 -#ifdef DEBUG_AUDIO
   1.334 -						fprintf(stderr, "NTO_PlayAudio: snd_pcm_plugin_write failed unknown errno %d %s\n",errno, snd_strerror(rval));
   1.335 -#endif
   1.336 +			else{
   1.337 +				#ifdef DEBUG_AUDIO
   1.338 +				fprintf(stderr, "NTO_PlayAudio: snd_pcm_plugin_write failed unknown errno %d %s\n",errno, snd_strerror(rval));
   1.339 +				#endif
   1.340  				return;
   1.341 -			
   1.342  			}
   1.343  			
   1.344  		}
   1.345 @@ -303,41 +270,39 @@
   1.346  		{
   1.347  			towrite -= written; //we wrote all remaining data
   1.348  		}
   1.349 -    } while ( (towrite > 0)  && (this->enabled) );
   1.350 +	} while ( (towrite > 0)  && (this->enabled) );
   1.351  
   1.352 -    /* If we couldn't write, assume fatal error for now */
   1.353 -    if ( towrite != 0 ) {
   1.354 -        this->enabled = 0;
   1.355 -    }
   1.356 +	/* If we couldn't write, assume fatal error for now */
   1.357 +	if ( towrite != 0 ) {
   1.358 +		this->enabled = 0;
   1.359 +	}
   1.360  	return;
   1.361  }
   1.362  
   1.363  static Uint8 *NTO_GetAudioBuf(_THIS)
   1.364  {
   1.365 - #ifdef DEBUG_AUDIO
   1.366 -		fprintf(stderr, "NTO_GetAudioBuf: pcm_buf %X\n",(Uint8 *)pcm_buf);
   1.367 -#endif
   1.368 +	#ifdef DEBUG_AUDIO
   1.369 +	fprintf(stderr, "NTO_GetAudioBuf: pcm_buf %X\n",(Uint8 *)pcm_buf);
   1.370 +	#endif
   1.371  	return(pcm_buf);
   1.372  }
   1.373  
   1.374  static void NTO_CloseAudio(_THIS)
   1.375  {
   1.376  	int rval;
   1.377 -	
   1.378 -#ifdef DEBUG_AUDIO
   1.379 -		fprintf(stderr, "NTO_CloseAudio\n");
   1.380 -#endif
   1.381  
   1.382 -	 this->enabled = 0;
   1.383 +	#ifdef DEBUG_AUDIO
   1.384 +	fprintf(stderr, "NTO_CloseAudio\n");
   1.385 +	#endif
   1.386 +
   1.387 +	this->enabled = 0;
   1.388  
   1.389  	if ( audio_handle != NULL ) {
   1.390 -		if ((rval = snd_pcm_plugin_flush(audio_handle,SND_PCM_CHANNEL_PLAYBACK)) < 0)
   1.391 -		{
   1.392 -        	SDL_SetError("snd_pcm_plugin_flush failed: %s\n",snd_strerror(rval));
   1.393 +		if ((rval = snd_pcm_plugin_flush(audio_handle,SND_PCM_CHANNEL_PLAYBACK)) < 0){
   1.394 +			SDL_SetError("snd_pcm_plugin_flush failed: %s\n",snd_strerror(rval));
   1.395  			return;
   1.396  		}
   1.397 -		if ((rval = snd_pcm_close(audio_handle)) < 0)
   1.398 -		{
   1.399 +		if ((rval = snd_pcm_close(audio_handle)) < 0){
   1.400  			SDL_SetError("snd_pcm_close failed: %s\n",snd_strerror(rval));
   1.401  			return;
   1.402  		}
   1.403 @@ -353,12 +318,12 @@
   1.404  	int twidth;
   1.405  	int found;
   1.406  
   1.407 -#ifdef DEBUG_AUDIO
   1.408 -		fprintf(stderr, "NTO_OpenAudio\n");
   1.409 -#endif
   1.410 +	#ifdef DEBUG_AUDIO
   1.411 +	fprintf(stderr, "NTO_OpenAudio\n");
   1.412 +	#endif
   1.413  	
   1.414  	audio_handle = NULL;
   1.415 -	 this->enabled = 0;
   1.416 +	this->enabled = 0;
   1.417  
   1.418  	if ( pcm_buf != NULL ) {
   1.419  		free((Uint8 *)pcm_buf); 
   1.420 @@ -369,67 +334,51 @@
   1.421  	init_pcm_cparams(&cparams);
   1.422  
   1.423  	/* Open the audio device */
   1.424 -	
   1.425 -	rval = snd_pcm_open_preferred(&audio_handle, &card_no, &device_no, OPEN_FLAGS);
   1.426 +	rval = snd_pcm_open_preferred(&audio_handle, NULL, NULL, OPEN_FLAGS);
   1.427  	if ( rval < 0 ) {
   1.428  		SDL_SetError("snd_pcm_open failed: %s\n", snd_strerror(rval));
   1.429  		return(-1);
   1.430  	}
   1.431  
   1.432 -    /* set to nonblocking mode */
   1.433 -    if ((rval = snd_pcm_nonblock_mode(audio_handle, 1))<0) //I assume 1 means on
   1.434 -    {
   1.435 -        SDL_SetError("snd_pcm_nonblock_mode failed: %s\n", snd_strerror(rval));
   1.436 -        return(-1);
   1.437 -    }
   1.438 -
   1.439 -    /* enable count status parameter */
   1.440 -    if ((rval = snd_pcm_plugin_set_disable(audio_handle, PLUGIN_DISABLE_MMAP))<0)
   1.441 -    {
   1.442 -        SDL_SetError("snd_pcm_plugin_set_disable failed: %s\n", snd_strerror(rval));
   1.443 -        return(-1);
   1.444 -    }
   1.445 -
   1.446 +	/* enable count status parameter */
   1.447 +	if ((rval = snd_pcm_plugin_set_disable(audio_handle, PLUGIN_DISABLE_MMAP))<0){
   1.448 +		SDL_SetError("snd_pcm_plugin_set_disable failed: %s\n", snd_strerror(rval));
   1.449 +		return(-1);
   1.450 +	}
   1.451  
   1.452  	/* Try for a closest match on audio format */
   1.453  	format = 0;
   1.454 - 	found = 0; // can't use format as SND_PCM_SFMT_U8 = 0 in nto
   1.455 + 	found = 0; /* can't use format as SND_PCM_SFMT_U8 = 0 in nto */
   1.456  	for ( test_format = SDL_FirstAudioFormat(spec->format);	!found ; ) 
   1.457 -
   1.458  	{
   1.459 -#ifdef DEBUG_AUDIO
   1.460 +		#ifdef DEBUG_AUDIO
   1.461  		fprintf(stderr, "Trying format 0x%4.4x spec->samples %d\n", test_format,spec->samples);
   1.462 -#endif
   1.463 -			/* if match found set format to equivalent ALSA format */
   1.464 -        switch ( test_format ) {
   1.465 +		#endif
   1.466 +		
   1.467 +		/* if match found set format to equivalent ALSA format */
   1.468 +		switch ( test_format ) {
   1.469  			case AUDIO_U8:
   1.470  				format = SND_PCM_SFMT_U8;
   1.471 -				cparams.buf.block.frag_size = spec->samples * spec->channels;
   1.472  				found = 1;
   1.473  				break;
   1.474  			case AUDIO_S8:
   1.475  				format = SND_PCM_SFMT_S8;
   1.476 -				cparams.buf.block.frag_size = spec->samples * spec->channels;
   1.477  				found = 1;
   1.478  				break;
   1.479  			case AUDIO_S16LSB:
   1.480  				format = SND_PCM_SFMT_S16_LE;
   1.481 -				cparams.buf.block.frag_size = spec->samples*2 * spec->channels;
   1.482  				found = 1;
   1.483  				break;
   1.484  			case AUDIO_S16MSB:
   1.485  				format = SND_PCM_SFMT_S16_BE;
   1.486 -				cparams.buf.block.frag_size = spec->samples*2 * spec->channels;
   1.487  				found = 1;
   1.488  				break;
   1.489  			case AUDIO_U16LSB:
   1.490  				format = SND_PCM_SFMT_U16_LE;
   1.491 -				cparams.buf.block.frag_size = spec->samples*2 * spec->channels;
   1.492  				found = 1;
   1.493  				break;
   1.494  			case AUDIO_U16MSB:
   1.495  				format = SND_PCM_SFMT_U16_BE;
   1.496 -				cparams.buf.block.frag_size = spec->samples*2 * spec->channels;
   1.497  				found = 1;
   1.498  				break;
   1.499  			default:
   1.500 @@ -439,13 +388,13 @@
   1.501  			test_format = SDL_NextAudioFormat();
   1.502  		}
   1.503  	}
   1.504 -	
   1.505 +
   1.506  	/* assumes test_format not 0 on success */
   1.507  	if ( test_format == 0 ) {
   1.508  		SDL_SetError("Couldn't find any hardware audio formats");
   1.509  		return(-1);
   1.510  	}
   1.511 -	
   1.512 +
   1.513  	spec->format = test_format;
   1.514  
   1.515  	/* Set the audio format */
   1.516 @@ -454,11 +403,10 @@
   1.517  	/* Set mono or stereo audio (currently only two channels supported) */
   1.518  	cparams.format.voices = spec->channels;
   1.519  	
   1.520 -#ifdef DEBUG_AUDIO
   1.521 +	#ifdef DEBUG_AUDIO
   1.522  	fprintf(stderr,"intializing channels %d\n", cparams.format.voices);
   1.523 -#endif
   1.524 +	#endif
   1.525  
   1.526 -	
   1.527  	/* Set rate */
   1.528  	cparams.format.rate = spec->freq ;
   1.529  
   1.530 @@ -469,80 +417,77 @@
   1.531  		return(-1);
   1.532  	}
   1.533  
   1.534 -    /*  Make sure channel is setup right one last time */
   1.535 -    memset( &csetup, 0, sizeof( csetup ) );
   1.536 -    csetup.channel = SND_PCM_CHANNEL_PLAYBACK;
   1.537 -    if ( snd_pcm_plugin_setup( audio_handle, &csetup ) < 0 )
   1.538 -    {
   1.539 -        SDL_SetError("Unable to setup playback channel\n" );
   1.540 -        return(-1);
   1.541 -    }
   1.542 -    else
   1.543 -    {
   1.544 -#ifdef DEBUG_AUDIO
   1.545 -        fprintf(stderr,"requested format: %d\n",cparams.format.format);
   1.546 -        fprintf(stderr,"requested frag size: %d\n",cparams.buf.block.frag_size);
   1.547 -        fprintf(stderr,"requested max frags: %d\n\n",cparams.buf.block.frags_max);
   1.548 +	/*  Make sure channel is setup right one last time */
   1.549 +	memset( &csetup, 0, sizeof( csetup ) );
   1.550 +	csetup.channel = SND_PCM_CHANNEL_PLAYBACK;
   1.551 +	if ( snd_pcm_plugin_setup( audio_handle, &csetup ) < 0 )
   1.552 +	{
   1.553 +		SDL_SetError("Unable to setup playback channel\n" );
   1.554 +		return(-1);
   1.555 +	}
   1.556 +	else
   1.557 +	{
   1.558 +		#ifdef DEBUG_AUDIO
   1.559 +		fprintf(stderr,"requested format: %d\n",cparams.format.format);
   1.560 +		fprintf(stderr,"requested frag size: %d\n",cparams.buf.block.frag_size);
   1.561 +		fprintf(stderr,"requested max frags: %d\n\n",cparams.buf.block.frags_max);
   1.562  
   1.563 -        fprintf(stderr,"real format: %d\n", csetup.format.format );
   1.564 -        fprintf(stderr,"real frag size : %d\n", csetup.buf.block.frag_size );
   1.565 +		fprintf(stderr,"real format: %d\n", csetup.format.format );
   1.566 +		fprintf(stderr,"real frag size : %d\n", csetup.buf.block.frag_size );
   1.567  		fprintf(stderr,"real max frags : %d\n", csetup.buf.block.frags_max );
   1.568 -#endif // DEBUG_AUDIO
   1.569 -    }
   1.570 -
   1.571 +		#endif
   1.572 +	}
   1.573  
   1.574 -    /*  Allocate memory to the audio buffer and initialize with silence
   1.575 -        (Note that buffer size must be a multiple of fragment size, so find closest multiple)
   1.576 -    */
   1.577 -    
   1.578 -    twidth = snd_pcm_format_width(format);
   1.579 -    if (twidth < 0) {
   1.580 -        printf("snd_pcm_format_width failed\n");
   1.581 -        twidth = 0;
   1.582 -    }
   1.583 -    
   1.584 -#ifdef DEBUG_AUDIO
   1.585 -    fprintf(stderr,"format is %d bits wide\n",twidth);
   1.586 -#endif      
   1.587 -    
   1.588 -    pcm_len = spec->size ;
   1.589 -    
   1.590 -  
   1.591 -#ifdef DEBUG_AUDIO    
   1.592 -    fprintf(stderr,"pcm_len set to %d\n", pcm_len);
   1.593 -#endif
   1.594 -    
   1.595 -    if (pcm_len == 0)
   1.596 -    {
   1.597 -        pcm_len = csetup.buf.block.frag_size;
   1.598 -    }
   1.599 -    
   1.600 -    pcm_buf = (Uint8*)malloc(pcm_len);
   1.601 -    if (pcm_buf == NULL) {
   1.602 -        SDL_SetError("pcm_buf malloc failed\n");
   1.603 -        return(-1);
   1.604 -    }
   1.605 -    memset(pcm_buf,spec->silence,pcm_len);
   1.606 +	/*
   1.607 +		Allocate memory to the audio buffer and initialize with silence (Note that
   1.608 +		buffer size must be a multiple of fragment size, so find closest multiple)
   1.609 +	*/
   1.610 +	
   1.611 +	twidth = snd_pcm_format_width(format);
   1.612 +	if (twidth < 0) {
   1.613 +		printf("snd_pcm_format_width failed\n");
   1.614 +		twidth = 0;
   1.615 +	}
   1.616 +
   1.617 +	#ifdef DEBUG_AUDIO
   1.618 +	fprintf(stderr,"format is %d bits wide\n",twidth);
   1.619 +	#endif
   1.620  
   1.621 -#ifdef DEBUG_AUDIO
   1.622 -	fprintf(stderr,"pcm_buf malloced and silenced.\n");
   1.623 -#endif
   1.624 +	pcm_len = spec->size ;
   1.625 +	
   1.626 +	#ifdef DEBUG_AUDIO
   1.627 +	fprintf(stderr,"pcm_len set to %d\n", pcm_len);
   1.628 +	#endif
   1.629 +
   1.630 +	if (pcm_len == 0){
   1.631 +		pcm_len = csetup.buf.block.frag_size;
   1.632 +	}
   1.633  
   1.634 -    /* get the file descriptor */
   1.635 -    if( (audio_fd = snd_pcm_file_descriptor(audio_handle, SND_PCM_CHANNEL_PLAYBACK)) < 0)
   1.636 -    {
   1.637 -       fprintf(stderr, "snd_pcm_file_descriptor failed with error code: %d\n", audio_fd);
   1.638 -    }
   1.639 +	pcm_buf = (Uint8*)malloc(pcm_len);
   1.640 +	if (pcm_buf == NULL) {
   1.641 +		SDL_SetError("pcm_buf malloc failed\n");
   1.642 +		return(-1);
   1.643 +	}
   1.644 +	memset(pcm_buf,spec->silence,pcm_len);
   1.645 +
   1.646 +	#ifdef DEBUG_AUDIO
   1.647 +	fprintf(stderr,"pcm_buf malloced and silenced.\n");
   1.648 +	#endif
   1.649 +
   1.650 +	/* get the file descriptor */
   1.651 +	if( (audio_fd = snd_pcm_file_descriptor(audio_handle, SND_PCM_CHANNEL_PLAYBACK)) < 0){
   1.652 +		fprintf(stderr, "snd_pcm_file_descriptor failed with error code: %d\n", audio_fd);
   1.653 +	}
   1.654  
   1.655  	/* Trigger audio playback */
   1.656  	rval = snd_pcm_plugin_prepare( audio_handle, SND_PCM_CHANNEL_PLAYBACK);
   1.657  	if (rval < 0) {
   1.658 -       SDL_SetError("snd_pcm_plugin_prepare failed: %s\n", snd_strerror (rval));
   1.659 -       return(-1);
   1.660 +		SDL_SetError("snd_pcm_plugin_prepare failed: %s\n", snd_strerror (rval));
   1.661 +		return(-1);
   1.662  	}
   1.663 -	
   1.664 -	 this->enabled = 1;
   1.665 -	 
   1.666 +
   1.667 +	this->enabled = 1;
   1.668 +
   1.669  	/* Get the parent process id (we're the parent of the audio thread) */
   1.670  	parent = getpid();
   1.671  
   1.672 @@ -550,5 +495,3 @@
   1.673  	return(0);
   1.674  }
   1.675  
   1.676 -
   1.677 -