src/audio/arts/SDL_artsaudio.c
author Ryan C. Gordon
Mon, 02 Jan 2012 15:07:26 -0500
branchSDL-1.2
changeset 6151 331f27f01cdb
parent 6137 4720145f848b
permissions -rw-r--r--
Use arts_suspend() in a loop to wait for arts to become ready.

Fixes Bugzilla #372.

Thanks to Patrice Mandin for the patch!
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@6137
     3
    Copyright (C) 1997-2012 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* Allow access to a raw mixing buffer */
slouken@0
    25
icculus@4015
    26
#ifdef HAVE_SIGNAL_H
icculus@4015
    27
#include <signal.h>
icculus@4015
    28
#endif
icculus@4015
    29
#include <unistd.h>
icculus@4015
    30
slouken@1358
    31
#include "SDL_timer.h"
slouken@0
    32
#include "SDL_audio.h"
slouken@1361
    33
#include "../SDL_audiomem.h"
slouken@1361
    34
#include "../SDL_audio_c.h"
slouken@1361
    35
#include "../SDL_audiodev_c.h"
slouken@0
    36
#include "SDL_artsaudio.h"
slouken@0
    37
slouken@1361
    38
#ifdef SDL_AUDIO_DRIVER_ARTS_DYNAMIC
slouken@294
    39
#include "SDL_name.h"
slouken@294
    40
#include "SDL_loadso.h"
slouken@294
    41
#else
slouken@294
    42
#define SDL_NAME(X)	X
slouken@294
    43
#endif
slouken@294
    44
slouken@0
    45
/* The tag name used by artsc audio */
slouken@1361
    46
#define ARTS_DRIVER_NAME         "arts"
slouken@0
    47
slouken@0
    48
/* Audio driver functions */
slouken@1361
    49
static int ARTS_OpenAudio(_THIS, SDL_AudioSpec *spec);
slouken@1361
    50
static void ARTS_WaitAudio(_THIS);
slouken@1361
    51
static void ARTS_PlayAudio(_THIS);
slouken@1361
    52
static Uint8 *ARTS_GetAudioBuf(_THIS);
slouken@1361
    53
static void ARTS_CloseAudio(_THIS);
slouken@0
    54
slouken@1361
    55
#ifdef SDL_AUDIO_DRIVER_ARTS_DYNAMIC
slouken@294
    56
slouken@1361
    57
static const char *arts_library = SDL_AUDIO_DRIVER_ARTS_DYNAMIC;
slouken@294
    58
static void *arts_handle = NULL;
slouken@294
    59
static int arts_loaded = 0;
slouken@294
    60
slouken@301
    61
static int (*SDL_NAME(arts_init))(void);
slouken@301
    62
static void (*SDL_NAME(arts_free))(void);
slouken@301
    63
static arts_stream_t (*SDL_NAME(arts_play_stream))(int rate, int bits, int channels, const char *name);
slouken@301
    64
static int (*SDL_NAME(arts_stream_set))(arts_stream_t s, arts_parameter_t param, int value);
slouken@301
    65
static int (*SDL_NAME(arts_stream_get))(arts_stream_t s, arts_parameter_t param);
slouken@301
    66
static int (*SDL_NAME(arts_write))(arts_stream_t s, const void *buffer, int count);
slouken@301
    67
static void (*SDL_NAME(arts_close_stream))(arts_stream_t s);
icculus@6151
    68
static int (*SDL_NAME(arts_suspend))(void);
icculus@3959
    69
static int (*SDL_NAME(arts_suspended))(void);
icculus@3959
    70
static const char *(*SDL_NAME(arts_error_text))(int errorcode);
slouken@301
    71
slouken@294
    72
