src/audio/arts/SDL_artsaudio.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 06 Mar 2002 11:23:08 +0000
changeset 297 f6ffac90895c
parent 294 d2d48e10f370
child 301 fb4c4c6a2773
permissions -rw-r--r--
Updated copyright information for 2002
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@297
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 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@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifdef SAVE_RCSID
slouken@0
    24
static char rcsid =
slouken@0
    25
 "@(#) $Id$";
slouken@0
    26
#endif
slouken@0
    27
slouken@0
    28
/* Allow access to a raw mixing buffer */
slouken@0
    29
slouken@0
    30
#include <sys/types.h>
slouken@0
    31
#include <stdlib.h>
slouken@0
    32
#include <stdio.h>
slouken@0
    33
#include <string.h>
slouken@0
    34
#include <errno.h>
slouken@0
    35
#include <signal.h>
slouken@0
    36
#include <unistd.h>
slouken@0
    37
slouken@0
    38
#include "SDL_audio.h"
slouken@0
    39
#include "SDL_error.h"
slouken@0
    40
#include "SDL_audiomem.h"
slouken@0
    41
#include "SDL_audio_c.h"
slouken@0
    42
#include "SDL_timer.h"
slouken@0
    43
#include "SDL_audiodev_c.h"
slouken@0
    44
#include "SDL_artsaudio.h"
slouken@0
    45
slouken@294
    46
#ifdef ARTSC_DYNAMIC
slouken@294
    47
#include "SDL_name.h"
slouken@294
    48
#include "SDL_loadso.h"
slouken@294
    49
#else
slouken@294
    50
#define SDL_NAME(X)	X
slouken@294
    51
#endif
slouken@294
    52
slouken@0
    53
/* The tag name used by artsc audio */
slouken@0
    54
#define ARTSC_DRIVER_NAME         "artsc"
slouken@0
    55
slouken@0
    56
/* Audio driver functions */
slouken@0
    57
static int ARTSC_OpenAudio(_THIS, SDL_AudioSpec *spec);
slouken@0
    58
static void ARTSC_WaitAudio(_THIS);
slouken@0
    59
static void ARTSC_PlayAudio(_THIS);
slouken@0
    60
static Uint8 *ARTSC_GetAudioBuf(_THIS);
slouken@0
    61
static void ARTSC_CloseAudio(_THIS);
slouken@0
    62
slouken@294
    63
#ifdef ARTSC_DYNAMIC
slouken@294
    64
slouken@294
    65
static const char *arts_library = ARTSC_DYNAMIC;
slouken@294
    66
static void *arts_handle = NULL;
slouken@294
    67
static int arts_loaded = 0;
slouken@294
    68
slouken@294
    69
static int (*SDL_NAME(arts_init))();
slouken@294
    70
static int (*SDL_NAME(arts_free))();
slouken@294
    71
static int (*SDL_NAME(arts_play_stream))();
slouken@294
    72
static int (*SDL_NAME(arts_stream_set))();
slouken@294
    73
static int (*SDL_NAME(arts_stream_get))();
slouken@294
    74
static int (*SDL_NAME(arts_write))();
slouken@294
    75
static int (*SDL_NAME(arts_close_stream))();
slouken@294
    76
static struct {
slouken@294
    77
	const char *name;
slouken@294
    78
	void **func;
slouken@294
    79
} arts_functions[] = {
slouken@294
    80
	{ "arts_init",		(void **)&SDL_NAME(arts_init)		},
slouken@294
    81
	{ "arts_free",		(void **)&SDL_NAME(arts_free)		},
slouken@294
    82
	{ "arts_play_stream",	(void **)&SDL_NAME(arts_play_stream)	},
slouken@294
    83
	{ "arts_stream_set",	(void **)&SDL_NAME(arts_stream_set)	},
slouken@294
    84
	{ "arts_stream_get",	(void **)&SDL_NAME(arts_stream_get)	},
slouken@294
    85
	{ "arts_write",		(void **)&SDL_NAME(arts_write)		},
slouken@294
    86
	{ "arts_close_stream",	(void **)&SDL_NAME(arts_close_stream)	},
slouken@294
    87
};
slouken@294
    88
slouken@294
    89
