changeset 25:4acb5260d684

Made the SDL audio callback much more robust.
author Ryan C. Gordon <icculus@icculus.org>
date Wed, 19 Sep 2001 14:05:36 +0000
parents 0fc0f91b1b84
children ddc3614c9042
files playsound/test_sdlsound.c
diffstat 1 files changed, 39 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- 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 */