static struct {
slouken@294
    73
	const char *name;
slouken@294
    74
	void **func;
slouken@294
    75
} arts_functions[] = {
slouken@294
    76
	{ "arts_init",		(void **)&SDL_NAME(arts_init)		},
slouken@294
    77
	{ "arts_free",		(void **)&SDL_NAME(arts_free)		},
slouken@294
    78
	{ "arts_play_stream",	(void **)&SDL_NAME(arts_play_stream)	},
slouken@294
    79
	{ "arts_stream_set",	(void **)&SDL_NAME(arts_stream_set)	},
slouken@294
    80
	{ "arts_stream_get",	(void **)&SDL_NAME(arts_stream_get)	},
slouken@294
    81
	{ "arts_write",		(void **)&SDL_NAME(arts_write)		},
slouken@294
    82
	{ "arts_close_stream",	(void **)&SDL_NAME(arts_close_stream)	},
icculus@6151
    83
	{ "arts_suspend",	(void **)&SDL_NAME(arts_suspend)	},
icculus@3959
    84
	{ "arts_suspended",	(void **)&SDL_NAME(arts_suspended)	},
icculus@3959
    85
	{ "arts_error_text",	(void **)&SDL_NAME(arts_error_text)	},
slouken@294
    86
};
slouken@294
    87
slouken@294
    88
static void UnloadARTSLibrary()
slouken@294
    89
{
slouken@294
    90
	if ( arts_loaded ) {
slouken@294
    91
		SDL_UnloadObject(arts_handle);
slouken@294
    92
		arts_handle = NULL;
slouken@294
    93
		arts_loaded = 0;
slouken@294
    94
	}
slouken@294
    95
}
slouken@294
    96
slouken@294
    97
static int LoadARTSLibrary(void)
slouken@294
    98
{
slouken@294
    99
	int i, retval = -1;
slouken@294
   100
slouken@294
   101
	arts_handle = SDL_LoadObject(arts_library);
slouken@294
   102
	if ( arts_handle ) {
slouken@294
   103
		arts_loaded = 1;
slouken@294
   104
		retval = 0;
slouken@1379
   105
		for ( i=0; i<SDL_arraysize(arts_functions); ++i ) {
slouken@294
   106
			*arts_functions[i].func = SDL_LoadFunction(arts_handle, arts_functions[i].name);
slouken@864
   107
			if ( !*arts_functions[i].func ) {
slouken@294
   108
				retval = -1;
slouken@294
   109
				UnloadARTSLibrary();
slouken@294
   110
				break;
slouken@294
   111
			}
slouken@294
   112
		}
slouken@294
   113
	}
slouken@294
   114
	return retval;
slouken@294
   115
}
slouken@294
   116
slouken@294
   117
#else
slouken@294
   118
slouken@294
   119
static void UnloadARTSLibrary()
slouken@294
   120
{
slouken@294
   121
	return;
slouken@294
   122
}
slouken@294
   123
slouken@294
   124
static int LoadARTSLibrary(void)
slouken@294
   125
{
slouken@294
   126
	return 0;
slouken@294
   127
}
slouken@294
   128
slouken@1361
   129
#endif /* SDL_AUDIO_DRIVER_ARTS_DYNAMIC */
slouken@294
   130
slouken@0
   131
/* Audio driver bootstrap functions */
slouken@0
   132
icculus@6151
   133
static int ARTS_Suspend(void)
icculus@6151
   134
{
icculus@6151
   135
	const Uint32 abortms = SDL_GetTicks() + 3000; /* give up after 3 secs */
icculus@6151
   136
	while ( (!SDL_NAME(arts_suspended)()) && (SDL_GetTicks() < abortms) ) {
icculus@6151
   137
		if ( SDL_NAME(arts_suspend)() ) {
icculus@6151
   138
			break;
icculus@6151
   139
		}
icculus@6151
   140
	}
icculus@6151
   141
icculus@6151
   142
	return SDL_NAME(arts_suspended)();
icculus@6151
   143
}
icculus@6151
   144
slouken@0
   145
