src/audio/nas/SDL_nasaudio.c
author Sam Lantinga
Sun, 04 Jan 2004 16:49:27 +0000
changeset 769 b8d311d90021
parent 297 f6ffac90895c
child 1312 c9b51268668f
permissions -rw-r--r--
Updated copyright information for 2004 (Happy New Year!)
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@769
     3
    Copyright (C) 1997-2004 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
    This driver was written by:
slouken@0
    23
    Erik Inge Bolsų
slouken@0
    24
    knan@mo.himolde.no
slouken@0
    25
*/
slouken@0
    26
slouken@0
    27
#ifdef SAVE_RCSID
slouken@0
    28
static char rcsid =
slouken@0
    29
 "@(#) $Id$";
slouken@0
    30
#endif
slouken@0
    31
slouken@0
    32
/* Allow access to a raw mixing buffer */
slouken@0
    33
slouken@0
    34
#include <stdlib.h>
slouken@0
    35
#include <stdio.h>
slouken@0
    36
#include <string.h>
slouken@0
    37
#include <errno.h>
slouken@0
    38
#include <signal.h>
slouken@0
    39
#include <unistd.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_nasaudio.h"
slouken@0
    48
slouken@0
    49
/* The tag name used by artsc audio */
slouken@0
    50
#define NAS_DRIVER_NAME         "nas"
slouken@0
    51
slouken@0
    52
static struct SDL_PrivateAudioData *this2 = NULL;
slouken@0
    53
slouken@0
    54
/* Audio driver functions */
slouken@0
    55
static int NAS_OpenAudio(_THIS, SDL_AudioSpec *spec);
slouken@0
    56
static void NAS_WaitAudio(_THIS);
slouken@0
    57
static void NAS_PlayAudio(_THIS);
slouken@0
    58
static Uint8 *NAS_GetAudioBuf(_THIS);
slouken@0
    59
static void NAS_CloseAudio(_THIS);
slouken@0
    60
slouken@0
    61
/* Audio driver bootstrap functions */
slouken@0
    62
slouken@0
    63
static int Audio_Available(void)
slouken@0
    64
{
slouken@0
    65
	AuServer *aud = AuOpenServer("", 0, NULL, 0, NULL, NULL);
slouken@0
    66
	if (!aud) return 0;
slouken@0
    67
slouken@0
    68
	AuCloseServer(aud);
slouken@0
    69
	return 1;
slouken@0
    70
}
slouken@0
    71
slouken@0
    72
static void Audio_DeleteDevice(SDL_AudioDevice *device)
slouken@0
    73
{
slouken@0
    74
	free(device->hidden);
slouken@0
    75
	free(device);
slouken@0
    76
}
slouken@0
    77
slouken@0
    78
static SDL_AudioDevice *Audio_CreateDevice(int devindex)
slouken@0
    79
{
slouken@0
    80
	SDL_AudioDevice *this;
slouken@0
    81
slouken@0
    82
	/* Initialize all variables that we clean on shutdown */
slouken@0
    83
	this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice));
slouken@0
    84
	if ( this ) {
slouken@0
    85
		memset(this, 0, (sizeof *this));
slouken@0
    86
		this->hidden = (struct SDL_PrivateAudioData *)
slouken@0
    87
				malloc((sizeof *this->hidden));
slouken@0
    88
	}
slouken@0
    89
	if ( (this == NULL) || (this->hidden == NULL) ) {
slouken@0
    90
		SDL_OutOfMemory();
slouken@0
    91
		if ( this ) {
slouken@0
    92
			free(this);
slouken@0
    93
		}
slouken@0
    94
		return(0);
slouken@0
    95
	}
slouken@0
    96
	memset(this->hidden, 0, (sizeof *this->hidden));
slouken@0
    97
slouken@0
    98
	/* Set the function pointers */
slouken@0
    99
	this->OpenAudio = NAS_OpenAudio;
slouken@0
   100
	this->WaitAudio = NAS_WaitAudio;
