diff tools/mod2wav.c @ 2530:aacf3bd1cceb

Cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 16 May 2020 06:38:52 +0300
parents bc05bcfc4598
children d56a0e86067a
line wrap: on
line diff
--- a/tools/mod2wav.c	Sat May 16 06:11:53 2020 +0300
+++ b/tools/mod2wav.c	Sat May 16 06:38:52 2020 +0300
@@ -230,7 +230,7 @@
     JSSPlayer *plr = NULL;
     size_t bufLen = 1024*4, dataTotal, dataWritten, sampSize;
     Uint8 *dataBuf = NULL;
-    int result = -1;
+    int res;
 
     dmInitProg("mod2wav", "XM/JSSMOD to WAV renderer", "0.2", NULL, NULL);
     dmVerbosity = 1;
@@ -238,66 +238,80 @@
     // Parse arguments
     if (!dmArgsProcess(argc, argv, optList, optListN,
         argHandleOpt, argHandleFile, OPTH_BAILOUT))
-        return 1;
+    {
+        res = 1;
+        goto exit;
+    }
+
 
     // Check arguments
     if (optInFilename == NULL || optOutFilename == NULL)
     {
-        dmErrorMsg("Input or output file not specified. Try --help.\n");
-        return 1;
+        res = dmError(DMERR_INVALID_ARGS,
+            "Input or output file not specified. Try --help.\n");
+        goto exit;
     }
 
     // Initialize miniJSS
     jssInit();
 
     // Open the source file
-    if ((result = dmf_open_stdio(optInFilename, "rb", &inFile)) != DMERR_OK)
+    if ((res = dmf_open_stdio(optInFilename, "rb", &inFile)) != DMERR_OK)
     {
-        dmErrorMsg("Error opening input file '%s', %d: %s\n",
-            optInFilename, result, dmErrorStr(result));
+        dmErrorMsg("Error opening input file '%s': %s\n",
+            optInFilename, dmErrorStr(res));
         goto exit;
     }
 
     // Read module file
     dmMsg(1, "Reading file: %s\n", optInFilename);
 #ifdef JSS_SUP_XM
-    result = jssLoadXM(inFile, &mod, TRUE);
+    if (mod == NULL)
+    {
+        dmMsg(2, "* Trying XM...\n");
+        dmfreset(inFile);
+        if ((res = jssLoadXM(inFile, &mod, TRUE)) == DMERR_OK)
+        {
+            dmfreset(inFile);
+            res = jssLoadXM(inFile, &mod, FALSE);
+        }
+    }
 #endif
 #ifdef JSS_SUP_JSSMOD
-    dmfreset(inFile);
-    if (result != DMERR_OK)
+    if (mod == NULL)
     {
         dmMsg(1, "* Trying JSSMOD ...\n");
-        result = jssLoadJSSMOD(inFile, &mod, TRUE);
         dmfreset(inFile);
-        if (result == DMERR_OK)
-            result = jssLoadJSSMOD(inFile, &mod, FALSE);
-    }
-    else
-    {
-        dmMsg(2, "* Trying XM...\n");
-        result = jssLoadXM(inFile, &mod, FALSE);
+        if ((res = jssLoadJSSMOD(inFile, &mod, TRUE)) == DMERR_OK)
+        {
+            dmfreset(inFile);
+            res = jssLoadJSSMOD(inFile, &mod, FALSE);
+        }
     }
 #endif
     dmf_close(inFile);
 
     // Check for errors, we still might have some data tho
-    if (result != DMERR_OK)
+    if (res != DMERR_OK)
     {
-        dmErrorMsg("Error loading module file, %d: %s\n",
-            result, dmErrorStr(result));
+        dmErrorMsg("Error loading module file: %s\n",
+            dmErrorStr(res));
         goto exit;
     }
 
     // Check if we have anything
     if (mod == NULL)