static void UnloadARTSLibrary()
slouken@294
    90
{
slouken@294
    91
	if ( arts_loaded ) {
slouken@294
    92
		SDL_UnloadObject(arts_handle);
slouken@294
    93
		arts_handle = NULL;
slouken@294
    94
		arts_loaded = 0;
slouken@294
    95
	}
slouken@294
    96
}
slouken@294
    97
slouken@294
    98
static int LoadARTSLibrary(void)
slouken@294
    99
{
slouken@294
   100
	int i, retval = -1;
slouken@294
   101
slouken@294
   102
	arts_handle = SDL_LoadObject(arts_library);
slouken@294
   103
	if ( arts_handle ) {
slouken@294
   104
		arts_loaded = 1;
slouken@294
   105
		retval = 0;
slouken@294
   106
		for ( i=0; i<SDL_TABLESIZE(arts_functions); ++i ) {
slouken@294
   107
			*arts_functions[i].func = SDL_LoadFunction(arts_handle, arts_functions[i].name);
slouken@294
   108
			if ( ! arts_functions[i].func ) {
slouken@294
   109
				retval = -1;
slouken@294
   110
				UnloadARTSLibrary();
slouken@294
   111
				break;
slouken@294
   112
			}
slouken@294
   113
		}
slouken@294
   114
	}
slouken@294
   115
	return retval;
slouken@294
   116
}
slouken@294
   117
slouken@294
   118
#else
slouken@294
   119
slouken@294
   120
static void UnloadARTSLibrary()
slouken@294
   121
{
slouken@294
   122
	return;
slouken@294
   123
}
slouken@294
   124
slouken@294
   125
static int LoadARTSLibrary(void)
slouken@294
   126
{
slouken@294
   127
	return 0;
slouken@294
   128
}
slouken@294
   129
slouken@294
   130
#endif /* ARTSC_DYNAMIC */
slouken@294
   131
slouken@0
   132
/* Audio driver bootstrap functions */
slouken@0
   133
slouken@0
   134
static int Audio_Available(void)
slouken@0
   135
{
slouken@294
   136
	int available = 0;
slouken@294
   137
slouken@294
   138
	if ( LoadARTSLibrary() < 0 ) {
slouken@294
   139
		return available;
slouken@294
   140
	}
slouken@294
   141
	if ( SDL_NAME(arts_init)() == 0 ) {
slouken@294
   142
		available = 1;
slouken@294
   143
		SDL_NAME(arts_free)();
slouken@294
   144
	}
slouken@294
   145
	UnloadARTSLibrary();
slouken@0
   146
}
slouken@0
   147
slouken@0
   148
static void Audio_DeleteDevice(SDL_AudioDevice *device)
slouken@0
   149
{
slouken@0
   150
	free(device->hidden);
slouken@0
   151
	free(device);
slouken@294
   152
	UnloadARTSLibrary();
slouken@0
   153
}
slouken@0
   154
slouken@0
   155
static SDL_AudioDevice *Audio_CreateDevice(int devindex)
slouken@0
   156
{
slouken@0
   157
	SDL_AudioDevice *this;
slouken@0
   158
slouken@0
   159
	/* Initialize all variables that we clean on shutdown */
slouken@294
   160
	LoadARTSLibrary();
slouken@0
   161
	this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice));
slouken@0
   162
	if ( this ) {
slouken@0
   163
		memset(this, 0, (sizeof *this));
slouken@0
   164
		this->hidden = (struct SDL_PrivateAudioData *)
slouken@0
   165
				malloc((sizeof *this->hidden));
slouken@0
   166
	}
slouken@0
   167
	if ( (this == NULL) || (this->hidden == NULL) ) {
slouken@0
   168
		SDL_OutOfMemory();
slouken@0
   169
		if ( this ) {
slouken@0
   170
			free(this);
slouken@0
   171
		}
slouken@0
   172
		return(0);
slouken@0
   173
	}
slouken@0
   174
	memset(this->hidden, 0, (sizeof *this->hidden));
slouken@0
   175
	stream = 0;
slouken@0
   176
slouken@0
   177
	/* Set the function pointers */
slouken@0
   178
	this->OpenAudio = ARTSC_OpenAudio;
