src/audio/nas/SDL_nasaudio.c
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1402 d910939febfa
child 1668 4da1ee79c9af
     1.1 --- a/src/audio/nas/SDL_nasaudio.c	Sun May 21 17:27:13 2006 +0000
     1.2 +++ b/src/audio/nas/SDL_nasaudio.c	Sun May 28 13:04:16 2006 +0000
     1.3 @@ -43,257 +43,275 @@
     1.4  static struct SDL_PrivateAudioData *this2 = NULL;
     1.5  
     1.6  /* Audio driver functions */
     1.7 -static int NAS_OpenAudio(_THIS, SDL_AudioSpec *spec);
     1.8 -static void NAS_WaitAudio(_THIS);
     1.9 -static void NAS_PlayAudio(_THIS);
    1.10 -static Uint8 *NAS_GetAudioBuf(_THIS);
    1.11 -static void NAS_CloseAudio(_THIS);
    1.12 +static int NAS_OpenAudio (_THIS, SDL_AudioSpec * spec);
    1.13 +static void NAS_WaitAudio (_THIS);
    1.14 +static void NAS_PlayAudio (_THIS);
    1.15 +static Uint8 *NAS_GetAudioBuf (_THIS);
    1.16 +static void NAS_CloseAudio (_THIS);
    1.17  
    1.18  /* Audio driver bootstrap functions */
    1.19  
    1.20 -static int Audio_Available(void)
    1.21 +static int
    1.22 +Audio_Available (void)
    1.23  {
    1.24 -	AuServer *aud = AuOpenServer("", 0, NULL, 0, NULL, NULL);
    1.25 -	if (!aud) return 0;
    1.26 +    AuServer *aud = AuOpenServer ("", 0, NULL, 0, NULL, NULL);
    1.27 +    if (!aud)
    1.28 +        return 0;
    1.29  
    1.30 -	AuCloseServer(aud);
    1.31 -	return 1;
    1.32 +    AuCloseServer (aud);
    1.33 +    return 1;
    1.34  }
    1.35  
    1.36 -static void Audio_DeleteDevice(SDL_AudioDevice *device)
    1.37 +static void
    1.38 +Audio_DeleteDevice (SDL_AudioDevice * device)
    1.39  {
    1.40 -	SDL_free(device->hidden);
    1.41 -	SDL_free(device);
    1.42 +    SDL_free (device->hidden);
    1.43 +    SDL_free (device);
    1.44  }
    1.45  
    1.46 -static SDL_AudioDevice *Audio_CreateDevice(int devindex)
    1.47 +static SDL_AudioDevice *
    1.48 +Audio_CreateDevice (int devindex)
    1.49  {
    1.50 -	SDL_AudioDevice *this;
    1.51 +    SDL_AudioDevice *this;
    1.52  
    1.53 -	/* Initialize all variables that we clean on shutdown */
    1.54 -	this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
    1.55 -	if ( this ) {
    1.56 -		SDL_memset(this, 0, (sizeof *this));
    1.57 -		this->hidden = (struct SDL_PrivateAudioData *)
    1.58 -				SDL_malloc((sizeof *this->hidden));
    1.59 -	}
    1.60 -	if ( (this == NULL) || (this->hidden == NULL) ) {
    1.61 -		SDL_OutOfMemory();
    1.62 -		if ( this ) {
    1.63 -			SDL_free(this);
    1.64 -		}
    1.65 -		return(0);
    1.66 -	}
    1.67 -	SDL_memset(this->hidden, 0, (sizeof *this->hidden));
    1.68 +    /* Initialize all variables that we clean on shutdown */
    1.69 +    this = (SDL_AudioDevice *) SDL_malloc (sizeof (SDL_AudioDevice));
    1.70 +    if (this) {
    1.71 +        SDL_memset (this, 0, (sizeof *this));
    1.72 +        this->hidden = (struct SDL_PrivateAudioData *)
    1.73 +            SDL_malloc ((sizeof *this->hidden));
    1.74 +    }
    1.75 +    if ((this == NULL) || (this->hidden == NULL)) {
    1.76 +        SDL_OutOfMemory ();
    1.77 +        if (this) {
    1.78 +            SDL_free (this);
    1.79 +        }
    1.80 +        return (0);
    1.81 +    }
    1.82 +    SDL_memset (this->hidden, 0, (sizeof *this->hidden));
    1.83  
    1.84 -	/* Set the function pointers */
    1.85 -	this->OpenAudio = NAS_OpenAudio;
    1.86 -	this->WaitAudio = NAS_WaitAudio;
    1.87 -	this->PlayAudio = NAS_PlayAudio;
    1.88 -	this->GetAudioBuf = NAS_GetAudioBuf;
    1.89 -	this->CloseAudio = NAS_CloseAudio;
    1.90 +    /* Set the function pointers */
    1.91 +    this->OpenAudio = NAS_OpenAudio;
    1.92 +    this->WaitAudio = NAS_WaitAudio;
    1.93 +    this->PlayAudio = NAS_PlayAudio;
    1.94 +    this->GetAudioBuf = NAS_GetAudioBuf;
    1.95 +    this->CloseAudio = NAS_CloseAudio;
    1.96  
    1.97 -	this->free = Audio_DeleteDevice;
    1.98 +    this->free = Audio_DeleteDevice;
    1.99  
   1.100 -	return this;
   1.101 +    return this;
   1.102  }
   1.103  
   1.104  AudioBootStrap NAS_bootstrap = {
   1.105 -	NAS_DRIVER_NAME, "Network Audio System",
   1.106 -	Audio_Available, Audio_CreateDevice
   1.107 +    NAS_DRIVER_NAME, "Network Audio System",
   1.108 +    Audio_Available, Audio_CreateDevice
   1.109  };
   1.110  
   1.111  /* This function waits until it is possible to write a full sound buffer */
   1.112 -static void NAS_WaitAudio(_THIS)
   1.113 +static void
   1.114 +NAS_WaitAudio (_THIS)
   1.115  {
   1.116 -	while ( this->hidden->buf_free < this->hidden->mixlen ) {
   1.117 -		AuEvent ev;
   1.118 -		AuNextEvent(this->hidden->aud, AuTrue, &ev);
   1.119 -		AuDispatchEvent(this->hidden->aud, &ev);
   1.120 -	}
   1.121 +    while (this->hidden->buf_free < this->hidden->mixlen) {
   1.122 +        AuEvent ev;
   1.123 +        AuNextEvent (this->hidden->aud, AuTrue, &ev);
   1.124 +        AuDispatchEvent (this->hidden->aud, &ev);
   1.125 +    }
   1.126  }
   1.127  
   1.128 -static void NAS_PlayAudio(_THIS)
   1.129 +static void
   1.130 +NAS_PlayAudio (_THIS)
   1.131  {
   1.132 -	while (this->hidden->mixlen > this->hidden->buf_free) { /* We think the buffer is full? Yikes! Ask the server for events,
   1.133 -				    in the hope that some of them is LowWater events telling us more
   1.134 -				    of the buffer is free now than what we think. */
   1.135 -		AuEvent ev;
   1.136 -		AuNextEvent(this->hidden->aud, AuTrue, &ev);
   1.137 -		AuDispatchEvent(this->hidden->aud, &ev);
   1.138 -	}
   1.139 -	this->hidden->buf_free -= this->hidden->mixlen;
   1.140 +    while (this->hidden->mixlen > this->hidden->buf_free) {     /* We think the buffer is full? Yikes! Ask the server for events,
   1.141 +                                                                   in the hope that some of them is LowWater events telling us more
   1.142 +                                                                   of the buffer is free now than what we think. */
   1.143 +        AuEvent ev;
   1.144 +        AuNextEvent (this->hidden->aud, AuTrue, &ev);
   1.145 +        AuDispatchEvent (this->hidden->aud, &ev);
   1.146 +    }
   1.147 +    this->hidden->buf_free -= this->hidden->mixlen;
   1.148  
   1.149 -	/* Write the audio data */
   1.150 -	AuWriteElement(this->hidden->aud, this->hidden->flow, 0, this->hidden->mixlen, this->hidden->mixbuf, AuFalse, NULL);
   1.151 +    /* Write the audio data */
   1.152 +    AuWriteElement (this->hidden->aud, this->hidden->flow, 0,
   1.153 +                    this->hidden->mixlen, this->hidden->mixbuf, AuFalse,
   1.154 +                    NULL);
   1.155  
   1.156 -	this->hidden->written += this->hidden->mixlen;
   1.157 -	
   1.158 +    this->hidden->written += this->hidden->mixlen;
   1.159 +
   1.160  #ifdef DEBUG_AUDIO
   1.161 -	fprintf(stderr, "Wrote %d bytes of audio data\n", this->hidden->mixlen);
   1.162 +    fprintf (stderr, "Wrote %d bytes of audio data\n", this->hidden->mixlen);
   1.163  #endif
   1.164  }
   1.165  
   1.166 -static Uint8 *NAS_GetAudioBuf(_THIS)
   1.167 +static Uint8 *
   1.168 +NAS_GetAudioBuf (_THIS)
   1.169  {
   1.170 -	return(this->hidden->mixbuf);
   1.171 +    return (this->hidden->mixbuf);
   1.172  }
   1.173  
   1.174 -static void NAS_CloseAudio(_THIS)
   1.175 +static void
   1.176 +NAS_CloseAudio (_THIS)
   1.177  {
   1.178 -	if ( this->hidden->mixbuf != NULL ) {
   1.179 -		SDL_FreeAudioMem(this->hidden->mixbuf);
   1.180 -		this->hidden->mixbuf = NULL;
   1.181 -	}
   1.182 -	if ( this->hidden->aud ) {
   1.183 -		AuCloseServer(this->hidden->aud);
   1.184 -		this->hidden->aud = 0;
   1.185 -	}
   1.186 +    if (this->hidden->mixbuf != NULL) {
   1.187 +        SDL_FreeAudioMem (this->hidden->mixbuf);
   1.188 +        this->hidden->mixbuf = NULL;
   1.189 +    }
   1.190 +    if (this->hidden->aud) {
   1.191 +        AuCloseServer (this->hidden->aud);
   1.192 +        this->hidden->aud = 0;
   1.193 +    }
   1.194  }
   1.195  
   1.196 -static unsigned char sdlformat_to_auformat(unsigned int fmt)
   1.197 +static unsigned char
   1.198 +sdlformat_to_auformat (unsigned int fmt)
   1.199  {
   1.200 -  switch (fmt)
   1.201 -    {
   1.202 +    switch (fmt) {
   1.203      case AUDIO_U8:
   1.204 -      return AuFormatLinearUnsigned8;
   1.205 +        return AuFormatLinearUnsigned8;
   1.206      case AUDIO_S8:
   1.207 -      return AuFormatLinearSigned8;
   1.208 +        return AuFormatLinearSigned8;
   1.209      case AUDIO_U16LSB:
   1.210 -      return AuFormatLinearUnsigned16LSB;
   1.211 +        return AuFormatLinearUnsigned16LSB;
   1.212      case AUDIO_U16MSB:
   1.213 -      return AuFormatLinearUnsigned16MSB;
   1.214 +        return AuFormatLinearUnsigned16MSB;
   1.215      case AUDIO_S16LSB:
   1.216 -      return AuFormatLinearSigned16LSB;
   1.217 +        return AuFormatLinearSigned16LSB;
   1.218      case AUDIO_S16MSB:
   1.219 -      return AuFormatLinearSigned16MSB;
   1.220 +        return AuFormatLinearSigned16MSB;
   1.221      }
   1.222 -  return AuNone;
   1.223 +    return AuNone;
   1.224  }
   1.225  
   1.226  static AuBool
   1.227 -event_handler(AuServer* aud, AuEvent* ev, AuEventHandlerRec* hnd)
   1.228 +event_handler (AuServer * aud, AuEvent * ev, AuEventHandlerRec * hnd)
   1.229  {
   1.230 -	switch (ev->type) {
   1.231 -	case AuEventTypeElementNotify: {
   1.232 -		AuElementNotifyEvent* event = (AuElementNotifyEvent *)ev;
   1.233 +    switch (ev->type) {
   1.234 +    case AuEventTypeElementNotify:
   1.235 +        {
   1.236 +            AuElementNotifyEvent *event = (AuElementNotifyEvent *) ev;
   1.237  
   1.238 -		switch (event->kind) {
   1.239 -		case AuElementNotifyKindLowWater:
   1.240 -			if (this2->buf_free >= 0) {
   1.241 -				this2->really += event->num_bytes;
   1.242 -				gettimeofday(&this2->last_tv, 0);
   1.243 -				this2->buf_free += event->num_bytes;
   1.244 -			} else {
   1.245 -				this2->buf_free = event->num_bytes;
   1.246 -			}
   1.247 -			break;
   1.248 -		case AuElementNotifyKindState:
   1.249 -			switch (event->cur_state) {
   1.250 -			case AuStatePause:
   1.251 -				if (event->reason != AuReasonUser) {
   1.252 -					if (this2->buf_free >= 0) {
   1.253 -						this2->really += event->num_bytes;
   1.254 -						gettimeofday(&this2->last_tv, 0);
   1.255 -						this2->buf_free += event->num_bytes;
   1.256 -					} else {
   1.257 -						this2->buf_free = event->num_bytes;
   1.258 -					}
   1.259 -				}
   1.260 -				break;
   1.261 -			}
   1.262 -		}
   1.263 -	}
   1.264 -	}
   1.265 -	return AuTrue;
   1.266 +            switch (event->kind) {
   1.267 +            case AuElementNotifyKindLowWater:
   1.268 +                if (this2->buf_free >= 0) {
   1.269 +                    this2->really += event->num_bytes;
   1.270 +                    gettimeofday (&this2->last_tv, 0);
   1.271 +                    this2->buf_free += event->num_bytes;
   1.272 +                } else {
   1.273 +                    this2->buf_free = event->num_bytes;
   1.274 +                }
   1.275 +                break;
   1.276 +            case AuElementNotifyKindState:
   1.277 +                switch (event->cur_state) {
   1.278 +                case AuStatePause:
   1.279 +                    if (event->reason != AuReasonUser) {
   1.280 +                        if (this2->buf_free >= 0) {
   1.281 +                            this2->really += event->num_bytes;
   1.282 +                            gettimeofday (&this2->last_tv, 0);
   1.283 +                            this2->buf_free += event->num_bytes;
   1.284 +                        } else {
   1.285 +                            this2->buf_free = event->num_bytes;
   1.286 +                        }
   1.287 +                    }
   1.288 +                    break;
   1.289 +                }
   1.290 +            }
   1.291 +        }
   1.292 +    }
   1.293 +    return AuTrue;
   1.294  }
   1.295  
   1.296  static AuDeviceID
   1.297 -find_device(_THIS, int nch)
   1.298 +find_device (_THIS, int nch)
   1.299  {
   1.300 -	int i;
   1.301 -	for (i = 0; i < AuServerNumDevices(this->hidden->aud); i++) {
   1.302 -		if ((AuDeviceKind(AuServerDevice(this->hidden->aud, i)) ==
   1.303 -				AuComponentKindPhysicalOutput) &&
   1.304 -			AuDeviceNumTracks(AuServerDevice(this->hidden->aud, i)) == nch) {
   1.305 -			return AuDeviceIdentifier(AuServerDevice(this->hidden->aud, i));
   1.306 -		}
   1.307 -	}
   1.308 -	return AuNone;
   1.309 +    int i;
   1.310 +    for (i = 0; i < AuServerNumDevices (this->hidden->aud); i++) {
   1.311 +        if ((AuDeviceKind (AuServerDevice (this->hidden->aud, i)) ==
   1.312 +             AuComponentKindPhysicalOutput) &&
   1.313 +            AuDeviceNumTracks (AuServerDevice (this->hidden->aud, i)) ==
   1.314 +            nch) {
   1.315 +            return AuDeviceIdentifier (AuServerDevice (this->hidden->aud, i));
   1.316 +        }
   1.317 +    }
   1.318 +    return AuNone;
   1.319  }
   1.320  
   1.321 -static int NAS_OpenAudio(_THIS, SDL_AudioSpec *spec)
   1.322 +static int
   1.323 +NAS_OpenAudio (_THIS, SDL_AudioSpec * spec)
   1.324  {
   1.325 -	AuElement elms[3];
   1.326 -	int buffer_size;
   1.327 -	Uint16 test_format, format;
   1.328 +    AuElement elms[3];
   1.329 +    int buffer_size;
   1.330 +    Uint16 test_format, format;
   1.331 +
   1.332 +    this->hidden->mixbuf = NULL;
   1.333  
   1.334 -	this->hidden->mixbuf = NULL;
   1.335 +    /* Try for a closest match on audio format */
   1.336 +    format = 0;
   1.337 +    for (test_format = SDL_FirstAudioFormat (spec->format);
   1.338 +         !format && test_format;) {
   1.339 +        format = sdlformat_to_auformat (test_format);
   1.340  
   1.341 -	/* Try for a closest match on audio format */
   1.342 -	format = 0;
   1.343 -	for ( test_format = SDL_FirstAudioFormat(spec->format);
   1.344 -						! format && test_format; ) {
   1.345 -		format = sdlformat_to_auformat(test_format);
   1.346 +        if (format == AuNone) {
   1.347 +            test_format = SDL_NextAudioFormat ();
   1.348 +        }
   1.349 +    }
   1.350 +    if (format == 0) {
   1.351 +        SDL_SetError ("Couldn't find any hardware audio formats");
   1.352 +        return (-1);
   1.353 +    }
   1.354 +    spec->format = test_format;
   1.355  
   1.356 -		if (format == AuNone) {
   1.357 -			test_format = SDL_NextAudioFormat();
   1.358 -		}
   1.359 -	}
   1.360 -	if ( format == 0 ) {
   1.361 -		SDL_SetError("Couldn't find any hardware audio formats");
   1.362 -		return(-1);
   1.363 -	}
   1.364 -	spec->format = test_format;
   1.365 +    this->hidden->aud = AuOpenServer ("", 0, NULL, 0, NULL, NULL);
   1.366 +    if (this->hidden->aud == 0) {
   1.367 +        SDL_SetError ("Couldn't open connection to NAS server");
   1.368 +        return (-1);
   1.369 +    }
   1.370 +
   1.371 +    this->hidden->dev = find_device (this, spec->channels);
   1.372 +    if ((this->hidden->dev == AuNone)
   1.373 +        || (!(this->hidden->flow = AuCreateFlow (this->hidden->aud, NULL)))) {
   1.374 +        AuCloseServer (this->hidden->aud);
   1.375 +        this->hidden->aud = 0;
   1.376 +        SDL_SetError
   1.377 +            ("Couldn't find a fitting playback device on NAS server");
   1.378 +        return (-1);
   1.379 +    }
   1.380  
   1.381 -	this->hidden->aud = AuOpenServer("", 0, NULL, 0, NULL, NULL);
   1.382 -	if (this->hidden->aud == 0)
   1.383 -	{
   1.384 -		SDL_SetError("Couldn't open connection to NAS server");
   1.385 -		return (-1);
   1.386 -	}
   1.387 -	
   1.388 -	this->hidden->dev = find_device(this, spec->channels);
   1.389 -	if ((this->hidden->dev == AuNone) || (!(this->hidden->flow = AuCreateFlow(this->hidden->aud, NULL)))) {
   1.390 -		AuCloseServer(this->hidden->aud);
   1.391 -		this->hidden->aud = 0;
   1.392 -		SDL_SetError("Couldn't find a fitting playback device on NAS server");
   1.393 -		return (-1);
   1.394 -	}
   1.395 -	
   1.396 -	buffer_size = spec->freq;
   1.397 -	if (buffer_size < 4096)
   1.398 -		buffer_size = 4096; 
   1.399 +    buffer_size = spec->freq;
   1.400 +    if (buffer_size < 4096)
   1.401 +        buffer_size = 4096;
   1.402  
   1.403 -	if (buffer_size > 32768)
   1.404 -		buffer_size = 32768; /* So that the buffer won't get unmanageably big. */
   1.405 +    if (buffer_size > 32768)
   1.406 +        buffer_size = 32768;    /* So that the buffer won't get unmanageably big. */
   1.407  
   1.408 -	/* Calculate the final parameters for this audio specification */
   1.409 -	SDL_CalculateAudioSpec(spec);
   1.410 +    /* Calculate the final parameters for this audio specification */
   1.411 +    SDL_CalculateAudioSpec (spec);
   1.412 +
   1.413 +    this2 = this->hidden;
   1.414  
   1.415 -	this2 = this->hidden;
   1.416 +    AuMakeElementImportClient (elms, spec->freq, format, spec->channels,
   1.417 +                               AuTrue, buffer_size, buffer_size / 4, 0, NULL);
   1.418 +    AuMakeElementExportDevice (elms + 1, 0, this->hidden->dev, spec->freq,
   1.419 +                               AuUnlimitedSamples, 0, NULL);
   1.420 +    AuSetElements (this->hidden->aud, this->hidden->flow, AuTrue, 2, elms,
   1.421 +                   NULL);
   1.422 +    AuRegisterEventHandler (this->hidden->aud, AuEventHandlerIDMask, 0,
   1.423 +                            this->hidden->flow, event_handler,
   1.424 +                            (AuPointer) NULL);
   1.425  
   1.426 -	AuMakeElementImportClient(elms, spec->freq, format, spec->channels, AuTrue,
   1.427 -				buffer_size, buffer_size / 4, 0, NULL);
   1.428 -	AuMakeElementExportDevice(elms+1, 0, this->hidden->dev, spec->freq,
   1.429 -				AuUnlimitedSamples, 0, NULL);
   1.430 -	AuSetElements(this->hidden->aud, this->hidden->flow, AuTrue, 2, elms, NULL);
   1.431 -	AuRegisterEventHandler(this->hidden->aud, AuEventHandlerIDMask, 0, this->hidden->flow,
   1.432 -				event_handler, (AuPointer) NULL);
   1.433 -
   1.434 -	AuStartFlow(this->hidden->aud, this->hidden->flow, NULL);
   1.435 +    AuStartFlow (this->hidden->aud, this->hidden->flow, NULL);
   1.436  
   1.437 -	/* Allocate mixing buffer */
   1.438 -	this->hidden->mixlen = spec->size;
   1.439 -	this->hidden->mixbuf = (Uint8 *)SDL_AllocAudioMem(this->hidden->mixlen);
   1.440 -	if ( this->hidden->mixbuf == NULL ) {
   1.441 -		return(-1);
   1.442 -	}
   1.443 -	SDL_memset(this->hidden->mixbuf, spec->silence, spec->size);
   1.444 +    /* Allocate mixing buffer */
   1.445 +    this->hidden->mixlen = spec->size;
   1.446 +    this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem (this->hidden->mixlen);
   1.447 +    if (this->hidden->mixbuf == NULL) {
   1.448 +        return (-1);
   1.449 +    }
   1.450 +    SDL_memset (this->hidden->mixbuf, spec->silence, spec->size);
   1.451  
   1.452 -	/* Get the parent process id (we're the parent of the audio thread) */
   1.453 -	this->hidden->parent = getpid();
   1.454 +    /* Get the parent process id (we're the parent of the audio thread) */
   1.455 +    this->hidden->parent = getpid ();
   1.456  
   1.457 -	/* We're ready to rock and roll. :-) */
   1.458 -	return(0);
   1.459 +    /* We're ready to rock and roll. :-) */
   1.460 +    return (0);
   1.461  }
   1.462 +
   1.463 +/* vi: set ts=4 sw=4 expandtab: */