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