static int Audio_Available(void)
slouken@0
   146
{
slouken@294
   147
	int available = 0;
slouken@294
   148
slouken@294
   149
	if ( LoadARTSLibrary() < 0 ) {
slouken@294
   150
		return available;
slouken@294
   151
	}
slouken@294
   152
	if ( SDL_NAME(arts_init)() == 0 ) {
icculus@6151
   153
		if ( ARTS_Suspend() ) {
icculus@3959
   154
			/* Play a stream so aRts doesn't crash */
icculus@3959
   155
			arts_stream_t stream2;
icculus@3959
   156
			stream2=SDL_NAME(arts_play_stream)(44100, 16, 2, "SDL");
icculus@3959
   157
			SDL_NAME(arts_write)(stream2, "", 0);
icculus@3959
   158
			SDL_NAME(arts_close_stream)(stream2);
icculus@3959
   159
			available = 1;
icculus@3959
   160
		}
slouken@294
   161
		SDL_NAME(arts_free)();
slouken@294
   162
	}
slouken@294
   163
	UnloadARTSLibrary();
slouken@474
   164
slouken@474
   165
	return available;
slouken@0
   166
}
slouken@0
   167
slouken@0
   168
static void Audio_DeleteDevice(SDL_AudioDevice *device)
slouken@0
   169
{
slouken@1336
   170
	SDL_free(device->hidden);
slouken@1336
   171
	SDL_free(device);
slouken@294
   172
	UnloadARTSLibrary();
slouken@0
   173
}
slouken@0
   174
slouken@0
   175
static SDL_AudioDevice *Audio_CreateDevice(int devindex)
slouken@0
   176
{
slouken@0
   177
	SDL_AudioDevice *this;
slouken@0
   178
slouken@0
   179
	/* Initialize all variables that we clean on shutdown */
slouken@294
   180
	LoadARTSLibrary();
slouken@1336
   181
	this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
slouken@0
   182
	if ( this ) {
slouken@1336
   183
		SDL_memset(this, 0, (sizeof *this));
slouken@0
   184
		this->hidden = (struct SDL_PrivateAudioData *)
slouken@1336
   185
				SDL_malloc((sizeof *this->hidden));
slouken@0
   186
	}
slouken@0
   187
	if ( (this == NULL) || (this->hidden == NULL) ) {
slouken@0
   188
		SDL_OutOfMemory();
slouken@0
   189
		if ( this ) {
slouken@1336
   190
			SDL_free(this);
slouken@0
   191
		}
slouken@0
   192
		return(0);
slouken@0
   193
	}
slouken@1336
   194
	SDL_memset(this->hidden, 0, (sizeof *this->hidden));
slouken@0
   195
	stream = 0;
slouken@0
   196
slouken@0
   197
	/* Set the function pointers */
slouken@1361
   198
	this->OpenAudio = ARTS_OpenAudio;
slouken@1361
   199
	this->WaitAudio = ARTS_WaitAudio;
slouken@1361
   200
	this->PlayAudio = ARTS_PlayAudio;
slouken@1361
   201
	this->GetAudioBuf = ARTS_GetAudioBuf;
slouken@1361
   202
	this->CloseAudio = ARTS_CloseAudio;
slouken@0
   203
slouken@0
   204
	this->free = Audio_DeleteDevice;
slouken@0
   205
slouken@0
   206
	return this;
slouken@0
   207
}
slouken@0
   208
slouken@1361
   209
AudioBootStrap ARTS_bootstrap = {
slouken@1361
   210
	ARTS_DRIVER_NAME, "Analog Realtime Synthesizer",
slouken@0
   211
	Audio_Available, Audio_CreateDevice
slouken@0
   212
};
slouken@0
   213
slouken@0
   214
/* This function waits until it is possible to write a full sound buffer */
slouken@1361
   215
