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.

606 lines
18KB

  1. /* _______ ____ __ ___ ___
  2. * \ _ \ \ / \ / \ \ / / ' ' '
  3. * | | \ \ | | || | \/ | . .
  4. * | | | | | | || ||\ /| |
  5. * | | | | | | || || \/ | | ' ' '
  6. * | | | | | | || || | | . .
  7. * | |_/ / \ \__// || | |
  8. * /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
  9. * / \
  10. * / . \
  11. * readokt.c - Code to read an Oktalyzer module / / \ \
  12. * from an open file. | < / \_
  13. * | \/ /\ /
  14. * By Christopher Snowhill. \_ / > /
  15. * | \ / /
  16. * | ' /
  17. * \__/
  18. */
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <math.h>
  22. #include "dumb.h"
  23. #include "internal/it.h"
  24. static int it_okt_read_pattern(IT_PATTERN *pattern, const unsigned char *data,
  25. int length, int n_channels) {
  26. int pos;
  27. int channel;
  28. int row;
  29. int n_rows;
  30. IT_ENTRY *entry;
  31. if (length < 2)
  32. return -1;
  33. n_rows = (data[0] << 8) | data[1];
  34. if (!n_rows)
  35. n_rows = 64;
  36. if (length < 2 + (n_rows * n_channels * 4))
  37. return -1;
  38. pattern->n_rows = n_rows;
  39. /* compute number of entries */
  40. pattern->n_entries = n_rows; /* Account for the row end markers */
  41. pos = 2;
  42. for (row = 0; row < pattern->n_rows; row++) {
  43. for (channel = 0; channel < n_channels; channel++) {
  44. if (data[pos + 0] | data[pos + 2])
  45. pattern->n_entries++;
  46. pos += 4;
  47. }
  48. }
  49. pattern->entry =
  50. (IT_ENTRY *)malloc(pattern->n_entries * sizeof(*pattern->entry));
  51. if (!pattern->entry)
  52. return -1;
  53. entry = pattern->entry;
  54. pos = 2;
  55. for (row = 0; row < n_rows; row++) {
  56. for (channel = 0; channel < n_channels; channel++) {
  57. if (data[pos + 0] | data[pos + 2]) {
  58. entry->channel = channel;
  59. entry->mask = 0;
  60. if (data[pos + 0] > 0 && data[pos + 0] <= 36) {
  61. entry->mask |= IT_ENTRY_NOTE | IT_ENTRY_INSTRUMENT;
  62. entry->note = data[pos + 0] + 35;
  63. entry->instrument = data[pos + 1] + 1;
  64. }
  65. entry->effect = 0;
  66. entry->effectvalue = data[pos + 3];
  67. switch (data[pos + 2]) {
  68. case 2:
  69. if (data[pos + 3])
  70. entry->effect = IT_PORTAMENTO_DOWN;
  71. break; // XXX code calls this rs_portu, but it's adding to
  72. // the period, which decreases the pitch
  73. case 13:
  74. if (data[pos + 3])
  75. entry->effect = IT_OKT_NOTE_SLIDE_DOWN;
  76. break;
  77. case 21:
  78. if (data[pos + 3])
  79. entry->effect = IT_OKT_NOTE_SLIDE_DOWN_ROW;
  80. break;
  81. case 1:
  82. if (data[pos + 3])
  83. entry->effect = IT_PORTAMENTO_UP;
  84. break; // XXX same deal here, increasing the pitch
  85. case 17:
  86. if (data[pos + 3])
  87. entry->effect = IT_OKT_NOTE_SLIDE_UP;
  88. break;
  89. case 30:
  90. if (data[pos + 3])
  91. entry->effect = IT_OKT_NOTE_SLIDE_UP_ROW;
  92. break;
  93. case 10:
  94. if (data[pos + 3])
  95. entry->effect = IT_OKT_ARPEGGIO_3;
  96. break;
  97. case 11:
  98. if (data[pos + 3])
  99. entry->effect = IT_OKT_ARPEGGIO_4;
  100. break;
  101. case 12:
  102. if (data[pos + 3])
  103. entry->effect = IT_OKT_ARPEGGIO_5;
  104. break;
  105. case 15:
  106. entry->effect = IT_S;
  107. entry->effectvalue =
  108. EFFECT_VALUE(IT_S_SET_FILTER, data[pos + 3] & 0x0F);
  109. break;
  110. case 25:
  111. entry->effect = IT_JUMP_TO_ORDER;
  112. break;
  113. case 27:
  114. entry->note = IT_NOTE_OFF;
  115. entry->mask |= IT_ENTRY_NOTE;
  116. break;
  117. case 28:
  118. entry->effect = IT_SET_SPEED;
  119. break;
  120. case 31:
  121. if (data[pos + 3] <= 0x40)
  122. entry->effect = IT_SET_CHANNEL_VOLUME;
  123. else if (data[pos + 3] <= 0x50) {
  124. entry->effect = IT_OKT_VOLUME_SLIDE_DOWN;
  125. entry->effectvalue = data[pos + 3] - 0x40;
  126. } else if (data[pos + 3] <= 0x60) {
  127. entry->effect = IT_OKT_VOLUME_SLIDE_UP;
  128. entry->effectvalue = data[pos + 3] - 0x50;
  129. } else if (data[pos + 3] <= 0x70) {
  130. entry->effect = IT_OKT_VOLUME_SLIDE_DOWN;
  131. entry->effectvalue = data[pos + 3] - 0x50;
  132. } else if (data[pos + 3] <= 0x80) {
  133. entry->effect = IT_OKT_VOLUME_SLIDE_UP;
  134. entry->effectvalue = data[pos + 3] - 0x60;
  135. }
  136. break;
  137. }
  138. if (entry->effect)
  139. entry->mask |= IT_ENTRY_EFFECT;
  140. entry++;
  141. }
  142. pos += 4;
  143. }
  144. IT_SET_END_ROW(entry);
  145. entry++;
  146. }
  147. return 0;
  148. }
  149. static void it_okt_read_sample_header(IT_SAMPLE *sample,
  150. const unsigned char *data) {
  151. int loop_start, loop_length;
  152. memcpy(sample->name, data, 20);
  153. sample->name[20] = 0;
  154. sample->filename[0] = 0;
  155. sample->length =
  156. (data[20] << 24) | (data[21] << 16) | (data[22] << 8) | data[23];
  157. sample->global_volume = 64;
  158. sample->default_volume = data[29];
  159. loop_start = ((data[24] << 8) | data[25]) << 1;
  160. loop_length = ((data[26] << 8) | data[27]) << 1;
  161. sample->sus_loop_start = loop_start;
  162. sample->sus_loop_end = loop_start + loop_length;
  163. if (sample->length <= 0) {
  164. sample->flags = 0;
  165. return;
  166. }
  167. sample->flags = IT_SAMPLE_EXISTS;
  168. sample->default_pan = 0;
  169. sample->C5_speed =
  170. (int)(AMIGA_CLOCK /
  171. 214.0); //(long)(16726.0*pow(DUMB_PITCH_BASE, finetune*32));
  172. sample->finetune = 0;
  173. if (sample->sus_loop_end > sample->length)
  174. sample->sus_loop_end = sample->length;
  175. if (loop_length > 2)
  176. sample->flags |= IT_SAMPLE_SUS_LOOP;
  177. sample->vibrato_speed = 0;
  178. sample->vibrato_depth = 0;
  179. sample->vibrato_rate = 0;
  180. sample->vibrato_waveform = 0; // do we have to set _all_ these?
  181. sample->max_resampling_quality = -1;
  182. }
  183. static int it_okt_read_sample_data(IT_SAMPLE *sample, const char *data,
  184. int length) {
  185. if (length && sample->length) {
  186. if (length < sample->length) {
  187. sample->length = length;
  188. if (length < sample->sus_loop_end)
  189. sample->sus_loop_end = length;
  190. }
  191. sample->data = malloc(length);
  192. if (!sample->data)
  193. return -1;
  194. memcpy(sample->data, data, length);
  195. }
  196. return 0;
  197. }
  198. typedef struct IFF_CHUNK IFF_CHUNK;
  199. typedef struct IFF_CHUNKED IFF_CHUNKED;
  200. struct IFF_CHUNK {
  201. unsigned type;
  202. unsigned char *data;
  203. unsigned size;
  204. };
  205. struct IFF_CHUNKED {
  206. unsigned chunk_count;
  207. IFF_CHUNK *chunks;
  208. };
  209. static IFF_CHUNKED *dumbfile_read_okt(DUMBFILE *f) {
  210. IFF_CHUNKED *mod = (IFF_CHUNKED *)malloc(sizeof(*mod));
  211. if (!mod)
  212. return NULL;
  213. mod->chunk_count = 0;
  214. mod->chunks = 0;
  215. for (;;) {
  216. long bytes_read;
  217. IFF_CHUNK *chunk = (IFF_CHUNK *)realloc(
  218. mod->chunks, (mod->chunk_count + 1) * sizeof(IFF_CHUNK));
  219. if (!chunk) {
  220. if (mod->chunks)
  221. free(mod->chunks);
  222. free(mod);
  223. return NULL;
  224. }
  225. mod->chunks = chunk;
  226. chunk += mod->chunk_count;
  227. bytes_read = dumbfile_mgetl(f);
  228. if (bytes_read < 0)
  229. break;
  230. chunk->type = (unsigned int)bytes_read;
  231. chunk->size = (unsigned int)dumbfile_mgetl(f);
  232. if (dumbfile_error(f))
  233. break;
  234. chunk->data = (unsigned char *)malloc(chunk->size);
  235. if (!chunk->data) {
  236. free(mod->chunks);
  237. free(mod);
  238. return NULL;
  239. }
  240. bytes_read = dumbfile_getnc((char *)chunk->data, chunk->size, f);
  241. if (bytes_read < chunk->size) {
  242. if (bytes_read <= 0) {
  243. free(chunk->data);
  244. break;
  245. } else {
  246. chunk->size = (unsigned int)bytes_read;
  247. mod->chunk_count++;
  248. break;
  249. }
  250. }
  251. mod->chunk_count++;
  252. }
  253. if (!mod->chunk_count) {
  254. if (mod->chunks)
  255. free(mod->chunks);
  256. free(mod);
  257. mod = NULL;
  258. }
  259. return mod;
  260. }
  261. void free_okt(IFF_CHUNKED *mod) {
  262. unsigned i;
  263. if (mod) {
  264. if (mod->chunks) {
  265. for (i = 0; i < mod->chunk_count; i++) {
  266. if (mod->chunks[i].data)
  267. free(mod->chunks[i].data);
  268. }
  269. free(mod->chunks);
  270. }
  271. free(mod);
  272. }
  273. }
  274. const IFF_CHUNK *get_chunk_by_type(IFF_CHUNKED *mod, unsigned type,
  275. unsigned offset) {
  276. unsigned i;
  277. if (mod) {
  278. if (mod->chunks) {
  279. for (i = 0; i < mod->chunk_count; i++) {
  280. if (mod->chunks[i].type == type) {
  281. if (!offset)
  282. return &mod->chunks[i];
  283. else
  284. offset--;
  285. }
  286. }
  287. }
  288. }
  289. return NULL;
  290. }
  291. unsigned get_chunk_count(IFF_CHUNKED *mod, unsigned type) {
  292. unsigned i, count = 0;
  293. if (mod) {
  294. if (mod->chunks) {
  295. for (i = 0; i < mod->chunk_count; i++) {
  296. if (mod->chunks[i].type == type)
  297. count++;
  298. }
  299. }
  300. }
  301. return count;
  302. }
  303. static DUMB_IT_SIGDATA *it_okt_load_sigdata(DUMBFILE *f) {
  304. DUMB_IT_SIGDATA *sigdata;
  305. int n_channels;
  306. int i, j, k, l;
  307. IFF_CHUNKED *mod;
  308. const IFF_CHUNK *chunk;
  309. char signature[8];
  310. if (dumbfile_getnc(signature, 8, f) < 8 ||
  311. memcmp(signature, "OKTASONG", 8)) {
  312. return NULL;
  313. }
  314. mod = dumbfile_read_okt(f);
  315. if (!mod)
  316. return NULL;
  317. sigdata = (DUMB_IT_SIGDATA *)malloc(sizeof(*sigdata));
  318. if (!sigdata) {
  319. free_okt(mod);
  320. return NULL;
  321. }
  322. sigdata->name[0] = 0;
  323. chunk = get_chunk_by_type(mod, DUMB_ID('S', 'P', 'E', 'E'), 0);
  324. if (!chunk || chunk->size < 2) {
  325. free(sigdata);
  326. free_okt(mod);
  327. return NULL;
  328. }
  329. sigdata->speed = (chunk->data[0] << 8) | chunk->data[1];
  330. chunk = get_chunk_by_type(mod, DUMB_ID('S', 'A', 'M', 'P'), 0);
  331. if (!chunk || chunk->size < 32) {
  332. free(sigdata);
  333. free_okt(mod);
  334. return NULL;
  335. }
  336. sigdata->n_samples = chunk->size / 32;
  337. chunk = get_chunk_by_type(mod, DUMB_ID('C', 'M', 'O', 'D'), 0);
  338. if (!chunk || chunk->size < 8) {
  339. free(sigdata);
  340. free_okt(mod);
  341. return NULL;
  342. }
  343. n_channels = 0;
  344. for (i = 0; i < 4; i++) {
  345. j = (chunk->data[i * 2] << 8) | chunk->data[i * 2 + 1];
  346. if (!j)
  347. n_channels++;
  348. else if (j == 1)
  349. n_channels += 2;
  350. }
  351. if (!n_channels) {
  352. free(sigdata);
  353. free_okt(mod);
  354. return NULL;
  355. }
  356. sigdata->n_pchannels = n_channels;
  357. sigdata->sample =
  358. (IT_SAMPLE *)malloc(sigdata->n_samples * sizeof(*sigdata->sample));
  359. if (!sigdata->sample) {
  360. free(sigdata);
  361. free_okt(mod);
  362. return NULL;
  363. }
  364. sigdata->song_message = NULL;
  365. sigdata->order = NULL;
  366. sigdata->instrument = NULL;
  367. sigdata->pattern = NULL;
  368. sigdata->midi = NULL;
  369. sigdata->checkpoint = NULL;
  370. sigdata->n_instruments = 0;
  371. for (i = 0; i < sigdata->n_samples; i++)
  372. sigdata->sample[i].data = NULL;
  373. chunk = get_chunk_by_type(mod, DUMB_ID('S', 'A', 'M', 'P'), 0);
  374. for (i = 0; i < sigdata->n_samples; i++) {
  375. it_okt_read_sample_header(&sigdata->sample[i], chunk->data + 32 * i);
  376. }
  377. sigdata->restart_position = 0;
  378. chunk = get_chunk_by_type(mod, DUMB_ID('P', 'L', 'E', 'N'), 0);
  379. if (!chunk || chunk->size < 2) {
  380. _dumb_it_unload_sigdata(sigdata);
  381. free_okt(mod);
  382. return NULL;
  383. }
  384. sigdata->n_orders = (chunk->data[0] << 8) | chunk->data[1];
  385. // what if this is > 128?
  386. if (sigdata->n_orders <= 0 || sigdata->n_orders > 128) {
  387. _dumb_it_unload_sigdata(sigdata);
  388. free_okt(mod);
  389. return NULL;
  390. }
  391. chunk = get_chunk_by_type(mod, DUMB_ID('P', 'A', 'T', 'T'), 0);
  392. if (!chunk || chunk->size < (unsigned)sigdata->n_orders) {
  393. _dumb_it_unload_sigdata(sigdata);
  394. free_okt(mod);
  395. return NULL;
  396. }
  397. sigdata->order = (unsigned char *)malloc(sigdata->n_orders);
  398. if (!sigdata->order) {
  399. _dumb_it_unload_sigdata(sigdata);
  400. free_okt(mod);
  401. return NULL;
  402. }
  403. memcpy(sigdata->order, chunk->data, sigdata->n_orders);
  404. /* Work out how many patterns there are. */
  405. chunk = get_chunk_by_type(mod, DUMB_ID('S', 'L', 'E', 'N'), 0);
  406. if (!chunk || chunk->size < 2) {
  407. _dumb_it_unload_sigdata(sigdata);
  408. free_okt(mod);
  409. return NULL;
  410. }
  411. sigdata->n_patterns = (chunk->data[0] << 8) | chunk->data[1];
  412. j = get_chunk_count(mod, DUMB_ID('P', 'B', 'O', 'D'));
  413. if (sigdata->n_patterns > j)
  414. sigdata->n_patterns = j;
  415. if (!sigdata->n_patterns) {
  416. _dumb_it_unload_sigdata(sigdata);
  417. free_okt(mod);
  418. return NULL;
  419. }
  420. sigdata->pattern =
  421. (IT_PATTERN *)malloc(sigdata->n_patterns * sizeof(*sigdata->pattern));
  422. if (!sigdata->pattern) {
  423. _dumb_it_unload_sigdata(sigdata);
  424. free_okt(mod);
  425. return NULL;
  426. }
  427. for (i = 0; i < sigdata->n_patterns; i++)
  428. sigdata->pattern[i].entry = NULL;
  429. /* Read in the patterns */
  430. for (i = 0; i < sigdata->n_patterns; i++) {
  431. chunk = get_chunk_by_type(mod, DUMB_ID('P', 'B', 'O', 'D'), i);
  432. if (it_okt_read_pattern(&sigdata->pattern[i], chunk->data, chunk->size,
  433. n_channels) != 0) {
  434. _dumb_it_unload_sigdata(sigdata);
  435. free_okt(mod);
  436. return NULL;
  437. }
  438. }
  439. /* And finally, the sample data */
  440. k = get_chunk_count(mod, DUMB_ID('S', 'B', 'O', 'D'));
  441. for (i = 0, j = 0; i < sigdata->n_samples && j < k; i++) {
  442. if (sigdata->sample[i].flags & IT_SAMPLE_EXISTS) {
  443. chunk = get_chunk_by_type(mod, DUMB_ID('S', 'B', 'O', 'D'), j);
  444. if (it_okt_read_sample_data(&sigdata->sample[i],
  445. (const char *)chunk->data,
  446. chunk->size)) {
  447. _dumb_it_unload_sigdata(sigdata);
  448. free_okt(mod);
  449. return NULL;
  450. }
  451. j++;
  452. }
  453. }
  454. for (; i < sigdata->n_samples; i++) {
  455. sigdata->sample[i].flags = 0;
  456. }
  457. chunk = get_chunk_by_type(mod, DUMB_ID('C', 'M', 'O', 'D'), 0);
  458. for (i = 0, j = 0; i < n_channels && j < 4; j++) {
  459. k = (chunk->data[j * 2] << 8) | chunk->data[j * 2 + 1];
  460. l = (j == 1 || j == 2) ? 48 : 16;
  461. if (k == 0) {
  462. sigdata->channel_pan[i++] = l;
  463. } else if (k == 1) {
  464. sigdata->channel_pan[i++] = l;
  465. sigdata->channel_pan[i++] = l;
  466. }
  467. }
  468. free_okt(mod);
  469. /* Now let's initialise the remaining variables, and we're done! */
  470. sigdata->flags = IT_WAS_AN_OKT | IT_WAS_AN_XM | IT_WAS_A_MOD |
  471. IT_OLD_EFFECTS | IT_COMPATIBLE_GXX | IT_STEREO;
  472. sigdata->global_volume = 128;
  473. sigdata->mixing_volume = 48;
  474. /* We want 50 ticks per second; 50/6 row advances per second;
  475. * 50*10=500 row advances per minute; 500/4=125 beats per minute.
  476. */
  477. sigdata->tempo = 125;
  478. sigdata->pan_separation = 128;
  479. memset(sigdata->channel_volume, 64, DUMB_IT_N_CHANNELS);
  480. memset(sigdata->channel_pan + n_channels, 32,
  481. DUMB_IT_N_CHANNELS - n_channels);
  482. if (_dumb_it_fix_invalid_orders(sigdata) < 0) {
  483. _dumb_it_unload_sigdata(sigdata);
  484. return NULL;
  485. }
  486. return sigdata;
  487. }
  488. DUH *dumb_read_okt_quick(DUMBFILE *f) {
  489. sigdata_t *sigdata;
  490. DUH_SIGTYPE_DESC *descptr = &_dumb_sigtype_it;
  491. sigdata = it_okt_load_sigdata(f);
  492. if (!sigdata)
  493. return NULL;
  494. {
  495. const char *tag[1][2];
  496. tag[0][0] = "FORMAT";
  497. tag[0][1] = "Oktalyzer";
  498. return make_duh(-1, 1, (const char *const(*)[2])tag, 1, &descptr,
  499. &sigdata);
  500. }
  501. }