1 /* _______ ____ __ ___ ___
2 * \ _ \ \ / \ / \ \ / / ' ' '
3 * | | \ \ | | || | \/ | . .
4 * | | | | | | || ||\ /| |
5 * | | | | | | || || \/ | | ' ' '
6 * | | | | | | || || | | . .
7 * | |_/ / \ \__// || | |
8 * /_______/ynamic \____/niversal /__\ /____\usic /| . . ibliotheque
9 * / \
10 * / . \
11 * readxm.c - Code to read a Fast Tracker II / / \ \
12 * module from an open file. | < / \_
13 * | \/ /\ /
14 * By Julien Cugniere. Some bits of code taken \_ / > /
15 * from reads3m.c. | \ / /
16 * | ' /
17 * \__/
18 */
19
20 #include <stdlib.h>
21 #include <string.h>
22 #include <math.h>
23
24 #include "dumb.h"
25 #include "internal/it.h"
26
27
28
29 /** TODO:
30
31 * XM_TREMOLO doesn't sound quite right...
32 * XM_E_SET_FINETUNE done but not tested yet.
33 * XM_SET_ENVELOPE_POSITION todo.
34
35 * VIBRATO conversion needs to be checked (sample/effect/volume). Plus check
36 that effect memory is correct when using XM_VOLSLIDE_VIBRATO.
37 - sample vibrato (instrument vibrato) is now handled correctly. - entheh
38
39 * XM_E_SET_VIBRATO/TREMOLO_CONTROL: effectvalue&4 -> don't retrig wave when
40 a new instrument is played. In retrigger_note()?. Is it worth implementing?
41
42 * Lossy fadeout approximation. 0..31 converted to 0 --> won't fade at all.
43
44 * Replace DUMB's sawtooth by ramp_down/ramp_up. Update XM loader.
45
46 * A lot of things need to be reset when the end of the song is reached.
47
48 * It seems that IT and XM don't behave the same way when dealing with
49 mixed loops. When IT encounters multiple SBx (x>0) commands on the same
50 row, it decrements the loop count for all, but only execute the loop of
51 the last one (highest channel). FT2 only decrements the loop count of the
52 last one. Not that I know of any modules using so convoluted combinations!
53
54 * Maybe we could remove patterns that don't appear in the order table ? Or
55 provide a function to "optimize" a DUMB_IT_SIGDATA ?
56
57 */
58
59
60
61 #define XM_LINEAR_FREQUENCY 1 /* otherwise, use amiga slides */
62
63 #define XM_ENTRY_PACKED 128
64 #define XM_ENTRY_NOTE 1
65 #define XM_ENTRY_INSTRUMENT 2
66 #define XM_ENTRY_VOLUME 4
67 #define XM_ENTRY_EFFECT 8
68 #define XM_ENTRY_EFFECTVALUE 16
69
70 #define XM_NOTE_OFF 97
71
72 #define XM_ENVELOPE_ON 1
73 #define XM_ENVELOPE_SUSTAIN 2
74 #define XM_ENVELOPE_LOOP 4
75
76 #define XM_SAMPLE_NO_LOOP 0
77 #define XM_SAMPLE_FORWARD_LOOP 1
78 #define XM_SAMPLE_PINGPONG_LOOP 2
79 #define XM_SAMPLE_16BIT 16
80 #define XM_SAMPLE_STEREO 32
81
82 #define XM_VIBRATO_SINE 0
83 #define XM_VIBRATO_SQUARE 1
84 #define XM_VIBRATO_RAMP_DOWN 2
85 #define XM_VIBRATO_RAMP_UP 3
86
87
88
89 /* Probably useless :) */
90 static const char xm_convert_vibrato[] = {
91 IT_VIBRATO_SINE,
92 IT_VIBRATO_SQUARE,
93 IT_VIBRATO_SAWTOOTH,
94 IT_VIBRATO_SAWTOOTH
95 };
96
97
98
99 #define XM_MAX_SAMPLES_PER_INSTRUMENT 16
100
101
102
103 /* Extra data that doesn't fit inside IT_INSTRUMENT */
104 typedef struct XM_INSTRUMENT_EXTRA
105 {
106 int n_samples;
107 int vibrato_type;
108 int vibrato_sweep; /* 0-0xFF */
109 int vibrato_depth; /* 0-0x0F */
110 int vibrato_speed; /* 0-0x3F */
111 }
112 XM_INSTRUMENT_EXTRA;
113
114
115
116 /* Frees the original block if it can't resize it or if size is 0, and acts
117 * as malloc if ptr is NULL.
118 */
safe_realloc(void * ptr,size_t size)119 static void *safe_realloc(void *ptr, size_t size)
120 {
121 if (ptr == NULL)
122 return malloc(size);
123
124 if (size == 0) {
125 free(ptr);
126 return NULL;
127 } else {
128 void *new_block = realloc(ptr, size);
129 if (!new_block)
130 free(ptr);
131 return new_block;
132 }
133 }
134
135
136
137 /* The interpretation of the XM volume column is left to the player. Here, we
138 * just filter bad values.
139 */
140 // This function is so tiny now, should we inline it?
it_xm_convert_volume(int volume,IT_ENTRY * entry)141 static void it_xm_convert_volume(int volume, IT_ENTRY *entry)
142 {
143 entry->mask |= IT_ENTRY_VOLPAN;
144 entry->volpan = volume;
145
146 switch (volume >> 4) {
147 case 0xA: /* set vibrato speed */
148 case 0xB: /* vibrato */
149 case 0xF: /* tone porta */
150 case 0x6: /* vol slide up */
151 case 0x7: /* vol slide down */
152 case 0x8: /* fine vol slide up */
153 case 0x9: /* fine vol slide down */
154 case 0xC: /* set panning */
155 case 0xD: /* pan slide left */
156 case 0xE: /* pan slide right */
157 case 0x1: /* set volume */
158 case 0x2: /* set volume */
159 case 0x3: /* set volume */
160 case 0x4: /* set volume */
161 break;
162
163 case 0x5:
164 if (volume == 0x50)
165 break; /* set volume */
166 /* else fall through */
167
168 default:
169 entry->mask &= ~IT_ENTRY_VOLPAN;
170 break;
171 }
172 }
173
174
175
it_xm_read_pattern(IT_PATTERN * pattern,DUMBFILE * f,int n_channels,unsigned char * buffer)176 static int it_xm_read_pattern(IT_PATTERN *pattern, DUMBFILE *f, int n_channels, unsigned char *buffer)
177 {
178 int size;
179 int pos;
180 int channel;
181 int row;
182 int effect, effectvalue;
183 IT_ENTRY *entry;
184
185 /* pattern header size */
186 if (dumbfile_igetl(f) != 0x09) {
187 TRACE("XM error: unexpected pattern header size\n");
188 return -1;
189 }
190
191 /* pattern data packing type */
192 if (dumbfile_getc(f) != 0) {
193 TRACE("XM error: unexpected pattern packing type\n");
194 return -1;
195 }
196
197 pattern->n_rows = dumbfile_igetw(f); /* 1..256 */
198 size = dumbfile_igetw(f);
199 pattern->n_entries = 0;
200
201 if (dumbfile_error(f))
202 return -1;
203
204 if (size == 0)
205 return 0;
206
207 if (size > 1280 * n_channels) {
208 TRACE("XM error: pattern data size > %d bytes\n", 1280 * n_channels);
209 return -1;
210 }
211
212 if (dumbfile_getnc(buffer, size, f) < size)
213 return -1;
214
215 /* compute number of entries */
216 pattern->n_entries = 0;
217 pos = channel = row = 0;
218 while (pos < size) {
219 if (!(buffer[pos] & XM_ENTRY_PACKED) || (buffer[pos] & 31))
220 pattern->n_entries++;
221
222 channel++;
223 if (channel >= n_channels) {
224 channel = 0;
225 row++;
226 pattern->n_entries++;
227 }
228
229 if (buffer[pos] & XM_ENTRY_PACKED) {
230 static const char offset[] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
231 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5 };
232 pos += 1 + offset[buffer[pos] & 31];
233 } else {
234 pos += 5;
235 }
236 }
237
238 if (row != pattern->n_rows) {
239 TRACE("XM error: wrong number of rows in pattern data\n");
240 return -1;
241 }
242
243 pattern->entry = malloc(pattern->n_entries * sizeof(*pattern->entry));
244 if (!pattern->entry)
245 return -1;
246
247 /* read the entries */
248 entry = pattern->entry;
249 pos = channel = row = 0;
250 while (pos < size) {
251 unsigned char mask;
252
253 if (buffer[pos] & XM_ENTRY_PACKED)
254 mask = buffer[pos++] & 31;
255 else
256 mask = 31;
257
258 if (mask) {
259 ASSERT(entry < pattern->entry + pattern->n_entries);
260
261 entry->channel = channel;
262 entry->mask = 0;
263
264 if (mask & XM_ENTRY_NOTE) {
265 int note = buffer[pos++]; /* 1-96 <=> C0-B7 */
266 entry->note = (note == XM_NOTE_OFF) ? (IT_NOTE_OFF) : (note-1);
267 entry->mask |= IT_ENTRY_NOTE;
268 }
269
270 if (mask & XM_ENTRY_INSTRUMENT) {
271 entry->instrument = buffer[pos++]; /* 1-128 */
272 entry->mask |= IT_ENTRY_INSTRUMENT;
273 }
274
275 if (mask & XM_ENTRY_VOLUME)
276 it_xm_convert_volume(buffer[pos++], entry);
277
278 effect = effectvalue = 0;
279 if (mask & XM_ENTRY_EFFECT) effect = buffer[pos++];
280 if (mask & XM_ENTRY_EFFECTVALUE) effectvalue = buffer[pos++];
281 _dumb_it_xm_convert_effect(effect, effectvalue, entry);
282
283 entry++;
284 }
285
286 channel++;
287 if (channel >= n_channels) {
288 channel = 0;
289 row++;
290 IT_SET_END_ROW(entry);
291 entry++;
292 }
293 }
294
295 return 0;
296 }
297
298
299
it_xm_make_envelope(IT_ENVELOPE * envelope,const unsigned short * data,int y_offset)300 static int it_xm_make_envelope(IT_ENVELOPE *envelope, const unsigned short *data, int y_offset)
301 {
302 int i, pos;
303
304 if (envelope->n_nodes > 12) {
305 TRACE("XM error: wrong number of envelope nodes (%d)\n", envelope->n_nodes);
306 envelope->n_nodes = 0;
307 return -1;
308 }
309
310 pos = 0;
311 for (i = 0; i < envelope->n_nodes; i++) {
312 envelope->node_t[i] = data[pos++];
313 if (data[pos] > 64) {
314 TRACE("XM error: out-of-range envelope node (node_y[%d]=%d)\n", i, data[pos]);
315 envelope->n_nodes = 0;
316 return -1;
317 }
318 envelope->node_y[i] = (signed char)(data[pos++] + y_offset);
319 }
320
321 return 0;
322 }
323
324
325
it_xm_read_instrument(IT_INSTRUMENT * instrument,XM_INSTRUMENT_EXTRA * extra,DUMBFILE * f)326 static int it_xm_read_instrument(IT_INSTRUMENT *instrument, XM_INSTRUMENT_EXTRA *extra, DUMBFILE *f)
327 {
328 unsigned long size, bytes_read;
329 unsigned short vol_points[24];
330 unsigned short pan_points[24];
331 int i, type;
332
333 /* Header size. Tends to be more than the actual size of the structure.
334 * So unread bytes must be skipped before reading the first sample
335 * header.
336 */
337 size = dumbfile_igetl(f);
338
339 dumbfile_getnc(instrument->name, 22, f);
340 instrument->name[22] = 0;
341 instrument->filename[0] = 0;
342 dumbfile_skip(f, 1); /* Instrument type. Should be 0, but seems random. */
343 extra->n_samples = dumbfile_igetw(f);
344
345 if (dumbfile_error(f) || (unsigned int)extra->n_samples > XM_MAX_SAMPLES_PER_INSTRUMENT)
346 return -1;
347
348 bytes_read = 4 + 22 + 1 + 2;
349
350 if (extra->n_samples) {
351 /* sample header size */
352 if (dumbfile_igetl(f) != 0x28) {
353 TRACE("XM error: unexpected sample header size\n");
354 return -1;
355 }
356
357 /* sample map */
358 for (i = 0; i < 96; i++) {
359 instrument->map_sample[i] = dumbfile_getc(f) + 1;
360 instrument->map_note[i] = i;
361 }
362
363 if (dumbfile_error(f))
364 return 1;
365
366 /* volume/panning envelopes */
367 for (i = 0; i < 24; i++)
368 vol_points[i] = dumbfile_igetw(f);
369 for (i = 0; i < 24; i++)
370 pan_points[i] = dumbfile_igetw(f);
371
372 instrument->volume_envelope.n_nodes = dumbfile_getc(f);
373 instrument->pan_envelope.n_nodes = dumbfile_getc(f);
374
375 if (dumbfile_error(f))
376 return -1;
377
378 instrument->volume_envelope.sus_loop_start = dumbfile_getc(f);
379 instrument->volume_envelope.loop_start = dumbfile_getc(f);
380 instrument->volume_envelope.loop_end = dumbfile_getc(f);
381
382 instrument->pan_envelope.sus_loop_start = dumbfile_getc(f);
383 instrument->pan_envelope.loop_start = dumbfile_getc(f);
384 instrument->pan_envelope.loop_end = dumbfile_getc(f);
385
386 /* The envelope handler for XM files won't use sus_loop_end. */
387
388 type = dumbfile_getc(f);
389 instrument->volume_envelope.flags = 0;
390 if ((type & XM_ENVELOPE_ON) && instrument->volume_envelope.n_nodes)
391 instrument->volume_envelope.flags |= IT_ENVELOPE_ON;
392 if (type & XM_ENVELOPE_LOOP) instrument->volume_envelope.flags |= IT_ENVELOPE_LOOP_ON;
393 #if 1
394 if (type & XM_ENVELOPE_SUSTAIN) instrument->volume_envelope.flags |= IT_ENVELOPE_SUSTAIN_LOOP;
395 #else // This is now handled in itrender.c
396 /* let's avoid fading out when reaching the last envelope node */
397 if (!(type & XM_ENVELOPE_LOOP)) {
398 instrument->volume_envelope.loop_start = instrument->volume_envelope.n_nodes-1;
399 instrument->volume_envelope.loop_end = instrument->volume_envelope.n_nodes-1;
400 }
401 instrument->volume_envelope.flags |= IT_ENVELOPE_LOOP_ON;
402 #endif
403
404 type = dumbfile_getc(f);
405 instrument->pan_envelope.flags = 0;
406 if ((type & XM_ENVELOPE_ON) && instrument->pan_envelope.n_nodes)
407 instrument->pan_envelope.flags |= IT_ENVELOPE_ON;
408 if (type & XM_ENVELOPE_LOOP) instrument->pan_envelope.flags |= IT_ENVELOPE_LOOP_ON; // should this be here?
409 if (type & XM_ENVELOPE_SUSTAIN) instrument->pan_envelope.flags |= IT_ENVELOPE_SUSTAIN_LOOP;
410
411 if (it_xm_make_envelope(&instrument->volume_envelope, vol_points, 0) != 0) {
412 TRACE("XM error: volume envelope\n");
413 if (instrument->volume_envelope.flags & IT_ENVELOPE_ON) return -1;
414 }
415
416 if (it_xm_make_envelope(&instrument->pan_envelope, pan_points, -32) != 0) {
417 TRACE("XM error: pan envelope\n");
418 if (instrument->pan_envelope.flags & IT_ENVELOPE_ON) return -1;
419 }
420
421 instrument->pitch_envelope.flags = 0;
422
423 extra->vibrato_type = dumbfile_getc(f);
424 extra->vibrato_sweep = dumbfile_getc(f);
425 extra->vibrato_depth = dumbfile_getc(f);
426 extra->vibrato_speed = dumbfile_getc(f);
427
428 if (dumbfile_error(f) || extra->vibrato_type >= 4)
429 return -1;
430
431 /** WARNING: lossy approximation */
432 instrument->fadeout = (dumbfile_igetw(f)*128 + 64)/0xFFF;
433
434 dumbfile_skip(f, 2); /* reserved */
435
436 bytes_read += 4 + 96 + 48 + 48 + 14*1 + 2 + 2;
437 } else
438 for (i = 0; i < 96; i++)
439 instrument->map_sample[i] = 0;
440
441 if (dumbfile_skip(f, size - bytes_read))
442 return -1;
443
444 instrument->new_note_action = NNA_NOTE_CUT;
445 instrument->dup_check_type = DCT_OFF;
446 instrument->dup_check_action = DCA_NOTE_CUT;
447 instrument->pp_separation = 0;
448 instrument->pp_centre = 60; /* C-5 */
449 instrument->global_volume = 128;
450 instrument->default_pan = 32;
451 instrument->random_volume = 0;
452 instrument->random_pan = 0;
453 instrument->filter_cutoff = 0;
454 instrument->filter_resonance = 0;
455
456 return 0;
457 }
458
459
460
461 /* I (entheh) have two XM files saved by a very naughty program. After a
462 * 16-bit sample, it saved a rogue byte. The length of the sample was indeed
463 * an odd number, incremented to include the rogue byte.
464 *
465 * In this function we are converting sample lengths and loop points so they
466 * are measured in samples. This means we forget about the extra bytes, and
467 * they don't get skipped. So we fail trying to read the next instrument.
468 *
469 * To get around this, this function returns the number of rogue bytes that
470 * won't be accounted for by reading sample->length samples. It returns a
471 * negative number on failure.
472 */
it_xm_read_sample_header(IT_SAMPLE * sample,DUMBFILE * f)473 static int it_xm_read_sample_header(IT_SAMPLE *sample, DUMBFILE *f)
474 {
475 int type;
476 int relative_note_number; /* relative to C4 */
477 int finetune;
478 int roguebytes;
479 int roguebytesmask;
480
481 sample->length = dumbfile_igetl(f);
482 sample->loop_start = dumbfile_igetl(f);
483 sample->loop_end = sample->loop_start + dumbfile_igetl(f);
484 sample->global_volume = 64;
485 sample->default_volume = dumbfile_getc(f);
486 finetune = (signed char)dumbfile_getc(f); /* -128..127 <=> -1 semitone .. +127/128 of a semitone */
487 type = dumbfile_getc(f);
488 sample->default_pan = dumbfile_getc(f); /* 0-255 */
489 relative_note_number = (signed char)dumbfile_getc(f);
490
491 dumbfile_skip(f, 1); /* reserved */
492
493 dumbfile_getnc(sample->name, 22, f);
494 sample->name[22] = 0;
495
496 sample->filename[0] = 0;
497
498 if (dumbfile_error(f))
499 return -1;
500
501 sample->C5_speed = (long)(16726.0*pow(DUMB_SEMITONE_BASE, relative_note_number)*pow(DUMB_PITCH_BASE, finetune*2));
502
503 sample->flags = IT_SAMPLE_EXISTS;
504
505 roguebytes = (int)sample->length;
506 roguebytesmask = 3;
507
508 if (type & XM_SAMPLE_16BIT) {
509 sample->flags |= IT_SAMPLE_16BIT;
510 sample->length >>= 1;
511 sample->loop_start >>= 1;
512 sample->loop_end >>= 1;
513 } else
514 roguebytesmask >>= 1;
515
516 if (type & XM_SAMPLE_STEREO) {
517 sample->flags |= IT_SAMPLE_STEREO;
518 sample->length >>= 1;
519 sample->loop_start >>= 1;
520 sample->loop_end >>= 1;
521 } else
522 roguebytesmask >>= 1;
523
524 roguebytes &= roguebytesmask;
525
526 if ((unsigned int)sample->loop_start < (unsigned int)sample->loop_end) {
527 if (type & XM_SAMPLE_FORWARD_LOOP) sample->flags |= IT_SAMPLE_LOOP;
528 if (type & XM_SAMPLE_PINGPONG_LOOP) sample->flags |= IT_SAMPLE_LOOP | IT_SAMPLE_PINGPONG_LOOP;
529 }
530
531 if (sample->length <= 0)
532 sample->flags &= ~IT_SAMPLE_EXISTS;
533 else if ((unsigned int)sample->loop_end > (unsigned int)sample->length)
534 sample->flags &= ~IT_SAMPLE_LOOP;
535 else if ((unsigned int)sample->loop_start >= (unsigned int)sample->loop_end)
536 sample->flags &= ~IT_SAMPLE_LOOP;
537
538 return roguebytes;
539 }
540
541
542
it_xm_read_sample_data(IT_SAMPLE * sample,unsigned char roguebytes,DUMBFILE * f)543 static int it_xm_read_sample_data(IT_SAMPLE *sample, unsigned char roguebytes, DUMBFILE *f)
544 {
545 int old;
546 long i;
547 long truncated_size;
548 int n_channels;
549 long datasize;
550
551 if (!(sample->flags & IT_SAMPLE_EXISTS))
552 return dumbfile_skip(f, roguebytes);
553
554 /* let's get rid of the sample data coming after the end of the loop */
555 if ((sample->flags & IT_SAMPLE_LOOP) && sample->loop_end < sample->length) {
556 truncated_size = sample->length - sample->loop_end;
557 sample->length = sample->loop_end;
558 } else {
559 truncated_size = 0;
560 }
561
562 n_channels = sample->flags & IT_SAMPLE_STEREO ? 2 : 1;
563 datasize = sample->length * n_channels;
564
565 sample->data = malloc(datasize * (sample->flags & IT_SAMPLE_16BIT ? 2 : 1));
566 if (!sample->data)
567 return -1;
568
569 /* sample data is stored as signed delta values */
570 old = 0;
571 if (sample->flags & IT_SAMPLE_16BIT)
572 for (i = 0; i < sample->length; i++)
573 ((short *)sample->data)[i*n_channels] = old += dumbfile_igetw(f);
574 else
575 for (i = 0; i < sample->length; i++)
576 ((signed char *)sample->data)[i*n_channels] = old += dumbfile_getc(f);
577
578 /* skip truncated data */
579 dumbfile_skip(f, (sample->flags & IT_SAMPLE_16BIT) ? (2*truncated_size) : (truncated_size));
580
581 if (sample->flags & IT_SAMPLE_STEREO) {
582 old = 0;
583 if (sample->flags & IT_SAMPLE_16BIT)
584 for (i = 1; i < datasize; i += 2)
585 ((short *)sample->data)[i] = old += dumbfile_igetw(f);
586 else
587 for (i = 1; i < datasize; i += 2)
588 ((signed char *)sample->data)[i] = old += dumbfile_getc(f);
589
590 /* skip truncated data */
591 dumbfile_skip(f, (sample->flags & IT_SAMPLE_16BIT) ? (2*truncated_size) : (truncated_size));
592 }
593
594 dumbfile_skip(f, roguebytes);
595
596 if (dumbfile_error(f))
597 return -1;
598
599 return 0;
600 }
601
602
603
604 /* "Real programmers don't document. If it was hard to write,
605 * it should be hard to understand."
606 *
607 * (Never trust the documentation provided with a tracker.
608 * Real files are the only truth...)
609 */
it_xm_load_sigdata(DUMBFILE * f)610 static DUMB_IT_SIGDATA *it_xm_load_sigdata(DUMBFILE *f)
611 {
612 DUMB_IT_SIGDATA *sigdata;
613 char id_text[18];
614
615 int flags;
616 int n_channels;
617 int total_samples;
618 int i, j;
619
620 /* check ID text */
621 if (dumbfile_getnc(id_text, 17, f) < 17)
622 return NULL;
623 id_text[17] = 0;
624 if (strcmp(id_text, "Extended Module: ") != 0) {
625 TRACE("XM error: Not an Extended Module\n");
626 return NULL;
627 }
628
629 sigdata = malloc(sizeof(*sigdata));
630 if (!sigdata)
631 return NULL;
632
633 /* song name */
634 if (dumbfile_getnc(sigdata->name, 20, f) < 20) {
635 free(sigdata);
636 return NULL;
637 }
638 sigdata->name[20] = 0;
639
640 if (dumbfile_getc(f) != 0x1A) {
641 TRACE("XM error: 0x1A not found\n");
642 free(sigdata);
643 return NULL;
644 }
645
646 /* tracker name */
647 if (dumbfile_skip(f, 20)) {
648 free(sigdata);
649 return NULL;
650 }
651
652 /* version number */
653 if (dumbfile_igetw(f) != 0x0104) {
654 TRACE("XM error: wrong format version\n");
655 free(sigdata);
656 return NULL;
657 }
658
659 /*
660 ------------------
661 --- Header ---
662 ------------------
663 */
664
665 /* header size */
666 if (dumbfile_igetl(f) != 0x0114) {
667 TRACE("XM error: unexpected header size\n");
668 free(sigdata);
669 return NULL;
670 }
671
672 sigdata->song_message = NULL;
673 sigdata->order = NULL;
674 sigdata->instrument = NULL;
675 sigdata->sample = NULL;
676 sigdata->pattern = NULL;
677 sigdata->midi = NULL;
678 sigdata->checkpoint = NULL;
679
680 sigdata->n_samples = 0;
681 sigdata->n_orders = dumbfile_igetw(f);
682 sigdata->restart_position = dumbfile_igetw(f);
683 n_channels = dumbfile_igetw(f); /* max 32 but we'll be lenient */
684 sigdata->n_patterns = dumbfile_igetw(f);
685 sigdata->n_instruments = dumbfile_igetw(f); /* max 128 */
686 flags = dumbfile_igetw(f);
687 sigdata->speed = dumbfile_igetw(f);
688 if (sigdata->speed == 0) sigdata->speed = 6; // Should we? What about tempo?
689 sigdata->tempo = dumbfile_igetw(f);
690
691 /* sanity checks */
692 if (dumbfile_error(f) || sigdata->n_orders <= 0 || sigdata->n_orders > 256 || sigdata->n_patterns > 256 || sigdata->n_instruments > 128 || n_channels > DUMB_IT_N_CHANNELS) {
693 _dumb_it_unload_sigdata(sigdata);
694 return NULL;
695 }
696
697 //if (sigdata->restart_position >= sigdata->n_orders)
698 //sigdata->restart_position = 0;
699
700 /* order table */
701 sigdata->order = malloc(sigdata->n_orders*sizeof(*sigdata->order));
702 if (!sigdata->order) {
703 _dumb_it_unload_sigdata(sigdata);
704 return NULL;
705 }
706 dumbfile_getnc(sigdata->order, sigdata->n_orders, f);
707 dumbfile_skip(f, 256 - sigdata->n_orders);
708
709 if (dumbfile_error(f)) {
710 _dumb_it_unload_sigdata(sigdata);
711 return NULL;
712 }
713
714 /*
715 --------------------
716 --- Patterns ---
717 --------------------
718 */
719
720 sigdata->pattern = malloc(sigdata->n_patterns * sizeof(*sigdata->pattern));
721 if (!sigdata->pattern) {
722 _dumb_it_unload_sigdata(sigdata);
723 return NULL;
724 }
725 for (i = 0; i < sigdata->n_patterns; i++)
726 sigdata->pattern[i].entry = NULL;
727
728 {
729 unsigned char *buffer = malloc(1280 * n_channels); /* 256 rows * 5 bytes */
730 if (!buffer) {
731 _dumb_it_unload_sigdata(sigdata);
732 return NULL;
733 }
734 for (i = 0; i < sigdata->n_patterns; i++) {
735 if (it_xm_read_pattern(&sigdata->pattern[i], f, n_channels, buffer) != 0) {
736 free(buffer);
737 _dumb_it_unload_sigdata(sigdata);
738 return NULL;
739 }
740 }
741 free(buffer);
742 }
743
744 /*
745 -----------------------------------
746 --- Instruments and Samples ---
747 -----------------------------------
748 */
749
750 sigdata->instrument = malloc(sigdata->n_instruments * sizeof(*sigdata->instrument));
751 if (!sigdata->instrument) {
752 _dumb_it_unload_sigdata(sigdata);
753 return NULL;
754 }
755
756 /* With XM, samples are not global, they're part of an instrument. In a
757 * file, each instrument is stored with its samples. Because of this, I
758 * don't know how to find how many samples are present in the file. Thus
759 * I have to do n_instruments reallocation on sigdata->sample.
760 * Looking at FT2, it doesn't seem possible to have more than 16 samples
761 * per instrument (even though n_samples is stored as 2 bytes). So maybe
762 * we could allocate a 128*16 array of samples, and shrink it back to the
763 * correct size when we know it?
764 * Alternatively, I could allocate samples by blocks of N (still O(n)),
765 * or double the number of allocated samples when I need more (O(log n)).
766 */
767 total_samples = 0;
768 sigdata->sample = NULL;
769
770 for (i = 0; i < sigdata->n_instruments; i++) {
771 XM_INSTRUMENT_EXTRA extra;
772
773 if (it_xm_read_instrument(&sigdata->instrument[i], &extra, f) < 0) {
774 TRACE("XM error: instrument %d\n", i+1);
775 _dumb_it_unload_sigdata(sigdata);
776 return NULL;
777 }
778
779 if (extra.n_samples) {
780 unsigned char roguebytes[XM_MAX_SAMPLES_PER_INSTRUMENT];
781
782 /* adjust instrument sample map (make indices absolute) */
783 for (j = 0; j < 96; j++)
784 sigdata->instrument[i].map_sample[j] += total_samples;
785
786 sigdata->sample = safe_realloc(sigdata->sample, sizeof(*sigdata->sample)*(total_samples+extra.n_samples));
787 if (!sigdata->sample) {
788 _dumb_it_unload_sigdata(sigdata);
789 return NULL;
790 }
791 for (j = total_samples; j < total_samples+extra.n_samples; j++)
792 sigdata->sample[j].data = NULL;
793
794 /* read instrument's samples */
795 for (j = 0; j < extra.n_samples; j++) {
796 IT_SAMPLE *sample = &sigdata->sample[total_samples+j];
797 int b = it_xm_read_sample_header(sample, f);
798 if (b < 0) {
799 _dumb_it_unload_sigdata(sigdata);
800 return NULL;
801 }
802 roguebytes[j] = b;
803 // Any reason why these can't be set inside it_xm_read_sample_header()?
804 sample->vibrato_speed = extra.vibrato_speed;
805 sample->vibrato_depth = extra.vibrato_depth;
806 sample->vibrato_rate = extra.vibrato_sweep;
807 /* Rate and sweep don't match, but the difference is
808 * accounted for in itrender.c.
809 */
810 sample->vibrato_waveform = xm_convert_vibrato[extra.vibrato_type];
811 }
812 for (j = 0; j < extra.n_samples; j++) {
813 if (it_xm_read_sample_data(&sigdata->sample[total_samples+j], roguebytes[j], f) != 0) {
814 _dumb_it_unload_sigdata(sigdata);
815 return NULL;
816 }
817 }
818 total_samples += extra.n_samples;
819 }
820 }
821
822 sigdata->n_samples = total_samples;
823
824 sigdata->flags = IT_WAS_AN_XM | IT_OLD_EFFECTS | IT_COMPATIBLE_GXX | IT_STEREO | IT_USE_INSTRUMENTS;
825 // Are we OK with IT_COMPATIBLE_GXX off?
826 //
827 // When specifying note + instr + tone portamento, and an old note is still playing (even after note off):
828 // - If Compatible Gxx is on, the new note will be triggered only if the instrument _changes_.
829 // - If Compatible Gxx is off, the new note will always be triggered, provided the instrument is specified.
830 // - FT2 seems to do the latter (unconfirmed).
831
832 // Err, wait. XM playback has its own code. The change made to the IT
833 // playbackc code didn't affect XM playback. Forget this then. There's
834 // still a bug in XM playback though, and it'll need some investigation...
835 // tomorrow...
836
837 // UPDATE: IT_COMPATIBLE_GXX is required to be on, so that tone porta has
838 // separate memory from portamento.
839
840 if (flags & XM_LINEAR_FREQUENCY)
841 sigdata->flags |= IT_LINEAR_SLIDES;
842
843 sigdata->global_volume = 128;
844 sigdata->mixing_volume = 48;
845 sigdata->pan_separation = 128;
846
847 memset(sigdata->channel_volume, 64, DUMB_IT_N_CHANNELS);
848 memset(sigdata->channel_pan, 32, DUMB_IT_N_CHANNELS);
849
850 _dumb_it_fix_invalid_orders(sigdata);
851
852 return sigdata;
853 }
854
855
856
857 #if 0 // no fucking way, dude!
858
859 /* The length returned is the time required to play from the beginning of the
860 * file to the last row of the last order (which is when the player will
861 * loop). Depending on the song, the sound might stop sooner.
862 * Due to fixed point roundoffs, I think this is only reliable to the second.
863 * Full precision could be achieved by using a double during the computation,
864 * or maybe a LONG_LONG.
865 */
866 long it_compute_length(const DUMB_IT_SIGDATA *sigdata)
867 {
868 IT_PATTERN *pattern;
869 int tempo, speed;
870 int loop_start[IT_N_CHANNELS];
871 char loop_count[IT_N_CHANNELS];
872 int order, entry;
873 int row_first_entry = 0;
874 int jump, jump_dest;
875 int delay, fine_delay;
876 int i;
877 long t;
878
879 if (!sigdata)
880 return 0;
881
882 tempo = sigdata->tempo;
883 speed = sigdata->speed;
884 order = entry = 0;
885 jump = jump_dest = 0;
886 t = 0;
887
888 /* for each PATTERN */
889 for (order = 0; order < sigdata->n_orders; order++) {
890
891 if (sigdata->order[order] == IT_ORDER_END) break;
892 if (sigdata->order[order] == IT_ORDER_SKIP) continue;
893
894 for (i = 0; i < IT_N_CHANNELS; i++)
895 loop_count[i] = -1;
896
897 pattern = &sigdata->pattern[ sigdata->order[order] ];
898 entry = 0;
899 if (jump == IT_BREAK_TO_ROW) {
900 int row = 0;
901 while (row < jump_dest)
902 if (pattern->entry[entry++].channel >= IT_N_CHANNELS)
903 row++;
904 }
905
906 /* for each ROW */
907 while (entry < pattern->n_entries) {
908 row_first_entry = entry;
909 delay = fine_delay = 0;
910 jump = 0;
911
912 /* for each note NOTE */
913 while (entry < pattern->n_entries && pattern->entry[entry].channel < IT_N_CHANNELS) {
914 int value = pattern->entry[entry].effectvalue;
915 int channel = pattern->entry[entry].channel;
916
917 switch (pattern->entry[entry].effect) {
918
919 case IT_SET_SPEED: speed = value; break;
920
921 case IT_JUMP_TO_ORDER:
922 if (value <= order) /* infinite loop */
923 return 0;
924 jump = IT_JUMP_TO_ORDER;
925 jump_dest = value;
926 break;
927
928 case IT_BREAK_TO_ROW:
929 jump = IT_BREAK_TO_ROW;
930 jump_dest = value;
931 break;
932
933 case IT_S:
934 switch (HIGH(value)) {
935 case IT_S_PATTERN_DELAY: delay = LOW(value); break;
936 case IT_S_FINE_PATTERN_DELAY: fine_delay = LOW(value); break;
937 case IT_S_PATTERN_LOOP:
938 if (LOW(value) == 0) {
939 loop_start[channel] = row_first_entry;
940 } else {
941 if (loop_count[channel] == -1)
942 loop_count[channel] = LOW(value);
943
944 if (loop_count[channel]) {
945 jump = IT_S_PATTERN_LOOP;
946 jump_dest = loop_start[channel];
947 }
948 loop_count[channel]--;
949 }
950 break;
951 }
952 break;
953
954 case IT_SET_SONG_TEMPO:
955 switch (HIGH(value)) { /* slides happen every non-row frames */
956 case 0: tempo = tempo - LOW(value)*(speed-1); break;
957 case 1: tempo = tempo + LOW(value)*(speed-1); break;
958 default: tempo = value;
959 }
960 tempo = MID(32, tempo, 255);
961 break;
962 }
963
964 entry++;
965 }
966
967 /* end of ROW */
968 entry++;
969 t += TICK_TIME_DIVIDEND * (speed*(1+delay) + fine_delay) / tempo;
970
971 if (jump == IT_JUMP_TO_ORDER) {
972 order = jump_dest - 1;
973 break;
974 } else if (jump == IT_BREAK_TO_ROW)
975 break;
976 else if (jump == IT_S_PATTERN_LOOP)
977 entry = jump_dest - 1;
978 }
979
980 /* end of PATTERN */
981 }
982
983 return t;
984 }
985
986 #endif /* 0 */
987
988
989
dumb_read_xm_quick(DUMBFILE * f)990 DUH *dumb_read_xm_quick(DUMBFILE *f)
991 {
992 sigdata_t *sigdata;
993
994 DUH_SIGTYPE_DESC *descptr = &_dumb_sigtype_it;
995
996 sigdata = it_xm_load_sigdata(f);
997
998 if (!sigdata)
999 return NULL;
1000
1001 {
1002 const char *tag[1][2];
1003 tag[0][0] = "TITLE";
1004 tag[0][1] = ((DUMB_IT_SIGDATA *)sigdata)->name;
1005 return make_duh(-1, 1, (const char *const (*)[2])tag, 1, &descptr, &sigdata);
1006 }
1007 }
1008