Skip to content

Commit

Permalink
Made the SDL audio callback much more robust.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Sep 19, 2001
1 parent 96fe699 commit 1ba77b1
Showing 1 changed file with 39 additions and 11 deletions.
50 changes: 39 additions & 11 deletions playsound/test_sdlsound.c
Expand Up @@ -86,23 +86,51 @@ static volatile int done_flag = 0;

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

if (sample->flags & SOUND_SAMPLEFLAG_EOF)
done_flag = 1;
if (overflowBytes > 0)
{
memcpy(stream, overflow, overflowBytes);
bw = overflowBytes;
overflowBytes = 0;
} /* if */

else if (sample->flags & SOUND_SAMPLEFLAG_ERROR)
while ((bw < len) && (!done_flag))
{
printf("Error condition in decoding!\n");
done_flag = 1;
} /* else if */
rc = Sound_Decode(sample);
if (rc > 0)
{
if (bw + rc > len)
{
overflowBytes = (bw + rc) - len;
memcpy(overflow,
((Uint8 *) sample->buffer) + (rc - overflowBytes),
overflowBytes);
rc -= overflowBytes;
} /* if */

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

if (sample->flags & SOUND_SAMPLEFLAG_EOF)
done_flag = 1;

else if (sample->flags & SOUND_SAMPLEFLAG_ERROR)
{
printf("Error condition in decoding!\n");
done_flag = 1;
} /* else if */
} /* while */

assert(rc <= len);
assert(bw <= len);

memcpy(stream, sample->buffer, rc);
if (rc < len)
memset(stream + rc, '\0', len - rc); /* (*shrug*) */
if (bw < len)
memset(stream + bw, '\0', len - bw);
} /* test_callback */


Expand Down

0 comments on commit 1ba77b1

Please sign in to comment.