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