An audio player for macOS 10.8 and newer. https://kode54.net/cog
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1469 lines
48KB

  1. /* _______ ____ __ ___ ___
  2. * \ _ \ \ / \ / \ \ / / ' ' '
  3. * | | \ \ | | || | \/ | . .
  4. * | | | | | | || ||\ /| |
  5. * | | | | | | || || \/ | | ' ' '
  6. * | | | | | | || || | | . .
  7. * | |_/ / \ \__// || | |
  8. * /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
  9. * / \
  10. * / . \
  11. * itread.c - Code to read an Impulse Tracker / / \ \
  12. * module from an open file. | < / \_
  13. * | \/ /\ /
  14. * Based on the loader from an IT player by Bob. \_ / > /
  15. * Adapted for DUMB by entheh. | \ / /
  16. * | ' /
  17. * \__/
  18. */
  19. #include <stdlib.h>
  20. #include <string.h> //might not be necessary later; required for memset
  21. #include "dumb.h"
  22. #include "internal/it.h"
  23. #ifndef min
  24. #define min(a, b) (((a) < (b)) ? (a) : (b))
  25. #endif
  26. //#define INVESTIGATE_OLD_INSTRUMENTS
  27. typedef unsigned char byte;
  28. typedef unsigned short word;
  29. typedef unsigned long dword;
  30. typedef struct readblock_crap readblock_crap;
  31. struct readblock_crap {
  32. unsigned char *sourcebuf;
  33. unsigned char *sourcepos;
  34. unsigned char *sourceend;
  35. int rembits;
  36. };
  37. static int readblock(DUMBFILE *f, readblock_crap *crap) {
  38. long size;
  39. int c;
  40. size = dumbfile_igetw(f);
  41. if (size < 0)
  42. return (int)size;
  43. crap->sourcebuf = malloc(size);
  44. if (!crap->sourcebuf)
  45. return -1;
  46. c = (int)dumbfile_getnc((char *)crap->sourcebuf, size, f);
  47. if (c < size) {
  48. free(crap->sourcebuf);
  49. crap->sourcebuf = NULL;
  50. return -1;
  51. }
  52. crap->sourcepos = crap->sourcebuf;
  53. crap->sourceend = crap->sourcebuf + size;
  54. crap->rembits = 8;
  55. return 0;
  56. }
  57. static void freeblock(readblock_crap *crap) {
  58. free(crap->sourcebuf);
  59. crap->sourcebuf = NULL;
  60. }
  61. static int readbits(int bitwidth, readblock_crap *crap) {
  62. int val = 0;
  63. int b = 0;
  64. if (crap->sourcepos >= crap->sourceend)
  65. return val;
  66. while (bitwidth > crap->rembits) {
  67. val |= *crap->sourcepos++ << b;
  68. if (crap->sourcepos >= crap->sourceend)
  69. return val;
  70. b += crap->rembits;
  71. bitwidth -= crap->rembits;
  72. crap->rembits = 8;
  73. }
  74. val |= (*crap->sourcepos & ((1 << bitwidth) - 1)) << b;
  75. *crap->sourcepos >>= bitwidth;
  76. crap->rembits -= bitwidth;
  77. return val;
  78. }
  79. /** WARNING - do we even need to pass `right`? */
  80. /** WARNING - why bother memsetting at all? The whole array is written... */
  81. // if we do memset, dumb_silence() would be neater...
  82. static int decompress8(DUMBFILE *f, signed char *data, int len, int it215,
  83. int stereo) {
  84. int blocklen, blockpos;
  85. byte bitwidth;
  86. word val;
  87. signed char d1, d2;
  88. readblock_crap crap;
  89. memset(&crap, 0, sizeof(crap));
  90. for (blocklen = 0, blockpos = 0; blocklen < len;
  91. blocklen++, blockpos += 1 + stereo)
  92. data[blockpos] = 0;
  93. while (len > 0) {
  94. // Read a block of compressed data:
  95. if (readblock(f, &crap))
  96. return -1;
  97. // Set up a few variables
  98. blocklen =
  99. (len < 0x8000) ? len : 0x8000; // Max block length is 0x8000 bytes
  100. blockpos = 0;
  101. bitwidth = 9;
  102. d1 = d2 = 0;
  103. // Start the decompression:
  104. while (blockpos < blocklen) {
  105. // Read a value:
  106. val = (word)readbits(bitwidth, &crap);
  107. // Check for bit width change:
  108. if (bitwidth < 7) { // Method 1:
  109. if (val == (1 << (bitwidth - 1))) {
  110. val = (word)readbits(3, &crap) + 1;
  111. bitwidth = (val < bitwidth) ? val : val + 1;
  112. continue;
  113. }
  114. } else if (bitwidth < 9) { // Method 2
  115. byte border = (0xFF >> (9 - bitwidth)) - 4;
  116. if (val > border && val <= (border + 8)) {
  117. val -= border;
  118. bitwidth = (val < bitwidth) ? val : val + 1;
  119. continue;
  120. }
  121. } else if (bitwidth == 9) { // Method 3
  122. if (val & 0x100) {
  123. bitwidth = (val + 1) & 0xFF;
  124. continue;
  125. }
  126. } else { // Illegal width, abort ?
  127. freeblock(&crap);
  128. return -1;
  129. }
  130. // Expand the value to signed byte:
  131. {
  132. signed char v; // The sample value:
  133. if (bitwidth < 8) {
  134. byte shift = 8 - bitwidth;
  135. v = (val << shift);
  136. v >>= shift;
  137. } else
  138. v = (signed char)val;
  139. // And integrate the sample value
  140. //(It always has to end with integration doesn't it ? ;-)
  141. d1 += v;
  142. d2 += d1;
  143. }
  144. // Store !
  145. /* Version 2.15 was an unofficial version with hacked compression
  146. * code. Yay, better compression :D
  147. */
  148. *data++ = it215 ? d2 : d1;
  149. data += stereo;
  150. len--;
  151. blockpos++;
  152. }
  153. freeblock(&crap);
  154. }
  155. return 0;
  156. }
  157. static int decompress16(DUMBFILE *f, short *data, int len, int it215,
  158. int stereo) {
  159. int blocklen, blockpos;
  160. byte bitwidth;
  161. long val;
  162. signed short d1, d2;
  163. readblock_crap crap;
  164. memset(&crap, 0, sizeof(crap));
  165. for (blocklen = 0, blockpos = 0; blocklen < len;
  166. blocklen++, blockpos += 1 + stereo)
  167. data[blockpos] = 0;
  168. while (len > 0) {
  169. // Read a block of compressed data:
  170. if (readblock(f, &crap))
  171. return -1;
  172. // Set up a few variables
  173. blocklen =
  174. (len < 0x4000) ? len : 0x4000; // Max block length is 0x4000 bytes
  175. blockpos = 0;
  176. bitwidth = 17;
  177. d1 = d2 = 0;
  178. // Start the decompression:
  179. while (blockpos < blocklen) {
  180. val = readbits(bitwidth, &crap);
  181. // Check for bit width change:
  182. if (bitwidth < 7) { // Method 1:
  183. if (val == (1 << (bitwidth - 1))) {
  184. val = readbits(4, &crap) + 1;
  185. bitwidth = (val < bitwidth) ? val : val + 1;
  186. continue;
  187. }
  188. } else if (bitwidth < 17) { // Method 2
  189. word border = (0xFFFF >> (17 - bitwidth)) - 8;
  190. if (val > border && val <= (border + 16)) {
  191. val -= border;
  192. bitwidth = val < bitwidth ? val : val + 1;
  193. continue;
  194. }
  195. } else if (bitwidth == 17) { // Method 3
  196. if (val & 0x10000) {
  197. bitwidth = (val + 1) & 0xFF;
  198. continue;
  199. }
  200. } else { // Illegal width, abort ?
  201. freeblock(&crap);
  202. return -1;
  203. }
  204. // Expand the value to signed byte:
  205. {
  206. short v; // The sample value:
  207. if (bitwidth < 16) {
  208. byte shift = 16 - bitwidth;
  209. v = (short)(val << shift);
  210. v >>= shift;
  211. } else
  212. v = (short)val;
  213. // And integrate the sample value
  214. //(It always has to end with integration doesn't it ? ;-)
  215. d1 += v;
  216. d2 += d1;
  217. }
  218. // Store !
  219. /* Version 2.15 was an unofficial version with hacked compression
  220. * code. Yay, better compression :D
  221. */
  222. *data++ = it215 ? d2 : d1;
  223. data += stereo;
  224. len--;
  225. blockpos++;
  226. }
  227. freeblock(&crap);
  228. }
  229. return 0;
  230. }
  231. static int it_read_envelope(IT_ENVELOPE *envelope, DUMBFILE *f) {
  232. int n;
  233. envelope->flags = dumbfile_getc(f);
  234. envelope->n_nodes = dumbfile_getc(f);
  235. if (envelope->n_nodes > 25) {
  236. TRACE("IT error: wrong number of envelope nodes (%d)\n",
  237. envelope->n_nodes);
  238. envelope->n_nodes = 0;
  239. return -1;
  240. }
  241. envelope->loop_start = dumbfile_getc(f);
  242. envelope->loop_end = dumbfile_getc(f);
  243. envelope->sus_loop_start = dumbfile_getc(f);
  244. envelope->sus_loop_end = dumbfile_getc(f);
  245. for (n = 0; n < envelope->n_nodes; n++) {
  246. envelope->node_y[n] = dumbfile_getc(f);
  247. envelope->node_t[n] = dumbfile_igetw(f);
  248. }
  249. dumbfile_skip(f, 75 - envelope->n_nodes * 3 + 1);
  250. if (envelope->n_nodes <= 0)
  251. envelope->flags &= ~IT_ENVELOPE_ON;
  252. else {
  253. if (envelope->loop_end >= envelope->n_nodes ||
  254. envelope->loop_start > envelope->loop_end)
  255. envelope->flags &= ~IT_ENVELOPE_LOOP_ON;
  256. if (envelope->sus_loop_end >= envelope->n_nodes ||
  257. envelope->sus_loop_start > envelope->sus_loop_end)
  258. envelope->flags &= ~IT_ENVELOPE_SUSTAIN_LOOP;
  259. }
  260. return dumbfile_error(f);
  261. }
  262. static int it_read_old_instrument(IT_INSTRUMENT *instrument, DUMBFILE *f) {
  263. int n;
  264. /*if (dumbfile_mgetl(f) != IT_INSTRUMENT_SIGNATURE)
  265. return -1;*/
  266. // XXX
  267. dumbfile_skip(f, 4);
  268. dumbfile_getnc((char *)instrument->filename, 13, f);
  269. instrument->filename[13] = 0;
  270. instrument->volume_envelope.flags = dumbfile_getc(f);
  271. instrument->volume_envelope.loop_start = dumbfile_getc(f);
  272. instrument->volume_envelope.loop_end = dumbfile_getc(f);
  273. instrument->volume_envelope.sus_loop_start = dumbfile_getc(f);
  274. instrument->volume_envelope.sus_loop_end = dumbfile_getc(f);
  275. /* Skip two unused bytes. */
  276. dumbfile_skip(f, 2);
  277. /* In the old instrument format, fadeout ranges from 0 to 64, and is
  278. * subtracted at intervals from a value starting at 512. In the new
  279. * format, all these values are doubled. Therefore we double when loading
  280. * from the old instrument format - that way we don't have to think about
  281. * it later.
  282. */
  283. instrument->fadeout = dumbfile_igetw(f) << 1;
  284. instrument->new_note_action = dumbfile_getc(f);
  285. instrument->dup_check_type = dumbfile_getc(f);
  286. instrument->dup_check_action = DCA_NOTE_CUT; // This might be wrong!
  287. /** WARNING - what is the duplicate check action for old-style instruments?
  288. */
  289. /* Skip Tracker Version and Number of Samples. These are only used in
  290. * separate instrument files. Also skip unused byte.
  291. */
  292. dumbfile_skip(f, 4);
  293. dumbfile_getnc((char *)instrument->name, 26, f);
  294. instrument->name[26] = 0;
  295. /* Skip unused bytes following the Instrument Name. */
  296. dumbfile_skip(f, 6);
  297. instrument->pp_separation = 0;
  298. instrument->pp_centre = 60;
  299. instrument->global_volume = 128;
  300. /** WARNING - should global_volume be 64 or something? */
  301. instrument->default_pan = 32;
  302. /** WARNING - should default_pan be 128, meaning don`t use? */
  303. instrument->random_volume = 0;
  304. instrument->random_pan = 0;
  305. for (n = 0; n < 120; n++) {
  306. instrument->map_note[n] = dumbfile_getc(f);
  307. instrument->map_sample[n] = dumbfile_getc(f);
  308. }
  309. /* Skip "Volume envelope (200 bytes)". */
  310. // - need to know better what this is for though.
  311. dumbfile_skip(f, 200);
  312. #ifdef INVESTIGATE_OLD_INSTRUMENTS
  313. fprintf(stderr, "Inst %02d Env:", n);
  314. #endif
  315. for (n = 0; n < 25; n++) {
  316. instrument->volume_envelope.node_t[n] = dumbfile_getc(f);
  317. instrument->volume_envelope.node_y[n] = dumbfile_getc(f);
  318. #ifdef INVESTIGATE_OLD_INSTRUMENTS
  319. fprintf(stderr, " %d,%d", instrument->volume_envelope.node_t[n],
  320. instrument->volume_envelope.node_y[n]);
  321. #endif
  322. // This loop is unfinished, as we can probably escape from it before
  323. // the end if we want to. Hence the otherwise useless dumbfile_skip()
  324. // call below.
  325. }
  326. dumbfile_skip(f, 50 - (n << 1));
  327. instrument->volume_envelope.n_nodes = n;
  328. #ifdef INVESTIGATE_OLD_INSTRUMENTS
  329. fprintf(stderr, "\n");
  330. #endif
  331. if (dumbfile_error(f))
  332. return -1;
  333. {
  334. IT_ENVELOPE *envelope = &instrument->volume_envelope;
  335. if (envelope->n_nodes <= 0)
  336. envelope->flags &= ~IT_ENVELOPE_ON;
  337. else {
  338. if (envelope->loop_end >= envelope->n_nodes ||
  339. envelope->loop_start > envelope->loop_end)
  340. envelope->flags &= ~IT_ENVELOPE_LOOP_ON;
  341. if (envelope->sus_loop_end >= envelope->n_nodes ||
  342. envelope->sus_loop_start > envelope->sus_loop_end)
  343. envelope->flags &= ~IT_ENVELOPE_SUSTAIN_LOOP;
  344. }
  345. }
  346. instrument->filter_cutoff = 127;
  347. instrument->filter_resonance = 0;
  348. instrument->pan_envelope.flags = 0;
  349. instrument->pitch_envelope.flags = 0;
  350. return 0;
  351. }
  352. static int it_read_instrument(IT_INSTRUMENT *instrument, DUMBFILE *f,
  353. int maxlen) {
  354. int n;
  355. long len;
  356. /*if (dumbfile_mgetl(f) != IT_INSTRUMENT_SIGNATURE)
  357. return -1;*/
  358. // XXX
  359. if (maxlen)
  360. len = dumbfile_pos(f);
  361. else
  362. len = 0;
  363. dumbfile_skip(f, 4);
  364. dumbfile_getnc((char *)instrument->filename, 13, f);
  365. instrument->filename[13] = 0;
  366. instrument->new_note_action = dumbfile_getc(f);
  367. instrument->dup_check_type = dumbfile_getc(f);
  368. instrument->dup_check_action = dumbfile_getc(f);
  369. instrument->fadeout = dumbfile_igetw(f);
  370. instrument->pp_separation = dumbfile_getc(f);
  371. instrument->pp_centre = dumbfile_getc(f);
  372. instrument->global_volume = dumbfile_getc(f);
  373. instrument->default_pan = dumbfile_getc(f);
  374. instrument->random_volume = dumbfile_getc(f);
  375. instrument->random_pan = dumbfile_getc(f);
  376. /* Skip Tracker Version and Number of Samples. These are only used in
  377. * separate instrument files. Also skip unused byte.
  378. */
  379. dumbfile_skip(f, 4);
  380. dumbfile_getnc((char *)instrument->name, 26, f);
  381. instrument->name[26] = 0;
  382. instrument->filter_cutoff = dumbfile_getc(f);
  383. instrument->filter_resonance = dumbfile_getc(f);
  384. /* Skip MIDI Channel, Program and Bank. */
  385. // dumbfile_skip(f, 4);
  386. /*instrument->output = dumbfile_getc(f);
  387. if ( instrument->output > 16 ) {
  388. instrument->output -= 128;
  389. } else {
  390. instrument->output = 0;
  391. }
  392. dumbfile_skip(f, 3);*/
  393. dumbfile_skip(f, 4);
  394. for (n = 0; n < 120; n++) {
  395. instrument->map_note[n] = dumbfile_getc(f);
  396. instrument->map_sample[n] = dumbfile_getc(f);
  397. }
  398. if (dumbfile_error(f))
  399. return -1;
  400. if (it_read_envelope(&instrument->volume_envelope, f))
  401. return -1;
  402. if (it_read_envelope(&instrument->pan_envelope, f))
  403. return -1;
  404. if (it_read_envelope(&instrument->pitch_envelope, f))
  405. return -1;
  406. if (maxlen) {
  407. len = dumbfile_pos(f) - len;
  408. if (maxlen - len < 124)
  409. return 0;
  410. }
  411. if (dumbfile_mgetl(f) == IT_MPTX_SIGNATURE) {
  412. for (n = 0; n < 120; n++) {
  413. instrument->map_sample[n] += dumbfile_getc(f) << 8;
  414. }
  415. if (dumbfile_error(f))
  416. return -1;
  417. }
  418. /*if ( dumbfile_mgetl(f) == IT_INSM_SIGNATURE ) {
  419. long end = dumbfile_igetl(f);
  420. end += dumbfile_pos(f);
  421. while ( dumbfile_pos(f) < end ) {
  422. int chunkid = dumbfile_igetl(f);
  423. switch ( chunkid ) {
  424. case DUMB_ID('P','L','U','G'):
  425. instrument->output = dumbfile_getc(f);
  426. break;
  427. default:
  428. chunkid = chunkid / 0x100 + dumbfile_getc(f)
  429. * 0x1000000; break;
  430. }
  431. }
  432. if (dumbfile_error(f))
  433. return -1;
  434. }*/
  435. return 0;
  436. }
  437. static int it_read_sample_header(IT_SAMPLE *sample, unsigned char *convert,
  438. long *offset, DUMBFILE *f) {
  439. /* XXX
  440. if (dumbfile_mgetl(f) != IT_SAMPLE_SIGNATURE)
  441. return -1;*/
  442. int hax = 0;
  443. long s = dumbfile_mgetl(f);
  444. if (s != IT_SAMPLE_SIGNATURE) {
  445. if (s == (IT_SAMPLE_SIGNATURE >> 16)) {
  446. s <<= 16;
  447. s |= dumbfile_mgetw(f);
  448. if (s != IT_SAMPLE_SIGNATURE)
  449. return -1;
  450. hax = 1;
  451. }
  452. }
  453. dumbfile_getnc((char *)sample->filename, 13, f);
  454. sample->filename[13] = 0;
  455. sample->global_volume = dumbfile_getc(f);
  456. sample->flags = dumbfile_getc(f);
  457. sample->default_volume = dumbfile_getc(f);
  458. dumbfile_getnc((char *)sample->name, 26, f);
  459. sample->name[26] = 0;
  460. *convert = dumbfile_getc(f);
  461. sample->default_pan = dumbfile_getc(f);
  462. sample->length = dumbfile_igetl(f);
  463. sample->loop_start = dumbfile_igetl(f);
  464. sample->loop_end = dumbfile_igetl(f);
  465. sample->C5_speed = dumbfile_igetl(f);
  466. sample->sus_loop_start = dumbfile_igetl(f);
  467. sample->sus_loop_end = dumbfile_igetl(f);
  468. #ifdef STEREO_SAMPLES_COUNT_AS_TWO
  469. if (sample->flags & IT_SAMPLE_STEREO) {
  470. sample->length >>= 1;
  471. sample->loop_start >>= 1;
  472. sample->loop_end >>= 1;
  473. sample->C5_speed >>= 1;
  474. sample->sus_loop_start >>= 1;
  475. sample->sus_loop_end >>= 1;
  476. }
  477. #endif
  478. if (sample->flags & IT_SAMPLE_EXISTS) {
  479. if (sample->length <= 0)
  480. sample->flags &= ~IT_SAMPLE_EXISTS;
  481. else {
  482. if ((unsigned int)sample->loop_end > (unsigned int)sample->length)
  483. sample->flags &= ~IT_SAMPLE_LOOP;
  484. else if ((unsigned int)sample->loop_start >=
  485. (unsigned int)sample->loop_end)
  486. sample->flags &= ~IT_SAMPLE_LOOP;
  487. if ((unsigned int)sample->sus_loop_end >
  488. (unsigned int)sample->length)
  489. sample->flags &= ~IT_SAMPLE_SUS_LOOP;
  490. else if ((unsigned int)sample->sus_loop_start >=
  491. (unsigned int)sample->sus_loop_end)
  492. sample->flags &= ~IT_SAMPLE_SUS_LOOP;
  493. /* We may be able to truncate the sample to save memory. */
  494. if (sample->flags & IT_SAMPLE_LOOP &&
  495. *convert !=
  496. 0xFF) { /* not truncating compressed samples, for now... */
  497. if ((sample->flags & IT_SAMPLE_SUS_LOOP) &&
  498. sample->sus_loop_end >= sample->loop_end)
  499. sample->length = sample->sus_loop_end;
  500. else
  501. sample->length = sample->loop_end;
  502. }
  503. }
  504. }
  505. *offset = dumbfile_igetl(f);
  506. sample->vibrato_speed = dumbfile_getc(f);
  507. sample->vibrato_depth = dumbfile_getc(f);
  508. if (!hax) {
  509. sample->vibrato_rate = dumbfile_getc(f);
  510. sample->vibrato_waveform = dumbfile_getc(f);
  511. } else {
  512. sample->vibrato_rate = 0;
  513. sample->vibrato_waveform = 0;
  514. }
  515. sample->finetune = 0;
  516. sample->max_resampling_quality = -1;
  517. return dumbfile_error(f);
  518. }
  519. long _dumb_it_read_sample_data_adpcm4(IT_SAMPLE *sample, DUMBFILE *f) {
  520. long n, len, delta;
  521. signed char *ptr, *end;
  522. signed char compression_table[16];
  523. if (dumbfile_getnc((char *)compression_table, 16, f) != 16)
  524. return -1;
  525. ptr = (signed char *)sample->data;
  526. delta = 0;
  527. end = ptr + sample->length;
  528. len = (sample->length + 1) / 2;
  529. for (n = 0; n < len; n++) {
  530. int b = dumbfile_getc(f);
  531. if (b < 0)
  532. return -1;
  533. delta += compression_table[b & 0x0F];
  534. *ptr++ = delta;
  535. if (ptr >= end)
  536. break;
  537. delta += compression_table[b >> 4];
  538. *ptr++ = delta;
  539. }
  540. return 0;
  541. }
  542. static long it_read_sample_data(IT_SAMPLE *sample, unsigned char convert,
  543. DUMBFILE *f) {
  544. long n;
  545. long datasize = sample->length;
  546. if (sample->flags & IT_SAMPLE_STEREO)
  547. datasize <<= 1;
  548. sample->data = malloc(datasize * (sample->flags & IT_SAMPLE_16BIT ? 2 : 1));
  549. if (!sample->data)
  550. return -1;
  551. if (!(sample->flags & IT_SAMPLE_16BIT) && (convert == 0xFF)) {
  552. if (_dumb_it_read_sample_data_adpcm4(sample, f) < 0)
  553. return -1;
  554. } else if (sample->flags & 8) {
  555. /* If the sample is packed, then we must unpack it. */
  556. /* Behavior as defined by greasemonkey's munch.py and observed by XMPlay
  557. * and OpenMPT */
  558. if (sample->flags & IT_SAMPLE_STEREO) {
  559. if (sample->flags & IT_SAMPLE_16BIT) {
  560. decompress16(f, (short *)sample->data, (int)(datasize >> 1),
  561. convert & 4, 1);
  562. decompress16(f, (short *)sample->data + 1, (int)(datasize >> 1),
  563. convert & 4, 1);
  564. } else {
  565. decompress8(f, (signed char *)sample->data,
  566. (int)(datasize >> 1), convert & 4, 1);
  567. decompress8(f, (signed char *)sample->data + 1,
  568. (int)(datasize >> 1), convert & 4, 1);
  569. }
  570. } else {
  571. if (sample->flags & IT_SAMPLE_16BIT)
  572. decompress16(f, (short *)sample->data, (int)datasize,
  573. convert & 4, 0);
  574. else
  575. decompress8(f, (signed char *)sample->data, (int)datasize,
  576. convert & 4, 0);
  577. }
  578. } else if (sample->flags & IT_SAMPLE_16BIT) {
  579. if (sample->flags & IT_SAMPLE_STEREO) {
  580. if (convert & 2) {
  581. for (n = 0; n < datasize; n += 2)
  582. ((short *)sample->data)[n] = dumbfile_mgetw(f);
  583. for (n = 1; n < datasize; n += 2)
  584. ((short *)sample->data)[n] = dumbfile_mgetw(f);
  585. } else {
  586. for (n = 0; n < datasize; n += 2)
  587. ((short *)sample->data)[n] = dumbfile_igetw(f);
  588. for (n = 1; n < datasize; n += 2)
  589. ((short *)sample->data)[n] = dumbfile_igetw(f);
  590. }
  591. } else {
  592. if (convert & 2)
  593. for (n = 0; n < datasize; n++)
  594. ((short *)sample->data)[n] = dumbfile_mgetw(f);
  595. else
  596. for (n = 0; n < datasize; n++)
  597. ((short *)sample->data)[n] = dumbfile_igetw(f);
  598. }
  599. } else {
  600. if (sample->flags & IT_SAMPLE_STEREO) {
  601. for (n = 0; n < datasize; n += 2)
  602. ((signed char *)sample->data)[n] = dumbfile_getc(f);
  603. for (n = 1; n < datasize; n += 2)
  604. ((signed char *)sample->data)[n] = dumbfile_getc(f);
  605. } else
  606. for (n = 0; n < datasize; n++)
  607. ((signed char *)sample->data)[n] = dumbfile_getc(f);
  608. }
  609. if (dumbfile_error(f))
  610. return -1;
  611. if (!(convert & 1)) {
  612. /* Convert to signed. */
  613. if (sample->flags & IT_SAMPLE_16BIT)
  614. for (n = 0; n < datasize; n++)
  615. ((short *)sample->data)[n] ^= 0x8000;
  616. else
  617. for (n = 0; n < datasize; n++)
  618. ((signed char *)sample->data)[n] ^= 0x80;
  619. }
  620. /* NOT SUPPORTED:
  621. *
  622. * convert & 4 - Samples stored as delta values
  623. * convert & 16 - Samples stored as TX-Wave 12-bit values
  624. * convert & 32 - Left/Right/All Stereo prompt
  625. */
  626. return 0;
  627. }
  628. //#define DETECT_DUPLICATE_CHANNELS
  629. #ifdef DETECT_DUPLICATE_CHANNELS
  630. #include <stdio.h>
  631. #endif
  632. static int it_read_pattern(IT_PATTERN *pattern, DUMBFILE *f,
  633. unsigned char *buffer) {
  634. unsigned char cmask[DUMB_IT_N_CHANNELS];
  635. unsigned char cnote[DUMB_IT_N_CHANNELS];
  636. unsigned char cinstrument[DUMB_IT_N_CHANNELS];
  637. unsigned char cvolpan[DUMB_IT_N_CHANNELS];
  638. unsigned char ceffect[DUMB_IT_N_CHANNELS];
  639. unsigned char ceffectvalue[DUMB_IT_N_CHANNELS];
  640. #ifdef DETECT_DUPLICATE_CHANNELS
  641. IT_ENTRY *dupentry[DUMB_IT_N_CHANNELS];
  642. #endif
  643. int n_entries = 0;
  644. int buflen;
  645. int bufpos = 0;
  646. IT_ENTRY *entry;
  647. unsigned char channel;
  648. unsigned char mask;
  649. memset(cmask, 0, sizeof(cmask));
  650. memset(cnote, 0, sizeof(cnote));
  651. memset(cinstrument, 0, sizeof(cinstrument));
  652. memset(cvolpan, 0, sizeof(cvolpan));
  653. memset(ceffect, 0, sizeof(ceffect));
  654. memset(ceffectvalue, 0, sizeof(ceffectvalue));
  655. #ifdef DETECT_DUPLICATE_CHANNELS
  656. {
  657. int i;
  658. for (i = 0; i < DUMB_IT_N_CHANNELS; i++)
  659. dupentry[i] = NULL;
  660. }
  661. #endif
  662. buflen = dumbfile_igetw(f);
  663. pattern->n_rows = dumbfile_igetw(f);
  664. /* Skip four unused bytes. */
  665. dumbfile_skip(f, 4);
  666. if (dumbfile_error(f))
  667. return -1;
  668. /* Read in the pattern data. */
  669. dumbfile_getnc((char *)buffer, buflen, f);
  670. if (dumbfile_error(f))
  671. return -1;
  672. /* Scan the pattern data, and work out how many entries we need room for. */
  673. while (bufpos < buflen) {
  674. unsigned char b = buffer[bufpos++];
  675. if (b == 0) {
  676. /* End of row */
  677. n_entries++;
  678. continue;
  679. }
  680. channel = (b - 1) & 63;
  681. if (b & 128)
  682. cmask[channel] = mask = buffer[bufpos++];
  683. else
  684. mask = cmask[channel];
  685. {
  686. static const unsigned char used[16] = {0, 1, 1, 2, 1, 2, 2, 3,
  687. 2, 3, 3, 4, 3, 4, 4, 5};
  688. n_entries += (mask != 0);
  689. bufpos += used[mask & 15];
  690. }
  691. }
  692. pattern->n_entries = n_entries;
  693. pattern->entry = malloc(n_entries * sizeof(*pattern->entry));
  694. if (!pattern->entry)
  695. return -1;
  696. bufpos = 0;
  697. memset(cmask, 0, sizeof(cmask));
  698. entry = pattern->entry;
  699. while (bufpos < buflen) {
  700. unsigned char b = buffer[bufpos++];
  701. if (b == 0) {
  702. /* End of row */
  703. IT_SET_END_ROW(entry);
  704. entry++;
  705. #ifdef DETECT_DUPLICATE_CHANNELS
  706. {
  707. int i;
  708. for (i = 0; i < DUMB_IT_N_CHANNELS; i++)
  709. dupentry[i] = NULL;
  710. }
  711. #endif
  712. continue;
  713. }
  714. channel = (b - 1) & 63;
  715. if (b & 128) {
  716. if (bufpos >= buflen)
  717. return -1;
  718. cmask[channel] = mask = buffer[bufpos++];
  719. } else
  720. mask = cmask[channel];
  721. if (mask) {
  722. entry->mask = (mask & 15) | (mask >> 4);
  723. entry->channel = channel;
  724. if (mask & IT_ENTRY_NOTE) {
  725. if (bufpos >= buflen)
  726. return -1;
  727. cnote[channel] = entry->note = buffer[bufpos++];
  728. } else if (mask & (IT_ENTRY_NOTE << 4))
  729. entry->note = cnote[channel];
  730. if (mask & IT_ENTRY_INSTRUMENT) {
  731. if (bufpos >= buflen)
  732. return -1;
  733. cinstrument[channel] = entry->instrument = buffer[bufpos++];
  734. } else if (mask & (IT_ENTRY_INSTRUMENT << 4))
  735. entry->instrument = cinstrument[channel];
  736. if (mask & IT_ENTRY_VOLPAN) {
  737. if (bufpos >= buflen)
  738. return -1;
  739. cvolpan[channel] = entry->volpan = buffer[bufpos++];
  740. } else if (mask & (IT_ENTRY_VOLPAN << 4))
  741. entry->volpan = cvolpan[channel];
  742. if (mask & IT_ENTRY_EFFECT) {
  743. if (bufpos + 1 >= buflen)
  744. return -1;
  745. ceffect[channel] = entry->effect = buffer[bufpos++];
  746. ceffectvalue[channel] = entry->effectvalue = buffer[bufpos++];
  747. } else {
  748. entry->effect = ceffect[channel];
  749. entry->effectvalue = ceffectvalue[channel];
  750. }
  751. #ifdef DETECT_DUPLICATE_CHANNELS
  752. if (dupentry[channel]) {
  753. FILE *f = fopen("dupentry.txt", "a");
  754. if (!f)
  755. abort();
  756. fprintf(f, "Two events on channel %d:", channel);
  757. fprintf(f, " Event #1:");
  758. if (dupentry[channel]->mask & IT_ENTRY_NOTE)
  759. fprintf(f, " %03d", dupentry[channel]->note);
  760. else
  761. fprintf(f, " ...");
  762. if (dupentry[channel]->mask & IT_ENTRY_INSTRUMENT)
  763. fprintf(f, " %03d", dupentry[channel]->instrument);
  764. else
  765. fprintf(f, " ...");
  766. if (dupentry[channel]->mask & IT_ENTRY_VOLPAN)
  767. fprintf(f, " %03d", dupentry[channel]->volpan);
  768. else
  769. fprintf(f, " ...");
  770. if (dupentry[channel]->mask & IT_ENTRY_EFFECT)
  771. fprintf(f, " %c%02X\n", 'A' - 1 + dupentry[channel]->effect,
  772. dupentry[channel]->effectvalue);
  773. else
  774. fprintf(f, " ...\n");
  775. fprintf(f, " Event #2:");
  776. if (entry->mask & IT_ENTRY_NOTE)
  777. fprintf(f, " %03d", entry->note);
  778. else
  779. fprintf(f, " ...");
  780. if (entry->mask & IT_ENTRY_INSTRUMENT)
  781. fprintf(f, " %03d", entry->instrument);
  782. else
  783. fprintf(f, " ...");
  784. if (entry->mask & IT_ENTRY_VOLPAN)
  785. fprintf(f, " %03d", entry->volpan);
  786. else
  787. fprintf(f, " ...");
  788. if (entry->mask & IT_ENTRY_EFFECT)
  789. fprintf(f, " %c%02X\n", 'A' - 1 + entry->effect,
  790. entry->effectvalue);
  791. else
  792. fprintf(f, " ...\n");
  793. fclose(f);
  794. }
  795. dupentry[channel] = entry;
  796. #endif
  797. entry++;
  798. }
  799. }
  800. ASSERT(entry == pattern->entry + n_entries);
  801. return 0;
  802. }
  803. /* Currently we assume the sample data are stored after the sample headers in
  804. * module files. This assumption may be unjustified; let me know if you have
  805. * trouble.
  806. */
  807. #define IT_COMPONENT_SONG_MESSAGE 1
  808. #define IT_COMPONENT_INSTRUMENT 2
  809. #define IT_COMPONENT_PATTERN 3
  810. #define IT_COMPONENT_SAMPLE 4
  811. typedef struct IT_COMPONENT {
  812. unsigned char type;
  813. unsigned short n;
  814. long offset;
  815. short sampfirst; /* component[sampfirst] = first sample data after this */
  816. short sampnext; /* sampnext is used to create linked lists of sample data */
  817. } IT_COMPONENT;
  818. static int it_component_compare(const void *e1, const void *e2) {
  819. return (int)(((const IT_COMPONENT *)e1)->offset -
  820. ((const IT_COMPONENT *)e2)->offset);
  821. }
  822. static sigdata_t *it_load_sigdata(DUMBFILE *f) {
  823. DUMB_IT_SIGDATA *sigdata;
  824. int cwt, cmwt;
  825. int special;
  826. int message_length, message_offset;
  827. IT_COMPONENT *component;
  828. int min_components;
  829. int n_components = 0;
  830. unsigned char sample_convert[4096];
  831. int n;
  832. unsigned char *buffer;
  833. if (dumbfile_mgetl(f) != IT_SIGNATURE) {
  834. return NULL;
  835. }
  836. sigdata = malloc(sizeof(*sigdata));
  837. if (!sigdata) {
  838. return NULL;
  839. }
  840. sigdata->song_message = NULL;
  841. sigdata->order = NULL;
  842. sigdata->instrument = NULL;
  843. sigdata->sample = NULL;
  844. sigdata->pattern = NULL;
  845. sigdata->midi = NULL;
  846. sigdata->checkpoint = NULL;
  847. dumbfile_getnc((char *)sigdata->name, 26, f);
  848. sigdata->name[26] = 0;
  849. /* Skip pattern row highlight info. */
  850. dumbfile_skip(f, 2);
  851. sigdata->n_orders = dumbfile_igetw(f);
  852. sigdata->n_instruments = dumbfile_igetw(f);
  853. sigdata->n_samples = dumbfile_igetw(f);
  854. sigdata->n_patterns = dumbfile_igetw(f);
  855. cwt = dumbfile_igetw(f);
  856. cmwt = dumbfile_igetw(f);
  857. sigdata->flags = dumbfile_igetw(f);
  858. special = dumbfile_igetw(f);
  859. sigdata->global_volume = dumbfile_getc(f);
  860. sigdata->mixing_volume = dumbfile_getc(f);
  861. sigdata->speed = dumbfile_getc(f);
  862. if (sigdata->speed == 0)
  863. sigdata->speed = 6; // Should we? What about tempo?
  864. sigdata->tempo = dumbfile_getc(f);
  865. sigdata->pan_separation = dumbfile_getc(f); /** WARNING: use this */
  866. /* Skip Pitch Wheel Depth */
  867. dumbfile_skip(f, 1);
  868. message_length = dumbfile_igetw(f);
  869. message_offset = (int)dumbfile_igetl(f);
  870. /* Skip Reserved. */
  871. dumbfile_skip(f, 4);
  872. dumbfile_getnc((char *)sigdata->channel_pan, DUMB_IT_N_CHANNELS, f);
  873. dumbfile_getnc((char *)sigdata->channel_volume, DUMB_IT_N_CHANNELS, f);
  874. // XXX sample count
  875. if (dumbfile_error(f) || sigdata->n_orders <= 0 ||
  876. sigdata->n_orders > 1024 || // Whoa, nelly.
  877. sigdata->n_instruments > 256 || sigdata->n_samples > 4000 ||
  878. sigdata->n_patterns > 256) {
  879. _dumb_it_unload_sigdata(sigdata);
  880. return NULL;
  881. }
  882. sigdata->order = malloc(sigdata->n_orders);
  883. if (!sigdata->order) {
  884. _dumb_it_unload_sigdata(sigdata);
  885. return NULL;
  886. }
  887. if (sigdata->n_instruments) {
  888. sigdata->instrument =
  889. malloc(sigdata->n_instruments * sizeof(*sigdata->instrument));
  890. if (!sigdata->instrument) {
  891. _dumb_it_unload_sigdata(sigdata);
  892. return NULL;
  893. }
  894. }
  895. if (sigdata->n_samples) {
  896. sigdata->sample = malloc(sigdata->n_samples * sizeof(*sigdata->sample));
  897. if (!sigdata->sample) {
  898. _dumb_it_unload_sigdata(sigdata);
  899. return NULL;
  900. }
  901. for (n = 0; n < sigdata->n_samples; n++)
  902. sigdata->sample[n].data = NULL;
  903. }
  904. if (sigdata->n_patterns) {
  905. sigdata->pattern =
  906. malloc(sigdata->n_patterns * sizeof(*sigdata->pattern));
  907. if (!sigdata->pattern) {
  908. _dumb_it_unload_sigdata(sigdata);
  909. return NULL;
  910. }
  911. for (n = 0; n < sigdata->n_patterns; n++)
  912. sigdata->pattern[n].entry = NULL;
  913. }
  914. if (dumbfile_getnc((char *)sigdata->order, sigdata->n_orders, f) <
  915. sigdata->n_orders) {
  916. _dumb_it_unload_sigdata(sigdata);
  917. return NULL;
  918. }
  919. sigdata->restart_position = 0;
  920. min_components = (special & 1) + sigdata->n_instruments +
  921. sigdata->n_samples + sigdata->n_patterns;
  922. component = malloc(min_components * sizeof(*component));
  923. if (!component) {
  924. _dumb_it_unload_sigdata(sigdata);
  925. return NULL;
  926. }
  927. if (special & 1) {
  928. component[n_components].type = IT_COMPONENT_SONG_MESSAGE;
  929. component[n_components].offset = message_offset;
  930. component[n_components].sampfirst = -1;
  931. n_components++;
  932. }
  933. for (n = 0; n < sigdata->n_instruments; n++) {
  934. component[n_components].type = IT_COMPONENT_INSTRUMENT;
  935. component[n_components].n = n;
  936. component[n_components].offset = dumbfile_igetl(f);
  937. component[n_components].sampfirst = -1;
  938. n_components++;
  939. }
  940. for (n = 0; n < sigdata->n_samples; n++) {
  941. component[n_components].type = IT_COMPONENT_SAMPLE;
  942. component[n_components].n = n;
  943. component[n_components].offset = dumbfile_igetl(f);
  944. component[n_components].sampfirst = -1;
  945. n_components++;
  946. }
  947. for (n = 0; n < sigdata->n_patterns; n++) {
  948. long offset = dumbfile_igetl(f);
  949. if (offset) {
  950. component[n_components].type = IT_COMPONENT_PATTERN;
  951. component[n_components].n = n;
  952. component[n_components].offset = offset;
  953. component[n_components].sampfirst = -1;
  954. n_components++;
  955. } else {
  956. /* Empty 64-row pattern */
  957. sigdata->pattern[n].n_rows = 64;
  958. sigdata->pattern[n].n_entries = 0;
  959. }
  960. }
  961. if (dumbfile_error(f)) {
  962. free(component);
  963. _dumb_it_unload_sigdata(sigdata);
  964. return NULL;
  965. }
  966. /*
  967. if (!(sigdata->flags & 128) != !(special & 8)) {
  968. fprintf(stderr, "Flags Bit 7 (\"Request embedded MIDI
  969. configuration\"): %s\n", sigdata->flags & 128 ? "=SET=" : "clear");
  970. fprintf(stderr, "Special Bit 3 (\"MIDI configuration embedded\")
  971. : %s\n", special & 8 ? "=SET=" : "clear"); fprintf(stderr, "entheh
  972. would like to investigate this IT file.\n"); fprintf(stderr, "Please contact
  973. him! entheh@users.sf.net\n");
  974. }
  975. */
  976. if (special & 8) {
  977. /* MIDI configuration is embedded. */
  978. unsigned char mididata[32];
  979. int i;
  980. sigdata->midi = malloc(sizeof(*sigdata->midi));
  981. if (!sigdata->midi) {
  982. free(component);
  983. _dumb_it_unload_sigdata(sigdata);
  984. return NULL;
  985. // Should we be happy with this outcome in some situations?
  986. }
  987. // What are we skipping?
  988. i = dumbfile_igetw(f);
  989. if (dumbfile_error(f) || dumbfile_skip(f, 8 * i)) {
  990. free(component);
  991. _dumb_it_unload_sigdata(sigdata);
  992. return NULL;
  993. }
  994. /* Read embedded MIDI configuration */
  995. // What are the first 9 commands for?
  996. if (dumbfile_skip(f, 32 * 9)) {
  997. free(component);
  998. _dumb_it_unload_sigdata(sigdata);
  999. return NULL;
  1000. }
  1001. for (i = 0; i < 16; i++) {
  1002. unsigned char len = 0;
  1003. int j, leftdigit = -1;
  1004. if (dumbfile_getnc((char *)mididata, 32, f) < 32) {
  1005. free(component);
  1006. _dumb_it_unload_sigdata(sigdata);
  1007. return NULL;
  1008. }
  1009. sigdata->midi->SFmacroz[i] = 0;
  1010. for (j = 0; j < 32; j++) {
  1011. if (leftdigit >= 0) {
  1012. if (mididata[j] == 0) {
  1013. sigdata->midi->SFmacro[i][len++] = leftdigit;
  1014. break;
  1015. } else if (mididata[j] == ' ')
  1016. sigdata->midi->SFmacro[i][len++] = leftdigit;
  1017. else if (mididata[j] >= '0' && mididata[j] <= '9')
  1018. sigdata->midi->SFmacro[i][len++] =
  1019. (leftdigit << 4) | (mididata[j] - '0');
  1020. else if (mididata[j] >= 'A' && mididata[j] <= 'F')
  1021. sigdata->midi->SFmacro[i][len++] =
  1022. (leftdigit << 4) | (mididata[j] - 'A' + 0xA);
  1023. leftdigit = -1;
  1024. } else if (mididata[j] == 0)
  1025. break;
  1026. else if (mididata[j] == 'z')
  1027. sigdata->midi->SFmacroz[i] |= 1 << len++;
  1028. else if (mididata[j] >= '0' && mididata[j] <= '9')
  1029. leftdigit = mididata[j] - '0';
  1030. else if (mididata[j] >= 'A' && mididata[j] <= 'F')
  1031. leftdigit = mididata[j] - 'A' + 0xA;
  1032. }
  1033. sigdata->midi->SFmacrolen[i] = len;
  1034. }
  1035. for (i = 0; i < 128; i++) {
  1036. unsigned char len = 0;
  1037. int j, leftdigit = -1;
  1038. dumbfile_getnc((char *)mididata, 32, f);
  1039. for (j = 0; j < 32; j++) {
  1040. if (leftdigit >= 0) {
  1041. if (mididata[j] == 0) {
  1042. sigdata->midi->Zmacro[i][len++] = leftdigit;
  1043. break;
  1044. } else if (mididata[j] == ' ')
  1045. sigdata->midi->Zmacro[i][len++] = leftdigit;
  1046. else if (mididata[j] >= '0' && mididata[j] <= '9')
  1047. sigdata->midi->Zmacro[i][len++] =
  1048. (leftdigit << 4) | (mididata[j] - '0');
  1049. else if (mididata[j] >= 'A' && mididata[j] <= 'F')
  1050. sigdata->midi->Zmacro[i][len++] =
  1051. (leftdigit << 4) | (mididata[j] - 'A' + 0xA);
  1052. leftdigit = -1;
  1053. } else if (mididata[j] == 0)
  1054. break;
  1055. else if (mididata[j] >= '0' && mididata[j] <= '9')
  1056. leftdigit = mididata[j] - '0';
  1057. else if (mididata[j] >= 'A' && mididata[j] <= 'F')
  1058. leftdigit = mididata[j] - 'A' + 0xA;
  1059. }
  1060. sigdata->midi->Zmacrolen[i] = len;
  1061. }
  1062. }
  1063. sigdata->flags &= IT_REAL_FLAGS;
  1064. qsort(component, n_components, sizeof(IT_COMPONENT), &it_component_compare);
  1065. buffer = malloc(65536);
  1066. if (!buffer) {
  1067. free(component);
  1068. _dumb_it_unload_sigdata(sigdata);
  1069. return NULL;
  1070. }
  1071. for (n = 0; n < n_components; n++) {
  1072. long offset;
  1073. int m;
  1074. /* XXX */
  1075. if (component[n].offset == 0) {
  1076. switch (component[n].type) {
  1077. case IT_COMPONENT_INSTRUMENT:
  1078. memset(&sigdata->instrument[component[n].n], 0,
  1079. sizeof(IT_INSTRUMENT));
  1080. break;
  1081. case IT_COMPONENT_SAMPLE:
  1082. memset(&sigdata->sample[component[n].n], 0, sizeof(IT_SAMPLE));
  1083. break;
  1084. case IT_COMPONENT_PATTERN: {
  1085. IT_PATTERN *p = &sigdata->pattern[component[n].n];
  1086. p->entry = 0;
  1087. p->n_rows = 64;
  1088. p->n_entries = 0;
  1089. } break;
  1090. }
  1091. continue;
  1092. }
  1093. if (dumbfile_seek(f, component[n].offset, DFS_SEEK_SET)) {
  1094. free(buffer);
  1095. free(component);
  1096. _dumb_it_unload_sigdata(sigdata);
  1097. return NULL;
  1098. }
  1099. switch (component[n].type) {
  1100. case IT_COMPONENT_SONG_MESSAGE:
  1101. if (n + 1 < n_components) {
  1102. message_length =
  1103. min(message_length,
  1104. (int)(component[n + 1].offset - component[n].offset));
  1105. }
  1106. sigdata->song_message = malloc(message_length + 1);
  1107. if (sigdata->song_message) {
  1108. if (dumbfile_getnc((char *)sigdata->song_message,
  1109. message_length, f) < message_length) {
  1110. free(buffer);
  1111. free(component);
  1112. _dumb_it_unload_sigdata(sigdata);
  1113. return NULL;
  1114. }
  1115. sigdata->song_message[message_length] = 0;
  1116. }
  1117. break;
  1118. case IT_COMPONENT_INSTRUMENT:
  1119. if (cmwt < 0x200)
  1120. m = it_read_old_instrument(&sigdata->instrument[component[n].n],
  1121. f);
  1122. else
  1123. m = it_read_instrument(
  1124. &sigdata->instrument[component[n].n], f,
  1125. (n + 1 < n_components)
  1126. ? (int)(component[n + 1].offset - component[n].offset)
  1127. : 0);
  1128. if (m) {
  1129. free(buffer);
  1130. free(component);
  1131. _dumb_it_unload_sigdata(sigdata);
  1132. return NULL;
  1133. }
  1134. break;
  1135. case IT_COMPONENT_PATTERN:
  1136. if (it_read_pattern(&sigdata->pattern[component[n].n], f, buffer)) {
  1137. free(buffer);
  1138. free(component);
  1139. _dumb_it_unload_sigdata(sigdata);
  1140. return NULL;
  1141. }
  1142. break;
  1143. case IT_COMPONENT_SAMPLE:
  1144. if (it_read_sample_header(&sigdata->sample[component[n].n],
  1145. &sample_convert[component[n].n], &offset,
  1146. f)) {
  1147. free(buffer);
  1148. free(component);
  1149. _dumb_it_unload_sigdata(sigdata);
  1150. return NULL;
  1151. }
  1152. if (sigdata->sample[component[n].n].flags & IT_SAMPLE_EXISTS) {
  1153. short *sample;
  1154. for (m = n + 1; m < n_components; m++)
  1155. if (component[m].offset > offset)
  1156. break;
  1157. m--;
  1158. sample = &component[m].sampfirst;
  1159. while (*sample >= 0 && component[*sample].offset <= offset)
  1160. sample = &component[*sample].sampnext;
  1161. component[n].sampnext = *sample;
  1162. *sample = n;
  1163. component[n].offset = offset;
  1164. }
  1165. }
  1166. m = component[n].sampfirst;
  1167. while (m >= 0) {
  1168. if (dumbfile_seek(f, component[m].offset, DFS_SEEK_SET)) {
  1169. free(buffer);
  1170. free(component);
  1171. _dumb_it_unload_sigdata(sigdata);
  1172. return NULL;
  1173. }
  1174. if (it_read_sample_data(&sigdata->sample[component[m].n],
  1175. sample_convert[component[m].n], f)) {
  1176. free(buffer);
  1177. free(component);
  1178. _dumb_it_unload_sigdata(sigdata);
  1179. return NULL;
  1180. }
  1181. m = component[m].sampnext;
  1182. }
  1183. }
  1184. for (n = 0; n < 10; n++) {
  1185. if (dumbfile_getc(f) == 'X') {
  1186. if (dumbfile_getc(f) == 'T') {
  1187. if (dumbfile_getc(f) == 'P') {
  1188. if (dumbfile_getc(f) == 'M') {
  1189. break;
  1190. }
  1191. }
  1192. }
  1193. }
  1194. }
  1195. if (!dumbfile_error(f) && n < 10) {
  1196. unsigned int mptx_id = (unsigned int)dumbfile_igetl(f);
  1197. while (!dumbfile_error(f) && mptx_id != DUMB_ID('M', 'P', 'T', 'S')) {
  1198. unsigned int size = dumbfile_igetw(f);
  1199. switch (mptx_id) {
  1200. /* TODO: Add instrument extension readers */
  1201. default:
  1202. dumbfile_skip(f, size * sigdata->n_instruments);
  1203. break;
  1204. }
  1205. mptx_id = (unsigned int)dumbfile_igetl(f);
  1206. }
  1207. mptx_id = (unsigned int)dumbfile_igetl(f);
  1208. while (!dumbfile_error(f) && dumbfile_pos(f) < dumbfile_get_size(f)) {
  1209. unsigned int size = dumbfile_igetw(f);
  1210. switch (mptx_id) {
  1211. /* TODO: Add more song extension readers */
  1212. case DUMB_ID('D', 'T', '.', '.'):
  1213. if (size == 2)
  1214. sigdata->tempo = dumbfile_igetw(f);
  1215. else if (size == 4)
  1216. sigdata->tempo = (int)dumbfile_igetl(f);
  1217. break;
  1218. default:
  1219. dumbfile_skip(f, size);
  1220. break;
  1221. }
  1222. mptx_id = (unsigned int)dumbfile_igetl(f);
  1223. }
  1224. }
  1225. free(buffer);
  1226. free(component);
  1227. if (_dumb_it_fix_invalid_orders(sigdata) < 0) {
  1228. _dumb_it_unload_sigdata(sigdata);
  1229. return NULL;
  1230. }
  1231. return sigdata;
  1232. }
  1233. DUH *dumb_read_it_quick(DUMBFILE *f) {
  1234. sigdata_t *sigdata;
  1235. DUH_SIGTYPE_DESC *descptr = &_dumb_sigtype_it;
  1236. sigdata = it_load_sigdata(f);
  1237. if (!sigdata)
  1238. return NULL;
  1239. {
  1240. const char *tag[2][2];
  1241. tag[0][0] = "TITLE";
  1242. tag[0][1] = (const char *)(((DUMB_IT_SIGDATA *)sigdata)->name);
  1243. tag[1][0] = "FORMAT";
  1244. tag[1][1] = "IT";
  1245. return make_duh(-1, 2, (const char *const(*)[2])tag, 1, &descptr,
  1246. &sigdata);
  1247. }
  1248. }