src/audio/arts/SDL_artsaudio.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 28 Aug 2002 23:25:09 +0000
changeset 474 583a07ab5444
parent 301 fb4c4c6a2773
child 769 b8d311d90021
permissions -rw-r--r--
Doh! Patch by Joel Ray Holveck
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@301
    69
static int (*SDL_NAME(arts_init))(void);
slouken@301
    70
static void (*SDL_NAME(arts_free))(void);
slouken@301
    71
static arts_stream_t (*SDL_NAME(arts_play_stream))(int rate, int bits, int channels, const char *name);
slouken@301
    72
static int (*SDL_NAME(arts_stream_set))(arts_stream_t s, arts_parameter_t param, int value);
slouken@301
    73
static int (*SDL_NAME(arts_stream_get))(arts_stream_t s, arts_parameter_t param);
slouken@301
    74
static int (*SDL_NAME(arts_write))(arts_stream_t s, const void *buffer, int count);
slouken@301
    75
static void (*SDL_NAME(arts_close_stream))(arts_stream_t s);
slouken@301
    76
slouken@294
    77
static struct {
slouken@294
    78
	const char *name;
slouken@294
    79
	void **func;
slouken@294
    80
} arts_functions[] = {
slouken@294
    81
	{ "arts_init",		(void **)&SDL_NAME(arts_init)		},
slouken@294
    82
	{ "arts_free",		(void **)&SDL_NAME(arts_free)		},
slouken@294
    83
	{ "arts_play_stream",	(void **)&SDL_NAME(arts_play_stream)	},
slouken@294
    84
	{ "arts_stream_set",	(void **)&SDL_NAME(arts_stream_set)	},
slouken@294
    85
	{ "arts_stream_get",	(void **)&SDL_NAME(arts_stream_get)	},
slouken@294
    86
	{ "arts_write",		(void **)&SDL_NAME(arts_write)		},
slouken@294
    87
	{ "arts_close_stream",	(void **)&SDL_NAME(arts_close_stream)	},
slouken@294
    88
};
slouken@294
    89
slouken@294
    90
static void UnloadARTSLibrary()
slouken@294
    91
{
slouken@294
    92
	if ( arts_loaded ) {
slouken@294
    93
		SDL_UnloadObject(arts_handle);
slouken@294
    94
		arts_handle = NULL;
slouken@294
    95
		arts_loaded = 0;
slouken@294
    96
	}
slouken@294
    97
}
slouken@294
    98
slouken@294
    99
static int LoadARTSLibrary(void)
slouken@294
   100
{
slouken@294
   101
	int i, retval = -1;
slouken@294
   102
slouken@294
   103
	arts_handle = SDL_LoadObject(arts_library);
slouken@294
   104
	if ( arts_handle ) {
slouken@294
   105
		arts_loaded = 1;
slouken@294
   106
		retval = 0;
slouken@294
   107
		for ( i=0; i<SDL_TABLESIZE(arts_functions); ++i ) {
slouken@294
   108
			*arts_functions[i].func = SDL_LoadFunction(arts_handle, arts_functions[i].name);
slouken@294
   109
			if ( ! arts_functions[i].func ) {
slouken@294
   110
				retval = -1;
slouken@294
   111
				UnloadARTSLibrary();
slouken@294
   112
				break;
slouken@294
   113
			}
slouken@294
   114
		}
slouken@294
   115
	}
slouken@294
   116
	return retval;
slouken@294
   117
}
slouken@294
   118
slouken@294
   119
#else
slouken@294
   120
slouken@294
   121
static void UnloadARTSLibrary()
slouken@294
   122
{
slouken@294
   123
	return;
slouken@294
   124
}
slouken@294
   125
slouken@294
   126
static int LoadARTSLibrary(void)
slouken@294
   127
{
slouken@294
   128
	return 0;
slouken@294
   129
}
slouken@294
   130
slouken@294
   131
#endif /* ARTSC_DYNAMIC */
slouken@294
   132
slouken@0
   133
/* Audio driver bootstrap functions */
slouken@0
   134
slouken@0
   135
static int Audio_Available(void)
slouken@0
   136
{
slouken@294
   137
	int available = 0;
slouken@294
   138
slouken@294
   139
	if ( LoadARTSLibrary() < 0 ) {
slouken@294
   140
		return available;
slouken@294
   141
	}
slouken@294
   142
	if ( SDL_NAME(arts_init)() == 0 ) {
slouken@294
   143
		available = 1;
slouken@294
   144
		SDL_NAME(arts_free)();
slouken@294
   145
	}
slouken@294
   146
	UnloadARTSLibrary();
slouken@474
   147
slouken@474
   148
	return available;
slouken@0
   149
}
slouken@0
   150
slouken@0
   151
static void Audio_DeleteDevice(SDL_AudioDevice *device)
slouken@0
   152
{
slouken@0
   153
	free(device->hidden);
slouken@0
   154
	free(device);
slouken@294
   155
	UnloadARTSLibrary();
slouken@0
   156
}
slouken@0
   157
