Skip to content

Commit

Permalink
Cleaned up the overflow buffer hack.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Oct 23, 2001
1 parent 1138c24 commit ae8b356
Showing 1 changed file with 30 additions and 37 deletions.
67 changes: 30 additions & 37 deletions playsound/playsound.c
Expand Up @@ -87,57 +87,42 @@ static void output_decoders(void)

static volatile int done_flag = 0;

static Uint8 *decoded_ptr = NULL;
static Uint32 decoded_bytes = 0;

static void audio_callback(void *userdata, Uint8 *stream, int len)
{
static Uint8 overflow[16384]; /* this is a hack. */
static Uint8 *overflow_ptr;
static int overflowBytes = 0;
Sound_Sample *sample = (Sound_Sample *) userdata;
int bw = 0; /* bytes written to stream*/
Uint32 rc; /* return code */

if (overflowBytes > 0)
{
bw = (overflowBytes < len) ? overflowBytes : len;
memcpy(stream, overflow_ptr, bw);
overflow_ptr += bw;
overflowBytes -= bw;
} /* if */
int cpysize;

while ((bw < len) && (!done_flag))
while (bw < len)
{
rc = Sound_Decode(sample);
if (rc > 0)
if (!decoded_bytes)
{
if ((bw + (int) rc) > len)
if (sample->flags & (SOUND_SAMPLEFLAG_ERROR|SOUND_SAMPLEFLAG_EOF))
{
overflowBytes = (bw + rc) - len;
memcpy(overflow,
((Uint8 *) sample->buffer) + (rc - overflowBytes),
overflowBytes);
overflow_ptr = overflow;
rc -= overflowBytes;
memset(stream + bw, '\0', len - bw);
done_flag = 1;
return;
} /* if */

memcpy(stream + bw, sample->buffer, rc);
bw += rc;
decoded_bytes = Sound_Decode(sample);
decoded_ptr = sample->buffer;
} /* if */

if (sample->flags & SOUND_SAMPLEFLAG_EOF)
done_flag = 1;
cpysize = len - bw;
if (cpysize > decoded_bytes)
cpysize = decoded_bytes;

else if (sample->flags & SOUND_SAMPLEFLAG_ERROR)
if (cpysize > 0)
{
fprintf(stderr, "Error condition in decoding!\n"
" problem: [%s].\n", Sound_GetError());
done_flag = 1;
} /* else if */
memcpy(stream + bw, decoded_ptr, cpysize);
bw += cpysize;
decoded_ptr += bw;
decoded_bytes -= bw;
} /* if */
} /* while */

assert(bw <= len);

if (bw < len)
memset(stream + bw, '\0', len - bw);
} /* audio_callback */


Expand Down Expand Up @@ -362,10 +347,18 @@ int main(int argc, char **argv)
done_flag = 0;
SDL_PauseAudio(0);
while (!done_flag)
{
SDL_Delay(10);
} /* while */
SDL_PauseAudio(1);
SDL_CloseAudio(); /* reopen with next sample's format if possible */

if (sample->flags & SOUND_SAMPLEFLAG_ERROR)
{
fprintf(stderr, "Error in decoding sound file!\n"
" reason: [%s].\n", Sound_GetError());
} /* if */

SDL_CloseAudio(); /* reopen with next sample's format if possible */
Sound_FreeSample(sample);
} /* for */

Expand Down

0 comments on commit ae8b356

Please sign in to comment.