src/audio/arts/SDL_artsaudio.c
author Sam Lantinga <slouken@lokigames.com>
Thu, 26 Apr 2001 16:45:43 +0000
changeset 0 74212992fb08
child 252 e8157fcb3114
permissions -rw-r--r--
Initial revision
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@0
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001  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@0
    20
    slouken@devolution.com
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@0
    46
/* The tag name used by artsc audio */
slouken@0
    47
#define ARTSC_DRIVER_NAME         "artsc"
slouken@0
    48
slouken@0
    49
/* Audio driver functions */
slouken@0
    50
static int ARTSC_OpenAudio(_THIS, SDL_AudioSpec *spec);
slouken@0
    51
static void ARTSC_WaitAudio(_THIS);
slouken@0
    52
static void ARTSC_PlayAudio(_THIS);
slouken@0
    53
static Uint8 *ARTSC_GetAudioBuf(_THIS);
slouken@0
    54
static void ARTSC_CloseAudio(_THIS);
slouken@0
    55
slouken@0
    56
/* Audio driver bootstrap functions */
slouken@0
    57
slouken@0
    58
static int Audio_Available(void)
slouken@0
    59
{
slouken@0
    60
	if(arts_init())
slouken@0
    61
		return 0;
slouken@0
    62
	else
slouken@0
    63
		return 1;
slouken@0
    64
}
slouken@0
    65
slouken@0
    66
static void Audio_DeleteDevice(SDL_AudioDevice *device)
slouken@0
    67
{
slouken@0
    68
	free(device->hidden);
slouken@0
    69
	free(device);
slouken@0
    70
}
slouken@0
    71
slouken@0
    72
static SDL_AudioDevice *Audio_CreateDevice(int devindex)
slouken@0
    73
{
slouken@0
    74
	SDL_AudioDevice *this;
slouken@0
    75
slouken@0
    76
	/* Initialize all variables that we clean on shutdown */
slouken@0
    77
	this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice));
slouken@0
    78
	if ( this ) {
slouken@0
    79
		memset(this, 0, (sizeof *this));
slouken@0
    80
		this->hidden = (struct SDL_PrivateAudioData *)
slouken@0
    81
				malloc((sizeof *this->hidden));
slouken@0
    82
	}
slouken@0
    83
	if ( (this == NULL) || (this->hidden == NULL) ) {
slouken@0
    84
		SDL_OutOfMemory();
slouken@0
    85
		if ( this ) {
slouken@0
    86
			free(this);
slouken@0
    87
		}
slouken@0
    88
		return(0);
slouken@0
    89
	}
slouken@0
    90
	memset(this->hidden, 0, (sizeof *this->hidden));
slouken@0
    91
	stream = 0;
slouken@0
    92
slouken@0
    93
	/* Set the function pointers */
slouken@0
    94
	this->OpenAudio = ARTSC_OpenAudio;
slouken@0
    95
	this->WaitAudio = ARTSC_WaitAudio;
slouken@0
    96
	this->PlayAudio = ARTSC_PlayAudio;
slouken@0
    97
	this->GetAudioBuf = ARTSC_GetAudioBuf;
slouken@0
    98
	this->CloseAudio = ARTSC_CloseAudio;
slouken@0
    99
slouken@0
   100
	this->free = Audio_DeleteDevice;
slouken@0
   101
slouken@0
   102
	return this;
slouken@0
   103
}
slouken@0
   104
slouken@0
   105
AudioBootStrap ARTSC_bootstrap = {
slouken@0
   106
	ARTSC_DRIVER_NAME, "Analog Realtime Synthesizer",
slouken@0
   107
	Audio_Available, Audio_CreateDevice
slouken@0
   108
};
slouken@0
   109
slouken@0
   110
/* This function waits until it is possible to write a full sound buffer */
slouken@0
   111
static void ARTSC_WaitAudio(_THIS)
slouken@0
   112
{
slouken@0
   113
	Sint32 ticks;
slouken@0
   114
slouken@0
   115
	/* Check to see if the thread-parent process is still alive */
slouken@0
   116
	{ static int cnt = 0;
slouken@0
   117
		/* Note that this only works with thread implementations 
slouken@0
   118
		   that use a different process id for each thread.
slouken@0
   119
		*/
slouken@0
   120
		if (parent && (((++cnt)%10) == 0)) { /* Check every 10 loops */
slouken@0
   121
			if ( kill(parent, 0) < 0 ) {
slouken@0
   122
				this->enabled = 0;
slouken@0
   123
			}
slouken@0
   124
		}
slouken@0
   125
	}
slouken@0
   126
slouken@0
   127
	/* Use timer for general audio synchronization */
slouken@0
   128
	ticks = ((Sint32)(next_frame - SDL_GetTicks()))-FUDGE_TICKS;
slouken@0
   129
	if ( ticks > 0 ) {
slouken@0
   130
		SDL_Delay(ticks);
slouken@0
   131
	}
slouken@0
   132
}
slouken@0
   133
slouken@0
   134