slouken@0
   179
	this->WaitAudio = ARTSC_WaitAudio;
slouken@0
   180
	this->PlayAudio = ARTSC_PlayAudio;
slouken@0
   181
	this->GetAudioBuf = ARTSC_GetAudioBuf;
slouken@0
   182
	this->CloseAudio = ARTSC_CloseAudio;
slouken@0
   183
slouken@0
   184
	this->free = Audio_DeleteDevice;
slouken@0
   185
slouken@0
   186
	return this;
slouken@0
   187
}
slouken@0
   188
slouken@0
   189
AudioBootStrap ARTSC_bootstrap = {
slouken@0
   190
	ARTSC_DRIVER_NAME, "Analog Realtime Synthesizer",
slouken@0
   191
	Audio_Available, Audio_CreateDevice
slouken@0
   192
};
slouken@0
   193
slouken@0
   194
/* This function waits until it is possible to write a full sound buffer */
slouken@0
   195
static void ARTSC_WaitAudio(_THIS)
slouken@0
   196
{
slouken@0
   197
	Sint32 ticks;
slouken@0
   198
slouken@0
   199
	/* Check to see if the thread-parent process is still alive */
slouken@0
   200
	{ static int cnt = 0;
slouken@0
   201
		/* Note that this only works with thread implementations 
slouken@0
   202
		   that use a different process id for each thread.
slouken@0
   203
		*/
slouken@0
   204
		if (parent && (((++cnt)%10) == 0)) { /* Check every 10 loops */
slouken@0
   205
			if ( kill(parent, 0) < 0 ) {
slouken@0
   206
				this->enabled = 0;
slouken@0
   207
			}
slouken@0
   208
		}
slouken@0
   209
	}
slouken@0
   210
slouken@0
   211
	/* Use timer for general audio synchronization */
slouken@0
   212
	ticks = ((Sint32)(next_frame - SDL_GetTicks()))-FUDGE_TICKS;
slouken@0
   213
	if ( ticks > 0 ) {
slouken@0
   214
		SDL_Delay(ticks);
slouken@0
   215
	}
slouken@0
   216
}
slouken@0
   217
slouken@0
   218
static void ARTSC_PlayAudio(_THIS)
slouken@0
   219
{
slouken@0
   220
	int written;
slouken@0
   221
slouken@0
   222
	/* Write the audio data */
slouken@294
   223
	written = SDL_NAME(arts_write)(stream, mixbuf, mixlen);
slouken@0
   224
	
slouken@0
   225
	/* If timer synchronization is enabled, set the next write frame */
slouken@0
   226
	if ( frame_ticks ) {
slouken@0
   227
		next_frame += frame_ticks;
slouken@0
   228
	}
slouken@0
   229
slouken@0
   230
	/* If we couldn't write, assume fatal error for now */
slouken@0
   231
	if ( written < 0 ) {
slouken@0
   232
		this->enabled = 0;
slouken@0
   233
	}
slouken@0
   234
#ifdef DEBUG_AUDIO
slouken@0
   235
	fprintf(stderr, "Wrote %d bytes of audio data\n", written);
slouken@0
   236
#endif
slouken@0
   237
}
slouken@0
   238
slouken@0
   239
static Uint8 *ARTSC_GetAudioBuf(_THIS)
slouken@0
   240
{
slouken@0
   241
	return(mixbuf);
slouken@0
   242
}
slouken@0
   243
slouken@0
   244
static void ARTSC_CloseAudio(_THIS)
slouken@0
   245
{
slouken@0
   246
	if ( mixbuf != NULL ) {
slouken@0
   247
		SDL_FreeAudioMem(mixbuf);
slouken@0
   248
		mixbuf = NULL;
slouken@0
   249
	}
slouken@0
   250
	if ( stream ) {
slouken@294
   251
		SDL_NAME(arts_close_stream)(stream);
slouken@0
   252
		stream = 0;
slouken@0
   253
	}
slouken@294
   254
	SDL_NAME(arts_free)();
slouken@0
   255
}
slouken@0
   256
slouken@0
   257
