2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997, 1998, 1999, 2000, 2001 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 slouken@devolution.com
22 This driver was written by:
32 /* Allow access to a raw mixing buffer */
41 #include "SDL_audio.h"
42 #include "SDL_error.h"
43 #include "SDL_audiomem.h"
44 #include "SDL_audio_c.h"
45 #include "SDL_timer.h"
46 #include "SDL_audiodev_c.h"
47 #include "SDL_nasaudio.h"
49 /* The tag name used by artsc audio */
50 #define NAS_DRIVER_NAME "nas"
52 static struct SDL_PrivateAudioData *this2 = NULL;
54 /* Audio driver functions */
55 static int NAS_OpenAudio(_THIS, SDL_AudioSpec *spec);
56 static void NAS_WaitAudio(_THIS);
57 static void NAS_PlayAudio(_THIS);
58 static Uint8 *NAS_GetAudioBuf(_THIS);
59 static void NAS_CloseAudio(_THIS);
61 /* Audio driver bootstrap functions */
63 static int Audio_Available(void)
65 AuServer *aud = AuOpenServer("", 0, NULL, 0, NULL, NULL);
72 static void Audio_DeleteDevice(SDL_AudioDevice *device)
78 static SDL_AudioDevice *Audio_CreateDevice(int devindex)
80 SDL_AudioDevice *this;
82 /* Initialize all variables that we clean on shutdown */
83 this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice));
85 memset(this, 0, (sizeof *this));
86 this->hidden = (struct SDL_PrivateAudioData *)
87 malloc((sizeof *this->hidden));
89 if ( (this == NULL) || (this->hidden == NULL) ) {
96 memset(this->hidden, 0, (sizeof *this->hidden));
98 /* Set the function pointers */
99 this->OpenAudio = NAS_OpenAudio;
100 this->WaitAudio = NAS_WaitAudio;
101 this->PlayAudio = NAS_PlayAudio;
102 this->GetAudioBuf = NAS_GetAudioBuf;
103 this->CloseAudio = NAS_CloseAudio;
105 this->free = Audio_DeleteDevice;
110 AudioBootStrap NAS_bootstrap = {
111 NAS_DRIVER_NAME, "Network Audio System",
112 Audio_Available, Audio_CreateDevice
115 /* This function waits until it is possible to write a full sound buffer */
116 static void NAS_WaitAudio(_THIS)
118 while ( this->hidden->buf_free < this->hidden->mixlen ) {
120 AuNextEvent(this->hidden->aud, AuTrue, &ev);
121 AuDispatchEvent(this->hidden->aud, &ev);
125 static void NAS_PlayAudio(_THIS)
127 while (this->hidden->mixlen > this->hidden->buf_free) { /* We think the buffer is full? Yikes! Ask the server for events,
128 in the hope that some of them is LowWater events telling us more
129 of the buffer is free now than what we think. */
131 AuNextEvent(this->hidden->aud, AuTrue, &ev);
132 AuDispatchEvent(this->hidden->aud, &ev);
134 this->hidden->buf_free -= this->hidden->mixlen;
136 /* Write the audio data */
137 AuWriteElement(this->hidden->aud, this->hidden->flow, 0, this->hidden->mixlen, this->hidden->mixbuf, AuFalse, NULL);
139 this->hidden->written += this->hidden->mixlen;
142 fprintf(stderr, "Wrote %d bytes of audio data\n", this->hidden->mixlen);
146 static Uint8 *NAS_GetAudioBuf(_THIS)
148 return(this->hidden->mixbuf);
151 static void NAS_CloseAudio(_THIS)
153 if ( this->hidden->mixbuf != NULL ) {
154 SDL_FreeAudioMem(this->hidden->mixbuf);
155 this->hidden->mixbuf = NULL;
157 if ( this->hidden->aud ) {
158 AuCloseServer(this->hidden->aud);
159 this->hidden->aud = 0;
163 static unsigned char sdlformat_to_auformat(unsigned int fmt)
168 return AuFormatLinearUnsigned8;
170 return AuFormatLinearSigned8;
172 return AuFormatLinearUnsigned16LSB;
174 return AuFormatLinearUnsigned16MSB;
176 return AuFormatLinearSigned16LSB;
178 return AuFormatLinearSigned16MSB;
184 event_handler(AuServer* aud, AuEvent* ev, AuEventHandlerRec* hnd)
187 case AuEventTypeElementNotify: {
188 AuElementNotifyEvent* event = (AuElementNotifyEvent *)ev;
190 switch (event->kind) {
191 case AuElementNotifyKindLowWater:
192 if (this2->buf_free >= 0) {
193 this2->really += event->num_bytes;
194 gettimeofday(&this2->last_tv, 0);
195 this2->buf_free += event->num_bytes;
197 this2->buf_free = event->num_bytes;
200 case AuElementNotifyKindState:
201 switch (event->cur_state) {
203 if (event->reason != AuReasonUser) {
204 if (this2->buf_free >= 0) {
205 this2->really += event->num_bytes;
206 gettimeofday(&this2->last_tv, 0);
207 this2->buf_free += event->num_bytes;
209 this2->buf_free = event->num_bytes;
221 find_device(_THIS, int nch)
224 for (i = 0; i < AuServerNumDevices(this->hidden->aud); i++) {
225 if ((AuDeviceKind(AuServerDevice(this->hidden->aud, i)) ==
226 AuComponentKindPhysicalOutput) &&
227 AuDeviceNumTracks(AuServerDevice(this->hidden->aud, i)) == nch) {
228 return AuDeviceIdentifier(AuServerDevice(this->hidden->aud, i));
234 static int NAS_OpenAudio(_THIS, SDL_AudioSpec *spec)
238 Uint16 test_format, format;
240 this->hidden->mixbuf = NULL;
242 /* Try for a closest match on audio format */
244 for ( test_format = SDL_FirstAudioFormat(spec->format);
245 ! format && test_format; ) {
246 format = sdlformat_to_auformat(test_format);
248 if (format == AuNone) {
249 test_format = SDL_NextAudioFormat();
253 SDL_SetError("Couldn't find any hardware audio formats");
256 spec->format = test_format;
258 this->hidden->aud = AuOpenServer("", 0, NULL, 0, NULL, NULL);
259 if (this->hidden->aud == 0)
261 SDL_SetError("Couldn't open connection to NAS server");
265 this->hidden->dev = find_device(this, spec->channels);
266 if ((this->hidden->dev == AuNone) || (!(this->hidden->flow = AuCreateFlow(this->hidden->aud, NULL)))) {
267 AuCloseServer(this->hidden->aud);
268 this->hidden->aud = 0;
269 SDL_SetError("Couldn't find a fitting playback device on NAS server");
273 buffer_size = spec->freq;
274 if (buffer_size < 4096)
277 if (buffer_size > 32768)
278 buffer_size = 32768; /* So that the buffer won't get unmanageably big. */
280 /* Calculate the final parameters for this audio specification */
281 SDL_CalculateAudioSpec(spec);
283 this2 = this->hidden;
285 AuMakeElementImportClient(elms, spec->freq, format, spec->channels, AuTrue,
286 buffer_size, buffer_size / 4, 0, NULL);
287 AuMakeElementExportDevice(elms+1, 0, this->hidden->dev, spec->freq,
288 AuUnlimitedSamples, 0, NULL);
289 AuSetElements(this->hidden->aud, this->hidden->flow, AuTrue, 2, elms, NULL);
290 AuRegisterEventHandler(this->hidden->aud, AuEventHandlerIDMask, 0, this->hidden->flow,
291 event_handler, (AuPointer) NULL);
293 AuStartFlow(this->hidden->aud, this->hidden->flow, NULL);
295 /* Allocate mixing buffer */
296 this->hidden->mixlen = spec->size;
297 this->hidden->mixbuf = (Uint8 *)SDL_AllocAudioMem(this->hidden->mixlen);
298 if ( this->hidden->mixbuf == NULL ) {
301 memset(this->hidden->mixbuf, spec->silence, spec->size);
303 /* Get the parent process id (we're the parent of the audio thread) */
304 this->hidden->parent = getpid();
306 /* We're ready to rock and roll. :-) */