changeset 19:16efabca7e04

Some work on making the audio work .. there is sound, but it's not correct. Also some buffer overflows seem to happen, so there will eventually be a crash.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 15 Mar 2013 20:27:12 +0200
parents ea16e1b51284
children 2ac93f2b93ae
files 3x666.c
diffstat 1 files changed, 27 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/3x666.c	Fri Mar 15 20:26:35 2013 +0200
+++ b/3x666.c	Fri Mar 15 20:27:12 2013 +0200
@@ -29,7 +29,9 @@
 
     BOOL exitFlag;
 
+    int mixFill, mixBufSize;
     Sint16 *mixBuf;
+
     int optVFlags;
     SDL_Surface *screen;
     SDL_Event event;
@@ -97,6 +99,8 @@
     va_end(ap);
 }
 
+
+
     
 static BOOL engineInitializeVideo()
 {
@@ -570,8 +574,26 @@
 static void engineAudioCallback(void *userdata, Uint8 *stream, int len)
 {
     (void) userdata;
-    int slen = len / sizeof(Sint16);
-    
+    // We inherently assume mono here, as the audio code is not
+    // capable of producing stereo anyway (for now, at least.)
+    int need = len / sizeof(Sint16);
+
+    // The audio rendering handling is a bit silly due to the way
+    // the original code works (assumes that it can always render
+    // a certain amount/duration of data, instead of being given
+    // the buffer duration/size to render. So we cope with that here.
+    while (engine.mixFill < need && engine.mixFill < engine.mixBufSize)
+    {
+        int got = engineRenderAudio(engine.mixBuf + engine.mixFill);
+        engine.mixFill += got;
+    }
+
+    memcpy(stream, engine.mixBuf, len);
+    engine.mixFill -= need;
+    if (engine.mixFill > 0)
+    {
+        memmove(engine.mixBuf, engine.mixBuf + need, engine.mixFill * sizeof(Sint16));
+    }
 }
 
 
@@ -908,7 +930,6 @@
     initSDL = TRUE;
 
     // Initialize audio parts
-    engine.mixBuf = malloc((1 + SET_MAXROWLGT) * sizeof(Sint16));
     engine.optAfmt.freq     = SET_AUDIO_FREQ;
     engine.optAfmt.format   = AUDIO_S16SYS;
     engine.optAfmt.channels = SET_AUDIO_CHN;
@@ -936,6 +957,9 @@
     audio_precalcs();
     setpal();
 
+    engine.mixBufSize = (32 + SET_MAXROWLGT) * sizeof(Sint16);
+    engine.mixBuf = malloc(engine.mixBufSize);
+
     // Start audio, enter main loop
     dmPrint("We are go.\n");
     SDL_LockAudio();