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    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Allow access to a raw mixing buffer */
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef HAVE_SIGNAL_H
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <signal.h>
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <unistd.h>
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_timer.h"
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_audio.h"
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_audiomem.h"
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_audio_c.h"
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_audiodev_c.h"
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_artsaudio.h"
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SDL_AUDIO_DRIVER_ARTS_DYNAMIC
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_name.h"
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_loadso.h"
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define SDL_NAME(X)	X
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The tag name used by artsc audio */
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define ARTS_DRIVER_NAME         "arts"
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Audio driver functions */
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int ARTS_OpenAudio(_THIS, SDL_AudioSpec *spec);
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void ARTS_WaitAudio(_THIS);
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void ARTS_PlayAudio(_THIS);
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Uint8 *ARTS_GetAudioBuf(_THIS);
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void ARTS_CloseAudio(_THIS);
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SDL_AUDIO_DRIVER_ARTS_DYNAMIC
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic const char *arts_library = SDL_AUDIO_DRIVER_ARTS_DYNAMIC;
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void *arts_handle = NULL;
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int arts_loaded = 0;
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int (*SDL_NAME(arts_init))(void);
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void (*SDL_NAME(arts_free))(void);
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic arts_stream_t (*SDL_NAME(arts_play_stream))(int rate, int bits, int channels, const char *name);
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int (*SDL_NAME(arts_stream_set))(arts_stream_t s, arts_parameter_t param, int value);
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int (*SDL_NAME(arts_stream_get))(arts_stream_t s, arts_parameter_t param);
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int (*SDL_NAME(arts_write))(arts_stream_t s, const void *buffer, int count);
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void (*SDL_NAME(arts_close_stream))(arts_stream_t s);
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int (*SDL_NAME(arts_suspend))(void);
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int (*SDL_NAME(arts_suspended))(void);
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic const char *(*SDL_NAME(arts_error_text))(int errorcode);
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic struct {
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	const char *name;
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	void **func;
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} arts_functions[] = {
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "arts_init",		(void **)&SDL_NAME(arts_init)		},
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "arts_free",		(void **)&SDL_NAME(arts_free)		},
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "arts_play_stream",	(void **)&SDL_NAME(arts_play_stream)	},
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "arts_stream_set",	(void **)&SDL_NAME(arts_stream_set)	},
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "arts_stream_get",	(void **)&SDL_NAME(arts_stream_get)	},
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "arts_write",		(void **)&SDL_NAME(arts_write)		},
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "arts_close_stream",	(void **)&SDL_NAME(arts_close_stream)	},
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "arts_suspend",	(void **)&SDL_NAME(arts_suspend)	},
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "arts_suspended",	(void **)&SDL_NAME(arts_suspended)	},
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ "arts_error_text",	(void **)&SDL_NAME(arts_error_text)	},
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void UnloadARTSLibrary()
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( arts_loaded ) {
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_UnloadObject(arts_handle);
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		arts_handle = NULL;
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		arts_loaded = 0;
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int LoadARTSLibrary(void)
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i, retval = -1;
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	arts_handle = SDL_LoadObject(arts_library);
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( arts_handle ) {
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		arts_loaded = 1;
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		retval = 0;
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for ( i=0; i<SDL_arraysize(arts_functions); ++i ) {
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			*arts_functions[i].func = SDL_LoadFunction(arts_handle, arts_functions[i].name);
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( !*arts_functions[i].func ) {
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				retval = -1;
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				UnloadARTSLibrary();
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return retval;
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void UnloadARTSLibrary()
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return;
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int LoadARTSLibrary(void)
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 0;
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_AUDIO_DRIVER_ARTS_DYNAMIC */
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Audio driver bootstrap functions */
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int ARTS_Suspend(void)
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	const Uint32 abortms = SDL_GetTicks() + 3000; /* give up after 3 secs */
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while ( (!SDL_NAME(arts_suspended)()) && (SDL_GetTicks() < abortms) ) {
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_NAME(arts_suspend)() ) {
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return SDL_NAME(arts_suspended)();
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int Audio_Available(void)
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int available = 0;
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( LoadARTSLibrary() < 0 ) {
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return available;
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_NAME(arts_init)() == 0 ) {
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( ARTS_Suspend() ) {
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Play a stream so aRts doesn't crash */
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			arts_stream_t stream2;
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			stream2=SDL_NAME(arts_play_stream)(44100, 16, 2, "SDL");
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_NAME(arts_write)(stream2, "", 0);
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_NAME(arts_close_stream)(stream2);
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			available = 1;
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_NAME(arts_free)();
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	UnloadARTSLibrary();
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return available;
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void Audio_DeleteDevice(SDL_AudioDevice *device)
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(device->hidden);
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(device);
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	UnloadARTSLibrary();
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_AudioDevice *Audio_CreateDevice(int devindex)
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_AudioDevice *this;
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Initialize all variables that we clean on shutdown */
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	LoadARTSLibrary();
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( this ) {
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_memset(this, 0, (sizeof *this));
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->hidden = (struct SDL_PrivateAudioData *)
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_malloc((sizeof *this->hidden));
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (this == NULL) || (this->hidden == NULL) ) {
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( this ) {
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_free(this);
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(0);
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(this->hidden, 0, (sizeof *this->hidden));
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	stream = 0;
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the function pointers */
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->OpenAudio = ARTS_OpenAudio;
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->WaitAudio = ARTS_WaitAudio;
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->PlayAudio = ARTS_PlayAudio;
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->GetAudioBuf = ARTS_GetAudioBuf;
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->CloseAudio = ARTS_CloseAudio;
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	this->free = Audio_DeleteDevice;
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return this;
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallAudioBootStrap ARTS_bootstrap = {
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ARTS_DRIVER_NAME, "Analog Realtime Synthesizer",
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Audio_Available, Audio_CreateDevice
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This function waits until it is possible to write a full sound buffer */
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void ARTS_WaitAudio(_THIS)
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Sint32 ticks;
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Check to see if the thread-parent process is still alive */
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{ static int cnt = 0;
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Note that this only works with thread implementations
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   that use a different process id for each thread.
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*/
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (parent && (((++cnt)%10) == 0)) { /* Check every 10 loops */
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( kill(parent, 0) < 0 ) {
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				this->enabled = 0;
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Use timer for general audio synchronization */
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	ticks = ((Sint32)(next_frame - SDL_GetTicks()))-FUDGE_TICKS;
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ticks > 0 ) {
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Delay(ticks);
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void ARTS_PlayAudio(_THIS)
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int written;
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Write the audio data */
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	written = SDL_NAME(arts_write)(stream, mixbuf, mixlen);
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* If timer synchronization is enabled, set the next write frame */
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( frame_ticks ) {
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		next_frame += frame_ticks;
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* If we couldn't write, assume fatal error for now */
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( written < 0 ) {
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		this->enabled = 0;
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_AUDIO
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	fprintf(stderr, "Wrote %d bytes of audio data\n", written);
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Uint8 *ARTS_GetAudioBuf(_THIS)
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(mixbuf);
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void ARTS_CloseAudio(_THIS)
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( mixbuf != NULL ) {
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_FreeAudioMem(mixbuf);
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		mixbuf = NULL;
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( stream ) {
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_NAME(arts_close_stream)(stream);
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		stream = 0;
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_NAME(arts_free)();
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int ARTS_OpenAudio(_THIS, SDL_AudioSpec *spec)
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int bits, frag_spec;
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint16 test_format, format;
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int error_code;
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Reset the timer synchronization flag */
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	frame_ticks = 0.0;
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mixbuf = NULL;
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Try for a closest match on audio format */
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	format = 0;
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	bits = 0;
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( test_format = SDL_FirstAudioFormat(spec->format);
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						! format && test_format; ) {
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef DEBUG_AUDIO
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		switch ( test_format ) {
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case AUDIO_U8:
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				bits = 8;
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				format = 1;
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case AUDIO_S16LSB:
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				bits = 16;
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				format = 1;
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			default:
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				format = 0;
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( ! format ) {
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			test_format = SDL_NextAudioFormat();
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( format == 0 ) {
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Couldn't find any hardware audio formats");
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	spec->format = test_format;
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	error_code = SDL_NAME(arts_init)();
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( error_code != 0 ) {
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Unable to initialize ARTS: %s", SDL_NAME(arts_error_text)(error_code));
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! ARTS_Suspend() ) {
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("ARTS can not open audio device");
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	stream = SDL_NAME(arts_play_stream)(spec->freq, bits, spec->channels, "SDL");
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Calculate the final parameters for this audio specification */
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_CalculateAudioSpec(spec);
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Determine the power of two of the fragment size */
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( frag_spec = 0; (0x01<<frag_spec) < spec->size; ++frag_spec );
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( (0x01<<frag_spec) != spec->size ) {
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Fragment size must be a power of two");
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	frag_spec |= 0x00020000;	/* two fragments, for low latency */
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef ARTS_P_PACKET_SETTINGS
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_NAME(arts_stream_set)(stream, ARTS_P_PACKET_SETTINGS, frag_spec);
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_NAME(arts_stream_set)(stream, ARTS_P_PACKET_SIZE, frag_spec&0xffff);
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_NAME(arts_stream_set)(stream, ARTS_P_PACKET_COUNT, frag_spec>>16);
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	spec->size = SDL_NAME(arts_stream_get)(stream, ARTS_P_PACKET_SIZE);
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Allocate mixing buffer */
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mixlen = spec->size;
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen);
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( mixbuf == NULL ) {
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(-1);
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(mixbuf, spec->silence, spec->size);
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Get the parent process id (we're the parent of the audio thread) */
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	parent = getpid();
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* We're ready to rock and roll. :-) */
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
363