+    {
+        res = dmError(DMERR_INIT_FAIL,
+            "Could not load module file.\n");
         goto exit;
+    }
 
     // Try to convert it
-    if ((result = jssConvertModuleForPlaying(mod)) != DMERR_OK)
+    if ((res = jssConvertModuleForPlaying(mod)) != DMERR_OK)
     {
-        dmErrorMsg("Could not convert module for playing, %d: %s\n",
-            result, dmErrorStr(result));
+        dmErrorMsg("Could not convert module for playing: %s\n",
+            dmErrorStr(res));
         goto exit;
     }
 
@@ -305,15 +319,17 @@
     dev = jvmInit(optOutFormat, optOutChannels, optOutFreq, JMIX_AUTO);
     if (dev == NULL)
     {
-        dmErrorMsg("jvmInit() returned NULL\n");
+        res = dmError(DMERR_INIT_FAIL,
+            "jvmInit() returned NULL\n");
         goto exit;
     }
 
     sampSize = jvmGetSampleSize(dev);
     if ((dataBuf = dmMalloc(bufLen * sampSize)) == NULL)
     {
-        dmErrorMsg("Could not allocate mixing buffer\n");
-        return 5;
+        res = dmError(DMERR_MALLOC,
+            "Could not allocate mixing buffer.\n");
+        goto exit;
     }
 
     dmMsg(1, "Using fmt=%d, bits=%d, channels=%d, freq=%d [%" DM_PRIu_SIZE_T " / sample]\n",
@@ -334,27 +350,31 @@
     jmpSetModule(plr, mod);
     if (optStartOrder >= 0)
     {
-        dmMsg(1, "Starting from song order #%d\n", optStartOrder);
-    } else
+        dmMsg(1, "Starting from song order #%d\n",
+            optStartOrder);
+    }
+    else
+    {
         optStartOrder = 0;
+    }
 
     jmpPlayOrder(plr, optStartOrder);
     jvmSetGlobalVol(dev, 150);
 
     if (optMuteOChannels > 0 && optMuteOChannels <= mod->nchannels)
     {
-        int i;
-        for (i = 0; i < mod->nchannels; i++)
+        for (int i = 0; i < mod->nchannels; i++)
             jvmMute(dev, i, TRUE);
+
         jvmMute(dev, optMuteOChannels - 1, FALSE);
     }
 
     // Open output file
     if ((outFile = fopen(optOutFilename, "wb")) == NULL)
     {
-        int err = dmGetErrno();
-        dmErrorMsg("Error opening output file '%s' #%d: %s.\n",
-            optInFilename, err, dmErrorStr(err));
+        res = dmGetErrno();
+        dmErrorMsg("Error opening output file '%s': %s.\n",
+            optInFilename, dmErrorStr(res));
         goto exit;
     }
 
@@ -380,12 +400,13 @@
         {
             jvmRenderAudio(dev, dataBuf, writeLen);
 #if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
-            jssEncodeSample16((Uint16 *)dataBuf, writeLen * optOutChannels, jsampSwapEndianess);
+            jssEncodeSample16((Uint16 *) dataBuf, writeLen * optOutChannels, jsampSwapEndianess);
 #endif
             dataWritten = fwrite(dataBuf, sampSize, writeLen, outFile);
             if (dataWritten < writeLen)
             {
-                dmErrorMsg("Error writing data!\n");
+                res = dmError(DMERR_FWRITE,
+                    "Error writing audio data!\n");
                 goto exit;
             }
             dataTotal += dataWritten;
@@ -398,7 +419,8 @@
     // Write the correct header
     if (fseek(outFile, 0L, SEEK_SET) != 0)
     {
-        dmErrorMsg("Error rewinding to header position!\n");
+        res = dmError(DMERR_FSEEK,
+            "Error rewinding to header position!\n");
         goto exit;
     }
 
@@ -417,5 +439,5 @@
     jssFreeModule(mod);
     jssClose();
 
-    return 0;
+    return res;
 }