slouken@0
   101
	this->PlayAudio = NAS_PlayAudio;
slouken@0
   102
	this->GetAudioBuf = NAS_GetAudioBuf;
slouken@0
   103
	this->CloseAudio = NAS_CloseAudio;
slouken@0
   104
slouken@0
   105
	this->free = Audio_DeleteDevice;
slouken@0
   106
slouken@0
   107
	return this;
slouken@0
   108
}
slouken@0
   109
slouken@0
   110
AudioBootStrap NAS_bootstrap = {
slouken@0
   111
	NAS_DRIVER_NAME, "Network Audio System",
slouken@0
   112
	Audio_Available, Audio_CreateDevice
slouken@0
   113
};
slouken@0
   114
slouken@0
   115
/* This function waits until it is possible to write a full sound buffer */
slouken@0
   116
static void NAS_WaitAudio(_THIS)
slouken@0
   117
{
slouken@0
   118
	while ( this->hidden->buf_free < this->hidden->mixlen ) {
slouken@0
   119
		AuEvent ev;
slouken@0
   120
		AuNextEvent(this->hidden->aud, AuTrue, &ev);
slouken@0
   121
		AuDispatchEvent(this->hidden->aud, &ev);
slouken@0
   122
	}
slouken@0
   123
}
slouken@0
   124
slouken@0
   125
static void NAS_PlayAudio(_THIS)
slouken@0
   126
{
slouken@0
   127
	while (this->hidden->mixlen > this->hidden->buf_free) { /* We think the buffer is full? Yikes! Ask the server for events,
slouken@0
   128
				    in the hope that some of them is LowWater events telling us more
slouken@0
   129
				    of the buffer is free now than what we think. */
slouken@0
   130
		AuEvent ev;
slouken@0
   131
		AuNextEvent(this->hidden->aud, AuTrue, &ev);
slouken@0
   132
		AuDispatchEvent(this->hidden->aud, &ev);
slouken@0
   133
	}
slouken@0
   134
	this->hidden->buf_free -= this->hidden->mixlen;
slouken@0
   135
slouken@0
   136
	/* Write the audio data */
slouken@0
   137
	AuWriteElement(this->hidden->aud, this->hidden->flow, 0, this->hidden->mixlen, this->hidden->mixbuf, AuFalse, NULL);
slouken@0
   138
slouken@0
   139
	this->hidden->written += this->hidden->mixlen;
slouken@0
   140
	
slouken@0
   141
#ifdef DEBUG_AUDIO
slouken@0
   142
	fprintf(stderr, "Wrote %d bytes of audio data\n", this->hidden->mixlen);
slouken@0
   143
#endif
slouken@0
   144
}
slouken@0
   145
slouken@0
   146
static Uint8 *NAS_GetAudioBuf(_THIS)
slouken@0
   147
{
slouken@0
   148
	return(this->hidden->mixbuf);
slouken@0
   149
}
slouken@0
   150
slouken@0
   151
static void NAS_CloseAudio(_THIS)
slouken@0
   152
{
slouken@0
   153
	if ( this->hidden->mixbuf != NULL ) {
slouken@0
   154
		SDL_FreeAudioMem(this->hidden->mixbuf);
slouken@0
   155
		this->hidden->mixbuf = NULL;
slouken@0
   156
	}
slouken@0
   157
	if ( this->hidden->aud ) {
slouken@0
   158
		AuCloseServer(this->hidden->aud);
slouken@0
   159
		this->hidden->aud = 0;
slouken@0
   160
	}
slouken@0
   161
}
slouken@0
   162
slouken@0
   163