static void ARTS_WaitAudio(_THIS)
slouken@0
   216
{
slouken@0
   217
	Sint32 ticks;
slouken@0
   218
slouken@0
   219
	/* Check to see if the thread-parent process is still alive */
slouken@0
   220
	{ static int cnt = 0;
slouken@0
   221
		/* Note that this only works with thread implementations 
slouken@0
   222
		   that use a different process id for each thread.
slouken@0
   223
		*/
slouken@0
   224
		if (parent && (((++cnt)%10) == 0)) { /* Check every 10 loops */
slouken@0
   225
			if ( kill(parent, 0) < 0 ) {
slouken@0
   226
				this->enabled = 0;
slouken@0
   227
			}
slouken@0
   228
		}
slouken@0
   229
	}
slouken@0
   230
slouken@0
   231
	/* Use timer for general audio synchronization */
slouken@0
   232
	ticks = ((Sint32)(next_frame - SDL_GetTicks()))-FUDGE_TICKS;
slouken@0
   233
	if ( ticks > 0 ) {
slouken@0
   234
		SDL_Delay(ticks);
slouken@0
   235
	}
slouken@0
   236
}
slouken@0
   237
slouken@1361
   238
static void ARTS_PlayAudio(_THIS)
slouken@0
   239
{
slouken@0
   240
	int written;
slouken@0
   241
slouken@0
   242
	/* Write the audio data */
slouken@294
   243
	written = SDL_NAME(arts_write)(stream, mixbuf, mixlen);
slouken@0
   244
	
slouken@0
   245
	/* If timer synchronization is enabled, set the next write frame */
slouken@0
   246
	if ( frame_ticks ) {
slouken@0
   247
		next_frame += frame_ticks;
slouken@0
   248
	}
slouken@0
   249
slouken@0
   250
	/* If we couldn't write, assume fatal error for now */
slouken@0
   251
	if ( written < 0 ) {
slouken@0
   252
		this->enabled = 0;
slouken@0
   253
	}
slouken@0
   254
#ifdef DEBUG_AUDIO
slouken@0
   255
	fprintf(stderr, "Wrote %d bytes of audio data\n", written);
slouken@0
   256
#endif
slouken@0
   257
}
slouken@0
   258
slouken@1361
   259
static Uint8 *ARTS_GetAudioBuf(_THIS)
slouken@0
   260
{
slouken@0
   261
	return(mixbuf);
slouken@0
   262
}
slouken@0
   263
slouken@1361
   264
static void ARTS_CloseAudio(_THIS)
slouken@0
   265
{
slouken@0
   266
	if ( mixbuf != NULL ) {
slouken@0
   267
		SDL_FreeAudioMem(mixbuf);
slouken@0
   268
		mixbuf = NULL;
slouken@0
   269
	}
slouken@0
   270
	if ( stream ) {
slouken@294
   271
		SDL_NAME(arts_close_stream)(stream);
slouken@0
   272
		stream = 0;
slouken@0
   273
	}
slouken@294
   274
	SDL_NAME(arts_free)();
slouken@0
   275
}
slouken@0
   276
slouken@1361
   277
