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