static void ARTSC_PlayAudio(_THIS)
slouken@0
   135
{
slouken@0
   136
	int written;
slouken@0
   137
slouken@0
   138
	/* Write the audio data */
slouken@0
   139
	written = arts_write(stream, mixbuf, mixlen);
slouken@0
   140
	
slouken@0
   141
	/* If timer synchronization is enabled, set the next write frame */
slouken@0
   142
	if ( frame_ticks ) {
slouken@0
   143
		next_frame += frame_ticks;
slouken@0
   144
	}
slouken@0
   145
slouken@0
   146
	/* If we couldn't write, assume fatal error for now */
slouken@0
   147
	if ( written < 0 ) {
slouken@0
   148
		this->enabled = 0;
slouken@0
   149
	}
slouken@0
   150
#ifdef DEBUG_AUDIO
slouken@0
   151
	fprintf(stderr, "Wrote %d bytes of audio data\n", written);
slouken@0
   152
#endif
slouken@0
   153
}
slouken@0
   154
slouken@0
   155
static Uint8 *ARTSC_GetAudioBuf(_THIS)
slouken@0
   156
{
slouken@0
   157
	return(mixbuf);
slouken@0
   158
}
slouken@0
   159
slouken@0
   160
static void ARTSC_CloseAudio(_THIS)
slouken@0
   161
{
slouken@0
   162
	if ( mixbuf != NULL ) {
slouken@0
   163
		SDL_FreeAudioMem(mixbuf);
slouken@0
   164
		mixbuf = NULL;
slouken@0
   165
	}
slouken@0
   166
	if ( stream ) {
slouken@0
   167
		arts_close_stream(stream);
slouken@0
   168
		stream = 0;
slouken@0
   169
	}
slouken@0
   170
}
slouken@0
   171
slouken@0
   172
static int ARTSC_OpenAudio(_THIS, SDL_AudioSpec *spec)
slouken@0
   173
{
slouken@0
   174
	int bits, frag_spec;
slouken@0
   175
	Uint16 test_format, format;
slouken@0
   176
slouken@0
   177
	/* Reset the timer synchronization flag */
slouken@0
   178
	frame_ticks = 0.0;
slouken@0
   179
slouken@0
   180
	mixbuf = NULL;
slouken@0
   181
slouken@0
   182
	/* Try for a closest match on audio format */
slouken@0
   183
	format = 0;
slouken@0
   184
	bits = 0;
slouken@0
   185
	for ( test_format = SDL_FirstAudioFormat(spec->format);
slouken@0
   186
						! format && test_format; ) {
slouken@0
   187
#ifdef DEBUG_AUDIO
slouken@0
   188
		fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
slouken@0
   189
#endif
slouken@0
   190
		switch ( test_format ) {
slouken@0
   191
			case AUDIO_U8:
slouken@0
   192
				bits = 8;
slouken@0
   193
				format = 1;
slouken@0
   194
				break;
slouken@0
   195
			case AUDIO_S16LSB:
slouken@0
   196
				bits = 16;
slouken@0
   197
				format = 1;
slouken@0
   198
				break;
slouken@0
   199
			default:
slouken@0
   200
				format = 0;
slouken@0
   201
				break;
slouken@0
   202
		}
slouken@0
   203
		if ( ! format ) {
slouken@0
   204
			test_format = SDL_NextAudioFormat();
slouken@0
   205
		}
slouken@0
   206
	}
slouken@0
   207
	if ( format == 0 ) {
slouken@0
   208
		SDL_SetError("Couldn't find any hardware audio formats");
slouken@0
   209
		return(-1);
slouken@0
   210
	}
slouken@0
   211
	spec->format = test_format;
slouken@0
   212
slouken@0
   213
	stream = arts_play_stream(spec->freq, bits, spec->channels, "SDL");
slouken@0
   214
slouken@0
   215
	/* Calculate the final parameters for this audio specification */
slouken@0
   216
	SDL_CalculateAudioSpec(spec);
slouken@0
   217
slouken@0
   218
	/* Determine the power of two of the fragment size */
slouken@0
   219
	for ( frag_spec = 0; (0x01<<frag_spec) < spec->size; ++frag_spec );
slouken@0
   220
	if ( (0x01<<frag_spec) != spec->size ) {
slouken@0
   221
		SDL_SetError("Fragment size must be a power of two");
slouken@0
   222
		return(-1);
slouken@0
   223
	}
slouken@0
   224
	frag_spec |= 0x00020000;	/* two fragments, for low latency */
slouken@0
   225
slouken@0
   226
#ifdef ARTS_P_PACKET_SETTINGS
slouken@0
   227
	arts_stream_set(stream, ARTS_P_PACKET_SETTINGS, frag_spec);
slouken@0
   228
#else
slouken@0
   229
	arts_stream_set(stream, ARTS_P_PACKET_SIZE, frag_spec&0xffff);
slouken@0
   230
	arts_stream_set(stream, ARTS_P_PACKET_COUNT, frag_spec>>16);
slouken@0
   231
#endif
slouken@0
   232
	spec->size = arts_stream_get(stream, ARTS_P_PACKET_SIZE);
slouken@0
   233
slouken@0
   234
	/* Allocate mixing buffer */
slouken@0
   235
	mixlen = spec->size;
slouken@0
   236
	mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen);
slouken@0
   237
	if ( mixbuf == NULL ) {
slouken@0
   238
		return(-1);
slouken@0
   239
	}
slouken@0
   240
	memset(mixbuf, spec->silence, spec->size);
slouken@0
   241
slouken@0
   242
	/* Get the parent process id (we're the parent of the audio thread) */
slouken@0
   243
	parent = getpid();
slouken@0
   244
slouken@0
   245
	/* We're ready to rock and roll. :-) */
slouken@0
   246
	return(0);
slouken@0
   247
}