static int ARTS_OpenAudio(_THIS, SDL_AudioSpec *spec)
slouken@0
   278
{
slouken@0
   279
	int bits, frag_spec;
slouken@0
   280
	Uint16 test_format, format;
icculus@3959
   281
	int error_code;
slouken@0
   282
slouken@0
   283
	/* Reset the timer synchronization flag */
slouken@0
   284
	frame_ticks = 0.0;
slouken@0
   285
slouken@0
   286
	mixbuf = NULL;
slouken@0
   287
slouken@0
   288
	/* Try for a closest match on audio format */
slouken@0
   289
	format = 0;
slouken@0
   290
	bits = 0;
slouken@0
   291
	for ( test_format = SDL_FirstAudioFormat(spec->format);
slouken@0
   292
						! format && test_format; ) {
slouken@0
   293
#ifdef DEBUG_AUDIO
slouken@0
   294
		fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
slouken@0
   295
#endif
slouken@0
   296
		switch ( test_format ) {
slouken@0
   297
			case AUDIO_U8:
slouken@0
   298
				bits = 8;
slouken@0
   299
				format = 1;
slouken@0
   300
				break;
slouken@0
   301
			case AUDIO_S16LSB:
slouken@0
   302
				bits = 16;
slouken@0
   303
				format = 1;
slouken@0
   304
				break;
slouken@0
   305
			default:
slouken@0
   306
				format = 0;
slouken@0
   307
				break;
slouken@0
   308
		}
slouken@0
   309
		if ( ! format ) {
slouken@0
   310
			test_format = SDL_NextAudioFormat();
slouken@0
   311
		}
slouken@0
   312
	}
slouken@0
   313
	if ( format == 0 ) {
slouken@0
   314
		SDL_SetError("Couldn't find any hardware audio formats");
slouken@0
   315
		return(-1);
slouken@0
   316
	}
slouken@0
   317
	spec->format = test_format;
slouken@0
   318
icculus@3959
   319
	error_code = SDL_NAME(arts_init)();
icculus@3959
   320
	if ( error_code != 0 ) {
icculus@3959
   321
		SDL_SetError("Unable to initialize ARTS: %s", SDL_NAME(arts_error_text)(error_code));
icculus@3959
   322
		return(-1);
icculus@3959
   323
	}
icculus@6151
   324
	if ( ! ARTS_Suspend() ) {
icculus@3959
   325
		SDL_SetError("ARTS can not open audio device");
slouken@294
   326
		return(-1);
slouken@294
   327
	}
slouken@294
   328
	stream = SDL_NAME(arts_play_stream)(spec->freq, bits, spec->channels, "SDL");
slouken@0
   329
slouken@0
   330
	/* Calculate the final parameters for this audio specification */
slouken@0
   331
	SDL_CalculateAudioSpec(spec);
slouken@0
   332
slouken@0
   333
	/* Determine the power of two of the fragment size */
slouken@0
   334
	for ( frag_spec = 0; (0x01<<frag_spec) < spec->size; ++frag_spec );
slouken@0
   335
	if ( (0x01<<frag_spec) != spec->size ) {
slouken@0
   336
		SDL_SetError("Fragment size must be a power of two");
slouken@0
   337
		return(-1);
slouken@0
   338
	}
slouken@0
   339
	frag_spec |= 0x00020000;	/* two fragments, for low latency */
slouken@0
   340
slouken@0
   341
#ifdef ARTS_P_PACKET_SETTINGS
slouken@294
   342
	SDL_NAME(arts_stream_set)(stream, ARTS_P_PACKET_SETTINGS, frag_spec);
slouken@0
   343
#else
slouken@294
   344
	SDL_NAME(arts_stream_set)(stream, ARTS_P_PACKET_SIZE, frag_spec&0xffff);
slouken@294
   345
	SDL_NAME(arts_stream_set)(stream, ARTS_P_PACKET_COUNT, frag_spec>>16);
slouken@0
   346
#endif
slouken@294
   347
	spec->size = SDL_NAME(arts_stream_get)(stream, ARTS_P_PACKET_SIZE);
slouken@0
   348
slouken@0
   349
	/* Allocate mixing buffer */
slouken@0
   350
	mixlen = spec->size;
slouken@0
   351
	mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen);
slouken@0
   352
	if ( mixbuf == NULL ) {
slouken@0
   353
		return(-1);
slouken@0
   354
	}
slouken@1336
   355
	SDL_memset(mixbuf, spec->silence, spec->size);
slouken@0
   356
slouken@0
   357
	/* Get the parent process id (we're the parent of the audio thread) */
slouken@0
   358
	parent = getpid();
slouken@0
   359
slouken@0
   360
	/* We're ready to rock and roll. :-) */
slouken@0
   361
	return(0);
slouken@0
   362
}