changeset 790:cb15452f738d

Merged.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 06 Aug 2013 14:59:57 +0300
parents 8b727fa3f529 (current diff) 999c7ad69051 (diff)
children bfb1d2bacd54
files
diffstat 3 files changed, 73 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/dmengine.c	Tue Aug 06 14:59:44 2013 +0300
+++ b/dmengine.c	Tue Aug 06 14:59:57 2013 +0300
@@ -341,23 +341,26 @@
 {
     JSS_LOCK(eng->jssPlr);
 
-    *playing = eng->jssPlr->isPlaying;
-    *row = eng->jssPlr->row;
-    *pat = eng->jssPlr->pattern;
+    *playing  = eng->jssPlr->isPlaying;
+    *row      = eng->jssPlr->row;
+    *pat      = eng->jssPlr->pattern;
     *npattern = eng->jssPlr->npattern;
-    *order = eng->jssPlr->order;
+    *order    = eng->jssPlr->order;
 
     JSS_UNLOCK(eng->jssPlr);
 }
 
+
 void engineGetJSSChannelInfo(DMEngineData *eng, const int channel, int *ninst, int *nextInst, int *freq, int *note)
 {
     JSS_LOCK(eng->jssPlr);
+
     JSSPlayerChannel *chn = &(eng->jssPlr->channels[channel]);
-    *ninst = chn->ninstrument;
+    *ninst    = chn->ninstrument;
     *nextInst = chn->nextInstrument;
-    *freq = chn->freq;
-    *note = chn->note;
+    *freq     = chn->freq;
+    *note     = chn->note;
+
     JSS_UNLOCK(eng->jssPlr);
 }
 #endif
@@ -391,3 +394,23 @@
 {
     return engineGetTime(engine, t) / 1000;
 }
+
+
+void enginePauseAudio(int status)
+{
+    if (status)
+        engine.audioStatus = SDL_AUDIO_PAUSED;
+    else
+        engine.audioStatus = SDL_AUDIO_PLAYING;
+
+    SDL_PauseAudio(status);
+}
+
+
+int engineGetVideoAspect(int width, int height)
+{
+    if (width > 0 && height > 0)
+        return (width * 1000) / height;
+    else
+        return 1;
+}
--- a/dmengine.h	Tue Aug 06 14:59:44 2013 +0300
+++ b/dmengine.h	Tue Aug 06 14:59:57 2013 +0300
@@ -250,17 +250,22 @@
     int optResFlags;
     char *optDataPath, *optPackFilename;
 
+    // Audio related
     SDL_AudioSpec optAfmt;
     int optAudioSetup;
 
+    int audioStatus;            // Current status of audio output (SDL_AUDIO_PLAYING, etc.)
     DMMutex *audioStreamMutex;
     Uint8 * audioStreamBuf;
-    size_t audioStreamLen;
+    size_t audioStreamLen;      // Length in samples
+    int audioSampleSize;        // Size of one audio sample in bytes (incl. all channels)
 
     // No-sound audio simulation thread stuff
     SDL_Thread *audioSimThread;
     Uint8 * audioSimBuf;
     size_t audioSimBufSize;
+    BOOL audioSimDone;
+    int  audioSimDelay;
 
 #ifdef DM_USE_JSS
     JSSMixer *jssDev;
@@ -361,6 +366,7 @@
 int    engineGetTick(DMEngineData *eng);
 float  engineGetTimeDT(DMEngineData *eng);
 
+void   enginePauseAudio(int status);
 int    engineGetVideoAspect(int width, int height);
 
 
--- a/dmsimple.c	Tue Aug 06 14:59:44 2013 +0300
+++ b/dmsimple.c	Tue Aug 06 14:59:57 2013 +0300
@@ -79,7 +79,14 @@
 #ifdef DM_USE_JSS
         case DM_ASETUP_JSS:
             if (engine.jssDev != NULL)
+            {
                 jvmRenderAudio(engine.jssDev, stream, len / jvmGetSampleSize(engine.jssDev));
+            }
+
+            JSS_LOCK(engine.jssPlr);
+            if (engine.jssPlr != NULL && !engine.jssPlr->isPlaying)
+                engine.exitFlag = TRUE;
+            JSS_UNLOCK(engine.jssPlr);
             break;
 #endif
 #ifdef DM_USE_TREMOR
@@ -102,13 +109,19 @@
 }
 
 
