Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Fixed a bug where when the audio starts paused all the DirectSound bu…
Browse files Browse the repository at this point in the history
…ffers

will end up getting locked and never unlocked and sound will never play.

Added a FIXME for Ryan to look at, too. :)
  • Loading branch information
slouken committed Oct 4, 2009
1 parent 6fd32b1 commit 79d56c8
Showing 1 changed file with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions src/audio/SDL_audio.c
Expand Up @@ -326,6 +326,7 @@ SDL_RunAudio(void *devicep)
void *udata;
void (SDLCALL * fill) (void *userdata, Uint8 * stream, int len);
int silence;
Uint32 delay;

/* For streaming when the buffer sizes don't match up */
Uint8 *istream;
Expand Down Expand Up @@ -379,9 +380,12 @@ SDL_RunAudio(void *devicep)
stream_len = device->spec.size;
}

/* Calculate the delay while paused */
delay = ((device->spec.samples * 1000) / device->spec.freq);

/* Determine if the streamer is necessary here */
if (device->use_streamer == 1) {
/* This code is almost the same as the old code. The difference is, instead of reding
/* This code is almost the same as the old code. The difference is, instead of reading
directly from the callback into "stream", then converting and sending the audio off,
we go: callback -> "istream" -> (conversion) -> streamer -> stream -> device.
However, reading and writing with streamer are done separately:
Expand All @@ -394,6 +398,12 @@ SDL_RunAudio(void *devicep)
stream's maximum length is, but I suspect 2*max(len_cvt, stream_len) is a good figure.
*/
while (device->enabled) {

if (device->paused) {
SDL_Delay(delay);
continue;
}

/* Only read in audio if the streamer doesn't have enough already (if it does not have enough samples to output) */
if (SDL_StreamLength(&device->streamer) < stream_len) {
/* Set up istream */
Expand All @@ -404,18 +414,19 @@ SDL_RunAudio(void *devicep)
continue;
}
} else {
/* FIXME: Ryan, this is probably wrong. I imagine we don't want to get
* a device buffer both here and below in the stream output.
*/
istream = current_audio.impl.GetDeviceBuf(device);
if (istream == NULL) {
istream = device->fake_stream;
}
}

/* Read from the callback into the _input_ stream */
if (!device->paused) {
SDL_mutexP(device->mixer_lock);
(*fill) (udata, istream, istream_len);
SDL_mutexV(device->mixer_lock);
}
SDL_mutexP(device->mixer_lock);
(*fill) (udata, istream, istream_len);
SDL_mutexV(device->mixer_lock);

/* Convert the audio if necessary and write to the streamer */
if (device->convert.needed) {
Expand Down Expand Up @@ -451,13 +462,12 @@ SDL_RunAudio(void *devicep)
SDL_StreamRead(&device->streamer, stream, stream_len);

/* Ready current buffer for play and change current buffer */
if (stream != device->fake_stream && !device->paused) {
if (stream != device->fake_stream) {
current_audio.impl.PlayDevice(device);
/* Wait for an audio buffer to become available */
current_audio.impl.WaitDevice(device);
} else {
SDL_Delay((device->spec.samples * 1000) /
device->spec.freq);
SDL_Delay(delay);
}
}

Expand All @@ -468,6 +478,11 @@ SDL_RunAudio(void *devicep)
/* Loop, filling the audio buffers */
while (device->enabled) {

if (device->paused) {
SDL_Delay(delay);
continue;
}

/* Fill the current buffer with sound */
if (device->convert.needed) {
if (device->convert.buf) {
Expand All @@ -482,11 +497,9 @@ SDL_RunAudio(void *devicep)
}
}

if (!device->paused) {
SDL_mutexP(device->mixer_lock);
(*fill) (udata, stream, stream_len);
SDL_mutexV(device->mixer_lock);
}
SDL_mutexP(device->mixer_lock);
(*fill) (udata, stream, stream_len);
SDL_mutexV(device->mixer_lock);

/* Convert the audio if necessary */
if (device->convert.needed) {
Expand All @@ -500,12 +513,12 @@ SDL_RunAudio(void *devicep)
}

/* Ready current buffer for play and change current buffer */
if (stream != device->fake_stream && !device->paused) {
if (stream != device->fake_stream) {
current_audio.impl.PlayDevice(device);
/* Wait for an audio buffer to become available */
current_audio.impl.WaitDevice(device);
} else {
SDL_Delay((device->spec.samples * 1000) / device->spec.freq);
SDL_Delay(delay);
}
}
}
Expand Down

0 comments on commit 79d56c8

Please sign in to comment.