# HG changeset patch # User Ryan C. Gordon # Date 1000908336 0 # Node ID 4acb5260d6846857c58e5281bf5f324d3eaa31ce # Parent 0fc0f91b1b84ef7f4f9e171bbe54a842ba8f8736 Made the SDL audio callback much more robust. diff -r 0fc0f91b1b84 -r 4acb5260d684 playsound/test_sdlsound.c --- a/playsound/test_sdlsound.c Wed Sep 19 14:02:56 2001 +0000 +++ b/playsound/test_sdlsound.c Wed Sep 19 14:05:36 2001 +0000 @@ -86,23 +86,51 @@ 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 */ - assert(rc <= len); + memcpy(stream + bw, sample->buffer, rc); + bw += rc; + } /* if */ + + if (sample->flags & SOUND_SAMPLEFLAG_EOF) + done_flag = 1; - memcpy(stream, sample->buffer, rc); - if (rc < len) - memset(stream + rc, '\0', len - rc); /* (*shrug*) */ + else if (sample->flags & SOUND_SAMPLEFLAG_ERROR) + { + printf("Error condition in decoding!\n"); + done_flag = 1; + } /* else if */ + } /* while */ + + assert(bw <= len); + + if (bw < len) + memset(stream + bw, '\0', len - bw); } /* test_callback */