src/audio/esd/SDL_esdaudio.c
author Sam Lantinga
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 an ESD network stream mixing buffer */
slouken@0
    29
slouken@0
    30
#ifdef ESD_SUPPORT
slouken@0
    31
slouken@0
    32
#include <stdlib.h>
slouken@0
    33
#include <stdio.h>
slouken@0
    34
#include <string.h>
slouken@0
    35
#include <errno.h>
slouken@0
    36
#include <signal.h>
slouken@0
    37
#include <unistd.h>
slouken@0
    38
slouken@0
    39
#include <esd.h>
slouken@0
    40
slouken@0
    41
#include "SDL_audio.h"
slouken@0
    42
#include "SDL_error.h"
slouken@0
    43
#include "SDL_audiomem.h"
slouken@0
    44
#include "SDL_audio_c.h"
slouken@0
    45
#include "SDL_timer.h"
slouken@0
    46
#include "SDL_audiodev_c.h"
slouken@0
    47
#include "SDL_esdaudio.h"
slouken@0
    48
slouken@0
    49
/* The tag name used by ESD audio */
slouken@0
    50
#define ESD_DRIVER_NAME		"esd"
slouken@0
    51
slouken@0
    52
/* Audio driver functions */
slouken@0
    53
static int ESD_OpenAudio(_THIS, SDL_AudioSpec *spec);
slouken@0
    54
static void ESD_WaitAudio(_THIS);
slouken@0
    55
static void ESD_PlayAudio(_THIS);
slouken@0
    56
static Uint8 *ESD_GetAudioBuf(_THIS);
slouken@0
    57
static void ESD_CloseAudio(_THIS);
slouken@0
    58
slouken@0
    59
/* Audio driver bootstrap functions */
slouken@0
    60
slouken@0
    61
static int Audio_Available(void)
slouken@0
    62
{
slouken@0
    63
	int connection;
slouken@0
    64
	int available;
slouken@0
    65
slouken@0
    66
	available = 0;
slouken@0
    67
	connection = esd_open_sound(NULL);
slouken@0
    68
	if ( connection >= 0 ) {
slouken@0
    69
		available = 1;
slouken@0
    70
		esd_close(connection);
slouken@0
    71
	}
slouken@0
    72
	return(available);
slouken@0
    73
}
slouken@0
    74
slouken@0
    75
static void Audio_DeleteDevice(SDL_AudioDevice *device)
slouken@0
    76
{
slouken@0
    77
	free(device->hidden);
slouken@0
    78
	free(device);
slouken@0
    79
}
slouken@0
    80
slouken@0
    81
static SDL_AudioDevice *Audio_CreateDevice(int devindex)
slouken@0
    82
{
slouken@0
    83
	SDL_AudioDevice *this;
slouken@0
    84
slouken@0
    85
	/* Initialize all variables that we clean on shutdown */
slouken@0
    86
	this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice));
slouken@0
    87
	if ( this ) {
slouken@0
    88
		memset(this, 0, (sizeof *this));
slouken@0
    89
		this->hidden = (struct SDL_PrivateAudioData *)
slouken@0
    90
				malloc((sizeof *this->hidden));
slouken@0
    91
	}
slouken@0
    92
	if ( (this == NULL) || (this->hidden == NULL) ) {
slouken@0
    93
		SDL_OutOfMemory();
slouken@0
    94
		if ( this ) {
slouken@0
    95
			free(this);
slouken@0
    96
		}
slouken@0
    97
		return(0);
slouken@0
    98
	}
slouken@0
    99
	memset(this->hidden, 0, (sizeof *this->hidden));
slouken@0
   100
	audio_fd = -1;
slouken@0
   101
slouken@0
   102
	/* Set the function pointers */
slouken@0
   103
	this->OpenAudio = ESD_OpenAudio;
slouken@0
   104
	this->WaitAudio = ESD_WaitAudio;
slouken@0
   105
	this->PlayAudio = ESD_PlayAudio;
