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