slouken@0
   158
static SDL_AudioDevice *Audio_CreateDevice(int devindex)
slouken@0
   159
{
slouken@0
   160
	SDL_AudioDevice *this;
slouken@0
   161
slouken@0
   162
	/* Initialize all variables that we clean on shutdown */
slouken@294
   163
	LoadARTSLibrary();
slouken@0
   164
	this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice));
slouken@0
   165
	if ( this ) {
slouken@0
   166
		memset(this, 0, (sizeof *this));
slouken@0
   167
		this->hidden = (struct SDL_PrivateAudioData *)
slouken@0
   168
				malloc((sizeof *this->hidden));
slouken@0
   169
	}
slouken@0
   170
	if ( (this == NULL) || (this->hidden == NULL) ) {
slouken@0
   171
		SDL_OutOfMemory();
slouken@0
   172
		if ( this ) {
slouken@0
   173
			free(this);
slouken@0
   174
		}
slouken@0
   175
		return(0);
slouken@0
   176
	}
slouken@0
   177
	memset(this->hidden, 0, (sizeof *this->hidden));
slouken@0
   178
	stream = 0;
slouken@0
   179
slouken@0
   180
	/* Set the function pointers */
slouken@0
   181
	this->OpenAudio = ARTSC_OpenAudio;
slouken@0
   182
	this->WaitAudio = ARTSC_WaitAudio;
slouken@0
   183
	this->PlayAudio = ARTSC_PlayAudio;
slouken@0
   184
	this->GetAudioBuf = ARTSC_GetAudioBuf;
slouken@0
   185
	this->CloseAudio = ARTSC_CloseAudio;
slouken@0
   186
slouken@0
   187
	this->free = Audio_DeleteDevice;
slouken@0
   188
slouken@0
   189
	return this;
slouken@0
   190
}
slouken@0
   191
slouken@0
   192
AudioBootStrap ARTSC_bootstrap = {
slouken@0
   193
	ARTSC_DRIVER_NAME, "Analog Realtime Synthesizer",
slouken@0
   194
	Audio_Available, Audio_CreateDevice
slouken@0
   195
};
slouken@0
   196
slouken@0
   197
/* This function waits until it is possible to write a full sound buffer */
slouken@0
   198
static void ARTSC_WaitAudio(_THIS)
slouken@0
   199
{
slouken@0
   200
	Sint32 ticks;
slouken@0
   201
slouken@0
   202
	/* Check to see if the thread-parent process is still alive */
slouken@0
   203
	{ static int cnt = 0;
slouken@0
   204
		/* Note that this only works with thread implementations 
slouken@0
   205
		   that use a different process id for each thread.
slouken@0
   206
		*/
slouken@0
   207
		if (parent && (((++cnt)%10) == 0)) { /* Check every 10 loops */
slouken@0
   208
			if ( kill(parent, 0) < 0 ) {
slouken@0
   209
				this->enabled = 0;
slouken@0
   210
			}
slouken@0
   211
		}
slouken@0
   212
	}
slouken@0
   213
slouken@0
   214
	/* Use timer for general audio synchronization */
slouken@0
   215
	ticks = ((Sint32)(next_frame - SDL_GetTicks()))-FUDGE_TICKS;
slouken@0
   216
	if ( ticks > 0 ) {
slouken@0
   217
		SDL_Delay(ticks);
slouken@0
   218
	}
slouken@0
   219
}
slouken@0
   220
slouken@0
   221
static void ARTSC_PlayAudio(_THIS)
slouken@0
   222
{
slouken@0
   223
	int written;
slouken@0
   224
slouken@0
   225
	/* Write the audio data */
slouken@294
   226
	written = SDL_NAME(arts_write)(stream, mixbuf, mixlen);
slouken@0
   227
	
slouken@0
   228
	/* If timer synchronization is enabled, set the next write frame */
slouken@0
   229
	if ( frame_ticks ) {
slouken@0
   230
		next_frame += frame_ticks;
slouken@0
   231
	}
slouken@0
   232
slouken@0
   233
	/* If we couldn't write, assume fatal error for now */
slouken@0
   234
	if ( written < 0 ) {
slouken@0
   235
		this->enabled = 0;
slouken@0
   236
	}
slouken@0
   237
#ifdef DEBUG_AUDIO
slouken@0
   238
	fprintf(stderr, "Wrote %d bytes of audio data\n", written);
slouken@0
   239
#endif
slouken@0
   240
}
slouken@0
   241
slouken@0
   242
static Uint8 *ARTSC_GetAudioBuf(_THIS)
slouken@0
   243
{
slouken@0
   244
	return(mixbuf);
slouken@0
   245
}
slouken@0
   246
slouken@0
   247
