changeset 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 88ee233bf849
children 2f32e178854a 8b727fa3f529
files dmengine.h dmlib.h dmsimple.c
diffstat 3 files changed, 41 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/dmengine.h	Wed May 29 15:16:43 2013 +0300
+++ b/dmengine.h	Tue Jul 30 15:29:19 2013 +0300
@@ -257,6 +257,11 @@
     Uint8 * audioStreamBuf;
     size_t audioStreamLen;
 
+    // No-sound audio simulation thread stuff
+    SDL_Thread *audioSimThread;
+    Uint8 * audioSimBuf;
+    size_t audioSimBufSize;
+
 #ifdef DM_USE_JSS
     JSSMixer *jssDev;
     JSSPlayer *jssPlr;
--- a/dmlib.h	Wed May 29 15:16:43 2013 +0300
+++ b/dmlib.h	Tue Jul 30 15:29:19 2013 +0300
@@ -11,6 +11,7 @@
 #include <SDL_endian.h>
 #include <SDL_types.h>
 #include <SDL_mutex.h>
+#include <SDL_thread.h>
 #include <SDL_video.h>
 #include <stdarg.h>
 
--- 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)