static unsigned char sdlformat_to_auformat(unsigned int fmt)
slouken@0
   164
{
slouken@0
   165
  switch (fmt)
slouken@0
   166
    {
slouken@0
   167
    case AUDIO_U8:
slouken@0
   168
      return AuFormatLinearUnsigned8;
slouken@0
   169
    case AUDIO_S8:
slouken@0
   170
      return AuFormatLinearSigned8;
slouken@0
   171
    case AUDIO_U16LSB:
slouken@0
   172
      return AuFormatLinearUnsigned16LSB;
slouken@0
   173
    case AUDIO_U16MSB:
slouken@0
   174
      return AuFormatLinearUnsigned16MSB;
slouken@0
   175
    case AUDIO_S16LSB:
slouken@0
   176
      return AuFormatLinearSigned16LSB;
slouken@0
   177
    case AUDIO_S16MSB:
slouken@0
   178
      return AuFormatLinearSigned16MSB;
slouken@0
   179
    }
slouken@0
   180
  return AuNone;
slouken@0
   181
}
slouken@0
   182
slouken@0
   183
static AuBool
slouken@0
   184
event_handler(AuServer* aud, AuEvent* ev, AuEventHandlerRec* hnd)
slouken@0
   185
{
slouken@0
   186
	switch (ev->type) {
slouken@0
   187
	case AuEventTypeElementNotify: {
slouken@0
   188
		AuElementNotifyEvent* event = (AuElementNotifyEvent *)ev;
slouken@0
   189
slouken@0
   190
		switch (event->kind) {
slouken@0
   191
		case AuElementNotifyKindLowWater:
slouken@0
   192
			if (this2->buf_free >= 0) {
slouken@0
   193
				this2->really += event->num_bytes;
slouken@0
   194
				gettimeofday(&this2->last_tv, 0);
slouken@0
   195
				this2->buf_free += event->num_bytes;
slouken@0
   196
			} else {
slouken@0
   197
				this2->buf_free = event->num_bytes;
slouken@0
   198
			}
slouken@0
   199
			break;
slouken@0
   200
		case AuElementNotifyKindState:
slouken@0
   201
			switch (event->cur_state) {
slouken@0
   202
			case AuStatePause:
slouken@0
   203
				if (event->reason != AuReasonUser) {
slouken@0
   204
					if (this2->buf_free >= 0) {
slouken@0
   205
						this2->really += event->num_bytes;
slouken@0
   206
						gettimeofday(&this2->last_tv, 0);
slouken@0
   207
						this2->buf_free += event->num_bytes;
slouken@0
   208
					} else {
slouken@0
   209
						this2->buf_free = event->num_bytes;
slouken@0
   210
					}
slouken@0
   211
				}
slouken@0
   212
				break;
slouken@0
   213
			}
slouken@0
   214
		}
slouken@0
   215
	}
slouken@0
   216
	}
slouken@0
   217
	return AuTrue;
slouken@0
   218
}
slouken@0
   219
slouken@0
   220
static AuDeviceID
slouken@0
   221
find_device(_THIS, int nch)
slouken@0
   222
{
slouken@0
   223
	int i;
slouken@0
   224
	for (i = 0; i < AuServerNumDevices(this->hidden->aud); i++) {
slouken@0
   225
		if ((AuDeviceKind(AuServerDevice(this->hidden->aud, i)) ==
slouken@0
   226
				AuComponentKindPhysicalOutput) &&
slouken@0
   227
			AuDeviceNumTracks(AuServerDevice(this->hidden->aud, i)) == nch) {
slouken@0
   228
			return AuDeviceIdentifier(AuServerDevice(this->hidden->aud, i));
slouken@0
   229
		}
slouken@0
   230
	}
slouken@0
   231
	return AuNone;
slouken@0
   232
}
slouken@0
   233
slouken@0
   234
