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