slouken@0
   106
	this->GetAudioBuf = ESD_GetAudioBuf;
slouken@0
   107
	this->CloseAudio = ESD_CloseAudio;
slouken@0
   108
slouken@0
   109
	this->free = Audio_DeleteDevice;
slouken@0
   110
slouken@0
   111
	return this;
slouken@0
   112
}
slouken@0
   113
slouken@0
   114
AudioBootStrap ESD_bootstrap = {
slouken@0
   115
	ESD_DRIVER_NAME, "Enlightened Sound Daemon",
slouken@0
   116
	Audio_Available, Audio_CreateDevice
slouken@0
   117
};
slouken@0
   118
slouken@0
   119
/* This function waits until it is possible to write a full sound buffer */
slouken@0
   120
static void ESD_WaitAudio(_THIS)
slouken@0
   121
{
slouken@0
   122
	Sint32 ticks;
slouken@0
   123
slouken@0
   124
	/* Check to see if the thread-parent process is still alive */
slouken@0
   125
	{ static int cnt = 0;
slouken@0
   126
		/* Note that this only works with thread implementations 
slouken@0
   127
		   that use a different process id for each thread.
slouken@0
   128
		*/
slouken@0
   129
		if (parent && (((++cnt)%10) == 0)) { /* Check every 10 loops */
slouken@0
   130
			if ( kill(parent, 0) < 0 ) {
slouken@0
   131
				this->enabled = 0;
slouken@0
   132
			}
slouken@0
   133
		}
slouken@0
   134
	}
slouken@0
   135
slouken@0
   136
	/* Use timer for general audio synchronization */
slouken@0
   137
	ticks = ((Sint32)(next_frame - SDL_GetTicks()))-FUDGE_TICKS;
slouken@0
   138
	if ( ticks > 0 ) {
slouken@0
   139
		SDL_Delay(ticks);
slouken@0
   140
	}
slouken@0
   141
}
slouken@0
   142
slouken@0
   143
static void ESD_PlayAudio(_THIS)
slouken@0
   144
{
slouken@0
   145
	int written;
slouken@0
   146
slouken@0
   147
	/* Write the audio data, checking for EAGAIN on broken audio drivers */
slouken@0
   148
	do {
slouken@0
   149
		written = write(audio_fd, mixbuf, mixlen);
slouken@0
   150
		if ( (written < 0) && ((errno == 0) || (errno == EAGAIN)) ) {
slouken@0
   151
			SDL_Delay(1);	/* Let a little CPU time go by */
slouken@0
   152
		}
slouken@0
   153
	} while ( (written < 0) && 
slouken@0
   154
	          ((errno == 0) || (errno == EAGAIN) || (errno == EINTR)) );
slouken@0
   155
slouken@0
   156
	/* Set the next write frame */
slouken@0
   157
	next_frame += frame_ticks;
slouken@0
   158
slouken@0
   159
	/* If we couldn't write, assume fatal error for now */
slouken@0
   160
	if ( written < 0 ) {
slouken@0
   161
		this->enabled = 0;
slouken@0
   162
	}
slouken@0
   163
}
slouken@0
   164
slouken@0
   165
static Uint8 *ESD_GetAudioBuf(_THIS)
slouken@0
   166
{
slouken@0
   167
	return(mixbuf);
slouken@0
   168
}
slouken@0
   169
slouken@0
   170
static void ESD_CloseAudio(_THIS)
slouken@0
   171
{
slouken@0
   172
	if ( mixbuf != NULL ) {
slouken@0
   173
		SDL_FreeAudioMem(mixbuf);
slouken@0
   174
		mixbuf = NULL;
slouken@0
   175
	}
slouken@0
   176
	if ( audio_fd >= 0 ) {
slouken@0
   177
		close(audio_fd);
slouken@0
   178
		audio_fd = -1;
slouken@0
   179
	}
slouken@0
   180
}
slouken@0
   181
slouken@0
   182