static int ARTSC_OpenAudio(_THIS, SDL_AudioSpec *spec)
slouken@0
   258
{
slouken@0
   259
	int bits, frag_spec;
slouken@0
   260
	Uint16 test_format, format;
slouken@0
   261
slouken@0
   262
	/* Reset the timer synchronization flag */
slouken@0
   263
	frame_ticks = 0.0;
slouken@0
   264
slouken@0
   265
	mixbuf = NULL;
slouken@0
   266
slouken@0
   267
	/* Try for a closest match on audio format */
slouken@0
   268
	format = 0;
slouken@0
   269
	bits = 0;
slouken@0
   270
	for ( test_format = SDL_FirstAudioFormat(spec->format);
slouken@0
   271
						! format && test_format; ) {
slouken@0
   272
#ifdef DEBUG_AUDIO
slouken@0
   273
		fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
slouken@0
   274
#endif
slouken@0
   275
		switch ( test_format ) {
slouken@0
   276
			case AUDIO_U8:
slouken@0
   277
				bits = 8;
slouken@0
   278
				format = 1;
slouken@0
   279
				break;
slouken@0
   280
			case AUDIO_S16LSB:
slouken@0
   281
				bits = 16;
slouken@0
   282
				format = 1;
slouken@0
   283
				break;
slouken@0
   284
			default:
slouken@0
   285
				format = 0;
slouken@0
   286
				break;
slouken@0
   287
		}
slouken@0
   288
		if ( ! format ) {
slouken@0
   289
			test_format = SDL_NextAudioFormat();
slouken@0
   290
		}
slouken@0
   291
	}
slouken@0
   292
	if ( format == 0 ) {
slouken@0
   293
		SDL_SetError("Couldn't find any hardware audio formats");
slouken@0
   294
		return(-1);
slouken@0
   295
	}
slouken@0
   296
	spec->format = test_format;
slouken@0
   297
slouken@294
   298
	if ( SDL_NAME(arts_init)() != 0 ) {
slouken@294
   299
		SDL_SetError("Unable to initialize ARTS");
slouken@294
   300
		return(-1);
slouken@294
   301
	}
slouken@294
   302
	stream = SDL_NAME(arts_play_stream)(spec->freq, bits, spec->channels, "SDL");
slouken@0
   303
slouken@0
   304
	/* Calculate the final parameters for this audio specification */
slouken@0
   305
	SDL_CalculateAudioSpec(spec);
slouken@0
   306
slouken@0
   307
	/* Determine the power of two of the fragment size */
slouken@0
   308
	for ( frag_spec = 0; (0x01<<frag_spec) < spec->size; ++frag_spec );
slouken@0
   309
	if ( (0x01<<frag_spec) != spec->size ) {
slouken@0
   310
		SDL_SetError("Fragment size must be a power of two");
slouken@0
   311
		return(-1);
slouken@0
   312
	}
slouken@0
   313
	frag_spec |= 0x00020000;	/* two fragments, for low latency */
slouken@0
   314
slouken@0
   315
#ifdef ARTS_P_PACKET_SETTINGS
slouken@294
   316
	SDL_NAME(arts_stream_set)(stream, ARTS_P_PACKET_SETTINGS, frag_spec);
slouken@0
   317
#else
slouken@294
   318
	SDL_NAME(arts_stream_set)(stream, ARTS_P_PACKET_SIZE, frag_spec&0xffff);
slouken@294
   319
	SDL_NAME(arts_stream_set)(stream, ARTS_P_PACKET_COUNT, frag_spec>>16);
slouken@0
   320
#endif
slouken@294
   321
	spec->size = SDL_NAME(arts_stream_get)(stream, ARTS_P_PACKET_SIZE);
slouken@0
   322
slouken@0
   323
	/* Allocate mixing buffer */
slouken@0
   324
	mixlen = spec->size;
slouken@0
   325
	mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen);
slouken@0
   326
	if ( mixbuf == NULL ) {
slouken@0
   327
		return(-1);
slouken@0
   328
	}
slouken@0
   329
	memset(mixbuf, spec->silence, spec->size);
slouken@0
   330
slouken@0
   331
	/* Get the parent process id (we're the parent of the audio thread) */
slouken@0
   332
	parent = getpid();
slouken@0
   333
slouken@0
   334
	/* We're ready to rock and roll. :-) */
slouken@0
   335
	return(0);
slouken@0
   336
}