src/audio/paudio/SDL_paudio.c
author Sam Lantinga
Mon, 06 Feb 2006 08:28:51 +0000
changeset 1330 450721ad5436
parent 1312 c9b51268668f
child 1336 3692456e7b0f
permissions -rw-r--r--
It's now possible to build SDL without any C runtime at all on Windows,
using Visual C++ 2005
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Carsten Griwodz
    20     griff@kom.tu-darmstadt.de
    21 
    22     based on linux/SDL_dspaudio.c by Sam Lantinga
    23 */
    24 
    25 /* Allow access to a raw mixing buffer */
    26 
    27 #include <stdlib.h>
    28 #include <stdio.h>
    29 #include <string.h>
    30 #include <errno.h>
    31 #include <unistd.h>
    32 #include <fcntl.h>
    33 #include <sys/time.h>
    34 #include <sys/ioctl.h>
    35 #include <sys/stat.h>
    36 
    37 #include "SDL_audio.h"
    38 #include "SDL_error.h"
    39 #include "SDL_audiomem.h"
    40 #include "SDL_audio_c.h"
    41 #include "SDL_timer.h"
    42 #include "SDL_audiodev_c.h"
    43 #include "SDL_paudio.h"
    44 
    45 #define DEBUG_AUDIO 1
    46 
    47 /* A conflict within AIX 4.3.3 <sys/> headers and probably others as well.
    48  * I guess nobody ever uses audio... Shame over AIX header files.  */
    49 #include <sys/machine.h>
    50 #undef BIG_ENDIAN
    51 #include <sys/audio.h>
    52 
    53 /* The tag name used by paud audio */
    54 #define Paud_DRIVER_NAME         "paud"
    55 
    56 /* Open the audio device for playback, and don't block if busy */
    57 /* #define OPEN_FLAGS	(O_WRONLY|O_NONBLOCK) */
    58 #define OPEN_FLAGS	O_WRONLY
    59 
    60 /* Audio driver functions */
    61 static int Paud_OpenAudio(_THIS, SDL_AudioSpec *spec);
    62 static void Paud_WaitAudio(_THIS);
    63 static void Paud_PlayAudio(_THIS);
    64 static Uint8 *Paud_GetAudioBuf(_THIS);
    65 static void Paud_CloseAudio(_THIS);
    66 
    67 /* Audio driver bootstrap functions */
    68 
    69 static int Audio_Available(void)
    70 {
    71 	int fd;
    72 	int available;
    73 
    74 	available = 0;
    75 	fd = SDL_OpenAudioPath(NULL, 0, OPEN_FLAGS, 0);
    76 	if ( fd >= 0 ) {
    77 		available = 1;
    78 		close(fd);
    79 	}
    80 	return(available);
    81 }
    82 
    83 static void Audio_DeleteDevice(SDL_AudioDevice *device)
    84 {
    85 	free(device->hidden);
    86 	free(device);
    87 }
    88 
    89 static SDL_AudioDevice *Audio_CreateDevice(int devindex)
    90 {
    91 	SDL_AudioDevice *this;
    92 
    93 	/* Initialize all variables that we clean on shutdown */
    94 	this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice));
    95 	if ( this ) {
    96 		memset(this, 0, (sizeof *this));
    97 		this->hidden = (struct SDL_PrivateAudioData *)
    98 				malloc((sizeof *this->hidden));
    99 	}
   100 	if ( (this == NULL) || (this->hidden == NULL) ) {
   101 		SDL_OutOfMemory();
   102 		if ( this ) {
   103 			free(this);
   104 		}
   105 		return(0);
   106 	}
   107 	memset(this->hidden, 0, (sizeof *this->hidden));
   108 	audio_fd = -1;
   109 
   110 	/* Set the function pointers */
   111 	this->OpenAudio = Paud_OpenAudio;
   112 	this->WaitAudio = Paud_WaitAudio;
   113 	this->PlayAudio = Paud_PlayAudio;
   114 	this->GetAudioBuf = Paud_GetAudioBuf;
   115 	this->CloseAudio = Paud_CloseAudio;
   116 
   117 	this->free = Audio_DeleteDevice;
   118 
   119 	return this;
   120 }
   121 
   122 AudioBootStrap Paud_bootstrap = {
   123 	Paud_DRIVER_NAME, "AIX Paudio",
   124 	Audio_Available, Audio_CreateDevice
   125 };
   126 
   127 /* This function waits until it is possible to write a full sound buffer */
   128 static void Paud_WaitAudio(_THIS)
   129 {
   130     fd_set fdset;
   131 
   132     /* See if we need to use timed audio synchronization */
   133     if ( frame_ticks ) {
   134         /* Use timer for general audio synchronization */
   135         Sint32 ticks;
   136 
   137         ticks = ((Sint32)(next_frame - SDL_GetTicks()))-FUDGE_TICKS;
   138         if ( ticks > 0 ) {
   139 	    SDL_Delay(ticks);
   140         }
   141     } else {
   142         audio_buffer  paud_bufinfo;
   143 
   144         /* Use select() for audio synchronization */
   145         struct timeval timeout;
   146         FD_ZERO(&fdset);
   147         FD_SET(audio_fd, &fdset);
   148 
   149         if ( ioctl(audio_fd, AUDIO_BUFFER, &paud_bufinfo) < 0 ) {
   150 #ifdef DEBUG_AUDIO
   151             fprintf(stderr, "Couldn't get audio buffer information\n");
   152 #endif
   153             timeout.tv_sec  = 10;
   154             timeout.tv_usec = 0;
   155         } else {
   156 	    long ms_in_buf = paud_bufinfo.write_buf_time;
   157             timeout.tv_sec  = ms_in_buf/1000;
   158 	    ms_in_buf       = ms_in_buf - timeout.tv_sec*1000;
   159             timeout.tv_usec = ms_in_buf*1000;
   160 #ifdef DEBUG_AUDIO
   161             fprintf( stderr,
   162 		     "Waiting for write_buf_time=%ld,%ld\n",
   163 		     timeout.tv_sec,
   164 		     timeout.tv_usec );
   165 #endif
   166 	}
   167 
   168 #ifdef DEBUG_AUDIO
   169         fprintf(stderr, "Waiting for audio to get ready\n");
   170 #endif
   171         if ( select(audio_fd+1, NULL, &fdset, NULL, &timeout) <= 0 ) {
   172             const char *message = "Audio timeout - buggy audio driver? (disabled)";
   173             /*
   174 	     * In general we should never print to the screen,
   175              * but in this case we have no other way of letting
   176              * the user know what happened.
   177              */
   178             fprintf(stderr, "SDL: %s - %s\n", strerror(errno), message);
   179             this->enabled = 0;
   180             /* Don't try to close - may hang */
   181             audio_fd = -1;
   182 #ifdef DEBUG_AUDIO
   183             fprintf(stderr, "Done disabling audio\n");
   184 #endif
   185         }
   186 #ifdef DEBUG_AUDIO
   187         fprintf(stderr, "Ready!\n");
   188 #endif
   189     }
   190 }
   191 
   192 static void Paud_PlayAudio(_THIS)
   193 {
   194 	int written;
   195 
   196 	/* Write the audio data, checking for EAGAIN on broken audio drivers */
   197 	do {
   198 		written = write(audio_fd, mixbuf, mixlen);
   199 		if ( (written < 0) && ((errno == 0) || (errno == EAGAIN)) ) {
   200 			SDL_Delay(1);	/* Let a little CPU time go by */
   201 		}
   202 	} while ( (written < 0) && 
   203 	          ((errno == 0) || (errno == EAGAIN) || (errno == EINTR)) );
   204 
   205 	/* If timer synchronization is enabled, set the next write frame */
   206 	if ( frame_ticks ) {
   207 		next_frame += frame_ticks;
   208 	}
   209 
   210 	/* If we couldn't write, assume fatal error for now */
   211 	if ( written < 0 ) {
   212 		this->enabled = 0;
   213 	}
   214 #ifdef DEBUG_AUDIO
   215 	fprintf(stderr, "Wrote %d bytes of audio data\n", written);
   216 #endif
   217 }
   218 
   219 static Uint8 *Paud_GetAudioBuf(_THIS)
   220 {
   221 	return mixbuf;
   222 }
   223 
   224 static void Paud_CloseAudio(_THIS)
   225 {
   226 	if ( mixbuf != NULL ) {
   227 		SDL_FreeAudioMem(mixbuf);
   228 		mixbuf = NULL;
   229 	}
   230 	if ( audio_fd >= 0 ) {
   231 		close(audio_fd);
   232 		audio_fd = -1;
   233 	}
   234 }
   235 
   236 static int Paud_OpenAudio(_THIS, SDL_AudioSpec *spec)
   237 {
   238 	char          audiodev[1024];
   239 	int           format;
   240 	int           bytes_per_sample;
   241 	Uint16        test_format;
   242 	audio_init    paud_init;
   243 	audio_buffer  paud_bufinfo;
   244 	audio_status  paud_status;
   245 	audio_control paud_control;
   246 	audio_change  paud_change;
   247 
   248 	/* Reset the timer synchronization flag */
   249 	frame_ticks = 0.0;
   250 
   251 	/* Open the audio device */
   252 	audio_fd = SDL_OpenAudioPath(audiodev, sizeof(audiodev), OPEN_FLAGS, 0);
   253 	if ( audio_fd < 0 ) {
   254 		SDL_SetError("Couldn't open %s: %s", audiodev, strerror(errno));
   255 		return -1;
   256 	}
   257 
   258 	/*
   259 	 * We can't set the buffer size - just ask the device for the maximum
   260 	 * that we can have.
   261 	 */
   262 	if ( ioctl(audio_fd, AUDIO_BUFFER, &paud_bufinfo) < 0 ) {
   263 		SDL_SetError("Couldn't get audio buffer information");
   264 		return -1;
   265 	}
   266 
   267 	mixbuf = NULL;
   268 
   269 	if ( spec->channels > 1 )
   270 	    spec->channels = 2;
   271 	else
   272 	    spec->channels = 1;
   273 
   274 	/*
   275 	 * Fields in the audio_init structure:
   276 	 *
   277 	 * Ignored by us:
   278 	 *
   279 	 * paud.loadpath[LOAD_PATH]; * DSP code to load, MWave chip only?
   280 	 * paud.slot_number;         * slot number of the adapter
   281 	 * paud.device_id;           * adapter identification number
   282 	 *
   283 	 * Input:
   284 	 *
   285 	 * paud.srate;           * the sampling rate in Hz
   286 	 * paud.bits_per_sample; * 8, 16, 32, ...
   287 	 * paud.bsize;           * block size for this rate
   288 	 * paud.mode;            * ADPCM, PCM, MU_LAW, A_LAW, SOURCE_MIX
   289 	 * paud.channels;        * 1=mono, 2=stereo
   290 	 * paud.flags;           * FIXED - fixed length data
   291 	 *                       * LEFT_ALIGNED, RIGHT_ALIGNED (var len only)
   292 	 *                       * TWOS_COMPLEMENT - 2's complement data
   293 	 *                       * SIGNED - signed? comment seems wrong in sys/audio.h
   294 	 *                       * BIG_ENDIAN
   295 	 * paud.operation;       * PLAY, RECORD
   296 	 *
   297 	 * Output:
   298 	 *
   299 	 * paud.flags;           * PITCH            - pitch is supported
   300 	 *                       * INPUT            - input is supported
   301 	 *                       * OUTPUT           - output is supported
   302 	 *                       * MONITOR          - monitor is supported
   303 	 *                       * VOLUME           - volume is supported
   304 	 *                       * VOLUME_DELAY     - volume delay is supported
   305 	 *                       * BALANCE          - balance is supported
   306 	 *                       * BALANCE_DELAY    - balance delay is supported
   307 	 *                       * TREBLE           - treble control is supported
   308 	 *                       * BASS             - bass control is supported
   309 	 *                       * BESTFIT_PROVIDED - best fit returned
   310 	 *                       * LOAD_CODE        - DSP load needed
   311 	 * paud.rc;              * NO_PLAY         - DSP code can't do play requests
   312 	 *                       * NO_RECORD       - DSP code can't do record requests
   313 	 *                       * INVALID_REQUEST - request was invalid
   314 	 *                       * CONFLICT        - conflict with open's flags
   315 	 *                       * OVERLOADED      - out of DSP MIPS or memory
   316 	 * paud.position_resolution; * smallest increment for position
   317 	 */
   318 
   319         paud_init.srate = spec->freq;
   320 	paud_init.mode = PCM;
   321 	paud_init.operation = PLAY;
   322 	paud_init.channels = spec->channels;
   323 
   324 	/* Try for a closest match on audio format */
   325 	format = 0;
   326 	for ( test_format = SDL_FirstAudioFormat(spec->format);
   327 						! format && test_format; ) {
   328 #ifdef DEBUG_AUDIO
   329 		fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
   330 #endif
   331 		switch ( test_format ) {
   332 			case AUDIO_U8:
   333 			    bytes_per_sample = 1;
   334 			    paud_init.bits_per_sample = 8;
   335 			    paud_init.flags = TWOS_COMPLEMENT | FIXED;
   336 			    format = 1;
   337 			    break;
   338 			case AUDIO_S8:
   339 			    bytes_per_sample = 1;
   340 			    paud_init.bits_per_sample = 8;
   341 			    paud_init.flags = SIGNED |
   342 					      TWOS_COMPLEMENT | FIXED;
   343 			    format = 1;
   344 			    break;
   345 			case AUDIO_S16LSB:
   346 			    bytes_per_sample = 2;
   347 			    paud_init.bits_per_sample = 16;
   348 			    paud_init.flags = SIGNED |
   349 					      TWOS_COMPLEMENT | FIXED;
   350 			    format = 1;
   351 			    break;
   352 			case AUDIO_S16MSB:
   353 			    bytes_per_sample = 2;
   354 			    paud_init.bits_per_sample = 16;
   355 			    paud_init.flags = BIG_ENDIAN |
   356 					      SIGNED |
   357 					      TWOS_COMPLEMENT | FIXED;
   358 			    format = 1;
   359 			    break;
   360 			case AUDIO_U16LSB:
   361 			    bytes_per_sample = 2;
   362 			    paud_init.bits_per_sample = 16;
   363 			    paud_init.flags = TWOS_COMPLEMENT | FIXED;
   364 			    format = 1;
   365 			    break;
   366 			case AUDIO_U16MSB:
   367 			    bytes_per_sample = 2;
   368 			    paud_init.bits_per_sample = 16;
   369 			    paud_init.flags = BIG_ENDIAN |
   370 					      TWOS_COMPLEMENT | FIXED;
   371 			    format = 1;
   372 			    break;
   373 			default:
   374 				break;
   375 		}
   376 		if ( ! format ) {
   377 			test_format = SDL_NextAudioFormat();
   378 		}
   379 	}
   380 	if ( format == 0 ) {
   381 #ifdef DEBUG_AUDIO
   382             fprintf(stderr, "Couldn't find any hardware audio formats\n");
   383 #endif
   384 	    SDL_SetError("Couldn't find any hardware audio formats");
   385 	    return -1;
   386 	}
   387 	spec->format = test_format;
   388 
   389 	/*
   390 	 * We know the buffer size and the max number of subsequent writes
   391 	 * that can be pending. If more than one can pend, allow the application
   392 	 * to do something like double buffering between our write buffer and
   393 	 * the device's own buffer that we are filling with write() anyway.
   394 	 *
   395 	 * We calculate spec->samples like this because SDL_CalculateAudioSpec()
   396 	 * will give put paud_bufinfo.write_buf_cap (or paud_bufinfo.write_buf_cap/2)
   397 	 * into spec->size in return.
   398 	 */
   399 	if ( paud_bufinfo.request_buf_cap == 1 )
   400 	{
   401 	    spec->samples = paud_bufinfo.write_buf_cap
   402 			  / bytes_per_sample
   403 			  / spec->channels;
   404 	}
   405 	else
   406 	{
   407 	    spec->samples = paud_bufinfo.write_buf_cap
   408 			  / bytes_per_sample
   409 			  / spec->channels
   410 			  / 2;
   411 	}
   412         paud_init.bsize = bytes_per_sample * spec->channels;
   413 
   414 	SDL_CalculateAudioSpec(spec);
   415 
   416 	/*
   417 	 * The AIX paud device init can't modify the values of the audio_init
   418 	 * structure that we pass to it. So we don't need any recalculation
   419 	 * of this stuff and no reinit call as in linux dsp and dma code.
   420 	 *
   421 	 * /dev/paud supports all of the encoding formats, so we don't need
   422 	 * to do anything like reopening the device, either.
   423 	 */
   424 	if ( ioctl(audio_fd, AUDIO_INIT, &paud_init) < 0 ) {
   425 	    switch ( paud_init.rc )
   426 	    {
   427 	    case 1 :
   428 		SDL_SetError("Couldn't set audio format: DSP can't do play requests");
   429 		return -1;
   430 		break;
   431 	    case 2 :
   432 		SDL_SetError("Couldn't set audio format: DSP can't do record requests");
   433 		return -1;
   434 		break;
   435 	    case 4 :
   436 		SDL_SetError("Couldn't set audio format: request was invalid");
   437 		return -1;
   438 		break;
   439 	    case 5 :
   440 		SDL_SetError("Couldn't set audio format: conflict with open's flags");
   441 		return -1;
   442 		break;
   443 	    case 6 :
   444 		SDL_SetError("Couldn't set audio format: out of DSP MIPS or memory");
   445 		return -1;
   446 		break;
   447 	    default :
   448 		SDL_SetError("Couldn't set audio format: not documented in sys/audio.h");
   449 		return -1;
   450 		break;
   451 	    }
   452 	}
   453 
   454 	/* Allocate mixing buffer */
   455 	mixlen = spec->size;
   456 	mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen);
   457 	if ( mixbuf == NULL ) {
   458 		return -1;
   459 	}
   460 	memset(mixbuf, spec->silence, spec->size);
   461 
   462 	/*
   463 	 * Set some paramters: full volume, first speaker that we can find.
   464 	 * Ignore the other settings for now.
   465 	 */
   466 	paud_change.input = AUDIO_IGNORE;         /* the new input source */
   467         paud_change.output = OUTPUT_1;            /* EXTERNAL_SPEAKER,INTERNAL_SPEAKER,OUTPUT_1 */
   468         paud_change.monitor = AUDIO_IGNORE;       /* the new monitor state */
   469         paud_change.volume = 0x7fffffff;          /* volume level [0-0x7fffffff] */
   470         paud_change.volume_delay = AUDIO_IGNORE;  /* the new volume delay */
   471         paud_change.balance = 0x3fffffff;         /* the new balance */
   472         paud_change.balance_delay = AUDIO_IGNORE; /* the new balance delay */
   473         paud_change.treble = AUDIO_IGNORE;        /* the new treble state */
   474         paud_change.bass = AUDIO_IGNORE;          /* the new bass state */
   475         paud_change.pitch = AUDIO_IGNORE;         /* the new pitch state */
   476 
   477 	paud_control.ioctl_request = AUDIO_CHANGE;
   478 	paud_control.request_info = (char*)&paud_change;
   479 	if ( ioctl(audio_fd, AUDIO_CONTROL, &paud_control) < 0 ) {
   480 #ifdef DEBUG_AUDIO
   481             fprintf(stderr, "Can't change audio display settings\n" );
   482 #endif
   483 	}
   484 
   485 	/*
   486 	 * Tell the device to expect data. Actual start will wait for
   487 	 * the first write() call.
   488 	 */
   489 	paud_control.ioctl_request = AUDIO_START;
   490 	paud_control.position = 0;
   491 	if ( ioctl(audio_fd, AUDIO_CONTROL, &paud_control) < 0 ) {
   492 #ifdef DEBUG_AUDIO
   493             fprintf(stderr, "Can't start audio play\n" );
   494 #endif
   495 	    SDL_SetError("Can't start audio play");
   496 	    return -1;
   497 	}
   498 
   499         /* Check to see if we need to use select() workaround */
   500         { char *workaround;
   501                 workaround = getenv("SDL_DSP_NOSELECT");
   502                 if ( workaround ) {
   503                         frame_ticks = (float)(spec->samples*1000)/spec->freq;
   504                         next_frame = SDL_GetTicks()+frame_ticks;
   505                 }
   506         }
   507 
   508 	/* Get the parent process id (we're the parent of the audio thread) */
   509 	parent = getpid();
   510 
   511 	/* We're ready to rock and roll. :-) */
   512 	return 0;
   513 }
   514