changeset 1157:e13269e17422

Clean up the sample data loading a bit.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 05 Mar 2015 06:59:45 +0200
parents 622a469932b1
children 31bc984b7cd4
files minijss/jloadxm.c
diffstat 1 files changed, 28 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/minijss/jloadxm.c	Thu Mar 05 06:12:06 2015 +0200
+++ b/minijss/jloadxm.c	Thu Mar 05 06:59:45 2015 +0200
@@ -404,16 +404,6 @@
         }
     }
 
-    // Allocate memory for sample data
-    if (inst->flags & jsf16bit)
-        inst->data = dmCalloc(inst->size, sizeof(Uint16));
-    else
-        inst->data = dmCalloc(inst->size, sizeof(Uint8));
-
-    if (inst->data == NULL)
-        JSSERROR(DMERR_MALLOC, DMERR_MALLOC,
-        "Could not allocate sample data #%d/%d.\n", ninst, nsample);
-
     return DMERR_OK;
 }
 
@@ -421,6 +411,7 @@
 static int jssXMLoadInstrumentData(DMResource *inFile, JSSInstrument *inst, int ninst, int nsample)
 {
     int ret;
+    size_t bsize;
 
     JSSDEBUG("desc....: '%s'\n"
          "size....: %d\n"
@@ -432,38 +423,44 @@
          inst->size, inst->loopS, inst->loopE,
          inst->volume, inst->flags);
 
+    // Allocate memory for sample data
+    bsize  = (inst->flags & jsf16bit) ? sizeof(Uint16) : sizeof(Uint8);
+    bsize *= inst->size;
+
+    if ((inst->data = dmMalloc(bsize)) == NULL)
+    {
+        JSSERROR(DMERR_MALLOC, DMERR_MALLOC,
+        "Could not allocate %d bytes of sample data for instrument/sample #%d/%d.\n",
+        bsize, ninst, nsample);
+    }
+
+    // Read sampledata
+    if (dmfread(inst->data, sizeof(Uint8), bsize, inFile) != bsize)
+    {
+        JSSERROR(DMERR_FREAD, DMERR_FREAD,
+        "Error reading sample data for instrument #%d/%d, %d bytes.\n",
+        ninst, nsample, bsize);
+    }
+
+    // Convert the sample data
     if (inst->flags & jsf16bit)
     {
-        // Read sampledata
-        if (dmfread(inst->data, sizeof(Uint16), inst->size, inFile) != (size_t) inst->size)
-            JSSERROR(DMERR_FREAD, DMERR_FREAD,
-            "Error reading sampledata for instrument #%d/%d, %d words.\n",
-            ninst, nsample, inst->size);
-
-        // Convert data
-        if ((ret = jssDecodeSample16((Uint16 *) inst->data, inst->size,
+        ret = jssDecodeSample16(
+            (Uint16 *) inst->data, inst->size,
 #if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
             (jsampDelta | jsampSwapEndianess)
 #else
             (jsampDelta)
 #endif
-            )) != DMERR_OK)
-            return ret;
+            );
     }
     else
     {
-        // Read sampledata
-        if (dmfread(inst->data, sizeof(Uint8), inst->size, inFile) != (size_t) inst->size)
-            JSSERROR(DMERR_FREAD, DMERR_FREAD,
-            "Error reading sampledata for instrument #%d/%d, %d bytes.\n",
-            ninst, nsample, inst->size);
-
-        // Convert data
-        if ((ret = jssDecodeSample8((Uint8 *) inst->data, inst->size,
-            (jsampDelta | jsampFlipSign))) != DMERR_OK)
-            return ret;
+        ret = jssDecodeSample8(
+            (Uint8 *) inst->data, inst->size,
+            (jsampDelta | jsampFlipSign));
     }
-    return DMERR_OK;
+    return ret;
 }