static void ARTSC_CloseAudio(_THIS)
slouken@0
   248
{
slouken@0
   249
	if ( mixbuf != NULL ) {
slouken@0
   250
		SDL_FreeAudioMem(mixbuf);
slouken@0
   251
		mixbuf = NULL;
slouken@0
   252
	}
slouken@0
   253
	if ( stream ) {
slouken@294
   254
		SDL_NAME(arts_close_stream)(stream);
slouken@0
   255
		stream = 0;
slouken@0
   256
	}
slouken@294
   257
	SDL_NAME(arts_free)();
slouken@0
   258
}
slouken@0
   259
slouken@0
   260
static int ARTSC_OpenAudio(_THIS, SDL_AudioSpec *spec)
slouken@0
   261
{
slouken@0
   262
	int bits, frag_spec;
slouken@0
   263
	Uint16 test_format, format;
slouken@0
   264
slouken@0
   265
	/* Reset the timer synchronization flag */
slouken@0
   266
	frame_ticks = 0.0;
slouken@0
   267
slouken@0
   268
	mixbuf = NULL;
slouken@0
   269
slouken@0
   270
	/* Try for a closest match on audio format */
slouken@0
   271
	format = 0;
slouken@0
   272
	bits = 0;
slouken@0
   273
	for ( test_format = SDL_FirstAudioFormat(spec->format);
slouken@0
   274
						! format && test_format; ) {
slouken@0
   275
#ifdef DEBUG_AUDIO
slouken@0
   276
		fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
slouken@0
   277
#endif
slouken@0
   278
		switch ( test_format ) {
slouken@0
   279
			case AUDIO_U8:
slouken@0
   280
				bits = 8;
slouken@0
   281
				format = 1;
slouken@0
   282
				break;
slouken@0
   283
			case AUDIO_S16LSB:
slouken@0
   284
				bits = 16;
slouken@0
   285
				format = 1;
slouken@0
   286
				break;
slouken@0
   287
			default:
slouken@0
   288
				format = 0;
slouken@0
   289
				break;
slouken@0
   290
		}
slouken@0
   291
		if ( ! format ) {
slouken@0
   292
			test_format = SDL_NextAudioFormat();
slouken@0
   293
		}
slouken@0
   294
	}
slouken@0
   295
	if ( format == 0 ) {
slouken@0
   296
		SDL_SetError("Couldn't find any hardware audio formats");
slouken@0
   297
		return(-1);
slouken@0
   298
	}
slouken@0
   299
	spec->format = test_format;
slouken@0
   300
slouken@294
   301
	if ( SDL_NAME(arts_init)() != 0 ) {
slouken@294
   302
		SDL_SetError("Unable to initialize ARTS");
slouken@294
   303
		return(-1);
slouken@294
   304
	}
slouken@294
   305
	stream = SDL_NAME(arts_play_stream)(spec->freq, bits, spec->channels, "SDL");
slouken@0
   306
slouken@0
   307
	/* Calculate the final parameters for this audio specification */
slouken@0
   308
	SDL_CalculateAudioSpec(spec);
slouken@0
   309
slouken@0
   310
	/* Determine the power of two of the fragment size */
slouken@0
   311
	for ( frag_spec = 0; (0x01<<frag_spec) < spec->size; ++frag_spec );
slouken@0
   312
	if ( (0x01<<frag_spec) != spec->size ) {
slouken@0
   313
		SDL_SetError("Fragment size must be a power of two");
slouken@0
   314
		return(-1);
slouken@0
   315
	}
slouken@0
   316
	frag_spec |= 0x00020000;	/* two fragments, for low latency */
slouken@0
   317
slouken@0
   318
#ifdef ARTS_P_PACKET_SETTINGS
slouken@294
   319
	SDL_NAME(arts_stream_set)(stream, ARTS_P_PACKET_SETTINGS, frag_spec);
slouken@0
   320
#else
slouken@294
   321
	SDL_NAME(arts_stream_set)(stream, ARTS_P_PACKET_SIZE, frag_spec&0xffff);
slouken@294
   322
	SDL_NAME(arts_stream_set)(stream, ARTS_P_PACKET_COUNT, frag_spec>>16);
slouken@0
   323
#endif
slouken@294
   324
	spec->size = SDL_NAME(arts_stream_get)(stream, ARTS_P_PACKET_SIZE);
slouken@0
   325
slouken@0
   326
	/* Allocate mixing buffer */
slouken@0
   327
	mixlen = spec->size;
slouken@0
   328
	mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen);
slouken@0
   329
	if ( mixbuf == NULL ) {
slouken@0
   330
		return(-1);
slouken@0
   331
	}
slouken@0
   332
	memset(mixbuf, spec->silence, spec->size);
slouken@0
   333
slouken@0
   334
	/* Get the parent process id (we're the parent of the audio thread) */
slouken@0
   335
	parent = getpid();
slouken@0
   336
slouken@0
   337
	/* We're ready to rock and roll. :-) */
slouken@0
   338
	return(0);
slouken@0
   339
}