# HG changeset patch # User Matti Hamalainen # Date 1425531585 -7200 # Node ID e13269e17422bb6cb1e48452c12099d7531ce66e # Parent 622a469932b11f6476fd3625e80d70511b1c75b2 Clean up the sample data loading a bit. diff -r 622a469932b1 -r e13269e17422 minijss/jloadxm.c --- 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; }