Fixed a bug where when the audio starts paused all the DirectSound buffers
authorSam Lantinga
Sun, 04 Oct 2009 09:18:48 +0000
changeset 333600fab0ebfe54
parent 3335 b8d313de8a65
child 3337 9ac6f0782dd6
Fixed a bug where when the audio starts paused all the DirectSound buffers
will end up getting locked and never unlocked and sound will never play.

Added a FIXME for Ryan to look at, too. :)
src/audio/SDL_audio.c
     1.1 --- a/src/audio/SDL_audio.c	Sun Oct 04 04:03:37 2009 +0000
     1.2 +++ b/src/audio/SDL_audio.c	Sun Oct 04 09:18:48 2009 +0000
     1.3 @@ -326,6 +326,7 @@
     1.4      void *udata;
     1.5      void (SDLCALL * fill) (void *userdata, Uint8 * stream, int len);
     1.6      int silence;
     1.7 +    Uint32 delay;
     1.8  
     1.9      /* For streaming when the buffer sizes don't match up */
    1.10      Uint8 *istream;
    1.11 @@ -379,9 +380,12 @@
    1.12          stream_len = device->spec.size;
    1.13      }
    1.14  
    1.15 +    /* Calculate the delay while paused */
    1.16 +    delay = ((device->spec.samples * 1000) / device->spec.freq);
    1.17 +
    1.18      /* Determine if the streamer is necessary here */
    1.19      if (device->use_streamer == 1) {
    1.20 -        /* This code is almost the same as the old code. The difference is, instead of reding
    1.21 +        /* This code is almost the same as the old code. The difference is, instead of reading
    1.22             directly from the callback into "stream", then converting and sending the audio off,
    1.23             we go: callback -> "istream" -> (conversion) -> streamer -> stream -> device.
    1.24             However, reading and writing with streamer are done separately:
    1.25 @@ -394,6 +398,12 @@
    1.26             stream's maximum length is, but I suspect 2*max(len_cvt, stream_len) is a good figure.
    1.27           */
    1.28          while (device->enabled) {
    1.29 +
    1.30 +            if (device->paused) {
    1.31 +                SDL_Delay(delay);
    1.32 +                continue;
    1.33 +            }
    1.34 +
    1.35              /* Only read in audio if the streamer doesn't have enough already (if it does not have enough samples to output) */
    1.36              if (SDL_StreamLength(&device->streamer) < stream_len) {
    1.37                  /* Set up istream */
    1.38 @@ -404,6 +414,9 @@
    1.39                          continue;
    1.40                      }
    1.41                  } else {
    1.42 +/* FIXME: Ryan, this is probably wrong.  I imagine we don't want to get
    1.43 + * a device buffer both here and below in the stream output.
    1.44 + */
    1.45                      istream = current_audio.impl.GetDeviceBuf(device);
    1.46                      if (istream == NULL) {
    1.47                          istream = device->fake_stream;
    1.48 @@ -411,11 +424,9 @@
    1.49                  }
    1.50  
    1.51                  /* Read from the callback into the _input_ stream */
    1.52 -                if (!device->paused) {
    1.53 -                    SDL_mutexP(device->mixer_lock);
    1.54 -                    (*fill) (udata, istream, istream_len);
    1.55 -                    SDL_mutexV(device->mixer_lock);
    1.56 -                }
    1.57 +                SDL_mutexP(device->mixer_lock);
    1.58 +                (*fill) (udata, istream, istream_len);
    1.59 +                SDL_mutexV(device->mixer_lock);
    1.60  
    1.61                  /* Convert the audio if necessary and write to the streamer */
    1.62                  if (device->convert.needed) {
    1.63 @@ -451,13 +462,12 @@
    1.64                  SDL_StreamRead(&device->streamer, stream, stream_len);
    1.65  
    1.66                  /* Ready current buffer for play and change current buffer */
    1.67 -                if (stream != device->fake_stream && !device->paused) {
    1.68 +                if (stream != device->fake_stream) {
    1.69                      current_audio.impl.PlayDevice(device);
    1.70                      /* Wait for an audio buffer to become available */
    1.71                      current_audio.impl.WaitDevice(device);
    1.72                  } else {
    1.73 -                    SDL_Delay((device->spec.samples * 1000) /
    1.74 -                              device->spec.freq);
    1.75 +                    SDL_Delay(delay);
    1.76                  }
    1.77              }
    1.78  
    1.79 @@ -468,6 +478,11 @@
    1.80          /* Loop, filling the audio buffers */
    1.81          while (device->enabled) {
    1.82  
    1.83 +            if (device->paused) {
    1.84 +                SDL_Delay(delay);
    1.85 +                continue;
    1.86 +            }
    1.87 +
    1.88              /* Fill the current buffer with sound */
    1.89              if (device->convert.needed) {
    1.90                  if (device->convert.buf) {
    1.91 @@ -482,11 +497,9 @@
    1.92                  }
    1.93              }
    1.94  
    1.95 -            if (!device->paused) {
    1.96 -                SDL_mutexP(device->mixer_lock);
    1.97 -                (*fill) (udata, stream, stream_len);
    1.98 -                SDL_mutexV(device->mixer_lock);
    1.99 -            }
   1.100 +            SDL_mutexP(device->mixer_lock);
   1.101 +            (*fill) (udata, stream, stream_len);
   1.102 +            SDL_mutexV(device->mixer_lock);
   1.103  
   1.104              /* Convert the audio if necessary */
   1.105              if (device->convert.needed) {
   1.106 @@ -500,12 +513,12 @@
   1.107              }
   1.108  
   1.109              /* Ready current buffer for play and change current buffer */
   1.110 -            if (stream != device->fake_stream && !device->paused) {
   1.111 +            if (stream != device->fake_stream) {
   1.112                  current_audio.impl.PlayDevice(device);
   1.113                  /* Wait for an audio buffer to become available */
   1.114                  current_audio.impl.WaitDevice(device);
   1.115              } else {
   1.116 -                SDL_Delay((device->spec.samples * 1000) / device->spec.freq);
   1.117 +                SDL_Delay(delay);
   1.118              }
   1.119          }
   1.120      }