ft2-clone

Fasttracker 2 clone
Log | Files | Refs | README | LICENSE

ft2_load_xm.c (14891B)


      1 /* Fasttracker II (or compatible) XM loader
      2 **
      3 ** Note: Data sanitation is done in the last stage
      4 ** of module loading, so you don't need to do that here.
      5 */
      6 
      7 #include <stdio.h>
      8 #include <stdint.h>
      9 #include <stdbool.h>
     10 #include "../ft2_header.h"
     11 #include "../ft2_module_loader.h"
     12 #include "../ft2_sample_ed.h"
     13 #include "../ft2_tables.h"
     14 #include "../ft2_sysreqs.h"
     15 
     16 static uint8_t packedPattData[65536];
     17 
     18 /* ModPlug Tracker & OpenMPT supports up to 32 samples per instrument for XMs -  we don't.
     19 ** For such modules, we use a temporary array here to store the extra sample data lengths
     20 ** we need to skip to be able to load the file (we lose the extra samples, though...).
     21 */
     22 static uint32_t extraSampleLengths[32-MAX_SMP_PER_INST];
     23 
     24 static bool loadInstrHeader(FILE *f, uint16_t i);
     25 static bool loadInstrSample(FILE *f, uint16_t i);
     26 static void unpackPatt(uint8_t *dst, uint8_t *src, uint16_t len, int32_t antChn);
     27 static bool loadPatterns(FILE *f, uint16_t antPtn, uint16_t xmVersion);
     28 static void unpackPatt(uint8_t *dst, uint8_t *src, uint16_t len, int32_t antChn);
     29 static void loadADPCMSample(FILE *f, sample_t *s); // ModPlug Tracker
     30 
     31 bool loadXM(FILE *f, uint32_t filesize)
     32 {
     33 	xmHdr_t h;
     34 
     35 	if (filesize < sizeof (h))
     36 	{
     37 		loaderMsgBox("Error: This file is either not a module, or is not supported.");
     38 		return false;
     39 	}
     40 
     41 	if (fread(&h, 1, sizeof (h), f) != sizeof (h))
     42 	{
     43 		loaderMsgBox("Error: This file is either not a module, or is not supported.");
     44 		return false;
     45 	}
     46 
     47 	if (h.version < 0x0102 || h.version > 0x0104)
     48 	{
     49 		loaderMsgBox("Error loading XM: Unsupported file version (v%01X.%02X).", (h.version >> 8) & 15, h.version & 0xFF);
     50 		return false;
     51 	}
     52 
     53 	if (h.numOrders > MAX_ORDERS)
     54 	{
     55 		loaderMsgBox("Error loading XM: The song has more than 256 orders!");
     56 		return false;
     57 	}
     58 
     59 	if (h.numPatterns > MAX_PATTERNS)
     60 	{
     61 		loaderMsgBox("Error loading XM: The song has more than 256 patterns!");
     62 		return false;
     63 	}
     64 
     65 	if (h.numChannels == 0)
     66 	{
     67 		loaderMsgBox("Error loading XM: This file is corrupt.");
     68 		return false;
     69 	}
     70 
     71 	if (h.numInstr > 256) // if >128 instruments, we fake-load up to 128 extra instruments and discard them
     72 	{
     73 		loaderMsgBox("Error loading XM: This file is corrupt.");
     74 		return false;
     75 	}
     76 
     77 	fseek(f, 60 + h.headerSize, SEEK_SET);
     78 	if (filesize != 336 && feof(f)) // 336 in length at this point = empty XM
     79 	{
     80 		loaderMsgBox("Error loading XM: The module is empty!");
     81 		return false;
     82 	}
     83 
     84 	memcpy(songTmp.name, h.name, 20);
     85 	songTmp.name[20] = '\0';
     86 
     87 	songTmp.songLength = h.numOrders;
     88 	songTmp.songLoopStart = h.songLoopStart;
     89 	songTmp.numChannels = (uint8_t)h.numChannels;
     90 	songTmp.BPM = h.BPM;
     91 	songTmp.speed = h.speed;
     92 	tmpLinearPeriodsFlag = h.flags & 1;
     93 
     94 	if (songTmp.songLength == 0)
     95 		songTmp.songLength = 1; // songTmp.songTab is already empty
     96 	else
     97 		memcpy(songTmp.orders, h.orders, songTmp.songLength);
     98 
     99 	// some strange XMs have the order list padded with 0xFF, remove them!
    100 	for (int16_t j = 255; j >= 0; j--)
    101 	{
    102 		if (songTmp.orders[j] != 0xFF)
    103 			break;
    104 
    105 		if (songTmp.songLength > j)
    106 			songTmp.songLength = j;
    107 	}
    108 
    109 	// even though XM supports 256 orders, FT2 supports only 255...
    110 	if (songTmp.songLength > 255)
    111 		songTmp.songLength = 255;
    112 
    113 	if (h.version < 0x0104)
    114 	{
    115 		// XM v1.02 and XM v1.03
    116 
    117 		for (uint16_t i = 1; i <= h.numInstr; i++)
    118 		{
    119 			if (!loadInstrHeader(f, i))
    120 				return false;
    121 		}
    122 
    123 		if (!loadPatterns(f, h.numPatterns, h.version))
    124 			return false;
    125 
    126 		for (uint16_t i = 1; i <= h.numInstr; i++)
    127 		{
    128 			if (!loadInstrSample(f, i))
    129 				return false;
    130 		}
    131 	}
    132 	else
    133 	{
    134 		// XM v1.04 (latest version)
    135 
    136 		if (!loadPatterns(f, h.numPatterns, h.version))
    137 			return false;
    138 
    139 		for (uint16_t i = 1; i <= h.numInstr; i++)
    140 		{
    141 			if (!loadInstrHeader(f, i))
    142 				return false;
    143 
    144 			if (!loadInstrSample(f, i))
    145 				return false;
    146 		}
    147 	}
    148 
    149 	// if we temporarily loaded more than 128 instruments, clear the extra allocated memory
    150 	if (h.numInstr > MAX_INST)
    151 	{
    152 		for (int32_t i = MAX_INST+1; i <= h.numInstr; i++)
    153 		{
    154 			if (instrTmp[i] != NULL)
    155 			{
    156 				free(instrTmp[i]);
    157 				instrTmp[i] = NULL;
    158 			}
    159 		}
    160 	}
    161 
    162 	/* We support loading XMs with up to 32 samples per instrument (ModPlug/OpenMPT),
    163 	** but only the first 16 will be loaded. Now make sure we set the number of samples
    164 	** back to max 16 in the headers before loading is done.
    165 	*/
    166 	bool instrHasMoreThan16Samples = false;
    167 	for (int32_t i = 1; i <= MAX_INST; i++)
    168 	{
    169 		if (instrTmp[i] != NULL && instrTmp[i]->numSamples > MAX_SMP_PER_INST)
    170 		{
    171 			instrHasMoreThan16Samples = true;
    172 			instrTmp[i]->numSamples = MAX_SMP_PER_INST;
    173 		}
    174 	}
    175 
    176 	if (songTmp.numChannels > MAX_CHANNELS)
    177 	{
    178 		songTmp.numChannels = MAX_CHANNELS;
    179 		loaderMsgBox("Warning: Module contains >32 channels. The extra channels will be discarded!");
    180 	}
    181 
    182 	if (h.numInstr > MAX_INST)
    183 		loaderMsgBox("Warning: Module contains >128 instruments. The extra instruments will be discarded!");
    184 
    185 	if (instrHasMoreThan16Samples)
    186 		loaderMsgBox("Warning: Module contains instrument(s) with >16 samples. The extra samples will be discarded!");
    187 
    188 	return true;
    189 }
    190 
    191 static bool loadInstrHeader(FILE *f, uint16_t i)
    192 {
    193 	uint32_t readSize;
    194 	xmInsHdr_t ih;
    195 	instr_t *ins;
    196 	xmSmpHdr_t *src;
    197 	sample_t *s;
    198 
    199 	memset(extraSampleLengths, 0, sizeof (extraSampleLengths));
    200 	memset(&ih, 0, sizeof (ih));
    201 
    202 	fread(&readSize, 4, 1, f);
    203 	fseek(f, -4, SEEK_CUR);
    204 
    205 	// yes, some XMs can have a header size of 0, and it usually means 263 bytes (INSTR_HEADER_SIZE)
    206 	if (readSize == 0 || readSize > INSTR_HEADER_SIZE)
    207 		readSize = INSTR_HEADER_SIZE;
    208 
    209 	if ((int32_t)readSize < 0)
    210 	{
    211 		loaderMsgBox("Error loading XM: This file is corrupt!");
    212 		return false;
    213 	}
    214 
    215 	fread(&ih, readSize, 1, f); // read instrument header
    216 
    217 	// FT2 bugfix: skip instrument header data if instrSize is above INSTR_HEADER_SIZE
    218 	if (ih.instrSize > INSTR_HEADER_SIZE)
    219 		fseek(f, ih.instrSize-INSTR_HEADER_SIZE, SEEK_CUR);
    220 
    221 	if (ih.numSamples < 0 || ih.numSamples > 32)
    222 	{
    223 		loaderMsgBox("Error loading XM: This file is corrupt (or not supported)!");
    224 		return false;
    225 	}
    226 
    227 	if (i <= MAX_INST) // copy over instrument names
    228 		memcpy(songTmp.instrName[i], ih.name, 22);
    229 
    230 	if (ih.numSamples > 0 && ih.numSamples <= 32)
    231 	{
    232 		if (!allocateTmpInstr(i))
    233 		{
    234 			loaderMsgBox("Not enough memory!");
    235 			return false;
    236 		}
    237 
    238 		// copy instrument header elements to our instrument struct
    239 
    240 		ins = instrTmp[i];
    241 		memcpy(ins->note2SampleLUT, ih.note2SampleLUT, 96);
    242 		memcpy(ins->volEnvPoints, ih.volEnvPoints, 12*2*sizeof(int16_t));
    243 		memcpy(ins->panEnvPoints, ih.panEnvPoints, 12*2*sizeof(int16_t));
    244 		ins->volEnvLength = ih.volEnvLength;
    245 		ins->panEnvLength = ih.panEnvLength;
    246 		ins->volEnvSustain = ih.volEnvSustain;
    247 		ins->volEnvLoopStart = ih.volEnvLoopStart;
    248 		ins->volEnvLoopEnd = ih.volEnvLoopEnd;
    249 		ins->panEnvSustain = ih.panEnvSustain;
    250 		ins->panEnvLoopStart = ih.panEnvLoopStart;
    251 		ins->panEnvLoopEnd = ih.panEnvLoopEnd;
    252 		ins->volEnvFlags = ih.volEnvFlags;
    253 		ins->panEnvFlags = ih.panEnvFlags;
    254 		ins->autoVibType = ih.vibType;
    255 		ins->autoVibSweep = ih.vibSweep;
    256 		ins->autoVibDepth = ih.vibDepth;
    257 		ins->autoVibRate = ih.vibRate;
    258 		ins->fadeout = ih.fadeout;
    259 		ins->midiOn = (ih.midiOn == 1) ? true : false;
    260 		ins->midiChannel = ih.midiChannel;
    261 		ins->midiProgram = ih.midiProgram;
    262 		ins->midiBend = ih.midiBend;
    263 		ins->mute = (ih.mute == 1) ? true : false; // correct logic, don't change this!
    264 		ins->numSamples = ih.numSamples; // used in loadInstrSample()
    265 
    266 		int32_t sampleHeadersToRead = ih.numSamples;
    267 		if (sampleHeadersToRead > MAX_SMP_PER_INST)
    268 			sampleHeadersToRead = MAX_SMP_PER_INST;
    269 
    270 		if (fread(ih.smp, sampleHeadersToRead * sizeof (xmSmpHdr_t), 1, f) != 1)
    271 		{
    272 			loaderMsgBox("General I/O error during loading!");
    273 			return false;
    274 		}
    275 
    276 		// if instrument contains more than 16 sample headers (unsupported), skip them
    277 		if (ih.numSamples > MAX_SMP_PER_INST) // can only be 0..32 at this point
    278 		{
    279 			const int32_t samplesToSkip = ih.numSamples-MAX_SMP_PER_INST;
    280 			for (int32_t j = 0; j < samplesToSkip; j++)
    281 			{
    282 				fread(&extraSampleLengths[j], 4, 1, f); // used for skipping data in loadInstrSample()
    283 				fseek(f, sizeof (xmSmpHdr_t)-4, SEEK_CUR);
    284 			}
    285 		}
    286 
    287 		for (int32_t j = 0; j < sampleHeadersToRead; j++)
    288 		{
    289 			s = &instrTmp[i]->smp[j];
    290 			src = &ih.smp[j];
    291 
    292 			// copy sample header elements to our sample struct
    293 
    294 			s->length = src->length;
    295 			s->loopStart = src->loopStart;
    296 			s->loopLength = src->loopLength;
    297 			s->volume = src->volume;
    298 			s->finetune = src->finetune;
    299 			s->flags = src->flags;
    300 			s->panning = src->panning;
    301 			s->relativeNote = src->relativeNote;
    302 
    303 			/* If the sample is 8-bit mono and nameLength (reserved) is 0xAD,
    304 			** then this is a 4-bit ADPCM compressed sample (ModPlug Tracker).
    305 			*/
    306 			if (src->nameLength == 0xAD && !(src->flags & (SAMPLE_16BIT | SAMPLE_STEREO)))
    307 				s->flags |= SAMPLE_ADPCM;
    308 
    309 			memcpy(s->name, src->name, 22);
    310 
    311 			// dst->dataPtr is set up later
    312 		}
    313 	}
    314 
    315 	return true;
    316 }
    317 
    318 static bool loadInstrSample(FILE *f, uint16_t i)
    319 {
    320 	if (instrTmp[i] == NULL)
    321 		return true; // empty instrument, let's just pretend it got loaded successfully
    322 
    323 	uint16_t k = instrTmp[i]->numSamples;
    324 	if (k > MAX_SMP_PER_INST)
    325 		k = MAX_SMP_PER_INST;
    326 
    327 	sample_t *s = instrTmp[i]->smp;
    328 
    329 	if (i > MAX_INST) // insNum > 128, just skip sample data
    330 	{
    331 		for (uint16_t j = 0; j < k; j++, s++)
    332 		{
    333 			if (s->length > 0)
    334 				fseek(f, s->length, SEEK_CUR);
    335 		}
    336 	}
    337 	else
    338 	{
    339 		for (uint16_t j = 0; j < k; j++, s++)
    340 		{
    341 			if (s->length <= 0)
    342 			{
    343 				s->length = 0;
    344 				s->loopStart = 0;
    345 				s->loopLength = 0;
    346 				s->flags = 0;
    347 			}
    348 			else
    349 			{
    350 				const int32_t lengthInFile = s->length;
    351 
    352 				bool sample16Bit = !!(s->flags & SAMPLE_16BIT);
    353 				bool stereoSample = !!(s->flags & SAMPLE_STEREO);
    354 				bool adpcmSample = !!(s->flags & SAMPLE_ADPCM); // ModPlug Tracker
    355 
    356 				if (sample16Bit) // we use units of samples (not bytes like in FT2)
    357 				{
    358 					s->length >>= 1;
    359 					s->loopStart >>= 1;
    360 					s->loopLength >>= 1;
    361 				}
    362 
    363 				if (s->length > MAX_SAMPLE_LEN)
    364 					s->length = MAX_SAMPLE_LEN;
    365 
    366 				if (!allocateSmpData(s, s->length, sample16Bit))
    367 				{
    368 					loaderMsgBox("Not enough memory!");
    369 					return false;
    370 				}
    371 
    372 				if (adpcmSample)
    373 				{
    374 					loadADPCMSample(f, s);
    375 				}
    376 				else
    377 				{
    378 					const int32_t sampleLengthInBytes = SAMPLE_LENGTH_BYTES(s);
    379 					fread(s->dataPtr, 1, sampleLengthInBytes, f);
    380 
    381 					if (sampleLengthInBytes < lengthInFile)
    382 						fseek(f, lengthInFile-sampleLengthInBytes, SEEK_CUR);
    383 
    384 					delta2Samp(s->dataPtr, s->length, s->flags);
    385 
    386 					if (stereoSample) // stereo sample - already downmixed to mono in delta2samp()
    387 					{
    388 						s->length >>= 1;
    389 						s->loopStart >>= 1;
    390 						s->loopLength >>= 1;
    391 
    392 						reallocateSmpData(s, s->length, sample16Bit); // dealloc unused memory
    393 					}
    394 				}
    395 			}
    396 
    397 			// remove stereo flag if present (already handled)
    398 			if (s->flags & SAMPLE_STEREO)
    399 				s->flags &= ~SAMPLE_STEREO;
    400 		}
    401 	}
    402 
    403 	// skip sample headers if we have more than 16 samples in instrument
    404 	if (instrTmp[i]->numSamples > MAX_SMP_PER_INST)
    405 	{
    406 		const int32_t samplesToSkip = instrTmp[i]->numSamples-MAX_SMP_PER_INST;
    407 		for (i = 0; i < samplesToSkip; i++)
    408 		{
    409 			if (extraSampleLengths[i] > 0)
    410 				fseek(f, extraSampleLengths[i], SEEK_CUR); 
    411 		}
    412 	}
    413 
    414 	return true;
    415 }
    416 
    417 static bool loadPatterns(FILE *f, uint16_t antPtn, uint16_t xmVersion)
    418 {
    419 	uint8_t tmpLen;
    420 	xmPatHdr_t ph;
    421 
    422 	bool pattLenWarn = false;
    423 	for (uint16_t i = 0; i < antPtn; i++)
    424 	{
    425 		if (fread(&ph.headerSize, 4, 1, f) != 1)
    426 			goto pattCorrupt;
    427 
    428 		if (fread(&ph.type, 1, 1, f) != 1)
    429 			goto pattCorrupt;
    430 
    431 		ph.numRows = 0;
    432 		if (xmVersion == 0x0102)
    433 		{
    434 			if (fread(&tmpLen, 1, 1, f) != 1)
    435 				goto pattCorrupt;
    436 
    437 			if (fread(&ph.dataSize, 2, 1, f) != 1)
    438 				goto pattCorrupt;
    439 
    440 			ph.numRows = tmpLen + 1; // +1 in v1.02
    441 
    442 			if (ph.headerSize > 8)
    443 				fseek(f, ph.headerSize - 8, SEEK_CUR);
    444 		}
    445 		else
    446 		{
    447 			if (fread(&ph.numRows, 2, 1, f) != 1)
    448 				goto pattCorrupt;
    449 
    450 			if (fread(&ph.dataSize, 2, 1, f) != 1)
    451 				goto pattCorrupt;
    452 
    453 			if (ph.headerSize > 9)
    454 				fseek(f, ph.headerSize - 9, SEEK_CUR);
    455 		}
    456 
    457 		if (feof(f))
    458 			goto pattCorrupt;
    459 
    460 		patternNumRowsTmp[i] = ph.numRows;
    461 		if (patternNumRowsTmp[i] > MAX_PATT_LEN)
    462 		{
    463 			patternNumRowsTmp[i] = MAX_PATT_LEN;
    464 			pattLenWarn = true;
    465 		}
    466 
    467 		if (ph.dataSize > 0)
    468 		{
    469 			if (!allocateTmpPatt(i, patternNumRowsTmp[i]))
    470 			{
    471 				loaderMsgBox("Not enough memory!");
    472 				return false;
    473 			}
    474 
    475 			if (fread(packedPattData, 1, ph.dataSize, f) != ph.dataSize)
    476 				goto pattCorrupt;
    477 
    478 			unpackPatt((uint8_t *)patternTmp[i], packedPattData, patternNumRowsTmp[i], songTmp.numChannels);
    479 			clearUnusedChannels(patternTmp[i], patternNumRowsTmp[i], songTmp.numChannels);
    480 		}
    481 
    482 		if (tmpPatternEmpty(i))
    483 		{
    484 			if (patternTmp[i] != NULL)
    485 			{
    486 				free(patternTmp[i]);
    487 				patternTmp[i] = NULL;
    488 			}
    489 
    490 			patternNumRowsTmp[i] = 64;
    491 		}
    492 	}
    493 
    494 	if (pattLenWarn)
    495 		loaderMsgBox("This module contains pattern(s) with a length above 256! They will be truncated.");
    496 
    497 	return true;
    498 
    499 pattCorrupt:
    500 	loaderMsgBox("Error loading XM: This file is corrupt!");
    501 	return false;
    502 }
    503 
    504 static void unpackPatt(uint8_t *dst, uint8_t *src, uint16_t len, int32_t antChn)
    505 {
    506 	int32_t j;
    507 
    508 	if (dst == NULL)
    509 		return;
    510 
    511 	const int32_t srcEnd = len * (sizeof (note_t) * antChn);
    512 	int32_t srcIdx = 0;
    513 
    514 	int32_t numChannels = antChn;
    515 	if (numChannels > MAX_CHANNELS)
    516 		numChannels = MAX_CHANNELS;
    517 
    518 	const int32_t pitch = sizeof (note_t) * (MAX_CHANNELS - antChn);
    519 	for (int32_t i = 0; i < len; i++)
    520 	{
    521 		for (j = 0; j < numChannels; j++)
    522 		{
    523 			if (srcIdx >= srcEnd)
    524 				return; // error!
    525 
    526 			const uint8_t note = *src++;
    527 			if (note & 0x80)
    528 			{
    529 				*dst++ = (note & 0x01) ? *src++ : 0;
    530 				*dst++ = (note & 0x02) ? *src++ : 0;
    531 				*dst++ = (note & 0x04) ? *src++ : 0;
    532 				*dst++ = (note & 0x08) ? *src++ : 0;
    533 				*dst++ = (note & 0x10) ? *src++ : 0;
    534 			}
    535 			else
    536 			{
    537 				*dst++ = note;
    538 				*dst++ = *src++;
    539 				*dst++ = *src++;
    540 				*dst++ = *src++;
    541 				*dst++ = *src++;
    542 			}
    543 
    544 			srcIdx += sizeof (note_t);
    545 		}
    546 
    547 		// if more than 32 channels, skip rest of the channels for this row
    548 		for (; j < antChn; j++)
    549 		{
    550 			if (srcIdx >= srcEnd)
    551 				return; // error!
    552 
    553 			const uint8_t note = *src++;
    554 			if (note & 0x80)
    555 			{
    556 				if (note & 0x01) src++;
    557 				if (note & 0x02) src++;
    558 				if (note & 0x04) src++;
    559 				if (note & 0x08) src++;
    560 				if (note & 0x10) src++;
    561 			}
    562 			else
    563 			{
    564 				src++;
    565 				src++;
    566 				src++;
    567 				src++;
    568 			}
    569 
    570 			srcIdx += sizeof (note_t);
    571 		}
    572 
    573 		// if song has <32 channels, align pointer to next row (skip unused channels)
    574 		if (antChn < MAX_CHANNELS)
    575 			dst += pitch;
    576 	}
    577 }
    578 
    579 static void loadADPCMSample(FILE *f, sample_t *s) // ModPlug Tracker
    580 {
    581 	int8_t deltaLUT[16];
    582 	fread(deltaLUT, 1, 16, f);
    583 
    584 	int8_t *dataPtr = s->dataPtr;
    585 	const int32_t dataLength = (s->length + 1) / 2;
    586 
    587 	int8_t currSample = 0;
    588 	for (int32_t i = 0; i < dataLength; i++)
    589 	{
    590 		const uint8_t nibbles = (uint8_t)fgetc(f);
    591 
    592 		currSample += deltaLUT[nibbles & 0x0F];
    593 		*dataPtr++ = currSample;
    594 
    595 		currSample += deltaLUT[nibbles >> 4];
    596 		*dataPtr++ = currSample;
    597 	}
    598 }