-static void engineAudioThreadFunc(void *userdata)
+static int engineAudioThreadFunc(void *userdata)
 {
     do
     {
-        engineAudioCallback(userdata, engine.audioSimBuf, engine.audioSimBufSize);
+        if (engine.audioStatus == SDL_AUDIO_PLAYING)
+        {
+            engineAudioCallback(userdata, engine.audioSimBuf, engine.audioSimBufSize);
+        }
+
         SDL_Delay(engine.audioSimDelay);
     } while (!engine.audioSimDone);
+
+    return 0;
 }
 
 
@@ -215,9 +228,9 @@
 int engineAddModeToList(int w, int h)
 {
     DMModeEntry *mode;
-    int i;
-    int aspect = engineGetVideoAspect(w, h);
+    int i, aspect = engineGetVideoAspect(w, h);
     
+    dmPrint(2, " - Proposed %d x %d\n", w, h);
     if (aspect <= 0)
         return DMERR_INVALID_ARGS;
     
@@ -255,6 +268,7 @@
     mode->w = w;
     mode->h = h;
     mode->aspect = engineGetVideoAspect(w, h);
+    dmPrint(2, " - %d x %d, %d\n", w, h, mode->aspect);
     
     nengineModeList++;
     
@@ -345,6 +359,7 @@
     if (modes != (SDL_Rect**) -1)
     {
         int i;
+        dmPrint(1, "Enumerating modes.\n");
         for (i = 0; modes[i] != NULL; i++)
             engineAddModeToList(modes[i]->w, modes[i]->h);
     }
@@ -598,15 +613,6 @@
 }
 
 
-int engineGetVideoAspect(int width, int height)
-{
-    if (width > 0 && height > 0)
-        return (width * 1000) / height;
-    else
-        return 1;
-}
-
-
 int main(int argc, char *argv[])
 {
     int err;
@@ -676,7 +682,7 @@
     }
     
     if (engine.optAfmt.samples == 0)
-        engine.optAfmt.samples = engine.optAfmt.freq / 16;
+        engine.optAfmt.samples = engine.optAfmt.freq / 50;
 
     switch (engine.optAudioSetup)
     {
@@ -719,30 +725,31 @@
     }
 
     // Initialize SDL audio
-    dmPrint(1, "Trying to init SDL audio with: fmt=%d, chn=%d, freq=%d\n",
-        engine.optAfmt.format, engine.optAfmt.channels, engine.optAfmt.freq);
+    dmPrint(1, "Trying to init SDL audio with: fmt=%d, chn=%d, freq=%d, samples=%d\n",
+        engine.optAfmt.format, engine.optAfmt.channels,
+        engine.optAfmt.freq, engine.optAfmt.samples);
 
+    engine.audioStatus      = SDL_AUDIO_STOPPED;
     engine.optAfmt.callback = engineAudioCallback;
     engine.audioStreamMutex = dmCreateMutex();
+
+    engine.audioSampleSize  = engine.optAfmt.channels;
+    switch (engine.optAfmt.format)
+    {
+        case AUDIO_S16SYS:
+        case AUDIO_U16SYS: engine.audioSampleSize *= 2; break;
+    }
     
     if (SDL_OpenAudio(&engine.optAfmt, NULL) < 0)
     {
-        int sampleSize;
-
         // We'll let this pass, as we want to support no-sound.
         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.audioSimDelay   = 1000 / 45;
+        engine.audioSimBufSize = (engine.optAfmt.freq / 45) * engine.audioSampleSize;
         engine.audioSimBuf     = dmMalloc(engine.audioSimBufSize);
+        engine.audioSimDone    = FALSE;
         engine.audioSimThread  = SDL_CreateThread(engineAudioThreadFunc, NULL);
     }
 
@@ -815,7 +822,7 @@
     dmPrint(1, "Starting up.\n");
 
     SDL_LockAudio();
-    SDL_PauseAudio(0);
+    enginePauseAudio(0);
     SDL_UnlockAudio();
 
     engine.startTime = SDL_GetTicks();
@@ -921,7 +928,7 @@
     SDL_ShowCursor(SDL_ENABLE);
 
     SDL_LockAudio();
-    SDL_PauseAudio(1);
+    enginePauseAudio(1);
 #ifdef DM_USE_JSS
     if (engine.optAudioSetup == DM_ASETUP_JSS)
     {