# HG changeset patch # User Matti Hamalainen # Date 1530373499 -10800 # Node ID cedb5ca1533bfba580223103162283cae32a21bb # Parent c27f7cd256848220fe901f30bbad22818a5c2448 Clean up the sample conversion code a bit. Improve error handling in xm2jss. diff -r c27f7cd25684 -r cedb5ca1533b minijss/jssmod.c --- a/minijss/jssmod.c Sat Jun 30 18:09:34 2018 +0300 +++ b/minijss/jssmod.c Sat Jun 30 18:44:59 2018 +0300 @@ -34,14 +34,14 @@ /* Encodes a given 8-bit sample */ -BOOL jssEncodeSample8(Uint8 * data, const size_t len, const int ops) +int jssEncodeSample8(Uint8 * data, const size_t len, const int ops) { - size_t count = len; - Sint8 t, value = 0; + Sint8 value = 0; while (count--) + for (size_t offs = 0; offs < len; offs++) { - t = *data; + Sint8 t = data[offs]; if (ops & jsampFlipSign) t ^= 0x80; @@ -53,7 +53,7 @@ t = n; } - *(data++) = t; + data[offs] = t; } return TRUE; @@ -62,27 +62,27 @@ /* Decodes a given 16-bit sample */ -BOOL jssEncodeSample16(Uint16 * data, const size_t len, const int ops) +int jssEncodeSample16(Uint16 * data, const size_t len, const int ops) { // "Split" the 16-bit samples into 8-bit halves if (ops & jsampSplit) { // Allocate temporary processing buffer - size_t count, bufSize = len * sizeof(Sint16); Uint8 *bp1, *bp2; - Sint16 *sdata, *tmpBuf = dmMalloc(bufSize); - if (tmpBuf == NULL) return FALSE; + size_t bufSize = len * sizeof(Sint16); + Sint16 *tmpBuf; - sdata = tmpBuf; + if ((tmpBuf = dmMalloc(bufSize)) == NULL) + return DMERR_MALLOC; + bp1 = (Uint8 *) data; bp2 = bp1 + len; - count = len; - while (count--) + for (size_t offs = 0; offs < len; offs++) { - Sint16 t = (*sdata++); - *bp1++ = t >> 8; - *bp2++ = t & 0xff; + const Sint16 t = tmpBuf[offs]; + bp1[offs] = t >> 8; + bp2[offs] = t & 0xff; } memcpy(data, tmpBuf, bufSize); @@ -92,22 +92,23 @@ } else { - Sint16 t, p, value = 0, *sdata = (Sint16 *) data; - size_t count = len; + Sint16 value = 0, *sdata = (Sint16 *) data; - while (count--) + for (size_t offs = 0; offs < len; offs++) { + Sint16 t; + if (ops & jsampSwapEndianess) { - p = *sdata; + const Sint16 p = sdata[offs]; t = ((p >> 8) & 0xff) | ((p & 0xff) << 8); } else - t = *sdata; + t = sdata[offs]; if (ops & jsampDelta) { - int n = t - value; + const int n = t - value; value = t; t = n; } @@ -115,10 +116,11 @@ if (ops & jsampFlipSign) t ^= 0x8000; - *(sdata++) = t; + sdata[offs] = t; } } - return TRUE; + + return DMERR_OK; } #endif @@ -128,12 +130,11 @@ */ int jssDecodeSample8(Uint8 * data, const size_t len, const int ops) { - size_t count = len; - Sint8 t, value = 0; + Sint8 value = 0; - while (count--) + for (size_t offs = 0; offs < len; offs++) { - t = *data; + Sint8 t = data[offs]; if (ops & jsampDelta) t = value = t + value; @@ -141,8 +142,9 @@ if (ops & jsampFlipSign) t ^= 0x80; - *(data++) = t; + data[offs] = t; } + return DMERR_OK; } @@ -153,9 +155,9 @@ { if (ops & jsampSplit) { - size_t count, bufSize = len * sizeof(Uint16); + size_t bufSize = len * sizeof(Uint16); Uint8 *bp1, *bp2; - Sint16 *tmpBuf, *sdata; + Sint16 *tmpBuf; int ret; if ((ret = jssDecodeSample8((Uint8 *) data, bufSize, ops)) != DMERR_OK) @@ -164,28 +166,30 @@ if ((tmpBuf = dmMalloc(bufSize)) == NULL) return DMERR_MALLOC; + // Copy original data to temporary buffer memcpy(tmpBuf, data, bufSize); - sdata = (Sint16 *) data; bp1 = (Uint8 *) tmpBuf; bp2 = bp1 + len; - count = len; - while (count--) + + for (size_t offs = 0; offs < len; offs++) { - *sdata++ = (*bp1++ << 8) | (*bp2++ & 0xff); + data[offs] = (bp1[offs] << 8) | (bp2[offs] & 0xff); } dmFree(tmpBuf); } else { - Sint16 t, p, value = 0, *sdata = (Sint16 *) data; - size_t count = len; - while (count--) + Sint16 value = 0, *sdata = (Sint16 *) data; + + for (size_t offs = 0; offs < len; offs++) { + Sint16 t; + if (ops & jsampSwapEndianess) { - p = *sdata; + const Sint16 p = sdata[offs]; t = ((p >> 8) & 0xff) | ((p & 0xff) << 8); } else @@ -197,9 +201,10 @@ if (ops & jsampFlipSign) t ^= 0x8000; - *(sdata++) = t; + sdata[offs] = t; } } + return DMERR_OK; } @@ -208,16 +213,15 @@ */ int jssConvertSampleTo16(void **dst, void * src, const size_t len) { - size_t count = len; Uint8 *in = (Uint8 *) src; Sint16 *out; if ((*dst = out = dmMalloc(sizeof(Sint16) * len)) == NULL) return DMERR_MALLOC; - while (count--) + for (size_t offs = 0; offs < len; offs++) { - *(out++) = (*(in++) * 256) - 32768; + out[offs] = (in[offs] * 256) - 32768; } return DMERR_OK; diff -r c27f7cd25684 -r cedb5ca1533b minijss/jssmod.h --- a/minijss/jssmod.h Sat Jun 30 18:09:34 2018 +0300 +++ b/minijss/jssmod.h Sat Jun 30 18:44:59 2018 +0300 @@ -297,8 +297,8 @@ #ifndef JSS_LIGHT char * jssASCIItoStr(const char *src, const char endByte, const size_t len); -BOOL jssEncodeSample8(Uint8 *, const size_t, const int); -BOOL jssEncodeSample16(Uint16 *, const size_t, const int); +int jssEncodeSample8(Uint8 *, const size_t, const int); +int jssEncodeSample16(Uint16 *, const size_t, const int); #endif int jssDecodeSample8(Uint8 *, const size_t, const int); int jssDecodeSample16(Uint16 *, const size_t, const int); diff -r c27f7cd25684 -r cedb5ca1533b tools/xm2jss.c --- a/tools/xm2jss.c Sat Jun 30 18:09:34 2018 +0300 +++ b/tools/xm2jss.c Sat Jun 30 18:44:59 2018 +0300 @@ -452,7 +452,7 @@ const size_t patBufSize = 256*1024; // 256kB pattern buffer Uint8 *patBuf; size_t totalSize; - int index; + int index, res; // Check the module if (module == NULL) @@ -705,12 +705,19 @@ if (inst->flags & jsf16bit) { bsize *= sizeof(Uint16); - jssEncodeSample16(inst->data, inst->size, flags16); + res = jssEncodeSample16(inst->data, inst->size, flags16); } else { bsize *= sizeof(Uint8); - jssEncodeSample8(inst->data, inst->size, flags8); + res = jssEncodeSample8(inst->data, inst->size, flags8); + } + + if (res != DMERR_OK) + { + JSSERROR(res, res, + "Error encoding sample for instrument #%d: %s\n", + index, dmErrorStr(res)); } if (!dm_fwrite_str(outFile, inst->data, bsize))