/* Try to get the name of the program */
slouken@0
   183
static char *get_progname(void)
slouken@0
   184
{
slouken@0
   185
	char *progname = NULL;
slouken@0
   186
#ifdef linux
slouken@0
   187
	FILE *fp;
slouken@0
   188
	static char temp[BUFSIZ];
slouken@0
   189
slouken@0
   190
	sprintf(temp, "/proc/%d/cmdline", getpid());
slouken@0
   191
	fp = fopen(temp, "r");
slouken@0
   192
	if ( fp != NULL ) {
slouken@0
   193
		if ( fgets(temp, sizeof(temp)-1, fp) ) {
slouken@0
   194
			progname = strrchr(temp, '/');
slouken@0
   195
			if ( progname == NULL ) {
slouken@0
   196
				progname = temp;
slouken@0
   197
			} else {
slouken@0
   198
				progname = progname+1;
slouken@0
   199
			}
slouken@0
   200
		}
slouken@0
   201
		fclose(fp);
slouken@0
   202
	}
slouken@0
   203
#endif
slouken@0
   204
	return(progname);
slouken@0
   205
}
slouken@0
   206
slouken@0
   207
static int ESD_OpenAudio(_THIS, SDL_AudioSpec *spec)
slouken@0
   208
{
slouken@0
   209
	esd_format_t format;
slouken@0
   210
slouken@0
   211
	/* Convert audio spec to the ESD audio format */
slouken@0
   212
	format = (ESD_STREAM | ESD_PLAY);
slouken@0
   213
	switch ( spec->format & 0xFF ) {
slouken@0
   214
		case 8:
slouken@0
   215
			format |= ESD_BITS8;
slouken@0
   216
			break;
slouken@0
   217
		case 16:
slouken@0
   218
			format |= ESD_BITS16;
slouken@0
   219
			break;
slouken@0
   220
		default:
slouken@0
   221
			SDL_SetError("Unsupported ESD audio format");
slouken@0
   222
			return(-1);
slouken@0
   223
	}
slouken@0
   224
	if ( spec->channels == 1 ) {
slouken@0
   225
		format |= ESD_MONO;
slouken@0
   226
	} else {
slouken@0
   227
		format |= ESD_STEREO;
slouken@0
   228
	}
slouken@0
   229
#if 0
slouken@0
   230
	spec->samples = ESD_BUF_SIZE;	/* Darn, no way to change this yet */
slouken@0
   231
#endif
slouken@0
   232
slouken@0
   233
	/* Open a connection to the ESD audio server */
slouken@0
   234
	audio_fd = esd_play_stream(format, spec->freq, NULL, get_progname());
slouken@0
   235
	if ( audio_fd < 0 ) {
slouken@0
   236
		SDL_SetError("Couldn't open ESD connection");
slouken@0
   237
		return(-1);
slouken@0
   238
	}
slouken@0
   239
slouken@0
   240
	/* Calculate the final parameters for this audio specification */
slouken@0
   241
	SDL_CalculateAudioSpec(spec);
slouken@0
   242
	frame_ticks = (float)(spec->samples*1000)/spec->freq;
slouken@0
   243
	next_frame = SDL_GetTicks()+frame_ticks;
slouken@0
   244
slouken@0
   245
	/* Allocate mixing buffer */
slouken@0
   246
	mixlen = spec->size;
slouken@0
   247
	mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen);
slouken@0
   248
	if ( mixbuf == NULL ) {
slouken@0
   249
		return(-1);
slouken@0
   250
	}
slouken@0
   251
	memset(mixbuf, spec->silence, spec->size);
slouken@0
   252
slouken@0
   253
	/* Get the parent process id (we're the parent of the audio thread) */
slouken@0
   254
	parent = getpid();
slouken@0
   255
slouken@0
   256
	/* We're ready to rock and roll. :-) */
slouken@0
   257
	return(0);
slouken@0
   258
}
slouken@0
   259
slouken@0
   260
#endif /* ESD_SUPPORT */