static int NAS_OpenAudio(_THIS, SDL_AudioSpec *spec)
slouken@0
   235
{
slouken@0
   236
	AuElement elms[3];
slouken@0
   237
	int buffer_size;
slouken@0
   238
	Uint16 test_format, format;
slouken@0
   239
slouken@0
   240
	this->hidden->mixbuf = NULL;
slouken@0
   241
slouken@0
   242
	/* Try for a closest match on audio format */
slouken@0
   243
	format = 0;
slouken@0
   244
	for ( test_format = SDL_FirstAudioFormat(spec->format);
slouken@0
   245
						! format && test_format; ) {
slouken@0
   246
		format = sdlformat_to_auformat(test_format);
slouken@0
   247
slouken@0
   248
		if (format == AuNone) {
slouken@0
   249
			test_format = SDL_NextAudioFormat();
slouken@0
   250
		}
slouken@0
   251
	}
slouken@0
   252
	if ( format == 0 ) {
slouken@0
   253
		SDL_SetError("Couldn't find any hardware audio formats");
slouken@0
   254
		return(-1);
slouken@0
   255
	}
slouken@0
   256
	spec->format = test_format;
slouken@0
   257
slouken@0
   258
	this->hidden->aud = AuOpenServer("", 0, NULL, 0, NULL, NULL);
slouken@0
   259
	if (this->hidden->aud == 0)
slouken@0
   260
	{
slouken@0
   261
		SDL_SetError("Couldn't open connection to NAS server");
slouken@0
   262
		return (-1);
slouken@0
   263
	}
slouken@0
   264
	
slouken@0
   265
	this->hidden->dev = find_device(this, spec->channels);
slouken@0
   266
	if ((this->hidden->dev == AuNone) || (!(this->hidden->flow = AuCreateFlow(this->hidden->aud, NULL)))) {
slouken@0
   267
		AuCloseServer(this->hidden->aud);
slouken@0
   268
		this->hidden->aud = 0;
slouken@0
   269
		SDL_SetError("Couldn't find a fitting playback device on NAS server");
slouken@0
   270
		return (-1);
slouken@0
   271
	}
slouken@0
   272
	
slouken@0
   273
	buffer_size = spec->freq;
slouken@0
   274
	if (buffer_size < 4096)
slouken@0
   275
		buffer_size = 4096; 
slouken@0
   276
slouken@0
   277
	if (buffer_size > 32768)
slouken@0
   278
		buffer_size = 32768; /* So that the buffer won't get unmanageably big. */
slouken@0
   279
slouken@0
   280
	/* Calculate the final parameters for this audio specification */
slouken@0
   281
	SDL_CalculateAudioSpec(spec);
slouken@0
   282
slouken@0
   283
	this2 = this->hidden;
slouken@0
   284
slouken@0
   285
	AuMakeElementImportClient(elms, spec->freq, format, spec->channels, AuTrue,
slouken@0
   286
				buffer_size, buffer_size / 4, 0, NULL);
slouken@0
   287
	AuMakeElementExportDevice(elms+1, 0, this->hidden->dev, spec->freq,
slouken@0
   288
				AuUnlimitedSamples, 0, NULL);
slouken@0
   289
	AuSetElements(this->hidden->aud, this->hidden->flow, AuTrue, 2, elms, NULL);
slouken@0
   290
	AuRegisterEventHandler(this->hidden->aud, AuEventHandlerIDMask, 0, this->hidden->flow,
slouken@0
   291
				event_handler, (AuPointer) NULL);
slouken@0
   292
slouken@0
   293
	AuStartFlow(this->hidden->aud, this->hidden->flow, NULL);
slouken@0
   294
slouken@0
   295
	/* Allocate mixing buffer */
slouken@0
   296
	this->hidden->mixlen = spec->size;
slouken@0
   297
	this->hidden->mixbuf = (Uint8 *)SDL_AllocAudioMem(this->hidden->mixlen);
slouken@0
   298
	if ( this->hidden->mixbuf == NULL ) {
slouken@0
   299
		return(-1);
slouken@0
   300
	}
slouken@0
   301
	memset(this->hidden->mixbuf, spec->silence, spec->size);
slouken@0
   302
slouken@0
   303
	/* Get the parent process id (we're the parent of the audio thread) */
slouken@0
   304
	this->hidden->parent = getpid();
slouken@0
   305
slouken@0
   306
	/* We're ready to rock and roll. :-) */
slouken@0
   307
	return(0);
slouken@0
   308
}