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.

1419 lines
42KB

  1. /* _______ ____ __ ___ ___
  2. * \ _ \ \ / \ / \ \ / / ' ' '
  3. * | | \ \ | | || | \/ | . .
  4. * | | | | | | || ||\ /| |
  5. * | | | | | | || || \/ | | ' ' '
  6. * | | | | | | || || | | . .
  7. * | |_/ / \ \__// || | |
  8. * /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
  9. * / \
  10. * / . \
  11. * readpsm.c - Code to read a Protracker Studio / / \ \
  12. * module from an open file. | < / \_
  13. * | \/ /\ /
  14. * By Christopher Snowhill. \_ / > /
  15. * | \ / /
  16. * | ' /
  17. * \__/
  18. */
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "dumb.h"
  22. #include "internal/it.h"
  23. #ifndef min
  24. #define min(a, b) (((a) < (b)) ? (a) : (b))
  25. #endif
  26. #ifdef _MSC_VER
  27. #define snprintf sprintf_s
  28. #endif
  29. #define PSMV_OLD 940730
  30. #define PSMV_NEW 940902
  31. typedef struct _PSMCHUNK {
  32. int id;
  33. size_t len;
  34. unsigned char *data;
  35. } PSMCHUNK;
  36. typedef struct _PSMEVENT {
  37. int type;
  38. unsigned char data[8];
  39. } PSMEVENT;
  40. #define PSM_EVENT_END 0
  41. #define PSM_EVENT_PLAY_PATTERN 1
  42. #define PSM_EVENT_JUMP_TO_LINE 4
  43. #define PSM_EVENT_SET_SPEED 7
  44. #define PSM_EVENT_SET_BPM 8
  45. #define PSM_EVENT_SAMPLE_MAP_TABLE 12
  46. #define PSM_EVENT_CHANGE_PAN 13
  47. #define PSM_EVENT_CHANGE_VOL 14
  48. static int it_psm_process_sample(IT_SAMPLE *sample, const unsigned char *data,
  49. size_t len, int id, int version) {
  50. int flags;
  51. int insno;
  52. size_t length;
  53. int loopstart;
  54. int loopend;
  55. int panpos;
  56. int defvol;
  57. int samplerate;
  58. if (len < 0x60)
  59. return -1;
  60. flags = data[0];
  61. if (version == PSMV_OLD) {
  62. memcpy(sample->name, data + 0x0D, 34);
  63. sample->name[34] = 0;
  64. insno = data[0x34] | (data[0x35] << 8);
  65. length = data[0x36] | (data[0x37] << 8) | (data[0x38] << 16) |
  66. (data[0x39] << 24);
  67. loopstart = data[0x3A] | (data[0x3B] << 8) | (data[0x3C] << 16) |
  68. (data[0x3D] << 24);
  69. loopend = data[0x3E] | (data[0x3F] << 8) | (data[0x40] << 16) |
  70. (data[0x41] << 24);
  71. panpos = data[0x43];
  72. defvol = data[0x44];
  73. samplerate = data[0x49] | (data[0x4A] << 8) | (data[0x4B] << 16) |
  74. (data[0x4C] << 24);
  75. } else /*if (version == PSMV_NEW)*/ {
  76. memcpy(sample->name, data + 0x11, 34);
  77. sample->name[34] = 0;
  78. insno = data[0x38] | (data[0x39] << 8);
  79. length = data[0x3A] | (data[0x3B] << 8) | (data[0x3C] << 16) |
  80. (data[0x3D] << 24);
  81. loopstart = data[0x3E] | (data[0x3F] << 8) | (data[0x40] << 16) |
  82. (data[0x41] << 24);
  83. loopend = data[0x42] | (data[0x43] << 8) | (data[0x44] << 16) |
  84. (data[0x45] << 24);
  85. panpos = data[0x48];
  86. defvol = data[0x49];
  87. samplerate = data[0x4E] | (data[0x4F] << 8) | (data[0x50] << 16) |
  88. (data[0x51] << 24);
  89. }
  90. if (insno != id)
  91. return -1;
  92. if (length <= 0) {
  93. sample->flags &= ~IT_SAMPLE_EXISTS;
  94. return 0;
  95. }
  96. if ((length > len - 0x60) || ((flags & 0x7F) != 0))
  97. return -1;
  98. sample->flags = IT_SAMPLE_EXISTS;
  99. sample->length = length;
  100. sample->loop_start = loopstart;
  101. sample->loop_end = loopend;
  102. sample->C5_speed = samplerate;
  103. sample->default_volume = defvol >> 1;
  104. sample->default_pan = 0;
  105. sample->filename[0] = 0;
  106. sample->global_volume = 64;
  107. sample->vibrato_speed = 0;
  108. sample->vibrato_depth = 0;
  109. sample->vibrato_rate = 0;
  110. sample->vibrato_waveform = IT_VIBRATO_SINE;
  111. sample->finetune = 0;
  112. sample->max_resampling_quality = -1;
  113. if (flags & 0x80) {
  114. if (((unsigned int)sample->loop_end <= (unsigned int)sample->length) &&
  115. ((unsigned int)sample->loop_start <
  116. (unsigned int)sample->loop_end)) {
  117. sample->length = sample->loop_end;
  118. sample->flags |= IT_SAMPLE_LOOP;
  119. }
  120. }
  121. sample->data = malloc(sample->length);
  122. if (!sample->data)
  123. return -1;
  124. flags = 0;
  125. data += 0x60;
  126. for (insno = 0; insno < sample->length; insno++) {
  127. flags += (signed char)(*data++);
  128. ((signed char *)sample->data)[insno] = flags;
  129. }
  130. return 0;
  131. }
  132. static int it_psm_process_pattern(IT_PATTERN *pattern,
  133. const unsigned char *data, size_t len,
  134. int speed, int bpm, const unsigned char *pan,
  135. const int *vol, int version) {
  136. size_t length, pos, rowlen;
  137. int nrows, row;
  138. unsigned flags, chan;
  139. IT_ENTRY *entry;
  140. length = data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
  141. if (len > length)
  142. len = length;
  143. if (version == PSMV_OLD) {
  144. if (len < 10)
  145. return -1;
  146. data += 8;
  147. len -= 8;
  148. } else /*if (version == PSMV_NEW)*/ {
  149. if (len < 14)
  150. return -1;
  151. data += 12;
  152. len -= 12;
  153. }
  154. nrows = data[0] | (data[1] << 8);
  155. if (!nrows) {
  156. pattern->n_rows = 0;
  157. pattern->n_entries = 0;
  158. pattern->entry = NULL;
  159. return 0;
  160. }
  161. pattern->n_rows = nrows;
  162. data += 2;
  163. len -= 2;
  164. pattern->n_entries = 0;
  165. row = 0;
  166. pos = 2;
  167. rowlen = data[0] | (data[1] << 8);
  168. while ((row < nrows) && (pos < len)) {
  169. if (pos >= rowlen) {
  170. row++;
  171. rowlen += data[pos] | (data[pos + 1] << 8);
  172. pos += 2;
  173. continue;
  174. }
  175. flags = data[pos++];
  176. chan = data[pos++];
  177. if (chan > 63)
  178. return -1;
  179. if (flags & 0xF0) {
  180. pattern->n_entries++;
  181. if (flags & 0x80)
  182. pos++;
  183. if (flags & 0x40)
  184. pos++;
  185. if (flags & 0x20)
  186. pos++;
  187. if (flags & 0x10) {
  188. switch (data[pos]) {
  189. case 0x29:
  190. pos++;
  191. case 0x33:
  192. pos++;
  193. default:
  194. pos += 2;
  195. }
  196. }
  197. }
  198. }
  199. if (!pattern->n_entries)
  200. return 0;
  201. pattern->n_entries += nrows;
  202. if (speed)
  203. pattern->n_entries++;
  204. if (bpm >= 0x20)
  205. pattern->n_entries++;
  206. for (pos = 0; pos < 32; pos++) {
  207. if (!(pan[pos * 2 + 1] & 0xF9))
  208. pattern->n_entries++;
  209. if (vol[pos] != -1)
  210. pattern->n_entries++;
  211. }
  212. pattern->entry = malloc(pattern->n_entries * sizeof(*pattern->entry));
  213. if (!pattern->entry)
  214. return -1;
  215. entry = pattern->entry;
  216. if (speed) {
  217. entry->channel = 0;
  218. entry->mask = IT_ENTRY_EFFECT;
  219. entry->effect = IT_SET_SPEED;
  220. entry->effectvalue = speed;
  221. entry++;
  222. }
  223. if (bpm >= 0x20) {
  224. entry->channel = 0;
  225. entry->mask = IT_ENTRY_EFFECT;
  226. entry->effect = IT_SET_SONG_TEMPO;
  227. entry->effectvalue = bpm;
  228. entry++;
  229. }
  230. for (pos = 0; pos < 32; pos++) {
  231. if (!(pan[pos * 2 + 1] & 0xF9)) {
  232. entry->channel = pos;
  233. entry->mask = IT_ENTRY_EFFECT;
  234. switch (pan[pos * 2 + 1]) {
  235. case 0:
  236. entry->effect = IT_SET_PANNING;
  237. entry->effectvalue = pan[pos * 2] ^ 128;
  238. break;
  239. case 2:
  240. entry->effect = IT_S;
  241. entry->effectvalue = EFFECT_VALUE(IT_S_SET_SURROUND_SOUND, 1);
  242. break;
  243. case 4:
  244. entry->effect = IT_SET_PANNING;
  245. entry->effectvalue = 128;
  246. break;
  247. }
  248. entry++;
  249. }
  250. if (vol[pos] != -1) {
  251. entry->channel = pos;
  252. entry->mask = IT_ENTRY_EFFECT;
  253. entry->effect = IT_SET_CHANNEL_VOLUME;
  254. entry->effectvalue = (vol[pos] + 2) >> 2;
  255. entry++;
  256. }
  257. }
  258. row = 0;
  259. pos = 2;
  260. rowlen = data[0] | (data[1] << 8);
  261. while ((row < nrows) && (pos < len)) {
  262. if (pos >= rowlen) {
  263. IT_SET_END_ROW(entry);
  264. entry++;
  265. row++;
  266. rowlen += data[pos] | (data[pos + 1] << 8);
  267. pos += 2;
  268. continue;
  269. }
  270. flags = data[pos++];
  271. entry->channel = data[pos++];
  272. entry->mask = 0;
  273. if (flags & 0xF0) {
  274. if (flags & 0x80) {
  275. entry->mask |= IT_ENTRY_NOTE;
  276. if (version == PSMV_OLD) {
  277. if ((data[pos] < 0x80))
  278. entry->note =
  279. (data[pos] >> 4) * 12 + (data[pos] & 0x0f) + 12;
  280. else
  281. entry->mask &= ~IT_ENTRY_NOTE;
  282. } else /*if (version == PSMV_NEW)*/ {
  283. if ((data[pos]) && (data[pos] < 84))
  284. entry->note = data[pos] + 35;
  285. else
  286. entry->mask &= ~IT_ENTRY_NOTE;
  287. }
  288. pos++;
  289. }
  290. if (flags & 0x40) {
  291. entry->mask |= IT_ENTRY_INSTRUMENT;
  292. entry->instrument = data[pos++] + 1;
  293. }
  294. if (flags & 0x20) {
  295. entry->mask |= IT_ENTRY_VOLPAN;
  296. entry->volpan = (data[pos++] + 1) >> 1;
  297. }
  298. if (flags & 0x10) {
  299. entry->mask |= IT_ENTRY_EFFECT;
  300. length = data[pos + 1];
  301. switch (data[pos]) {
  302. case 1:
  303. entry->effect = IT_VOLUME_SLIDE;
  304. if (version == PSMV_OLD)
  305. entry->effectvalue = ((length & 0x1e) << 3) | 0xF;
  306. else /*if (version == PSMV_NEW)*/
  307. entry->effectvalue = (length << 4) | 0xF;
  308. break;
  309. case 2:
  310. entry->effect = IT_VOLUME_SLIDE;
  311. if (version == PSMV_OLD)
  312. entry->effectvalue = (length << 3) & 0xF0;
  313. else /*if (version == PSMV_NEW)*/
  314. entry->effectvalue = (length << 4) & 0xF0;
  315. break;
  316. case 3:
  317. entry->effect = IT_VOLUME_SLIDE;
  318. if (version == PSMV_OLD)
  319. entry->effectvalue = (length >> 1) | 0xF0;
  320. else /*if (version == PSMV_NEW)*/
  321. entry->effectvalue = length | 0xF0;
  322. break;
  323. case 4:
  324. entry->effect = IT_VOLUME_SLIDE;
  325. if (version == PSMV_OLD)
  326. entry->effectvalue = (length >> 1) & 0xF;
  327. else /*if (version == PSMV_NEW)*/
  328. entry->effectvalue = length & 0xF;
  329. break;
  330. case 12:
  331. entry->effect = IT_PORTAMENTO_UP;
  332. if (version == PSMV_OLD) {
  333. if (length < 4)
  334. entry->effectvalue = length | 0xF0;
  335. else
  336. entry->effectvalue = length >> 2;
  337. } else /*if (version == PSMV_NEW)*/ {
  338. entry->effectvalue = length;
  339. }
  340. break;
  341. case 14:
  342. entry->effect = IT_PORTAMENTO_DOWN;
  343. if (version == PSMV_OLD) {
  344. if (length < 4)
  345. entry->effectvalue = length | 0xF0;
  346. else
  347. entry->effectvalue = length >> 2;
  348. } else /*if (version == PSMV_NEW)*/ {
  349. entry->effectvalue = length;
  350. }
  351. break;
  352. case 15:
  353. entry->effect = IT_TONE_PORTAMENTO;
  354. if (version == PSMV_OLD)
  355. entry->effectvalue = length >> 2;
  356. else /*if (version == PSMV_NEW)*/
  357. entry->effectvalue = length;
  358. break;
  359. case 0x15:
  360. entry->effect = IT_VIBRATO;
  361. entry->effectvalue = length;
  362. break;
  363. case 0x18:
  364. entry->effect = IT_VOLSLIDE_VIBRATO;
  365. entry->effectvalue = length;
  366. break;
  367. case 0x29:
  368. entry->effect = IT_SET_SAMPLE_OFFSET;
  369. entry->effectvalue = data[pos + 2];
  370. pos += 2;
  371. break;
  372. case 0x2A:
  373. entry->effect = IT_RETRIGGER_NOTE;
  374. entry->effectvalue = length;
  375. break;
  376. case 0x33:
  377. #if 0
  378. entry->effect = IT_POSITION_JUMP;
  379. entry->effectvalue = data[pos+2];
  380. #else
  381. entry->mask &= ~IT_ENTRY_EFFECT;
  382. #endif
  383. pos++;
  384. break;
  385. case 0x34:
  386. entry->effect = IT_BREAK_TO_ROW;
  387. entry->effectvalue = length;
  388. break;
  389. case 0x3D:
  390. entry->effect = IT_SET_SPEED;
  391. entry->effectvalue = length;
  392. break;
  393. case 0x3E:
  394. if (length >= 0x20) {
  395. entry->effect = IT_SET_SONG_TEMPO;
  396. entry->effectvalue = length;
  397. } else {
  398. entry->mask &= ~IT_ENTRY_EFFECT;
  399. }
  400. break;
  401. case 0x47:
  402. entry->effect = IT_ARPEGGIO;
  403. entry->effectvalue = length;
  404. break;
  405. default:
  406. return -1;
  407. }
  408. pos += 2;
  409. }
  410. if (entry->mask)
  411. entry++;
  412. }
  413. }
  414. while (row < nrows) {
  415. IT_SET_END_ROW(entry);
  416. entry++;
  417. row++;
  418. }
  419. pattern->n_entries = (int)((long)entry - (long)pattern->entry);
  420. if (!pattern->n_entries)
  421. return -1;
  422. return 0;
  423. }
  424. static void free_chunks(PSMCHUNK *chunk, int count) {
  425. int n;
  426. for (n = 0; n < count; n++) {
  427. if (chunk[n].data)
  428. free(chunk[n].data);
  429. }
  430. free(chunk);
  431. }
  432. static void dumb_it_optimize_orders(DUMB_IT_SIGDATA *sigdata);
  433. static int pattcmp(const unsigned char *, const unsigned char *, size_t);
  434. static DUMB_IT_SIGDATA *it_psm_load_sigdata(DUMBFILE *f, int *ver,
  435. int subsong) {
  436. DUMB_IT_SIGDATA *sigdata;
  437. PSMCHUNK *chunk;
  438. int n_chunks = 0;
  439. PSMCHUNK *songchunk = 0;
  440. int n_song_chunks = 0;
  441. PSMEVENT *event = 0;
  442. int n_events = 0;
  443. unsigned char *ptr;
  444. int n, length;
  445. int o;
  446. int found;
  447. int n_patterns = 0;
  448. long first_pattern_line = -1;
  449. int first_pattern;
  450. int speed, bpm;
  451. unsigned char pan[64];
  452. int vol[32];
  453. if (dumbfile_mgetl(f) != DUMB_ID('P', 'S', 'M', ' '))
  454. goto error;
  455. length = (int)dumbfile_igetl(f);
  456. if (dumbfile_mgetl(f) != DUMB_ID('F', 'I', 'L', 'E'))
  457. goto error;
  458. chunk = calloc(768, sizeof(*chunk));
  459. while (length >= 8) {
  460. if (n_chunks >= 768)
  461. goto error_fc;
  462. chunk[n_chunks].id = (unsigned int)dumbfile_mgetl(f);
  463. n = (int)dumbfile_igetl(f);
  464. length -= 8;
  465. if ((signed int)n <= 0 || n > length)
  466. goto error_fc;
  467. chunk[n_chunks].len = n;
  468. if (n) {
  469. ptr = malloc(n);
  470. if (!ptr)
  471. goto error_fc;
  472. if (dumbfile_getnc((char *)ptr, n, f) < n) {
  473. free(ptr);
  474. goto error_fc;
  475. }
  476. chunk[n_chunks].data = ptr;
  477. }
  478. n_chunks++;
  479. length -= n;
  480. }
  481. if (!n_chunks)
  482. goto error_fc;
  483. sigdata = malloc(sizeof(*sigdata));
  484. if (!sigdata)
  485. goto error_fc;
  486. sigdata->n_patterns = 0;
  487. sigdata->n_samples = 0;
  488. sigdata->name[0] = 0;
  489. found = 0;
  490. for (n = 0; n < n_chunks; n++) {
  491. PSMCHUNK *c = &chunk[n];
  492. switch (c->id) {
  493. case DUMB_ID('S', 'D', 'F', 'T'):
  494. /* song data format? */
  495. if ((found & 1) || (c->len != 8) || memcmp(c->data, "MAINSONG", 8))
  496. goto error_sd;
  497. found |= 1;
  498. break;
  499. case DUMB_ID('S', 'O', 'N', 'G'):
  500. if (/*(found & 2) ||*/ (c->len <
  501. 11) /*|| memcmp(c->data, "MAINSONG", 8)*/)
  502. goto error_sd;
  503. found |= 2;
  504. break;
  505. case DUMB_ID('D', 'S', 'M', 'P'):
  506. sigdata->n_samples++;
  507. break;
  508. case DUMB_ID('T', 'I', 'T', 'L'):
  509. length = min(sizeof(sigdata->name) - 1, (unsigned)c->len);
  510. memcpy(sigdata->name, c->data, length);
  511. sigdata->name[length] = 0;
  512. }
  513. }
  514. if (found != 3 || !sigdata->n_samples)
  515. goto error_sd;
  516. sigdata->song_message = NULL;
  517. sigdata->order = NULL;
  518. sigdata->instrument = NULL;
  519. sigdata->sample = NULL;
  520. sigdata->pattern = NULL;
  521. sigdata->midi = NULL;
  522. sigdata->checkpoint = NULL;
  523. sigdata->n_instruments = 0;
  524. sigdata->n_orders = 0;
  525. for (n = 0; n < n_chunks; n++) {
  526. PSMCHUNK *c = &chunk[n];
  527. if (c->id == DUMB_ID('S', 'O', 'N', 'G')) {
  528. if (subsong == 0)
  529. break;
  530. subsong--;
  531. }
  532. }
  533. if (n == n_chunks)
  534. return NULL;
  535. subsong = (int)n;
  536. /*for (n = 0; n < n_chunks; n++) {
  537. PSMCHUNK * c = &chunk[n];
  538. if (c->id == DUMB_ID('S','O','N','G')) {*/
  539. {
  540. PSMCHUNK *c = &chunk[subsong];
  541. {
  542. ptr = c->data;
  543. if (ptr[10] > 32)
  544. goto error_usd;
  545. sigdata->n_pchannels = ptr[10];
  546. length = (int)(c->len - 11);
  547. ptr += 11;
  548. songchunk = 0;
  549. if (length >= 8) {
  550. songchunk = malloc(256 * sizeof(*songchunk));
  551. if (!songchunk)
  552. goto error_usd;
  553. while (length >= 8) {
  554. if (n_song_chunks >= 256)
  555. goto error_sc;
  556. songchunk[n_song_chunks].id =
  557. DUMB_ID(ptr[0], ptr[1], ptr[2], ptr[3]);
  558. n = ptr[4] | (ptr[5] << 8) | (ptr[6] << 16) |
  559. (ptr[7] << 24);
  560. length -= 8;
  561. if ((signed int)n < 0 || n > length)
  562. goto error_sc;
  563. songchunk[n_song_chunks].len = n;
  564. songchunk[n_song_chunks].data = ptr + 8;
  565. n_song_chunks++;
  566. length -= n;
  567. ptr += 8 + n;
  568. }
  569. }
  570. /*break;*/
  571. }
  572. }
  573. if (!n_song_chunks)
  574. goto error_sc;
  575. found = 0;
  576. for (n = 0; n < n_song_chunks; n++) {
  577. PSMCHUNK *c = &songchunk[n];
  578. if (c->id == DUMB_ID('D', 'A', 'T', 'E')) {
  579. /* date of the library build / format spec */
  580. if (c->len == 6) {
  581. length = (int)c->len;
  582. ptr = c->data;
  583. while (length > 0) {
  584. if (*ptr >= '0' && *ptr <= '9') {
  585. found = (found * 10) + (*ptr - '0');
  586. } else {
  587. found = 0;
  588. break;
  589. }
  590. ptr++;
  591. length--;
  592. }
  593. }
  594. break;
  595. }
  596. }
  597. /*
  598. if (found != 940506 &&
  599. found != 940509 &&
  600. found != 940510 &&
  601. found != 940530 &&
  602. found != 940629 &&
  603. found != PSMV_OLD &&
  604. found != 941011 &&
  605. found != PSMV_NEW &&
  606. found != 940906 &&
  607. found != 940903 &&
  608. found != 940914 &&
  609. found != 941213 &&
  610. found != 800211) WTF?
  611. goto error_sc;
  612. */
  613. *ver = found;
  614. if (found == 800211 || found == PSMV_NEW || found == 940903 ||
  615. found == 940906 || found == 940914 || found == 941213)
  616. found = PSMV_NEW;
  617. else
  618. found = PSMV_OLD;
  619. memset(sigdata->channel_volume, 64, DUMB_IT_N_CHANNELS);
  620. for (n = 0; n < DUMB_IT_N_CHANNELS; n += 4) {
  621. int sep = 32 * dumb_it_default_panning_separation / 100;
  622. sigdata->channel_pan[n] = 32 - sep;
  623. sigdata->channel_pan[n + 1] = 32 + sep;
  624. sigdata->channel_pan[n + 2] = 32 + sep;
  625. sigdata->channel_pan[n + 3] = 32 - sep;
  626. }
  627. for (n = 0; n < n_song_chunks; n++) {
  628. PSMCHUNK *c = &songchunk[n];
  629. switch (c->id) {
  630. case DUMB_ID('O', 'P', 'L', 'H'):
  631. if (c->len < 2)
  632. goto error_sc;
  633. ptr = c->data;
  634. o = ptr[0] | (ptr[1] << 8);
  635. if (!o)
  636. goto error_sc;
  637. event = malloc(o * sizeof(*event));
  638. if (!event)
  639. goto error_sc;
  640. length = (int)(c->len - 2);
  641. ptr += 2;
  642. while ((length > 0) && (n_events < o)) {
  643. event[n_events].type = *ptr;
  644. switch (*ptr) {
  645. case PSM_EVENT_END:
  646. ptr++;
  647. length--;
  648. break;
  649. case PSM_EVENT_PLAY_PATTERN:
  650. if (found == PSMV_OLD) {
  651. if (length < 5)
  652. goto error_ev;
  653. memcpy(event[n_events].data, ptr + 1, 4);
  654. ptr += 5;
  655. length -= 5;
  656. } else /*if (found == PSMV_NEW)*/ {
  657. if (length < 9)
  658. goto error_ev;
  659. memcpy(event[n_events].data, ptr + 1, 8);
  660. ptr += 9;
  661. length -= 9;
  662. }
  663. break;
  664. case PSM_EVENT_SET_SPEED:
  665. case PSM_EVENT_SET_BPM:
  666. if (length < 2)
  667. goto error_ev;
  668. event[n_events].data[0] = ptr[1];
  669. ptr += 2;
  670. length -= 2;
  671. break;
  672. case PSM_EVENT_JUMP_TO_LINE:
  673. case PSM_EVENT_CHANGE_VOL:
  674. if (length < 3)
  675. goto error_ev;
  676. memcpy(event[n_events].data, ptr + 1, 2);
  677. ptr += 3;
  678. length -= 3;
  679. break;
  680. case PSM_EVENT_SAMPLE_MAP_TABLE:
  681. if (length < 7)
  682. goto error_ev;
  683. memcpy(event[n_events].data, ptr + 1, 6);
  684. ptr += 7;
  685. length -= 7;
  686. break;
  687. case PSM_EVENT_CHANGE_PAN:
  688. if (length < 4)
  689. goto error_ev;
  690. memcpy(event[n_events].data, ptr + 1, 3);
  691. ptr += 4;
  692. length -= 4;
  693. break;
  694. default:
  695. goto error_ev;
  696. }
  697. n_events++;
  698. }
  699. break;
  700. case DUMB_ID('P', 'P', 'A', 'N'):
  701. length = (int)c->len;
  702. if (length & 1)
  703. goto error_ev;
  704. ptr = c->data;
  705. o = 0;
  706. while (length > 0) {
  707. switch (ptr[0]) {
  708. case 0:
  709. sigdata->channel_pan[o] =
  710. ((((int)(signed char)ptr[1]) * 32) / 127) + 32;
  711. break;
  712. case 2:
  713. sigdata->channel_pan[o] = IT_SURROUND;
  714. break;
  715. case 4:
  716. sigdata->channel_pan[o] = 32;
  717. break;
  718. }
  719. ptr += 2;
  720. length -= 2;
  721. if (++o >= DUMB_IT_N_CHANNELS)
  722. break;
  723. }
  724. break;
  725. /*
  726. case DUMB_ID('P','A','T','T'):
  727. case DUMB_ID('D','S','A','M'):
  728. */
  729. }
  730. }
  731. sigdata->flags = IT_STEREO | IT_OLD_EFFECTS | IT_COMPATIBLE_GXX;
  732. sigdata->global_volume = 128;
  733. sigdata->speed = 6;
  734. sigdata->tempo = 125;
  735. sigdata->mixing_volume = 48;
  736. sigdata->pan_separation = 128;
  737. speed = 0;
  738. bpm = 0;
  739. memset(pan, 255, sizeof(pan));
  740. memset(vol, 255, sizeof(vol));
  741. sigdata->n_patterns = n_events;
  742. sigdata->pattern = malloc(sigdata->n_patterns * sizeof(*sigdata->pattern));
  743. if (!sigdata->pattern)
  744. goto error_ev;
  745. for (n = 0; n < sigdata->n_patterns; n++)
  746. sigdata->pattern[n].entry = NULL;
  747. for (n = 0; n < n_events; n++) {
  748. PSMEVENT *e = &event[n];
  749. switch (e->type) {
  750. case PSM_EVENT_END:
  751. n = n_events;
  752. break;
  753. case PSM_EVENT_PLAY_PATTERN:
  754. for (o = 0; o < n_chunks; o++) {
  755. PSMCHUNK *c = &chunk[o];
  756. if (c->id == DUMB_ID('P', 'B', 'O', 'D')) {
  757. ptr = c->data;
  758. length = (int)c->len;
  759. if (found == PSMV_OLD) {
  760. if (length < 8)
  761. goto error_ev;
  762. if (!pattcmp(ptr + 4, e->data, 4)) {
  763. if (it_psm_process_pattern(
  764. &sigdata->pattern[n_patterns], ptr, length,
  765. speed, bpm, pan, vol, found))
  766. goto error_ev;
  767. if (first_pattern_line < 0) {
  768. first_pattern_line = n;
  769. first_pattern = o;
  770. }
  771. e->data[0] = n_patterns;
  772. e->data[1] = n_patterns >> 8;
  773. n_patterns++;
  774. break;
  775. }
  776. } else /*if (found == PSMV_NEW)*/ {
  777. if (length < 12)
  778. goto error_ev;
  779. if (!pattcmp(ptr + 4, e->data, 8)) {
  780. if (it_psm_process_pattern(
  781. &sigdata->pattern[n_patterns], ptr, length,
  782. speed, bpm, pan, vol, found))
  783. goto error_ev;
  784. if (first_pattern_line < 0) {
  785. first_pattern_line = n;
  786. first_pattern = o;
  787. }
  788. e->data[0] = n_patterns;
  789. e->data[1] = n_patterns >> 8;
  790. n_patterns++;
  791. break;
  792. }
  793. }
  794. }
  795. }
  796. if (o == n_chunks)
  797. goto error_ev;
  798. speed = 0;
  799. bpm = 0;
  800. memset(pan, 255, sizeof(pan));
  801. memset(vol, 255, sizeof(vol));
  802. e->type = PSM_EVENT_END;
  803. break;
  804. case PSM_EVENT_JUMP_TO_LINE:
  805. o = e->data[0] | (e->data[1] << 8);
  806. if (o >= n_events)
  807. goto error_ev;
  808. if (o == 0) {
  809. /* whew! easy case! */
  810. sigdata->restart_position = 0;
  811. n = n_events;
  812. } else if (o == n) {
  813. /* freeze */
  814. n = n_events;
  815. } else if (o > n) {
  816. /* jump ahead, setting played event numbers to zero will prevent
  817. * endless looping */
  818. n = o - 1;
  819. } else if (o >= first_pattern_line) {
  820. /* another semi-easy case */
  821. sigdata->restart_position =
  822. event[o].data[0] | (event[o].data[1] << 8);
  823. n = n_events;
  824. } else {
  825. /* crud, try to simulate rerunning all of the commands from the
  826. * indicated line up to the first pattern, then dupe the first
  827. * pattern again.
  828. */
  829. /*
  830. PSMCHUNK * c = &chunk[first_pattern];
  831. for (; o < first_pattern_line; o++) {
  832. PSMEVENT * ev = &event[o];
  833. switch (ev->type) {
  834. case PSM_EVENT_SET_SPEED:
  835. speed = ev->data[0];
  836. break;
  837. case PSM_EVENT_SET_BPM:
  838. bpm = ev->data[0];
  839. break;
  840. case PSM_EVENT_CHANGE_PAN:
  841. if (ev->data[0] > 31) goto error_ev;
  842. pan[ev->data[0] * 2] = ev->data[1];
  843. pan[ev->data[0] * 2 + 1] = ev->data[2];
  844. break;
  845. case PSM_EVENT_CHANGE_VOL:
  846. if (ev->data[0] > 31) goto error_ev;
  847. vol[ev->data[0]] = ev->data[1];
  848. break;
  849. }
  850. }
  851. if (it_psm_process_pattern(&sigdata->pattern[n_patterns],
  852. c->data, c->len, speed, bpm, pan, vol, found)) goto error_ev;
  853. n_patterns++;
  854. sigdata->restart_position = 1;
  855. n = n_events;
  856. Eh, what the hell? PSM has no panning commands anyway.
  857. */
  858. sigdata->restart_position = 0;
  859. n = n_events;
  860. }
  861. e->type = PSM_EVENT_END;
  862. break;
  863. case PSM_EVENT_SET_SPEED:
  864. speed = e->data[0];
  865. break;
  866. case PSM_EVENT_SET_BPM:
  867. bpm = e->data[0];
  868. break;
  869. case PSM_EVENT_CHANGE_PAN:
  870. o = e->data[0];
  871. if (o > 31)
  872. goto error_ev;
  873. pan[o * 2] = e->data[1];
  874. pan[o * 2 + 1] = e->data[2];
  875. break;
  876. case PSM_EVENT_CHANGE_VOL:
  877. o = e->data[0];
  878. if (o > 31)
  879. goto error_ev;
  880. vol[o] = e->data[1];
  881. break;
  882. case PSM_EVENT_SAMPLE_MAP_TABLE:
  883. if (e->data[0] != 0 || e->data[1] != 0xFF || e->data[2] != 0 ||
  884. e->data[3] != 0 || e->data[4] != 1 || e->data[5] != 0)
  885. goto error_ev;
  886. break;
  887. }
  888. }
  889. if (n_patterns > 256)
  890. goto error_ev;
  891. sigdata->sample = malloc(sigdata->n_samples * sizeof(*sigdata->sample));
  892. if (!sigdata->sample)
  893. goto error_ev;
  894. for (n = 0; n < sigdata->n_samples; n++) {
  895. sigdata->sample[n].data = NULL;
  896. sigdata->sample[n].flags = 0;
  897. }
  898. o = 0;
  899. for (n = 0; n < n_chunks; n++) {
  900. PSMCHUNK *c = &chunk[n];
  901. if (c->id == DUMB_ID('D', 'S', 'M', 'P')) {
  902. if (it_psm_process_sample(&sigdata->sample[o], c->data, c->len, o,
  903. found))
  904. goto error_ev;
  905. o++;
  906. }
  907. }
  908. sigdata->n_orders = n_patterns;
  909. sigdata->n_patterns = n_patterns;
  910. sigdata->order = malloc(n_patterns);
  911. for (n = 0; n < n_patterns; n++) {
  912. sigdata->order[n] = n;
  913. }
  914. free(event);
  915. free(songchunk);
  916. free_chunks(chunk, n_chunks);
  917. if (_dumb_it_fix_invalid_orders(sigdata) < 0) {
  918. _dumb_it_unload_sigdata(sigdata);
  919. return NULL;
  920. }
  921. dumb_it_optimize_orders(sigdata);
  922. return sigdata;
  923. error_ev:
  924. free(event);
  925. error_sc:
  926. if (songchunk)
  927. free(songchunk);
  928. error_usd:
  929. _dumb_it_unload_sigdata(sigdata);
  930. goto error_fc;
  931. error_sd:
  932. free(sigdata);
  933. error_fc:
  934. free_chunks(chunk, n_chunks);
  935. error:
  936. return NULL;
  937. }
  938. static int it_order_compare(const void *e1, const void *e2) {
  939. if (*((const char *)e1) < *((const char *)e2))
  940. return -1;
  941. if (*((const char *)e1) > *((const char *)e2))
  942. return 1;
  943. return 0;
  944. }
  945. /*
  946. static int it_optimize_compare(const void *e1, const void *e2) {
  947. if (((const IT_ENTRY *)e1)->channel < ((const IT_ENTRY *)e2)->channel)
  948. return -1;
  949. if (((const IT_ENTRY *)e1)->channel > ((const IT_ENTRY *)e2)->channel)
  950. return 1;
  951. return 0;
  952. }
  953. */
  954. static int it_entry_compare(const IT_ENTRY *e1, const IT_ENTRY *e2) {
  955. if (IT_IS_END_ROW(e1) && IT_IS_END_ROW(e2))
  956. return 1;
  957. if (e1->channel != e2->channel)
  958. return 0;
  959. if (e1->mask != e2->mask)
  960. return 0;
  961. if ((e1->mask & IT_ENTRY_NOTE) && (e1->note != e2->note))
  962. return 0;
  963. if ((e1->mask & IT_ENTRY_INSTRUMENT) && (e1->instrument != e2->instrument))
  964. return 0;
  965. if ((e1->mask & IT_ENTRY_VOLPAN) && (e1->volpan != e2->volpan))
  966. return 0;
  967. if ((e1->mask & IT_ENTRY_EFFECT) &&
  968. ((e1->effect != e2->effect) || (e1->effectvalue != e2->effectvalue)))
  969. return 0;
  970. return 1;
  971. }
  972. /*
  973. static void dumb_it_optimize_pattern(IT_PATTERN * pattern) {
  974. IT_ENTRY * entry, * end;
  975. IT_ENTRY * rowstart, * rowend;
  976. IT_ENTRY * current;
  977. if (!pattern->n_entries || !pattern->entry) return;
  978. current = entry = pattern->entry;
  979. end = entry + pattern->n_entries;
  980. while (entry < end) {
  981. rowstart = entry;
  982. while (!IT_IS_END_ROW(entry)) entry++;
  983. rowend = entry;
  984. if (rowend > rowstart + 1)
  985. qsort(rowstart, rowend - rowstart, sizeof(IT_ENTRY),
  986. &it_optimize_compare); entry = rowstart; while (entry < rowend) { if
  987. (!(entry->mask)) {} else if (it_entry_compare(entry, current)) {} else if
  988. (!(current->mask) ||
  989. ((entry->channel == current->channel)
  990. &&
  991. ((entry->mask | current->mask) ==
  992. (entry->mask ^ current->mask)))) { current->mask |= entry->mask; if (entry->mask
  993. & IT_ENTRY_NOTE) current->note = entry->note; if (entry->mask &
  994. IT_ENTRY_INSTRUMENT) current->instrument = entry->instrument; if (entry->mask &
  995. IT_ENTRY_VOLPAN) current->volpan = entry->volpan; if (entry->mask &
  996. IT_ENTRY_EFFECT) { current->effect = entry->effect; current->effectvalue =
  997. entry->effectvalue;
  998. }
  999. } else {
  1000. if (++current < entry) *current = *entry;
  1001. }
  1002. entry++;
  1003. }
  1004. if (++current < entry) *current = *entry;
  1005. entry++;
  1006. }
  1007. current++;
  1008. if (current < end) {
  1009. IT_ENTRY * opt;
  1010. pattern->n_entries = current - pattern->entry;
  1011. opt = realloc(pattern->entry, pattern->n_entries *
  1012. sizeof(*pattern->entry)); if (opt) pattern->entry = opt;
  1013. }
  1014. }
  1015. */
  1016. static int it_pattern_compare(const IT_PATTERN *p1, const IT_PATTERN *p2) {
  1017. IT_ENTRY *e1, *end;
  1018. IT_ENTRY *e2;
  1019. if (p1 == p2)
  1020. return 1;
  1021. if (p1->n_entries != p2->n_entries)
  1022. return 0;
  1023. e1 = p1->entry;
  1024. end = e1 + p1->n_entries;
  1025. e2 = p2->entry;
  1026. while (e1 < end) {
  1027. if (!it_entry_compare(e1, e2))
  1028. return 0;
  1029. e1++;
  1030. e2++;
  1031. }
  1032. return 1;
  1033. }
  1034. static void dumb_it_optimize_orders(DUMB_IT_SIGDATA *sigdata) {
  1035. int n, o, p;
  1036. /*int last_invalid = (sigdata->flags & IT_WAS_AN_XM) ? 255 : 253;*/
  1037. unsigned char *order_list;
  1038. int n_patterns;
  1039. IT_PATTERN *pattern;
  1040. if (!sigdata->n_orders || !sigdata->n_patterns)
  1041. return;
  1042. n_patterns = 0;
  1043. order_list = malloc(sigdata->n_orders);
  1044. if (!order_list)
  1045. return;
  1046. for (n = 0; n < sigdata->n_orders; n++) {
  1047. if (sigdata->order[n] < sigdata->n_patterns) {
  1048. for (o = 0; o < n_patterns; o++) {
  1049. if (sigdata->order[n] == order_list[o])
  1050. break;
  1051. }
  1052. if (o == n_patterns) {
  1053. order_list[n_patterns++] = sigdata->order[n];
  1054. }
  1055. }
  1056. }
  1057. if (!n_patterns) {
  1058. free(order_list);
  1059. return;
  1060. }
  1061. /*for (n = 0; n < n_patterns; n++) {
  1062. dumb_it_optimize_pattern(&sigdata->pattern[order_list[n]]);
  1063. }*/
  1064. for (n = 0; n < n_patterns; n++) {
  1065. for (o = n + 1; o < n_patterns; o++) {
  1066. if ((order_list[n] != order_list[o]) &&
  1067. it_pattern_compare(&sigdata->pattern[order_list[n]],
  1068. &sigdata->pattern[order_list[o]])) {
  1069. for (p = 0; p < sigdata->n_orders; p++) {
  1070. if (sigdata->order[p] == order_list[o]) {
  1071. sigdata->order[p] = order_list[n];
  1072. }
  1073. }
  1074. for (p = o + 1; p < n_patterns; p++) {
  1075. if (order_list[p] == order_list[o]) {
  1076. order_list[p] = order_list[n];
  1077. }
  1078. }
  1079. order_list[o] = order_list[n];
  1080. }
  1081. }
  1082. }
  1083. qsort(order_list, n_patterns, sizeof(*order_list), &it_order_compare);
  1084. for (n = 0, o = 0; n < n_patterns; n++) {
  1085. if (order_list[n] != order_list[o]) {
  1086. if (++o < n)
  1087. order_list[o] = order_list[n];
  1088. }
  1089. }
  1090. n_patterns = o + 1;
  1091. pattern = malloc(n_patterns * sizeof(*pattern));
  1092. if (!pattern) {
  1093. free(order_list);
  1094. return;
  1095. }
  1096. for (n = 0; n < n_patterns; n++) {
  1097. pattern[n] = sigdata->pattern[order_list[n]];
  1098. }
  1099. for (n = 0; n < sigdata->n_patterns; n++) {
  1100. for (o = 0; o < n_patterns; o++) {
  1101. if (order_list[o] == n)
  1102. break;
  1103. }
  1104. if (o == n_patterns) {
  1105. if (sigdata->pattern[n].entry)
  1106. free(sigdata->pattern[n].entry);
  1107. }
  1108. }
  1109. free(sigdata->pattern);
  1110. sigdata->pattern = pattern;
  1111. sigdata->n_patterns = n_patterns;
  1112. for (n = 0; n < sigdata->n_orders; n++) {
  1113. for (o = 0; o < n_patterns; o++) {
  1114. if (sigdata->order[n] == order_list[o]) {
  1115. sigdata->order[n] = o;
  1116. break;
  1117. }
  1118. }
  1119. }
  1120. free(order_list);
  1121. }
  1122. int dumb_get_psm_subsong_count(DUMBFILE *f) {
  1123. size_t length;
  1124. int subsongs;
  1125. long l;
  1126. if (dumbfile_mgetl(f) != DUMB_ID('P', 'S', 'M', ' '))
  1127. return 0;
  1128. length = dumbfile_igetl(f);
  1129. if (dumbfile_mgetl(f) != DUMB_ID('F', 'I', 'L', 'E'))
  1130. return 0;
  1131. subsongs = 0;
  1132. while (length >= 8 && !dumbfile_error(f)) {
  1133. if (dumbfile_mgetl(f) == DUMB_ID('S', 'O', 'N', 'G'))
  1134. subsongs++;
  1135. l = dumbfile_igetl(f);
  1136. dumbfile_skip(f, l);
  1137. length -= l + 8;
  1138. }
  1139. if (dumbfile_error(f))
  1140. return 0;
  1141. return subsongs;
  1142. }
  1143. /* Eww */
  1144. int pattcmp(const unsigned char *a, const unsigned char *b, size_t l) {
  1145. long i, j;
  1146. unsigned long na, nb;
  1147. char *p;
  1148. na = nb = 0;
  1149. i = memcmp(a, b, l);
  1150. if (!i)
  1151. return 0;
  1152. /* damnit */
  1153. for (i = 0; i < l; ++i) {
  1154. if (a[i] >= '0' && a[i] <= '9')
  1155. break;
  1156. }
  1157. if (i < l) {
  1158. na = strtoul((const char *)a + i, &p, 10);
  1159. if ((const unsigned char *)p == a + i)
  1160. return 1;
  1161. }
  1162. for (j = 0; j < l; ++j) {
  1163. if (b[j] >= '0' && b[j] <= '9')
  1164. break;
  1165. }
  1166. if (j < l) {
  1167. nb = strtoul((const char *)b + j, &p, 10);
  1168. if ((const unsigned char *)p == b + j)
  1169. return -1;
  1170. }
  1171. if (i < j)
  1172. return -1;
  1173. else if (i > j)
  1174. return 1;
  1175. i = memcmp(a, b, j);
  1176. if (i)
  1177. return (int)i;
  1178. return (int)(((long)na) - ((long)nb));
  1179. }
  1180. DUH *dumb_read_psm_quick(DUMBFILE *f, int subsong) {
  1181. sigdata_t *sigdata;
  1182. int ver;
  1183. DUH_SIGTYPE_DESC *descptr = &_dumb_sigtype_it;
  1184. sigdata = it_psm_load_sigdata(f, &ver, subsong);
  1185. if (!sigdata)
  1186. return NULL;
  1187. {
  1188. int n_tags = 2;
  1189. char version[16];
  1190. const char *tag[3][2];
  1191. tag[0][0] = "TITLE";
  1192. tag[0][1] = (const char *)(((DUMB_IT_SIGDATA *)sigdata)->name);
  1193. tag[1][0] = "FORMAT";
  1194. tag[1][1] = "PSM";
  1195. if (ver) {
  1196. tag[2][0] = "FORMATVERSION";
  1197. snprintf(version, 15, "%d", ver);
  1198. version[15] = 0;
  1199. tag[2][1] = (const char *)&version;
  1200. ++n_tags;
  1201. }
  1202. return make_duh(-1, n_tags, (const char *const(*)[2])tag, 1, &descptr,
  1203. &sigdata);
  1204. }
  1205. }