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