src/audio/dsp/SDL_dspaudio.c
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
     1.1 --- a/src/audio/dsp/SDL_dspaudio.c	Mon May 29 03:53:21 2006 +0000
     1.2 +++ b/src/audio/dsp/SDL_dspaudio.c	Mon May 29 04:04:35 2006 +0000
     1.3 @@ -58,56 +58,56 @@
     1.4  #define OPEN_FLAGS	(O_WRONLY|O_NONBLOCK)
     1.5  
     1.6  /* Audio driver functions */
     1.7 -static int DSP_OpenAudio (_THIS, SDL_AudioSpec * spec);
     1.8 -static void DSP_WaitAudio (_THIS);
     1.9 -static void DSP_PlayAudio (_THIS);
    1.10 -static Uint8 *DSP_GetAudioBuf (_THIS);
    1.11 -static void DSP_CloseAudio (_THIS);
    1.12 +static int DSP_OpenAudio(_THIS, SDL_AudioSpec * spec);
    1.13 +static void DSP_WaitAudio(_THIS);
    1.14 +static void DSP_PlayAudio(_THIS);
    1.15 +static Uint8 *DSP_GetAudioBuf(_THIS);
    1.16 +static void DSP_CloseAudio(_THIS);
    1.17  
    1.18  /* Audio driver bootstrap functions */
    1.19  
    1.20  static int
    1.21 -Audio_Available (void)
    1.22 +Audio_Available(void)
    1.23  {
    1.24      int fd;
    1.25      int available;
    1.26  
    1.27      available = 0;
    1.28 -    fd = SDL_OpenAudioPath (NULL, 0, OPEN_FLAGS, 0);
    1.29 +    fd = SDL_OpenAudioPath(NULL, 0, OPEN_FLAGS, 0);
    1.30      if (fd >= 0) {
    1.31          available = 1;
    1.32 -        close (fd);
    1.33 +        close(fd);
    1.34      }
    1.35      return (available);
    1.36  }
    1.37  
    1.38  static void
    1.39 -Audio_DeleteDevice (SDL_AudioDevice * device)
    1.40 +Audio_DeleteDevice(SDL_AudioDevice * device)
    1.41  {
    1.42 -    SDL_free (device->hidden);
    1.43 -    SDL_free (device);
    1.44 +    SDL_free(device->hidden);
    1.45 +    SDL_free(device);
    1.46  }
    1.47  
    1.48  static SDL_AudioDevice *
    1.49 -Audio_CreateDevice (int devindex)
    1.50 +Audio_CreateDevice(int devindex)
    1.51  {
    1.52      SDL_AudioDevice *this;
    1.53  
    1.54      /* Initialize all variables that we clean on shutdown */
    1.55 -    this = (SDL_AudioDevice *) SDL_malloc (sizeof (SDL_AudioDevice));
    1.56 +    this = (SDL_AudioDevice *) SDL_malloc(sizeof(SDL_AudioDevice));
    1.57      if (this) {
    1.58 -        SDL_memset (this, 0, (sizeof *this));
    1.59 +        SDL_memset(this, 0, (sizeof *this));
    1.60          this->hidden = (struct SDL_PrivateAudioData *)
    1.61 -            SDL_malloc ((sizeof *this->hidden));
    1.62 +            SDL_malloc((sizeof *this->hidden));
    1.63      }
    1.64      if ((this == NULL) || (this->hidden == NULL)) {
    1.65 -        SDL_OutOfMemory ();
    1.66 +        SDL_OutOfMemory();
    1.67          if (this) {
    1.68 -            SDL_free (this);
    1.69 +            SDL_free(this);
    1.70          }
    1.71          return (0);
    1.72      }
    1.73 -    SDL_memset (this->hidden, 0, (sizeof *this->hidden));
    1.74 +    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
    1.75      audio_fd = -1;
    1.76  
    1.77      /* Set the function pointers */
    1.78 @@ -129,44 +129,44 @@
    1.79  
    1.80  /* This function waits until it is possible to write a full sound buffer */
    1.81  static void
    1.82 -DSP_WaitAudio (_THIS)
    1.83 +DSP_WaitAudio(_THIS)
    1.84  {
    1.85      /* Not needed at all since OSS handles waiting automagically */
    1.86  }
    1.87  
    1.88  static void
    1.89 -DSP_PlayAudio (_THIS)
    1.90 +DSP_PlayAudio(_THIS)
    1.91  {
    1.92 -    if (write (audio_fd, mixbuf, mixlen) == -1) {
    1.93 -        perror ("Audio write");
    1.94 +    if (write(audio_fd, mixbuf, mixlen) == -1) {
    1.95 +        perror("Audio write");
    1.96          this->enabled = 0;
    1.97      }
    1.98  #ifdef DEBUG_AUDIO
    1.99 -    fprintf (stderr, "Wrote %d bytes of audio data\n", mixlen);
   1.100 +    fprintf(stderr, "Wrote %d bytes of audio data\n", mixlen);
   1.101  #endif
   1.102  }
   1.103  
   1.104  static Uint8 *
   1.105 -DSP_GetAudioBuf (_THIS)
   1.106 +DSP_GetAudioBuf(_THIS)
   1.107  {
   1.108      return (mixbuf);
   1.109  }
   1.110  
   1.111  static void
   1.112 -DSP_CloseAudio (_THIS)
   1.113 +DSP_CloseAudio(_THIS)
   1.114  {
   1.115      if (mixbuf != NULL) {
   1.116 -        SDL_FreeAudioMem (mixbuf);
   1.117 +        SDL_FreeAudioMem(mixbuf);
   1.118          mixbuf = NULL;
   1.119      }
   1.120      if (audio_fd >= 0) {
   1.121 -        close (audio_fd);
   1.122 +        close(audio_fd);
   1.123          audio_fd = -1;
   1.124      }
   1.125  }
   1.126  
   1.127  static int
   1.128 -DSP_OpenAudio (_THIS, SDL_AudioSpec * spec)
   1.129 +DSP_OpenAudio(_THIS, SDL_AudioSpec * spec)
   1.130  {
   1.131      char audiodev[1024];
   1.132      int format;
   1.133 @@ -175,9 +175,9 @@
   1.134      Uint16 test_format;
   1.135  
   1.136      /* Open the audio device */
   1.137 -    audio_fd = SDL_OpenAudioPath (audiodev, sizeof (audiodev), OPEN_FLAGS, 0);
   1.138 +    audio_fd = SDL_OpenAudioPath(audiodev, sizeof(audiodev), OPEN_FLAGS, 0);
   1.139      if (audio_fd < 0) {
   1.140 -        SDL_SetError ("Couldn't open %s: %s", audiodev, strerror (errno));
   1.141 +        SDL_SetError("Couldn't open %s: %s", audiodev, strerror(errno));
   1.142          return (-1);
   1.143      }
   1.144      mixbuf = NULL;
   1.145 @@ -185,29 +185,29 @@
   1.146      /* Make the file descriptor use blocking writes with fcntl() */
   1.147      {
   1.148          long flags;
   1.149 -        flags = fcntl (audio_fd, F_GETFL);
   1.150 +        flags = fcntl(audio_fd, F_GETFL);
   1.151          flags &= ~O_NONBLOCK;
   1.152 -        if (fcntl (audio_fd, F_SETFL, flags) < 0) {
   1.153 -            SDL_SetError ("Couldn't set audio blocking mode");
   1.154 -            DSP_CloseAudio (this);
   1.155 +        if (fcntl(audio_fd, F_SETFL, flags) < 0) {
   1.156 +            SDL_SetError("Couldn't set audio blocking mode");
   1.157 +            DSP_CloseAudio(this);
   1.158              return (-1);
   1.159          }
   1.160      }
   1.161  
   1.162      /* Get a list of supported hardware formats */
   1.163 -    if (ioctl (audio_fd, SNDCTL_DSP_GETFMTS, &value) < 0) {
   1.164 -        perror ("SNDCTL_DSP_GETFMTS");
   1.165 -        SDL_SetError ("Couldn't get audio format list");
   1.166 -        DSP_CloseAudio (this);
   1.167 +    if (ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &value) < 0) {
   1.168 +        perror("SNDCTL_DSP_GETFMTS");
   1.169 +        SDL_SetError("Couldn't get audio format list");
   1.170 +        DSP_CloseAudio(this);
   1.171          return (-1);
   1.172      }
   1.173  
   1.174      /* Try for a closest match on audio format */
   1.175      format = 0;
   1.176 -    for (test_format = SDL_FirstAudioFormat (spec->format);
   1.177 +    for (test_format = SDL_FirstAudioFormat(spec->format);
   1.178           !format && test_format;) {
   1.179  #ifdef DEBUG_AUDIO
   1.180 -        fprintf (stderr, "Trying format 0x%4.4x\n", test_format);
   1.181 +        fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
   1.182  #endif
   1.183          switch (test_format) {
   1.184          case AUDIO_U8:
   1.185 @@ -251,88 +251,87 @@
   1.186              break;
   1.187          }
   1.188          if (!format) {
   1.189 -            test_format = SDL_NextAudioFormat ();
   1.190 +            test_format = SDL_NextAudioFormat();
   1.191          }
   1.192      }
   1.193      if (format == 0) {
   1.194 -        SDL_SetError ("Couldn't find any hardware audio formats");
   1.195 -        DSP_CloseAudio (this);
   1.196 +        SDL_SetError("Couldn't find any hardware audio formats");
   1.197 +        DSP_CloseAudio(this);
   1.198          return (-1);
   1.199      }
   1.200      spec->format = test_format;
   1.201  
   1.202      /* Set the audio format */
   1.203      value = format;
   1.204 -    if ((ioctl (audio_fd, SNDCTL_DSP_SETFMT, &value) < 0) ||
   1.205 -        (value != format)) {
   1.206 -        perror ("SNDCTL_DSP_SETFMT");
   1.207 -        SDL_SetError ("Couldn't set audio format");
   1.208 -        DSP_CloseAudio (this);
   1.209 +    if ((ioctl(audio_fd, SNDCTL_DSP_SETFMT, &value) < 0) || (value != format)) {
   1.210 +        perror("SNDCTL_DSP_SETFMT");
   1.211 +        SDL_SetError("Couldn't set audio format");
   1.212 +        DSP_CloseAudio(this);
   1.213          return (-1);
   1.214      }
   1.215  
   1.216      /* Set the number of channels of output */
   1.217      value = spec->channels;
   1.218 -    if (ioctl (audio_fd, SNDCTL_DSP_CHANNELS, &value) < 0) {
   1.219 -        perror ("SNDCTL_DSP_CHANNELS");
   1.220 -        SDL_SetError ("Cannot set the number of channels");
   1.221 -        DSP_CloseAudio (this);
   1.222 +    if (ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &value) < 0) {
   1.223 +        perror("SNDCTL_DSP_CHANNELS");
   1.224 +        SDL_SetError("Cannot set the number of channels");
   1.225 +        DSP_CloseAudio(this);
   1.226          return (-1);
   1.227      }
   1.228      spec->channels = value;
   1.229  
   1.230      /* Set the DSP frequency */
   1.231      value = spec->freq;
   1.232 -    if (ioctl (audio_fd, SNDCTL_DSP_SPEED, &value) < 0) {
   1.233 -        perror ("SNDCTL_DSP_SPEED");
   1.234 -        SDL_SetError ("Couldn't set audio frequency");
   1.235 -        DSP_CloseAudio (this);
   1.236 +    if (ioctl(audio_fd, SNDCTL_DSP_SPEED, &value) < 0) {
   1.237 +        perror("SNDCTL_DSP_SPEED");
   1.238 +        SDL_SetError("Couldn't set audio frequency");
   1.239 +        DSP_CloseAudio(this);
   1.240          return (-1);
   1.241      }
   1.242      spec->freq = value;
   1.243  
   1.244      /* Calculate the final parameters for this audio specification */
   1.245 -    SDL_CalculateAudioSpec (spec);
   1.246 +    SDL_CalculateAudioSpec(spec);
   1.247  
   1.248      /* Determine the power of two of the fragment size */
   1.249      for (frag_spec = 0; (0x01U << frag_spec) < spec->size; ++frag_spec);
   1.250      if ((0x01U << frag_spec) != spec->size) {
   1.251 -        SDL_SetError ("Fragment size must be a power of two");
   1.252 -        DSP_CloseAudio (this);
   1.253 +        SDL_SetError("Fragment size must be a power of two");
   1.254 +        DSP_CloseAudio(this);
   1.255          return (-1);
   1.256      }
   1.257      frag_spec |= 0x00020000;    /* two fragments, for low latency */
   1.258  
   1.259      /* Set the audio buffering parameters */
   1.260  #ifdef DEBUG_AUDIO
   1.261 -    fprintf (stderr, "Requesting %d fragments of size %d\n",
   1.262 -             (frag_spec >> 16), 1 << (frag_spec & 0xFFFF));
   1.263 +    fprintf(stderr, "Requesting %d fragments of size %d\n",
   1.264 +            (frag_spec >> 16), 1 << (frag_spec & 0xFFFF));
   1.265  #endif
   1.266 -    if (ioctl (audio_fd, SNDCTL_DSP_SETFRAGMENT, &frag_spec) < 0) {
   1.267 -        perror ("SNDCTL_DSP_SETFRAGMENT");
   1.268 +    if (ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &frag_spec) < 0) {
   1.269 +        perror("SNDCTL_DSP_SETFRAGMENT");
   1.270      }
   1.271  #ifdef DEBUG_AUDIO
   1.272      {
   1.273          audio_buf_info info;
   1.274 -        ioctl (audio_fd, SNDCTL_DSP_GETOSPACE, &info);
   1.275 -        fprintf (stderr, "fragments = %d\n", info.fragments);
   1.276 -        fprintf (stderr, "fragstotal = %d\n", info.fragstotal);
   1.277 -        fprintf (stderr, "fragsize = %d\n", info.fragsize);
   1.278 -        fprintf (stderr, "bytes = %d\n", info.bytes);
   1.279 +        ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &info);
   1.280 +        fprintf(stderr, "fragments = %d\n", info.fragments);
   1.281 +        fprintf(stderr, "fragstotal = %d\n", info.fragstotal);
   1.282 +        fprintf(stderr, "fragsize = %d\n", info.fragsize);
   1.283 +        fprintf(stderr, "bytes = %d\n", info.bytes);
   1.284      }
   1.285  #endif
   1.286  
   1.287      /* Allocate mixing buffer */
   1.288      mixlen = spec->size;
   1.289 -    mixbuf = (Uint8 *) SDL_AllocAudioMem (mixlen);
   1.290 +    mixbuf = (Uint8 *) SDL_AllocAudioMem(mixlen);
   1.291      if (mixbuf == NULL) {
   1.292 -        DSP_CloseAudio (this);
   1.293 +        DSP_CloseAudio(this);
   1.294          return (-1);
   1.295      }
   1.296 -    SDL_memset (mixbuf, spec->silence, spec->size);
   1.297 +    SDL_memset(mixbuf, spec->silence, spec->size);
   1.298  
   1.299      /* Get the parent process id (we're the parent of the audio thread) */
   1.300 -    parent = getpid ();
   1.301 +    parent = getpid();
   1.302  
   1.303      /* We're ready to rock and roll. :-) */
   1.304      return (0);