shithub: dumb

Download patch

ref: 569b49a533a53b92dceac0eb6b4aa6641f65ae5e
parent: c7aadf292975e96c3036fc4f2e4248a5c727fa5d
author: Chris Moeller <kode54@gmail.com>
date: Tue Feb 19 16:08:02 EST 2013

Implemented support for compressed stereo samples

--- a/dumb/src/it/itread.c
+++ b/dumb/src/it/itread.c
@@ -109,7 +109,7 @@
 /** WARNING - do we even need to pass `right`? */
 /** WARNING - why bother memsetting at all? The whole array is written... */
 // if we do memset, dumb_silence() would be neater...
-static int decompress8(DUMBFILE *f, signed char *data, int len, int it215)
+static int decompress8(DUMBFILE *f, signed char *data, int len, int it215, int stereo)
 {
 	int blocklen, blockpos;
 	byte bitwidth;
@@ -119,7 +119,8 @@
 
 	memset(&crap, 0, sizeof(crap));
 
-	memset(data, 0, len * sizeof(*data));
+	for (blocklen = 0, blockpos = 0; blocklen < len; blocklen++, blockpos += 1 + stereo)
+		data[ blockpos ] = 0;
 
 	while (len > 0) {
 		//Read a block of compressed data:
@@ -185,6 +186,7 @@
 			 * code. Yay, better compression :D
 			 */
 			*data++ = it215 ? d2 : d1;
+			data += stereo;
 			len--;
 			blockpos++;
 		}
@@ -195,7 +197,7 @@
 
 
 
-static int decompress16(DUMBFILE *f, short *data, int len, int it215)
+static int decompress16(DUMBFILE *f, short *data, int len, int it215, int stereo)
 {
 	int blocklen, blockpos;
 	byte bitwidth;
@@ -205,7 +207,8 @@
 
 	memset(&crap, 0, sizeof(crap));
 
-	memset(data, 0, len * sizeof(*data));
+	for ( blocklen = 0, blockpos = 0; blocklen < len; blocklen++, blockpos += 1 + stereo )
+		data[ blockpos ] = 0;
 
 	while (len > 0) {
 		//Read a block of compressed data:
@@ -270,6 +273,7 @@
 			 * code. Yay, better compression :D
 			 */
 			*data++ = it215 ? d2 : d1;
+			data += stereo;
 			len--;
 			blockpos++;
 		}
@@ -652,21 +656,22 @@
 	} else if (sample->flags & 8) {
 		/* If the sample is packed, then we must unpack it. */
 
-		/** WARNING - unresolved business here... test with ModPlug? */
+		/* Behavior as defined by greasemonkey's munch.py and observed by XMPlay and OpenMPT */
 
-		if (sample->flags & IT_SAMPLE_STEREO)
-			//exit(37); // TODO: if this ever happens, maybe sample->length should be doubled below?
-			return -1;
-
-/*
-//#ifndef STEREO_SAMPLES_COUNT_AS_TWO
-		ASSERT(!(sample->flags & IT_SAMPLE_STEREO));
-//#endif
-*/
-		if (sample->flags & IT_SAMPLE_16BIT)
-			decompress16(f, sample->data, datasize, ((cmwt >= 0x215) && (convert & 4)));
-		else
-			decompress8(f, sample->data, datasize, ((cmwt >= 0x215) && (convert & 4)));
+		if (sample->flags & IT_SAMPLE_STEREO) {
+			if (sample->flags & IT_SAMPLE_16BIT) {
+				decompress16(f, (short *) sample->data, datasize >> 1, ((cmwt >= 0x215) && (convert & 4)), 1);
+				decompress16(f, (short *) sample->data + 1, datasize >> 1, ((cmwt >= 0x215) && (convert & 4)), 1);
+			} else {
+				decompress8(f, (signed char *) sample->data, datasize >> 1, ((cmwt >= 0x215) && (convert & 4)), 1);
+				decompress8(f, (signed char *) sample->data + 1, datasize >> 1, ((cmwt >= 0x215) && (convert & 4)), 1);
+			}
+		} else {
+			if (sample->flags & IT_SAMPLE_16BIT)
+				decompress16(f, (short *) sample->data, datasize, ((cmwt >= 0x215) && (convert & 4)), 0);
+			else
+				decompress8(f, (signed char *) sample->data, datasize, ((cmwt >= 0x215) && (convert & 4)), 0);
+		}
  	} else if (sample->flags & IT_SAMPLE_16BIT) {
 		if (sample->flags & IT_SAMPLE_STEREO) {
 			if (convert & 2) {