Moved NAS audio driver to 1.3 API. SDL-ryan-multiple-audio-device
authorRyan C. Gordon <icculus@icculus.org>
Sat, 07 Oct 2006 05:56:59 +0000
branchSDL-ryan-multiple-audio-device
changeset 382118393b045759
parent 3820 1f156fd874fa
child 3822 748707e2ddd1
Moved NAS audio driver to 1.3 API.
src/audio/nas/SDL_nasaudio.c
src/audio/nas/SDL_nasaudio.h
     1.1 --- a/src/audio/nas/SDL_nasaudio.c	Sat Oct 07 05:36:36 2006 +0000
     1.2 +++ b/src/audio/nas/SDL_nasaudio.c	Sat Oct 07 05:56:59 2006 +0000
     1.3 @@ -36,22 +36,15 @@
     1.4  #include "../SDL_audio_c.h"
     1.5  #include "SDL_nasaudio.h"
     1.6  
     1.7 -/* The tag name used by artsc audio */
     1.8 +/* The tag name used by nas audio */
     1.9  #define NAS_DRIVER_NAME         "nas"
    1.10  
    1.11  static struct SDL_PrivateAudioData *this2 = NULL;
    1.12  
    1.13 -/* Audio driver functions */
    1.14 -static int NAS_OpenAudio(_THIS, SDL_AudioSpec * spec);
    1.15 -static void NAS_WaitAudio(_THIS);
    1.16 -static void NAS_PlayAudio(_THIS);
    1.17 -static Uint8 *NAS_GetAudioBuf(_THIS);
    1.18 -static void NAS_CloseAudio(_THIS);
    1.19 -
    1.20 -/* Audio driver bootstrap functions */
    1.21 +/* !!! FIXME: dynamic loading? */
    1.22  
    1.23  static int
    1.24 -Audio_Available(void)
    1.25 +NAS_Available(void)
    1.26  {
    1.27      AuServer *aud = AuOpenServer("", 0, NULL, 0, NULL, NULL);
    1.28      if (!aud)
    1.29 @@ -61,54 +54,9 @@
    1.30      return 1;
    1.31  }
    1.32  
    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 -}
    1.39 -
    1.40 -static SDL_AudioDevice *
    1.41 -Audio_CreateDevice(int devindex)
    1.42 -{
    1.43 -    SDL_AudioDevice *this;
    1.44 -
    1.45 -    /* Initialize all variables that we clean on shutdown */
    1.46 -    this = (SDL_AudioDevice *) SDL_malloc(sizeof(SDL_AudioDevice));
    1.47 -    if (this) {
    1.48 -        SDL_memset(this, 0, (sizeof *this));
    1.49 -        this->hidden = (struct SDL_PrivateAudioData *)
    1.50 -            SDL_malloc((sizeof *this->hidden));
    1.51 -    }
    1.52 -    if ((this == NULL) || (this->hidden == NULL)) {
    1.53 -        SDL_OutOfMemory();
    1.54 -        if (this) {
    1.55 -            SDL_free(this);
    1.56 -        }
    1.57 -        return (0);
    1.58 -    }
    1.59 -    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
    1.60 -
    1.61 -    /* Set the function pointers */
    1.62 -    this->OpenAudio = NAS_OpenAudio;
    1.63 -    this->WaitAudio = NAS_WaitAudio;
    1.64 -    this->PlayAudio = NAS_PlayAudio;
    1.65 -    this->GetAudioBuf = NAS_GetAudioBuf;
    1.66 -    this->CloseAudio = NAS_CloseAudio;
    1.67 -
    1.68 -    this->free = Audio_DeleteDevice;
    1.69 -
    1.70 -    return this;
    1.71 -}
    1.72 -
    1.73 -AudioBootStrap NAS_bootstrap = {
    1.74 -    NAS_DRIVER_NAME, "Network Audio System",
    1.75 -    Audio_Available, Audio_CreateDevice, 0
    1.76 -};
    1.77 -
    1.78  /* This function waits until it is possible to write a full sound buffer */
    1.79  static void
    1.80 -NAS_WaitAudio(_THIS)
    1.81 +NAS_WaitDevice(_THIS)
    1.82  {
    1.83      while (this->hidden->buf_free < this->hidden->mixlen) {
    1.84          AuEvent ev;
    1.85 @@ -118,11 +66,14 @@
    1.86  }
    1.87  
    1.88  static void
    1.89 -NAS_PlayAudio(_THIS)
    1.90 +NAS_PlayDevice(_THIS)
    1.91  {
    1.92 -    while (this->hidden->mixlen > this->hidden->buf_free) {     /* We think the buffer is full? Yikes! Ask the server for events,
    1.93 -                                                                   in the hope that some of them is LowWater events telling us more
    1.94 -                                                                   of the buffer is free now than what we think. */
    1.95 +    while (this->hidden->mixlen > this->hidden->buf_free) {
    1.96 +        /*
    1.97 +         * We think the buffer is full? Yikes! Ask the server for events,
    1.98 +         *  in the hope that some of them is LowWater events telling us more
    1.99 +         *  of the buffer is free now than what we think.
   1.100 +         */
   1.101          AuEvent ev;
   1.102          AuNextEvent(this->hidden->aud, AuTrue, &ev);
   1.103          AuDispatchEvent(this->hidden->aud, &ev);
   1.104 @@ -141,21 +92,25 @@
   1.105  }
   1.106  
   1.107  static Uint8 *
   1.108 -NAS_GetAudioBuf(_THIS)
   1.109 +NAS_GetDeviceBuf(_THIS)
   1.110  {
   1.111      return (this->hidden->mixbuf);
   1.112  }
   1.113  
   1.114  static void
   1.115 -NAS_CloseAudio(_THIS)
   1.116 +NAS_CloseDevice(_THIS)
   1.117  {
   1.118 -    if (this->hidden->mixbuf != NULL) {
   1.119 -        SDL_FreeAudioMem(this->hidden->mixbuf);
   1.120 -        this->hidden->mixbuf = NULL;
   1.121 -    }
   1.122 -    if (this->hidden->aud) {
   1.123 -        AuCloseServer(this->hidden->aud);
   1.124 -        this->hidden->aud = 0;
   1.125 +    if (this->hidden != NULL) {
   1.126 +        if (this->hidden->mixbuf != NULL) {
   1.127 +            SDL_FreeAudioMem(this->hidden->mixbuf);
   1.128 +            this->hidden->mixbuf = NULL;
   1.129 +        }
   1.130 +        if (this->hidden->aud) {
   1.131 +            AuCloseServer(this->hidden->aud);
   1.132 +            this->hidden->aud = 0;
   1.133 +        }
   1.134 +        SDL_free(this->hidden);
   1.135 +        this->hidden = NULL;
   1.136      }
   1.137  }
   1.138  
   1.139 @@ -232,46 +187,53 @@
   1.140  }
   1.141  
   1.142  static int
   1.143 -NAS_OpenAudio(_THIS, SDL_AudioSpec * spec)
   1.144 +NAS_OpenDevice(_THIS, const char *devname, int iscapture)
   1.145  {
   1.146      AuElement elms[3];
   1.147      int buffer_size;
   1.148      SDL_AudioFormat test_format, format;
   1.149  
   1.150 -    this->hidden->mixbuf = NULL;
   1.151 +    /* Initialize all variables that we clean on shutdown */
   1.152 +    this->hidden = (struct SDL_PrivateAudioData *)
   1.153 +                        SDL_malloc((sizeof *this->hidden));
   1.154 +    if (this->hidden == NULL) {
   1.155 +        SDL_OutOfMemory();
   1.156 +        return 0;
   1.157 +    }
   1.158 +    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
   1.159  
   1.160      /* Try for a closest match on audio format */
   1.161      format = 0;
   1.162 -    for (test_format = SDL_FirstAudioFormat(spec->format);
   1.163 +    for (test_format = SDL_FirstAudioFormat(this->spec.format);
   1.164           !format && test_format;) {
   1.165          format = sdlformat_to_auformat(test_format);
   1.166 -
   1.167          if (format == AuNone) {
   1.168              test_format = SDL_NextAudioFormat();
   1.169          }
   1.170      }
   1.171      if (format == 0) {
   1.172 -        SDL_SetError("Couldn't find any hardware audio formats");
   1.173 -        return (-1);
   1.174 +        NAS_CloseDevice(this);
   1.175 +        SDL_SetError("NAS: Couldn't find any hardware audio formats");
   1.176 +        return 0;
   1.177      }
   1.178 -    spec->format = test_format;
   1.179 +    this->spec.format = test_format;
   1.180  
   1.181      this->hidden->aud = AuOpenServer("", 0, NULL, 0, NULL, NULL);
   1.182      if (this->hidden->aud == 0) {
   1.183 -        SDL_SetError("Couldn't open connection to NAS server");
   1.184 -        return (-1);
   1.185 +        NAS_CloseDevice(this);
   1.186 +        SDL_SetError("NAS: Couldn't open connection to NAS server");
   1.187 +        return 0;
   1.188      }
   1.189  
   1.190 -    this->hidden->dev = find_device(this, spec->channels);
   1.191 +    this->hidden->dev = find_device(this, this->spec.channels);
   1.192      if ((this->hidden->dev == AuNone)
   1.193          || (!(this->hidden->flow = AuCreateFlow(this->hidden->aud, NULL)))) {
   1.194 -        AuCloseServer(this->hidden->aud);
   1.195 -        this->hidden->aud = 0;
   1.196 -        SDL_SetError("Couldn't find a fitting playback device on NAS server");
   1.197 -        return (-1);
   1.198 +        NAS_CloseDevice(this);
   1.199 +        SDL_SetError("NAS: Couldn't find a fitting device on NAS server");
   1.200 +        return 0;
   1.201      }
   1.202  
   1.203 -    buffer_size = spec->freq;
   1.204 +    buffer_size = this->spec.freq;
   1.205      if (buffer_size < 4096)
   1.206          buffer_size = 4096;
   1.207  
   1.208 @@ -279,16 +241,15 @@
   1.209          buffer_size = 32768;    /* So that the buffer won't get unmanageably big. */
   1.210  
   1.211      /* Calculate the final parameters for this audio specification */
   1.212 -    SDL_CalculateAudioSpec(spec);
   1.213 +    SDL_CalculateAudioSpec(&this->spec);
   1.214  
   1.215      this2 = this->hidden;
   1.216  
   1.217 -    AuMakeElementImportClient(elms, spec->freq, format, spec->channels,
   1.218 +    AuMakeElementImportClient(elms,this->spec.freq,format,this->spec.channels,
   1.219                                AuTrue, buffer_size, buffer_size / 4, 0, NULL);
   1.220 -    AuMakeElementExportDevice(elms + 1, 0, this->hidden->dev, spec->freq,
   1.221 +    AuMakeElementExportDevice(elms + 1, 0, this->hidden->dev, this->spec.freq,
   1.222                                AuUnlimitedSamples, 0, NULL);
   1.223 -    AuSetElements(this->hidden->aud, this->hidden->flow, AuTrue, 2, elms,
   1.224 -                  NULL);
   1.225 +    AuSetElements(this->hidden->aud, this->hidden->flow, AuTrue, 2, elms, NULL);
   1.226      AuRegisterEventHandler(this->hidden->aud, AuEventHandlerIDMask, 0,
   1.227                             this->hidden->flow, event_handler,
   1.228                             (AuPointer) NULL);
   1.229 @@ -296,18 +257,36 @@
   1.230      AuStartFlow(this->hidden->aud, this->hidden->flow, NULL);
   1.231  
   1.232      /* Allocate mixing buffer */
   1.233 -    this->hidden->mixlen = spec->size;
   1.234 +    this->hidden->mixlen = this->spec.size;
   1.235      this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
   1.236      if (this->hidden->mixbuf == NULL) {
   1.237 +        NAS_CloseDevice(this);
   1.238 +        SDL_OutOfMemory();
   1.239          return (-1);
   1.240      }
   1.241 -    SDL_memset(this->hidden->mixbuf, spec->silence, spec->size);
   1.242 -
   1.243 -    /* Get the parent process id (we're the parent of the audio thread) */
   1.244 -    this->hidden->parent = getpid();
   1.245 +    SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
   1.246  
   1.247      /* We're ready to rock and roll. :-) */
   1.248 -    return (0);
   1.249 +    return 1;
   1.250  }
   1.251  
   1.252 +static int
   1.253 +NAS_Init(SDL_AudioDriverImpl *impl)
   1.254 +{
   1.255 +    /* Set the function pointers */
   1.256 +    impl->OpenDevice = NAS_OpenDevice;
   1.257 +    impl->PlayDevice = NAS_PlayDevice;
   1.258 +    impl->WaitDevice = NAS_WaitDevice;
   1.259 +    impl->GetDeviceBuf = NAS_GetDeviceBuf;
   1.260 +    impl->CloseDevice = NAS_CloseDevice;
   1.261 +    impl->OnlyHasDefaultOutputDevice = 1;  /* !!! FIXME: is this true? */
   1.262 +
   1.263 +    return 1;
   1.264 +}
   1.265 +
   1.266 +AudioBootStrap NAS_bootstrap = {
   1.267 +    NAS_DRIVER_NAME, "Network Audio System",
   1.268 +    NAS_Available, NAS_Init, 0
   1.269 +};
   1.270 +
   1.271  /* vi: set ts=4 sw=4 expandtab: */
     2.1 --- a/src/audio/nas/SDL_nasaudio.h	Sat Oct 07 05:36:36 2006 +0000
     2.2 +++ b/src/audio/nas/SDL_nasaudio.h	Sat Oct 07 05:56:59 2006 +0000
     2.3 @@ -46,9 +46,6 @@
     2.4      AuFlowID flow;
     2.5      AuDeviceID dev;
     2.6  
     2.7 -    /* The parent process id, to detect when application quits */
     2.8 -    pid_t parent;
     2.9 -
    2.10      /* Raw mixing buffer */
    2.11      Uint8 *mixbuf;
    2.12      int mixlen;
    2.13 @@ -60,4 +57,5 @@
    2.14      int buf_free;
    2.15  };
    2.16  #endif /* _SDL_nasaudio_h */
    2.17 +
    2.18  /* vi: set ts=4 sw=4 expandtab: */