19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Carsten Griwodz
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    griff@kom.tu-darmstadt.de
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    based on linux/SDL_dspaudio.c by Sam Lantinga
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Allow access to a raw mixing buffer */
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <errno.h>
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <unistd.h>
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <fcntl.h>
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/time.h>
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/ioctl.h>
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/stat.h>
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_timer.h"
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_audio.h"
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_audiomem.h"
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_audio_c.h"
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_audiodev_c.h"
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_paudio.h"
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define DEBUG_AUDIO 1
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* A conflict within AIX 4.3.3 <sys/> headers and probably others as well.
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * I guess nobody ever uses audio... Shame over AIX header files.  */
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/machine.h>
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#undef BIG_ENDIAN
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/audio.h>
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The tag name used by paud audio */
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define Paud_DRIVER_NAME         "paud"
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Open the audio device for playback, and don't block if busy */
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* #define OPEN_FLAGS	(O_WRONLY|O_NONBLOCK) */
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define OPEN_FLAGS	O_WRONLY
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Audio driver functions */
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int Paud_OpenAudio(_THIS, SDL_AudioSpec *spec);
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Paud_WaitAudio(_THIS);
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Paud_PlayAudio(_THIS);
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Uint8 *Paud_GetAudioBuf(_THIS);
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Paud_CloseAudio(_THIS);
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Audio driver bootstrap functions */
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int Audio_Available(void)
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int fd;
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int available;
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	available = 0;
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fd = SDL_OpenAudioPath(NULL, 0, OPEN_FLAGS, 0);
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( fd >= 0 ) {
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		available = 1;
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		close(fd);
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(available);
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Audio_DeleteDevice(SDL_AudioDevice *device)
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(device->hidden);
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(device);
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_AudioDevice *Audio_CreateDevice(int devindex)
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_AudioDevice *this;
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Initialize all variables that we clean on shutdown */
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( this ) {
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_memset(this, 0, (sizeof *this));
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->hidden = (struct SDL_PrivateAudioData *)
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_malloc((sizeof *this->hidden));
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (this == NULL) || (this->hidden == NULL) ) {
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( this ) {
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(this);
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(0);
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(this->hidden, 0, (sizeof *this->hidden));
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	audio_fd = -1;
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the function pointers */
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->OpenAudio = Paud_OpenAudio;
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->WaitAudio = Paud_WaitAudio;
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->PlayAudio = Paud_PlayAudio;
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->GetAudioBuf = Paud_GetAudioBuf;
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->CloseAudio = Paud_CloseAudio;
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->free = Audio_DeleteDevice;
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return this;
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallAudioBootStrap Paud_bootstrap = {
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Paud_DRIVER_NAME, "AIX Paudio",
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Audio_Available, Audio_CreateDevice
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This function waits until it is possible to write a full sound buffer */
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Paud_WaitAudio(_THIS)
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    fd_set fdset;
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* See if we need to use timed audio synchronization */
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( frame_ticks ) {
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        /* Use timer for general audio synchronization */
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        Sint32 ticks;
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        ticks = ((Sint32)(next_frame - SDL_GetTicks()))-FUDGE_TICKS;
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( ticks > 0 ) {
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    SDL_Delay(ticks);
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } else {
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        audio_buffer  paud_bufinfo;
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        /* Use select() for audio synchronization */
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        struct timeval timeout;
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        FD_ZERO(&fdset);
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        FD_SET(audio_fd, &fdset);
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( ioctl(audio_fd, AUDIO_BUFFER, &paud_bufinfo) < 0 ) {
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_AUDIO
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            fprintf(stderr, "Couldn't get audio buffer information\n");
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            timeout.tv_sec  = 10;
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            timeout.tv_usec = 0;
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else {
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    long ms_in_buf = paud_bufinfo.write_buf_time;
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            timeout.tv_sec  = ms_in_buf/1000;
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    ms_in_buf       = ms_in_buf - timeout.tv_sec*1000;
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            timeout.tv_usec = ms_in_buf*1000;
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_AUDIO
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            fprintf( stderr,
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		     "Waiting for write_buf_time=%ld,%ld\n",
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		     timeout.tv_sec,
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		     timeout.tv_usec );
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_AUDIO
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        fprintf(stderr, "Waiting for audio to get ready\n");
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ( select(audio_fd+1, NULL, &fdset, NULL, &timeout) <= 0 ) {
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            const char *message = "Audio timeout - buggy audio driver? (disabled)";
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            /*
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	     * In general we should never print to the screen,
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall             * but in this case we have no other way of letting
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall             * the user know what happened.
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall             */
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            fprintf(stderr, "SDL: %s - %s\n", strerror(errno), message);
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            this->enabled = 0;
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            /* Don't try to close - may hang */
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            audio_fd = -1;
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_AUDIO
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            fprintf(stderr, "Done disabling audio\n");
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_AUDIO
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        fprintf(stderr, "Ready!\n");
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Paud_PlayAudio(_THIS)
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int written;
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Write the audio data, checking for EAGAIN on broken audio drivers */
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	do {
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		written = write(audio_fd, mixbuf, mixlen);
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( (written < 0) && ((errno == 0) || (errno == EAGAIN)) ) {
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_Delay(1);	/* Let a little CPU time go by */
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} while ( (written < 0) &&
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	          ((errno == 0) || (errno == EAGAIN) || (errno == EINTR)) );
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* If timer synchronization is enabled, set the next write frame */
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( frame_ticks ) {
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		next_frame += frame_ticks;
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* If we couldn't write, assume fatal error for now */
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( written < 0 ) {
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->enabled = 0;
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_AUDIO
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fprintf(stderr, "Wrote %d bytes of audio data\n", written);
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Uint8 *Paud_GetAudioBuf(_THIS)
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return mixbuf;
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Paud_CloseAudio(_THIS)
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( mixbuf != NULL ) {
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_FreeAudioMem(mixbuf);
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		mixbuf = NULL;
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( audio_fd >= 0 ) {
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		close(audio_fd);
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		audio_fd = -1;
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int Paud_OpenAudio(_THIS, SDL_AudioSpec *spec)
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	char          audiodev[1024];
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int           format;
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int           bytes_per_sample;
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint16        test_format;
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	audio_init    paud_init;
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	audio_buffer  paud_bufinfo;
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	audio_status  paud_status;
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	audio_control paud_control;
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	audio_change  paud_change;
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Reset the timer synchronization flag */
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	frame_ticks = 0.0;
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Open the audio device */
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	audio_fd = SDL_OpenAudioPath(audiodev, sizeof(audiodev), OPEN_FLAGS, 0);
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( audio_fd < 0 ) {
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Couldn't open %s: %s", audiodev, strerror(errno));
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/*
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * We can't set the buffer size - just ask the device for the maximum
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * that we can have.
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 */
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ioctl(audio_fd, AUDIO_BUFFER, &paud_bufinfo) < 0 ) {
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Couldn't get audio buffer information");
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mixbuf = NULL;
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( spec->channels > 1 )
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    spec->channels = 2;
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	else
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    spec->channels = 1;
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/*
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * Fields in the audio_init structure:
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 *
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * Ignored by us:
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 *
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * paud.loadpath[LOAD_PATH]; * DSP code to load, MWave chip only?
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * paud.slot_number;         * slot number of the adapter
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * paud.device_id;           * adapter identification number
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 *
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * Input:
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 *
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * paud.srate;           * the sampling rate in Hz
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * paud.bits_per_sample; * 8, 16, 32, ...
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * paud.bsize;           * block size for this rate
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * paud.mode;            * ADPCM, PCM, MU_LAW, A_LAW, SOURCE_MIX
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * paud.channels;        * 1=mono, 2=stereo
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * paud.flags;           * FIXED - fixed length data
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 *                       * LEFT_ALIGNED, RIGHT_ALIGNED (var len only)
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 *                       * TWOS_COMPLEMENT - 2's complement data
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 *                       * SIGNED - signed? comment seems wrong in sys/audio.h
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 *                       * BIG_ENDIAN
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * paud.operation;       * PLAY, RECORD
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 *
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * Output:
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 *
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * paud.flags;           * PITCH            - pitch is supported
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 *                       * INPUT            - input is supported
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 *                       * OUTPUT           - output is supported
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 *                       * MONITOR          - monitor is supported
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 *                       * VOLUME           - volume is supported
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 *                       * VOLUME_DELAY     - volume delay is supported
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 *                       * BALANCE          - balance is supported
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 *                       * BALANCE_DELAY    - balance delay is supported
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 *                       * TREBLE           - treble control is supported
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 *                       * BASS             - bass control is supported
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 *                       * BESTFIT_PROVIDED - best fit returned
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 *                       * LOAD_CODE        - DSP load needed
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * paud.rc;              * NO_PLAY         - DSP code can't do play requests
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 *                       * NO_RECORD       - DSP code can't do record requests
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 *                       * INVALID_REQUEST - request was invalid
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 *                       * CONFLICT        - conflict with open's flags
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 *                       * OVERLOADED      - out of DSP MIPS or memory
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * paud.position_resolution; * smallest increment for position
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 */
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        paud_init.srate = spec->freq;
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	paud_init.mode = PCM;
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	paud_init.operation = PLAY;
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	paud_init.channels = spec->channels;
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Try for a closest match on audio format */
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	format = 0;
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( test_format = SDL_FirstAudioFormat(spec->format);
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						! format && test_format; ) {
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_AUDIO
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		switch ( test_format ) {
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case AUDIO_U8:
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    bytes_per_sample = 1;
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    paud_init.bits_per_sample = 8;
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    paud_init.flags = TWOS_COMPLEMENT | FIXED;
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    format = 1;
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    break;
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case AUDIO_S8:
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    bytes_per_sample = 1;
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    paud_init.bits_per_sample = 8;
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    paud_init.flags = SIGNED |
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					      TWOS_COMPLEMENT | FIXED;
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    format = 1;
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    break;
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case AUDIO_S16LSB:
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    bytes_per_sample = 2;
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    paud_init.bits_per_sample = 16;
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    paud_init.flags = SIGNED |
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					      TWOS_COMPLEMENT | FIXED;
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    format = 1;
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    break;
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case AUDIO_S16MSB:
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    bytes_per_sample = 2;
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    paud_init.bits_per_sample = 16;
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    paud_init.flags = BIG_ENDIAN |
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					      SIGNED |
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					      TWOS_COMPLEMENT | FIXED;
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    format = 1;
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    break;
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case AUDIO_U16LSB:
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    bytes_per_sample = 2;
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    paud_init.bits_per_sample = 16;
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    paud_init.flags = TWOS_COMPLEMENT | FIXED;
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    format = 1;
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    break;
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case AUDIO_U16MSB:
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    bytes_per_sample = 2;
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    paud_init.bits_per_sample = 16;
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    paud_init.flags = BIG_ENDIAN |
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					      TWOS_COMPLEMENT | FIXED;
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    format = 1;
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    break;
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			default:
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( ! format ) {
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			test_format = SDL_NextAudioFormat();
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( format == 0 ) {
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_AUDIO
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            fprintf(stderr, "Couldn't find any hardware audio formats\n");
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    SDL_SetError("Couldn't find any hardware audio formats");
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    return -1;
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	spec->format = test_format;
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/*
3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * We know the buffer size and the max number of subsequent writes
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * that can be pending. If more than one can pend, allow the application
3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * to do something like double buffering between our write buffer and
3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * the device's own buffer that we are filling with write() anyway.
3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 *
3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * We calculate spec->samples like this because SDL_CalculateAudioSpec()
3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * will give put paud_bufinfo.write_buf_cap (or paud_bufinfo.write_buf_cap/2)
3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * into spec->size in return.
3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 */
3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( paud_bufinfo.request_buf_cap == 1 )
3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    spec->samples = paud_bufinfo.write_buf_cap
3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  / bytes_per_sample
4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  / spec->channels;
4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	else
4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    spec->samples = paud_bufinfo.write_buf_cap
4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  / bytes_per_sample
4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  / spec->channels
4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  / 2;
4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        paud_init.bsize = bytes_per_sample * spec->channels;
4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CalculateAudioSpec(spec);
4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/*
4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * The AIX paud device init can't modify the values of the audio_init
4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * structure that we pass to it. So we don't need any recalculation
4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * of this stuff and no reinit call as in linux dsp and dma code.
4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 *
4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * /dev/paud supports all of the encoding formats, so we don't need
4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * to do anything like reopening the device, either.
4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 */
4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ioctl(audio_fd, AUDIO_INIT, &paud_init) < 0 ) {
4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    switch ( paud_init.rc )
4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    {
4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case 1 :
4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Couldn't set audio format: DSP can't do play requests");
4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case 2 :
4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Couldn't set audio format: DSP can't do record requests");
4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case 4 :
4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Couldn't set audio format: request was invalid");
4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case 5 :
4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Couldn't set audio format: conflict with open's flags");
4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    case 6 :
4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Couldn't set audio format: out of DSP MIPS or memory");
4429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
4439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
4449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    default :
4459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Couldn't set audio format: not documented in sys/audio.h");
4469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
4479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		break;
4489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    }
4499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Allocate mixing buffer */
4529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mixlen = spec->size;
4539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen);
4549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( mixbuf == NULL ) {
4559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
4569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(mixbuf, spec->silence, spec->size);
4589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/*
4609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * Set some paramters: full volume, first speaker that we can find.
4619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * Ignore the other settings for now.
4629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 */
4639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	paud_change.input = AUDIO_IGNORE;         /* the new input source */
4649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        paud_change.output = OUTPUT_1;            /* EXTERNAL_SPEAKER,INTERNAL_SPEAKER,OUTPUT_1 */
4659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        paud_change.monitor = AUDIO_IGNORE;       /* the new monitor state */
4669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        paud_change.volume = 0x7fffffff;          /* volume level [0-0x7fffffff] */
4679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        paud_change.volume_delay = AUDIO_IGNORE;  /* the new volume delay */
4689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        paud_change.balance = 0x3fffffff;         /* the new balance */
4699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        paud_change.balance_delay = AUDIO_IGNORE; /* the new balance delay */
4709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        paud_change.treble = AUDIO_IGNORE;        /* the new treble state */
4719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        paud_change.bass = AUDIO_IGNORE;          /* the new bass state */
4729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        paud_change.pitch = AUDIO_IGNORE;         /* the new pitch state */
4739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	paud_control.ioctl_request = AUDIO_CHANGE;
4759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	paud_control.request_info = (char*)&paud_change;
4769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ioctl(audio_fd, AUDIO_CONTROL, &paud_control) < 0 ) {
4779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_AUDIO
4789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            fprintf(stderr, "Can't change audio display settings\n" );
4799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/*
4839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * Tell the device to expect data. Actual start will wait for
4849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * the first write() call.
4859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 */
4869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	paud_control.ioctl_request = AUDIO_START;
4879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	paud_control.position = 0;
4889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ioctl(audio_fd, AUDIO_CONTROL, &paud_control) < 0 ) {
4899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_AUDIO
4909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            fprintf(stderr, "Can't start audio play\n" );
4919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    SDL_SetError("Can't start audio play");
4939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    return -1;
4949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        /* Check to see if we need to use select() workaround */
4979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        { char *workaround;
4989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                workaround = SDL_getenv("SDL_DSP_NOSELECT");
4999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                if ( workaround ) {
5009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        frame_ticks = (float)(spec->samples*1000)/spec->freq;
5019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        next_frame = SDL_GetTicks()+frame_ticks;
5029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                }
5039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
5049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Get the parent process id (we're the parent of the audio thread) */
5069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	parent = getpid();
5079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* We're ready to rock and roll. :-) */
5099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 0;
5109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
512