changeset 67:b51c7fc264ab

Cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 01 Oct 2012 13:32:18 +0300
parents f18ad054a695
children 182e5fac93f5
files testpl.c
diffstat 1 files changed, 45 insertions(+), 65 deletions(-) [+]
line wrap: on
line diff
--- a/testpl.c	Mon Oct 01 11:52:42 2012 +0300
+++ b/testpl.c	Mon Oct 01 13:32:18 2012 +0300
@@ -109,13 +109,13 @@
 
 int main(int argc, char *argv[])
 {
-    SDL_AudioSpec *a_desired, *a_obtained;
-    DMResource *inFile;
+    SDL_AudioSpec afmt;
+    DMResource *file = NULL;
     char *sname = NULL;
     int result = -1;
-    JSSModule *m;
-    JSSMixer *d;
-    JSSPlayer *p;
+    JSSModule *mod = NULL;
+    JSSMixer *dev = NULL;
+    JSSPlayer *plr = NULL;
     int buflen = 4096;
 
     if (argc > 1)
@@ -123,8 +123,8 @@
 
     // Open the files
     if (sname == NULL)
-        inFile = dmf_create_stdio_stream(stdin);
-    else if ((inFile = dmf_create_stdio(sname)) == NULL)
+        file = dmf_create_stdio_stream(stdin);
+    else if ((file = dmf_create_stdio(sname)) == NULL)
     {
         fprintf(stderr, "Error opening input file '%s'. (%s)\n", sname, strerror(errno));
         return 1;
@@ -139,26 +139,26 @@
     fprintf(stderr, "Reading file: %s\n", sname);
 #ifdef JSS_SUP_XM
     fprintf(stderr, "* Trying XM...\n");
-    result = jssLoadXM(inFile, &m);
+    result = jssLoadXM(file, &mod);
 #endif
 #ifdef JSS_SUP_JSSMOD
     if (result != 0)
     {
-        size_t bufgot, bufsize = dmfsize(inFile);
+        size_t bufgot, bufsize = dmfsize(file);
         Uint8 *buf = dmMalloc(bufsize);
-        dmfseek(inFile, 0L, SEEK_SET);
+        dmfseek(file, 0L, SEEK_SET);
         fprintf(stderr, "* Trying JSSMOD (%d bytes, %p)...\n", bufsize, buf);
-        if ((bufgot = dmfread(buf, 1, bufsize, inFile)) != bufsize)
+        if ((bufgot = dmfread(buf, 1, bufsize, file)) != bufsize)
         {
             fprintf(stderr, "Error reading file (not enough data %d), #%d: %s\n",
-                bufgot, dmferror(inFile), dmErrorStr(dmferror(inFile)));
+                bufgot, dmferror(file), dmErrorStr(dmferror(file)));
             return 2;
         }
-        result = jssLoadJSSMOD(buf, bufsize, &m);
+        result = jssLoadJSSMOD(buf, bufsize, &mod);
         dmFree(buf);
     }
 #endif
-    dmf_close(inFile);
+    dmf_close(file);
 
     if (result != DMERR_OK)
     {
@@ -168,7 +168,7 @@
     }
 
     // Try to convert it
-    if ((result = jssConvertModuleForPlaying(m)) != DMERR_OK)
+    if ((result = jssConvertModuleForPlaying(mod)) != DMERR_OK)
     {
         fprintf(stderr, "Could not convert module for playing, %d: %s\n",
             result, dmErrorStr(result));
@@ -176,92 +176,72 @@
     }
 
     // Initialize SDL audio
-    fprintf(stderr, "Pre-initializing params\n");
-    a_desired = dmMalloc(sizeof(SDL_AudioSpec));
-    a_obtained = dmMalloc(sizeof(SDL_AudioSpec));
-    if (!a_desired || !a_obtained)
-    {
-        fprintf(stderr, "Could not allocate SDL shit\n");
-        return 3;
-    }
-
-    a_desired->freq     = 48000;
-    a_desired->format   = AUDIO_S16SYS;
-    a_desired->channels = 2;
+    afmt.freq     = 48000;
+    afmt.format   = AUDIO_S16SYS;
+    afmt.channels = 2;
     
     // Initialize mixing device
     fprintf(stderr, "Initializing miniJSS mixer with: %d, %d, %d\n",
-        JSS_AUDIO_S16, a_desired->channels, a_desired->freq);
+        JSS_AUDIO_S16, afmt.channels, afmt.freq);
 
-    d = jvmInit(JSS_AUDIO_S16, a_desired->channels, a_desired->freq, JMIX_AUTO);
-    if (!d) {
+    dev = jvmInit(JSS_AUDIO_S16, afmt.channels, afmt.freq, JMIX_AUTO);
+    if (dev == NULL)
+    {
         fprintf(stderr, "jvmInit() returned NULL\n");
         return 3;
     }
     
-    a_desired->samples  = buflen;
-    a_desired->callback = audioCallback;
-    a_desired->userdata = (void *) d;
+    afmt.samples  = buflen;
+    afmt.callback = audioCallback;
+    afmt.userdata = (void *) dev;
 
     // Open the audio device
     fprintf(stderr, "Trying to init SDL with: %d, %d, %d\n",
-        a_desired->format, a_desired->channels, a_desired->freq);
+        afmt.format, afmt.channels, afmt.freq);
     
-    if (SDL_OpenAudio(a_desired, a_obtained) < 0)
+    if (SDL_OpenAudio(&afmt, NULL) < 0)
     {
         fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
         return 4;
     }
     
-    fprintf(stderr, "Got: %d, %d, %d\n",
-        a_obtained->format, a_obtained->channels, a_obtained->freq);
-    
-    if ((a_obtained->format   != a_desired->format) ||
-        (a_obtained->channels != a_desired->channels) ||
-        (a_obtained->freq     != a_desired->freq))
-    {
-        fprintf(stderr, "Could not get wanted audio parameters from SDL!\n");
-        return 8;
-    }
-
-    free(a_desired);
-
     // Initialize player
-    p = jmpInit(d);
-    if (!p)
+    if ((plr = jmpInit(dev)) == NULL)
     {
         fprintf(stderr, "jmpInit() returned NULL\n");
         return 4;
     }
     
-    // Set callback
-    jvmSetCallback(d, jmpExec, p);
-    
     // Initialize playing
-    jmpSetModule(p, m);
-    jmpPlayOrder(p, 0);
-    jvmSetGlobalVol(d, 60);
+    jvmSetCallback(dev, jmpExec, plr);
+    jmpSetModule(plr, mod);
+    jmpPlayOrder(plr, 0);
+    jvmSetGlobalVol(dev, 60);
 
     // okay, main loop here ... "play" module and print out info
     printf("----------------------------------------------------\n");
     SDL_PauseAudio(0);
-    while (p->isPlaying)
+    while (plr->isPlaying)
     {
-        int r = p->row;
-        while (r == p->row && p->isPlaying)
+        int r = plr->row;
+        while (r == plr->row && plr->isPlaying)
             SDL_Delay(50);
 
-        printRow(stdout, p->pattern, p->row);
+        printRow(stdout, plr->pattern, plr->row);
         printf("\n");
     }
     
     printf("----------------------------------------------------\n");
 
-    // Free module data
-    jmpClose(p);
-    jvmClose(d);
-    jssFreeModule(m);
-    m = NULL;
+    SDL_PauseAudio(1);
+
+    SDL_LockAudio();
+    jmpClose(plr);
+    jvmClose(dev);
+    jssFreeModule(mod);
+    SDL_UnlockAudio();
+
+    SDL_Quit();
 
     return 0;
 }