1 /* MikMod sound library
2 (c) 1998, 1999, 2000, 2001, 2002 Miodrag Vallat and others - see file
3 AUTHORS for complete list.
5 This library is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of
8 the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 /*==============================================================================
23 $Id: load_it.c,v 1.4 2010/01/12 03:30:32 realtech Exp $
25 Impulse tracker (IT) module loader
27 ==============================================================================*/
44 #include "mikmod_internals.h"
47 extern int fprintf(FILE *, const char *, ...);
48 extern int toupper(int);
51 /*========== Module structure */
54 typedef struct ITHEADER
{
61 UWORD cwt
; /* Created with tracker (y.xx = 0x0yxx) */
62 UWORD cmwt
; /* Compatible with tracker ver > than val. */
64 UWORD special
; /* bit 0 set = song message attached */
66 UBYTE mixvol
; /* mixing volume [ignored] */
69 UBYTE pansep
; /* panning separation between channels */
78 /* sample information */
79 typedef struct ITSAMPLE
{
87 UWORD convert
; /* sample conversion flag */
98 UBYTE vibwave
; /* 0=sine, 1=rampdown, 2=square, 3=random (speed ignored) */
101 /* instrument information */
104 #define ITNOTECNT 120
105 typedef struct ITINSTHEADER
{
106 ULONG size
; /* (dword) Instrument size */
107 CHAR filename
[12]; /* (char) Instrument filename */
108 UBYTE zerobyte
; /* (byte) Instrument type (always 0) */
111 UBYTE volbeg
; /* (byte) Volume loop start (node) */
112 UBYTE volend
; /* (byte) Volume loop end (node) */
113 UBYTE volsusbeg
; /* (byte) Volume sustain begin (node) */
114 UBYTE volsusend
; /* (byte) Volume Sustain end (node) */
117 UBYTE panbeg
; /* (byte) channel loop start (node) */
118 UBYTE panend
; /* (byte) channel loop end (node) */
119 UBYTE pansusbeg
; /* (byte) channel sustain begin (node) */
120 UBYTE pansusend
; /* (byte) channel Sustain end (node) */
123 UBYTE pitbeg
; /* (byte) pitch loop start (node) */
124 UBYTE pitend
; /* (byte) pitch loop end (node) */
125 UBYTE pitsusbeg
; /* (byte) pitch sustain begin (node) */
126 UBYTE pitsusend
; /* (byte) pitch Sustain end (node) */
130 UWORD fadeout
; /* Envelope end / NNA volume fadeout */
131 UBYTE dnc
; /* Duplicate note check */
132 UBYTE dca
; /* Duplicate check action */
133 UBYTE dct
; /* Duplicate check type */
134 UBYTE nna
; /* New Note Action [0,1,2,3] */
135 UWORD trkvers
; /* tracker version used to save [files only] */
136 UBYTE ppsep
; /* Pitch-pan Separation */
137 UBYTE ppcenter
; /* Pitch-pan Center */
138 UBYTE rvolvar
; /* random volume varations */
139 UBYTE rpanvar
; /* random panning varations */
140 UWORD numsmp
; /* Number of samples in instrument [files only] */
141 CHAR name
[26]; /* Instrument name */
143 UWORD samptable
[ITNOTECNT
];/* sample for each note [note / samp pairs] */
144 UBYTE volenv
[200]; /* volume envelope (IT 1.x stuff) */
145 UBYTE oldvoltick
[ITENVCNT
];/* volume tick position (IT 1.x stuff) */
146 UBYTE volnode
[ITENVCNT
]; /* amplitude of volume nodes */
147 UWORD voltick
[ITENVCNT
]; /* tick value of volume nodes */
148 SBYTE pannode
[ITENVCNT
]; /* panenv - node points */
149 UWORD pantick
[ITENVCNT
]; /* tick value of panning nodes */
150 SBYTE pitnode
[ITENVCNT
]; /* pitchenv - node points */
151 UWORD pittick
[ITENVCNT
]; /* tick value of pitch nodes */
156 typedef struct ITNOTE
{
157 UBYTE note
,ins
,volpan
,cmd
,inf
;
160 /*========== Loader data */
162 static ULONG
*paraptr
=NULL
; /* parapointer array (see IT docs) */
163 static ITHEADER
*mh
=NULL
;
164 static ITNOTE
*itpat
=NULL
; /* allocate to space for one full pattern */
165 static UBYTE
*mask
=NULL
; /* arrays allocated to 64 elements and used for */
166 static ITNOTE
*last
=NULL
; /* uncompressing IT's pattern information */
168 static unsigned int old_effect
; /* if set, use S3M old-effects stuffs */
170 static CHAR
* IT_Version
[]={
172 "Compressed ImpulseTracker . ",
173 "ImpulseTracker 2.14p3",
174 "Compressed ImpulseTracker 2.14p3",
175 "ImpulseTracker 2.14p4",
176 "Compressed ImpulseTracker 2.14p4",
179 /* table for porta-to-note command within volume/panning column */
180 static UBYTE portatable
[10]= {0,1,4,8,16,32,64,96,128,255};
182 /*========== Loader code */
188 if(!_mm_read_UBYTES(id
,4,modreader
)) return 0;
189 if(!memcmp(id
,"IMPM",4)) return 1;
195 if(!(mh
=(ITHEADER
*)MikMod_malloc(sizeof(ITHEADER
)))) return 0;
196 if(!(poslookup
=(UBYTE
*)MikMod_malloc(256*sizeof(UBYTE
)))) return 0;
197 if(!(itpat
=(ITNOTE
*)MikMod_malloc(200*64*sizeof(ITNOTE
)))) return 0;
198 if(!(mask
=(UBYTE
*)MikMod_malloc(64*sizeof(UBYTE
)))) return 0;
199 if(!(last
=(ITNOTE
*)MikMod_malloc(64*sizeof(ITNOTE
)))) return 0;
204 void IT_Cleanup(void)
209 MikMod_free(poslookup
);
213 MikMod_free(paraptr
);
214 MikMod_free(origpositions
);
217 /* Because so many IT files have 64 channels as the set number used, but really
218 only use far less (usually from 8 to 24 still), I had to make this function,
219 which determines the number of channels that are actually USED by a pattern.
221 NOTE: You must first seek to the file location of the pattern before calling
226 static int IT_GetNumChannels(UWORD patrows
)
231 if((flag
=_mm_read_UBYTE(modreader
))==EOF
) {
232 _mm_errno
=MMERR_LOADING_PATTERN
;
240 if(flag
& 128) mask
[ch
]=_mm_read_UBYTE(modreader
);
241 if(mask
[ch
]&1) _mm_read_UBYTE(modreader
);
242 if(mask
[ch
]&2) _mm_read_UBYTE(modreader
);
243 if(mask
[ch
]&4) _mm_read_UBYTE(modreader
);
244 if(mask
[ch
]&8) { _mm_read_UBYTE(modreader
);_mm_read_UBYTE(modreader
); }
246 } while(row
<patrows
);
251 static UBYTE
* IT_ConvertTrack(ITNOTE
* tr
,UWORD numrows
)
254 UBYTE note
,ins
,volpan
;
258 for(t
=0;t
<numrows
;t
++) {
259 note
=tr
[t
*of
.numchn
].note
;
260 ins
=tr
[t
*of
.numchn
].ins
;
261 volpan
=tr
[t
*of
.numchn
].volpan
;
265 UniWriteByte(UNI_KEYOFF
);
267 UniPTEffect(0xc,-1); /* note cut command */
274 UniInstrument(ins
-1);
276 UniWriteByte(UNI_KEYOFF
);
277 else if(ins
!=255) { /* crap */
278 _mm_errno
=MMERR_LOADING_PATTERN
;
282 /* process volume / panning column
283 volume / panning effects do NOT all share the same memory address
286 UniVolEffect(VOL_VOLUME
,volpan
);
287 else if(volpan
==65) /* fine volume slide up (65-74) - A0 case */
288 UniVolEffect(VOL_VOLSLIDE
,0);
289 else if(volpan
<=74) { /* fine volume slide up (65-74) - general case */
290 UniVolEffect(VOL_VOLSLIDE
,0x0f+((volpan
-65)<<4));
291 } else if(volpan
==75) /* fine volume slide down (75-84) - B0 case */
292 UniVolEffect(VOL_VOLSLIDE
,0);
293 else if(volpan
<=84) { /* fine volume slide down (75-84) - general case*/
294 UniVolEffect(VOL_VOLSLIDE
,0xf0+(volpan
-75));
295 } else if(volpan
<=94) /* volume slide up (85-94) */
296 UniVolEffect(VOL_VOLSLIDE
,((volpan
-85)<<4));
297 else if(volpan
<=104)/* volume slide down (95-104) */
298 UniVolEffect(VOL_VOLSLIDE
,(volpan
-95));
299 else if(volpan
<=114)/* pitch slide down (105-114) */
300 UniVolEffect(VOL_PITCHSLIDEDN
,(volpan
-105));
301 else if(volpan
<=124)/* pitch slide up (115-124) */
302 UniVolEffect(VOL_PITCHSLIDEUP
,(volpan
-115));
303 else if(volpan
<=127) { /* crap */
304 _mm_errno
=MMERR_LOADING_PATTERN
;
306 } else if(volpan
<=192)
307 UniVolEffect(VOL_PANNING
,((volpan
-128)==64)?255:((volpan
-128)<<2));
308 else if(volpan
<=202)/* portamento to note */
309 UniVolEffect(VOL_PORTAMENTO
,portatable
[volpan
-193]);
310 else if(volpan
<=212)/* vibrato */
311 UniVolEffect(VOL_VIBRATO
,(volpan
-203));
312 else if((volpan
!=239)&&(volpan
!=255)) { /* crap */
313 _mm_errno
=MMERR_LOADING_PATTERN
;
317 S3MIT_ProcessCmd(tr
[t
*of
.numchn
].cmd
,tr
[t
*of
.numchn
].inf
,
318 old_effect
|S3MIT_IT
);
325 static int IT_ReadPattern(UWORD patrows
)
327 int row
=0,flag
,ch
,blah
;
328 ITNOTE
*itt
=itpat
,dummy
,*n
,*l
;
330 memset(itt
,255,200*64*sizeof(ITNOTE
));
333 if((flag
=_mm_read_UBYTE(modreader
))==EOF
) {
334 _mm_errno
= MMERR_LOADING_PATTERN
;
341 ch
=remap
[(flag
-1)&63];
348 if(flag
&128) mask
[ch
]=_mm_read_UBYTE(modreader
);
350 /* convert IT note off to internal note off */
351 if((l
->note
=n
->note
=_mm_read_UBYTE(modreader
))==255)
354 l
->ins
=n
->ins
=_mm_read_UBYTE(modreader
);
356 l
->volpan
=n
->volpan
=_mm_read_UBYTE(modreader
);
358 l
->cmd
=n
->cmd
=_mm_read_UBYTE(modreader
);
359 l
->inf
=n
->inf
=_mm_read_UBYTE(modreader
);
372 } while(row
<patrows
);
374 for(blah
=0;blah
<of
.numchn
;blah
++) {
375 if(!(of
.tracks
[numtrk
++]=IT_ConvertTrack(&itpat
[blah
],patrows
)))
382 static void LoadMidiString(MREADER
* modreader
,CHAR
* dest
)
386 _mm_read_UBYTES(dest
,32,modreader
);
388 /* remove blanks and uppercase all */
390 if(isalnum((int)*last
)) *(cur
++)=toupper((int)*last
);
396 /* Load embedded midi information for resonant filters */
397 static void IT_LoadMidiConfiguration(MREADER
* modreader
)
401 memset(filtermacros
,0,sizeof(filtermacros
));
402 memset(filtersettings
,0,sizeof(filtersettings
));
404 if (modreader
) { /* information is embedded in file */
408 dat
=_mm_read_I_UWORD(modreader
);
409 _mm_fseek(modreader
,8*dat
+0x120,SEEK_CUR
);
411 /* read midi macros */
412 for(i
=0;i
<UF_MAXMACRO
;i
++) {
413 LoadMidiString(modreader
,midiline
);
414 if((!strncmp(midiline
,"F0F00",5))&&
415 ((midiline
[5]=='0')||(midiline
[5]=='1')))
416 filtermacros
[i
]=(midiline
[5]-'0')|0x80;
419 /* read standalone filters */
420 for(i
=0x80;i
<0x100;i
++) {
421 LoadMidiString(modreader
,midiline
);
422 if((!strncmp(midiline
,"F0F00",5))&&
423 ((midiline
[5]=='0')||(midiline
[5]=='1'))) {
424 filtersettings
[i
].filter
=(midiline
[5]-'0')|0x80;
425 dat
=(midiline
[6])?(midiline
[6]-'0'):0;
426 if(midiline
[7])dat
=(dat
<<4)|(midiline
[7]-'0');
427 filtersettings
[i
].inf
=dat
;
430 } else { /* use default information */
431 filtermacros
[0]=FILT_CUT
;
432 for(i
=0x80;i
<0x90;i
++) {
433 filtersettings
[i
].filter
=FILT_RESONANT
;
434 filtersettings
[i
].inf
=(i
&0x7f)<<3;
438 for(i
=0;i
<0x80;i
++) {
439 filtersettings
[i
].filter
=filtermacros
[0];
440 filtersettings
[i
].inf
=i
;
444 int IT_Load(int curious
)
454 /* try to read module header */
455 _mm_read_I_ULONG(modreader
); /* kill the 4 byte header */
456 _mm_read_string(mh
->songname
,26,modreader
);
457 _mm_read_UBYTES(mh
->blank01
,2,modreader
);
458 mh
->ordnum
=_mm_read_I_UWORD(modreader
);
459 mh
->insnum
=_mm_read_I_UWORD(modreader
);
460 mh
->smpnum
=_mm_read_I_UWORD(modreader
);
461 mh
->patnum
=_mm_read_I_UWORD(modreader
);
462 mh
->cwt
=_mm_read_I_UWORD(modreader
);
463 mh
->cmwt
=_mm_read_I_UWORD(modreader
);
464 mh
->flags
=_mm_read_I_UWORD(modreader
);
465 mh
->special
=_mm_read_I_UWORD(modreader
);
466 mh
->globvol
=_mm_read_UBYTE(modreader
);
467 mh
->mixvol
=_mm_read_UBYTE(modreader
);
468 mh
->initspeed
=_mm_read_UBYTE(modreader
);
469 mh
->inittempo
=_mm_read_UBYTE(modreader
);
470 mh
->pansep
=_mm_read_UBYTE(modreader
);
471 mh
->zerobyte
=_mm_read_UBYTE(modreader
);
472 mh
->msglength
=_mm_read_I_UWORD(modreader
);
473 mh
->msgoffset
=_mm_read_I_ULONG(modreader
);
474 _mm_read_UBYTES(mh
->blank02
,4,modreader
);
475 _mm_read_UBYTES(mh
->pantable
,64,modreader
);
476 _mm_read_UBYTES(mh
->voltable
,64,modreader
);
478 if(_mm_eof(modreader
)) {
479 _mm_errno
=MMERR_LOADING_HEADER
;
483 /* set module variables */
484 of
.songname
= DupStr(mh
->songname
,26,0); /* make a cstr of songname */
486 of
.numpat
= mh
->patnum
;
487 of
.numins
= mh
->insnum
;
488 of
.numsmp
= mh
->smpnum
;
489 of
.initspeed
= mh
->initspeed
;
490 of
.inittempo
= mh
->inittempo
;
491 of
.initvolume
= mh
->globvol
;
492 of
.flags
|= UF_BGSLIDES
| UF_ARPMEM
;
493 if (!(mh
->flags
& 1))
494 of
.flags
|= UF_PANNING
;
497 if(mh
->songname
[25]) {
498 of
.numvoices
=1+mh
->songname
[25];
500 fprintf(stderr
,"Embedded IT limitation to %d voices\n",of
.numvoices
);
504 /* set the module type */
505 /* 2.17 : IT 2.14p4 */
506 /* 2.16 : IT 2.14p3 with resonant filters */
507 /* 2.15 : IT 2.14p3 (improved compression) */
508 if((mh
->cwt
<=0x219)&&(mh
->cwt
>=0x217))
509 of
.modtype
=StrDup(IT_Version
[mh
->cmwt
<0x214?4:5]);
510 else if (mh
->cwt
>=0x215)
511 of
.modtype
=StrDup(IT_Version
[mh
->cmwt
<0x214?2:3]);
513 of
.modtype
= StrDup(IT_Version
[mh
->cmwt
<0x214?0:1]);
514 of
.modtype
[mh
->cmwt
<0x214?15:26] = (mh
->cwt
>>8)+'0';
515 of
.modtype
[mh
->cmwt
<0x214?17:28] = ((mh
->cwt
>>4)&0xf)+'0';
516 of
.modtype
[mh
->cmwt
<0x214?18:29] = ((mh
->cwt
)&0xf)+'0';
520 of
.flags
|= UF_XMPERIODS
| UF_LINEAR
;
522 if((mh
->cwt
>=0x106)&&(mh
->flags
&16))
523 old_effect
=S3MIT_OLDSTYLE
;
527 /* set panning positions */
530 mh
->pantable
[t
]&=0x7f;
531 if(mh
->pantable
[t
]<64)
532 of
.panning
[t
]=mh
->pantable
[t
]<<2;
533 else if(mh
->pantable
[t
]==64)
535 else if(mh
->pantable
[t
]==100)
536 of
.panning
[t
]=PAN_SURROUND
;
537 else if(mh
->pantable
[t
]==127)
538 of
.panning
[t
]=PAN_CENTER
;
540 _mm_errno
=MMERR_LOADING_HEADER
;
546 of
.panning
[t
]=PAN_CENTER
;
548 /* set channel volumes */
549 memcpy(of
.chanvol
,mh
->voltable
,64);
551 /* read the order data */
552 if(!AllocPositions(mh
->ordnum
)) return 0;
553 if(!(origpositions
=MikMod_calloc(mh
->ordnum
,sizeof(UWORD
)))) return 0;
555 for(t
=0;t
<mh
->ordnum
;t
++) {
556 origpositions
[t
]=_mm_read_UBYTE(modreader
);
557 if((origpositions
[t
]>mh
->patnum
)&&(origpositions
[t
]<254))
558 origpositions
[t
]=255;
561 if(_mm_eof(modreader
)) {
562 _mm_errno
= MMERR_LOADING_HEADER
;
566 poslookupcnt
=mh
->ordnum
;
567 S3MIT_CreateOrders(curious
);
569 if(!(paraptr
=(ULONG
*)MikMod_malloc((mh
->insnum
+mh
->smpnum
+of
.numpat
)*
570 sizeof(ULONG
)))) return 0;
572 /* read the instrument, sample, and pattern parapointers */
573 _mm_read_I_ULONGS(paraptr
,mh
->insnum
+mh
->smpnum
+of
.numpat
,modreader
);
575 if(_mm_eof(modreader
)) {
576 _mm_errno
= MMERR_LOADING_HEADER
;
580 /* Check for and load midi information for resonant filters */
581 if(mh
->cmwt
>=0x216) {
583 IT_LoadMidiConfiguration(modreader
);
584 if(_mm_eof(modreader
)) {
585 _mm_errno
= MMERR_LOADING_HEADER
;
589 IT_LoadMidiConfiguration(NULL
);
593 /* Check for and load song comment */
594 if((mh
->special
&1)&&(mh
->cwt
>=0x104)&&(mh
->msglength
)) {
595 _mm_fseek(modreader
,(long)(mh
->msgoffset
),SEEK_SET
);
596 if(!ReadComment(mh
->msglength
)) return 0;
599 if(!(mh
->flags
&4)) of
.numins
=of
.numsmp
;
600 if(!AllocSamples()) return 0;
602 if(!AllocLinear()) return 0;
604 /* Load all samples */
606 for(t
=0;t
<mh
->smpnum
;t
++) {
609 /* seek to sample position */
610 _mm_fseek(modreader
,(long)(paraptr
[mh
->insnum
+t
]+4),SEEK_SET
);
612 /* load sample info */
613 _mm_read_string(s
.filename
,12,modreader
);
614 s
.zerobyte
= _mm_read_UBYTE(modreader
);
615 s
.globvol
= _mm_read_UBYTE(modreader
);
616 s
.flag
= _mm_read_UBYTE(modreader
);
617 s
.volume
= _mm_read_UBYTE(modreader
);
618 _mm_read_string(s
.sampname
,26,modreader
);
619 s
.convert
= _mm_read_UBYTE(modreader
);
620 s
.panning
= _mm_read_UBYTE(modreader
);
621 s
.length
= _mm_read_I_ULONG(modreader
);
622 s
.loopbeg
= _mm_read_I_ULONG(modreader
);
623 s
.loopend
= _mm_read_I_ULONG(modreader
);
624 s
.c5spd
= _mm_read_I_ULONG(modreader
);
625 s
.susbegin
= _mm_read_I_ULONG(modreader
);
626 s
.susend
= _mm_read_I_ULONG(modreader
);
627 s
.sampoffset
= _mm_read_I_ULONG(modreader
);
628 s
.vibspeed
= _mm_read_UBYTE(modreader
);
629 s
.vibdepth
= _mm_read_UBYTE(modreader
);
630 s
.vibrate
= _mm_read_UBYTE(modreader
);
631 s
.vibwave
= _mm_read_UBYTE(modreader
);
633 /* Generate an error if c5spd is > 512k, or samplelength > 256 megs
634 (nothing would EVER be that high) */
636 if(_mm_eof(modreader
)||(s
.c5spd
>0x7ffffL
)||(s
.length
>0xfffffffUL
)) {
637 _mm_errno
= MMERR_LOADING_SAMPLEINFO
;
641 /* Reality check for sample loop information */
643 ((s
.loopbeg
>0xfffffffUL
)||(s
.loopend
>0xfffffffUL
))) {
644 _mm_errno
= MMERR_LOADING_SAMPLEINFO
;
648 q
->samplename
= DupStr(s
.sampname
,26,0);
649 q
->speed
= s
.c5spd
/ 2;
650 q
->panning
= ((s
.panning
&127)==64)?255:(s
.panning
&127)<<2;
651 q
->length
= s
.length
;
652 q
->loopstart
= s
.loopbeg
;
653 q
->loopend
= s
.loopend
;
654 q
->volume
= s
.volume
;
655 q
->globvol
= s
.globvol
;
656 q
->seekpos
= s
.sampoffset
;
658 /* Convert speed to XM linear finetune */
659 if(of
.flags
&UF_LINEAR
)
660 q
->speed
=speed_to_finetune(s
.c5spd
,t
);
662 if(s
.panning
&128) q
->flags
|=SF_OWNPAN
;
665 q
->vibflags
|= AV_IT
;
666 q
->vibtype
= s
.vibwave
;
667 q
->vibsweep
= s
.vibrate
* 2;
668 q
->vibdepth
= s
.vibdepth
;
669 q
->vibrate
= s
.vibspeed
;
672 if(s
.flag
&2) q
->flags
|=SF_16BITS
;
673 if((s
.flag
&8)&&(mh
->cwt
>=0x214)) {
674 q
->flags
|=SF_ITPACKED
;
677 if(s
.flag
&16) q
->flags
|=SF_LOOP
;
678 if(s
.flag
&64) q
->flags
|=SF_BIDI
;
681 if(s
.convert
&1) q
->flags
|=SF_SIGNED
;
682 if(s
.convert
&4) q
->flags
|=SF_DELTA
;
687 /* Load instruments if instrument mode flag enabled */
689 if(!AllocInstruments()) return 0;
691 of
.flags
|=UF_NNA
|UF_INST
;
693 for(t
=0;t
<mh
->insnum
;t
++) {
696 /* seek to instrument position */
697 _mm_fseek(modreader
,paraptr
[t
]+4,SEEK_SET
);
699 /* load instrument info */
700 _mm_read_string(ih
.filename
,12,modreader
);
701 ih
.zerobyte
= _mm_read_UBYTE(modreader
);
703 /* load IT 1.xx inst header */
704 ih
.volflg
= _mm_read_UBYTE(modreader
);
705 ih
.volbeg
= _mm_read_UBYTE(modreader
);
706 ih
.volend
= _mm_read_UBYTE(modreader
);
707 ih
.volsusbeg
= _mm_read_UBYTE(modreader
);
708 ih
.volsusend
= _mm_read_UBYTE(modreader
);
709 _mm_read_I_UWORD(modreader
);
710 ih
.fadeout
= _mm_read_I_UWORD(modreader
);
711 ih
.nna
= _mm_read_UBYTE(modreader
);
712 ih
.dnc
= _mm_read_UBYTE(modreader
);
714 /* Read IT200+ header */
715 ih
.nna
= _mm_read_UBYTE(modreader
);
716 ih
.dct
= _mm_read_UBYTE(modreader
);
717 ih
.dca
= _mm_read_UBYTE(modreader
);
718 ih
.fadeout
= _mm_read_I_UWORD(modreader
);
719 ih
.ppsep
= _mm_read_UBYTE(modreader
);
720 ih
.ppcenter
= _mm_read_UBYTE(modreader
);
721 ih
.globvol
= _mm_read_UBYTE(modreader
);
722 ih
.chanpan
= _mm_read_UBYTE(modreader
);
723 ih
.rvolvar
= _mm_read_UBYTE(modreader
);
724 ih
.rpanvar
= _mm_read_UBYTE(modreader
);
727 ih
.trkvers
= _mm_read_I_UWORD(modreader
);
728 ih
.numsmp
= _mm_read_UBYTE(modreader
);
729 _mm_read_UBYTE(modreader
);
730 _mm_read_string(ih
.name
,26,modreader
);
731 _mm_read_UBYTES(ih
.blank01
,6,modreader
);
732 _mm_read_I_UWORDS(ih
.samptable
,ITNOTECNT
,modreader
);
734 /* load IT 1xx volume envelope */
735 _mm_read_UBYTES(ih
.volenv
,200,modreader
);
736 for(lp
=0;lp
<ITENVCNT
;lp
++) {
737 ih
.oldvoltick
[lp
] = _mm_read_UBYTE(modreader
);
738 ih
.volnode
[lp
] = _mm_read_UBYTE(modreader
);
741 /* load IT 2xx volume, pan and pitch envelopes */
742 #if defined __STDC__ || defined _MSC_VER || defined MPW_C
743 #define IT_LoadEnvelope(name,type) \
744 ih. name##flg =_mm_read_UBYTE(modreader); \
745 ih. name##pts =_mm_read_UBYTE(modreader); \
746 ih. name##beg =_mm_read_UBYTE(modreader); \
747 ih. name##end =_mm_read_UBYTE(modreader); \
748 ih. name##susbeg=_mm_read_UBYTE(modreader); \
749 ih. name##susend=_mm_read_UBYTE(modreader); \
750 for(lp=0;lp<ITENVCNT;lp++) { \
751 ih. name##node[lp]=_mm_read_##type (modreader); \
752 ih. name##tick[lp]=_mm_read_I_UWORD(modreader); \
754 _mm_read_UBYTE(modreader)
756 #define IT_LoadEnvelope(name,type) \
757 ih. name/**/flg =_mm_read_UBYTE(modreader); \
758 ih. name/**/pts =_mm_read_UBYTE(modreader); \
759 ih. name/**/beg =_mm_read_UBYTE(modreader); \
760 ih. name/**/end =_mm_read_UBYTE(modreader); \
761 ih. name/**/susbeg=_mm_read_UBYTE(modreader); \
762 ih. name/**/susend=_mm_read_UBYTE(modreader); \
763 for(lp=0;lp<ITENVCNT;lp++) { \
764 ih. name/**/node[lp]=_mm_read_/**/type (modreader); \
765 ih. name/**/tick[lp]=_mm_read_I_UWORD(modreader); \
767 _mm_read_UBYTE(modreader)
770 IT_LoadEnvelope(vol
,UBYTE
);
771 IT_LoadEnvelope(pan
,SBYTE
);
772 IT_LoadEnvelope(pit
,SBYTE
);
773 #undef IT_LoadEnvelope
776 if(_mm_eof(modreader
)) {
777 _mm_errno
= MMERR_LOADING_SAMPLEINFO
;
781 d
->volflg
|=EF_VOLENV
;
782 d
->insname
= DupStr(ih
.name
,26,0);
783 d
->nnatype
= ih
.nna
& NNA_MASK
;
786 d
->volfade
=ih
.fadeout
<< 6;
792 if(ih
.volflg
&1) d
->volflg
|=EF_ON
;
793 if(ih
.volflg
&2) d
->volflg
|=EF_LOOP
;
794 if(ih
.volflg
&4) d
->volflg
|=EF_SUSTAIN
;
796 /* XM conversion of IT envelope Array */
797 d
->volbeg
= ih
.volbeg
;
798 d
->volend
= ih
.volend
;
799 d
->volsusbeg
= ih
.volsusbeg
;
800 d
->volsusend
= ih
.volsusend
;
803 for(u
=0;u
<ITENVCNT
;u
++)
804 if(ih
.oldvoltick
[d
->volpts
]!=0xff) {
805 d
->volenv
[d
->volpts
].val
=(ih
.volnode
[d
->volpts
]<<2);
806 d
->volenv
[d
->volpts
].pos
=ih
.oldvoltick
[d
->volpts
];
812 d
->panning
=((ih
.chanpan
&127)==64)?255:(ih
.chanpan
&127)<<2;
813 if(!(ih
.chanpan
&128)) d
->flags
|=IF_OWNPAN
;
815 if(!(ih
.ppsep
& 128)) {
816 d
->pitpansep
=ih
.ppsep
<<2;
817 d
->pitpancenter
=ih
.ppcenter
;
818 d
->flags
|=IF_PITCHPAN
;
820 d
->globvol
=ih
.globvol
>>1;
821 d
->volfade
=ih
.fadeout
<<5;
826 d
->rvolvar
= ih
.rvolvar
;
827 d
->rpanvar
= ih
.rpanvar
;
830 #if defined __STDC__ || defined _MSC_VER || defined MPW_C
831 #define IT_ProcessEnvelope(name) \
832 if(ih. name##flg&1) d-> name##flg|=EF_ON; \
833 if(ih. name##flg&2) d-> name##flg|=EF_LOOP; \
834 if(ih. name##flg&4) d-> name##flg|=EF_SUSTAIN; \
835 d-> name##pts=ih. name##pts; \
836 d-> name##beg=ih. name##beg; \
837 d-> name##end=ih. name##end; \
838 d-> name##susbeg=ih. name##susbeg; \
839 d-> name##susend=ih. name##susend; \
841 for(u=0;u<ih. name##pts;u++) \
842 d-> name##env[u].pos=ih. name##tick[u]; \
844 if((d-> name##flg&EF_ON)&&(d-> name##pts<2)) \
845 d-> name##flg&=~EF_ON
847 #define IT_ProcessEnvelope(name) \
848 if(ih. name/**/flg&1) d-> name/**/flg|=EF_ON; \
849 if(ih. name/**/flg&2) d-> name/**/flg|=EF_LOOP; \
850 if(ih. name/**/flg&4) d-> name/**/flg|=EF_SUSTAIN; \
851 d-> name/**/pts=ih. name/**/pts; \
852 d-> name/**/beg=ih. name/**/beg; \
853 d-> name/**/end=ih. name/**/end; \
854 d-> name/**/susbeg=ih. name/**/susbeg; \
855 d-> name/**/susend=ih. name/**/susend; \
857 for(u=0;u<ih. name/**/pts;u++) \
858 d-> name/**/env[u].pos=ih. name/**/tick[u]; \
860 if((d-> name/**/flg&EF_ON)&&(d-> name/**/pts<2)) \
861 d-> name/**/flg&=~EF_ON
864 IT_ProcessEnvelope(vol
);
867 if (ih
.volpts
>= ENVPOINTS
)
868 ih
.volpts
= ENVPOINTS
-1;
870 for(u
=0;u
<ih
.volpts
;u
++)
871 d
->volenv
[u
].val
=(ih
.volnode
[u
]<<2);
873 IT_ProcessEnvelope(pan
);
874 for(u
=0;u
<ih
.panpts
;u
++)
876 ih
.pannode
[u
]==32?255:(ih
.pannode
[u
]+32)<<2;
878 IT_ProcessEnvelope(pit
);
879 for(u
=0;u
<ih
.pitpts
;u
++)
880 d
->pitenv
[u
].val
=ih
.pitnode
[u
]+32;
881 #undef IT_ProcessEnvelope
884 /* filter envelopes not supported yet */
886 ih
.pitpts
=ih
.pitbeg
=ih
.pitend
=0;
892 fprintf(stderr
, "\rFilter envelopes not supported yet\n");
899 for(u
=0;u
<ITNOTECNT
;u
++) {
900 d
->samplenote
[u
]=(ih
.samptable
[u
]&255);
902 (ih
.samptable
[u
]>>8)?((ih
.samptable
[u
]>>8)-1):0xffff;
903 if(d
->samplenumber
[u
]>=of
.numsmp
)
904 d
->samplenote
[u
]=255;
905 else if (of
.flags
&UF_LINEAR
) {
906 int note
=(int)d
->samplenote
[u
]+noteindex
[d
->samplenumber
[u
]];
907 d
->samplenote
[u
]=(note
<0)?0:(note
>255?255:note
);
913 } else if(of
.flags
& UF_LINEAR
) {
914 if(!AllocInstruments()) return 0;
918 for(t
=0;t
<mh
->smpnum
;t
++,d
++)
919 for(u
=0;u
<ITNOTECNT
;u
++) {
920 if(d
->samplenumber
[u
]>=of
.numsmp
)
921 d
->samplenote
[u
]=255;
923 int note
=(int)d
->samplenote
[u
]+noteindex
[d
->samplenumber
[u
]];
924 d
->samplenote
[u
]=(note
<0)?0:(note
>255?255:note
);
929 /* Figure out how many channels this song actually uses */
931 memset(remap
,-1,UF_MAXCHAN
*sizeof(UBYTE
));
932 for(t
=0;t
<of
.numpat
;t
++) {
935 /* seek to pattern position */
936 if(paraptr
[mh
->insnum
+mh
->smpnum
+t
]) { /* 0 -> empty 64 row pattern */
937 _mm_fseek(modreader
,((long)paraptr
[mh
->insnum
+mh
->smpnum
+t
]),SEEK_SET
);
938 _mm_read_I_UWORD(modreader
);
939 /* read pattern length (# of rows)
940 Impulse Tracker never creates patterns with less than 32 rows,
941 but some other trackers do, so we only check for more than 256
943 packlen
=_mm_read_I_UWORD(modreader
);
945 _mm_errno
=MMERR_LOADING_PATTERN
;
948 _mm_read_I_ULONG(modreader
);
949 if(IT_GetNumChannels(packlen
)) return 0;
953 /* give each of them a different number */
954 for(t
=0;t
<UF_MAXCHAN
;t
++)
956 remap
[t
]=of
.numchn
++;
958 of
.numtrk
= of
.numpat
*of
.numchn
;
960 if (of
.numvoices
<of
.numchn
) of
.numvoices
=of
.numchn
;
962 if(!AllocPatterns()) return 0;
963 if(!AllocTracks()) return 0;
965 for(t
=0;t
<of
.numpat
;t
++) {
968 /* seek to pattern position */
969 if(!paraptr
[mh
->insnum
+mh
->smpnum
+t
]) { /* 0 -> empty 64 row pattern */
971 for(u
=0;u
<of
.numchn
;u
++) {
975 for(k
=0;k
<64;k
++) UniNewline();
976 of
.tracks
[numtrk
++]=UniDup();
979 _mm_fseek(modreader
,((long)paraptr
[mh
->insnum
+mh
->smpnum
+t
]),SEEK_SET
);
980 packlen
=_mm_read_I_UWORD(modreader
);
981 of
.pattrows
[t
]=_mm_read_I_UWORD(modreader
);
982 _mm_read_I_ULONG(modreader
);
983 if(!IT_ReadPattern(of
.pattrows
[t
])) return 0;
990 CHAR
*IT_LoadTitle(void)
994 _mm_fseek(modreader
,4,SEEK_SET
);
995 if(!_mm_read_UBYTES(s
,26,modreader
)) return NULL
;
997 return(DupStr(s
,26,0));
1000 /*========== Loader information */
1002 MIKMODAPI MLOADER load_it
={
1005 "IT (Impulse Tracker)",