diff dmsimple.c @ 781:e15e0469499a

Add initial code for simulating audio playback while in no-sound situation.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 30 Jul 2013 15:29:19 +0300
parents b87c7fc646f9
children 2f32e178854a
line wrap: on
line diff
--- a/dmsimple.c	Wed May 29 15:16:43 2013 +0300
+++ b/dmsimple.c	Tue Jul 30 15:29:19 2013 +0300
@@ -102,6 +102,16 @@
 }
 
 
+static void engineAudioThreadFunc(void *userdata)
+{
+    do
+    {
+        engineAudioCallback(userdata, engine.audioSimBuf, engine.audioSimBufSize);
+        SDL_Delay(engine.audioSimDelay);
+    } while (!engine.audioSimDone);
+}
+
+
 static int engineShowProgress(int loaded, int total)
 {
     int dx = 60,
@@ -717,9 +727,23 @@
     
     if (SDL_OpenAudio(&engine.optAfmt, NULL) < 0)
     {
-        dmError("Couldn't open SDL audio: %s\n", SDL_GetError());
+        int sampleSize;
+
         // We'll let this pass, as we want to support no-sound.
-        // goto error_exit;
+        dmError("Couldn't open SDL audio, falling back to no sound: %s\n", SDL_GetError());
+
+        // Set up simulated audio thread
+        sampleSize = engine.optAfmt.channels;
+        switch (engine.optAfmt.format)
+        {
+            case AUDIO_S16SYS:
+            case AUDIO_U16SYS: sampleSize *= 2; break;
+        }
+
+        engine.audioSimDelay   = 1000 / 50;
+        engine.audioSimBufSize = (engine.optAfmt.freq * sampleSize) / 50;
+        engine.audioSimBuf     = dmMalloc(engine.audioSimBufSize);
+        engine.audioSimThread  = SDL_CreateThread(engineAudioThreadFunc, NULL);
     }
 
     // Initialize SDL video
@@ -906,6 +930,15 @@
         jssClose();
     }
 #endif
+
+    if (engine.audioSimThread != NULL)
+    {
+        dmMutexLock(engine.audioStreamMutex);
+        engine.audioSimDone = TRUE;
+        dmMutexUnlock(engine.audioStreamMutex);
+        SDL_WaitThread(engine.audioSimThread, NULL);
+    }
+
     SDL_UnlockAudio();
     
     if (engine.audioStreamMutex != NULL)