home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 February / PCO_0299.ISO / filesbbs / linux / mikmod-3.000 / mikmod-3 / mikmod-3.1.2 / loaders / load_it.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-07  |  26.3 KB  |  987 lines

  1. /*    MikMod sound library
  2.     (c) 1998 Miodrag Vallat and others - see file AUTHORS for complete list
  3.  
  4.     This library is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU Library General Public License as
  6.     published by the Free Software Foundation; either version 2 of
  7.     the License, or (at your option) any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU Library General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU Library General Public
  15.     License along with this library; if not, write to the Free Software
  16.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19. /*==============================================================================
  20.  
  21.   $Id: load_it.c,v 1.25 1998/12/07 06:00:14 miod Exp $
  22.  
  23.   Impulse tracker (IT) module loader
  24.  
  25. ==============================================================================*/
  26.  
  27. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30.  
  31. #include <ctype.h>
  32. #include <string.h>
  33.  
  34. #include <mikmod_internals.h>
  35.  
  36. /*========== Module structure */
  37.  
  38. /* header */
  39. typedef struct ITHEADER {
  40.     CHAR    songname[26];
  41.     UBYTE    blank01[2];
  42.     UWORD    ordnum;
  43.     UWORD    insnum;
  44.     UWORD    smpnum;
  45.     UWORD    patnum;
  46.     UWORD    cwt;        /* Created with tracker (y.xx = 0x0yxx) */
  47.     UWORD    cmwt;        /* Compatible with tracker ver > than val. */
  48.     UWORD    flags;
  49.     UWORD    special;    /* bit 0 set = song message attached */
  50.     UBYTE    globvol;
  51.     UBYTE    mixvol;        /* mixing volume [ignored] */
  52.     UBYTE    initspeed;
  53.     UBYTE    inittempo;
  54.     UBYTE    pansep;        /* panning separation between channels */
  55.     UBYTE    zerobyte;       
  56.     UWORD    msglength;
  57.     ULONG    msgoffset;
  58.     UBYTE    blank02[4];
  59.     UBYTE    pantable[64];
  60.     UBYTE    voltable[64];
  61. } ITHEADER;
  62.  
  63. /* sample information */
  64. typedef struct ITSAMPLE {
  65.     CHAR    filename[12];
  66.     UBYTE    zerobyte;
  67.     UBYTE    globvol;
  68.     UBYTE    flag;
  69.     UBYTE    volume;
  70.     UBYTE    panning;
  71.     CHAR    sampname[28];
  72.     UWORD    convert;    /* sample conversion flag */
  73.     ULONG    length;
  74.     ULONG    loopbeg;
  75.     ULONG    loopend;
  76.     ULONG    c5spd;
  77.     ULONG    susbegin;
  78.     ULONG    susend;
  79.     ULONG    sampoffset;
  80.     UBYTE    vibspeed;
  81.     UBYTE    vibdepth;
  82.     UBYTE    vibrate;
  83.     UBYTE    vibwave;    /* 0=sine, 1=rampdown, 2=square, 3=random (speed ignored) */
  84.     UBYTE    noteindex;    /* for converting c5spd to finetune */
  85. } ITSAMPLE;
  86.  
  87. /* instrument information */
  88.  
  89. #define ITENVCNT 25
  90. #define ITNOTECNT 120
  91. typedef struct ITINSTHEADER {
  92.     ULONG    size;            /* (dword) Instrument size */
  93.     CHAR    filename[12];    /* (char) Instrument filename */
  94.     UBYTE    zerobyte;        /* (byte) Instrument type (always 0) */
  95.     UBYTE    volflg;
  96.     UBYTE    volpts;   
  97.     UBYTE    volbeg;            /* (byte) Volume loop start (node) */
  98.     UBYTE    volend;            /* (byte) Volume loop end (node) */
  99.     UBYTE    volsusbeg;        /* (byte) Volume sustain begin (node) */
  100.     UBYTE    volsusend;        /* (byte) Volume Sustain end (node) */
  101.     UBYTE    panflg;
  102.     UBYTE    panpts;  
  103.     UBYTE    panbeg;            /* (byte) channel loop start (node) */
  104.     UBYTE    panend;            /* (byte) channel loop end (node) */
  105.     UBYTE    pansusbeg;        /* (byte) cahnnel sustain begin (node) */
  106.     UBYTE    pansusend;        /* (byte) channel Sustain end (node) */
  107.     UBYTE    pitflg;
  108.     UBYTE    pitpts;   
  109.     UBYTE    pitbeg;            /* (byte) pitch loop start (node) */
  110.     UBYTE    pitend;            /* (byte) pitch loop end (node) */
  111.     UBYTE    pitsusbeg;        /* (byte) pitch sustain begin (node) */
  112.     UBYTE    pitsusend;        /* (byte) pitch Sustain end (node) */
  113.     UWORD    blank;
  114.     UBYTE    globvol;
  115.     UBYTE    chanpan;
  116.     UWORD    fadeout;        /* Envelope end / NNA volume fadeout */
  117.     UBYTE    dnc;            /* Duplicate note check */
  118.     UBYTE    dca;            /* Duplicate check action */
  119.     UBYTE    dct;            /* Duplicate check type */
  120.     UBYTE    nna;            /* New Note Action [0,1,2,3] */
  121.     UWORD    trkvers;        /* tracker version used to save [files only] */
  122.     UBYTE    ppsep;            /* Pitch-pan Separation */
  123.     UBYTE    ppcenter;        /* Pitch-pan Center */
  124.     UBYTE    rvolvar;        /* random volume varations */
  125.     UBYTE    rpanvar;        /* random panning varations */
  126.     UWORD    numsmp;            /* Number of samples in instrument [files only] */
  127.     CHAR    name[26];        /* Instrument name */
  128.     UBYTE    blank01[6];
  129.     UWORD    samptable[ITNOTECNT];/* sample for each note [note / samp pairs] */
  130.     UBYTE    volenv[200];         /* volume envelope (IT 1.x stuff) */
  131.     UBYTE    oldvoltick[ITENVCNT];/* volume tick position (IT 1.x stuff) */
  132.     UBYTE    volnode[ITENVCNT];   /* aplitude of volume nodes */
  133.     UWORD    voltick[ITENVCNT];   /* tick value of volume nodes */
  134.     SBYTE    pannode[ITENVCNT];   /* panenv - node points */
  135.     UWORD    pantick[ITENVCNT];   /* tick value of panning nodes */
  136.     SBYTE    pitnode[ITENVCNT];   /* pitchenv - node points */
  137.     UWORD    pittick[ITENVCNT];   /* tick value of pitch nodes */
  138. } ITINSTHEADER;                       
  139.  
  140. /* unpacked note */
  141.  
  142. typedef struct ITNOTE {
  143.     UBYTE note,ins,volpan,cmd,inf;
  144. } ITNOTE;
  145.  
  146. /*========== Loader data */
  147.  
  148. static ULONG *paraptr=NULL;    /* parapointer array (see IT docs) */
  149. static ITHEADER *mh=NULL;
  150. static ITNOTE *itpat=NULL;    /* allocate to space for one full pattern */
  151. static UBYTE *mask=NULL;    /* arrays allocated to 64 elements and used for */
  152. static ITNOTE *last=NULL;    /* uncompressing IT's pattern information */
  153. static int numtrk=0;
  154. static int old_effect;        /* if set, use S3M old-effects stuffs */
  155. static int *noteindex;
  156.    
  157. CHAR* IT_Version[4]={
  158.     "ImpulseTracker  .  ",
  159.     "Compressed ImpulseTracker  .  ",
  160.     "ImpulseTracker 2.14p3",
  161.     "Compressed ImpulseTracker 2.14p3",
  162. };
  163.  
  164. /* table for porta-to-note command within volume/panning column */
  165. static UBYTE portatable[10]= {0,1,4,8,16,32,64,96,128,255};
  166.  
  167. /*========== Loader code */
  168.  
  169. UBYTE* IT_ConvertTrack(ITNOTE* tr,UWORD numrows);
  170.  
  171. BOOL IT_Test(void)
  172. {
  173.     UBYTE id[4];
  174.  
  175.     if(!_mm_read_UBYTES(id,4,modfp)) return 0;
  176.     if(!memcmp(id,"IMPM",4)) return 1;
  177.     return 0;
  178. }
  179.  
  180. BOOL IT_Init(void)
  181. {
  182.     if(!(mh=(ITHEADER*)_mm_malloc(sizeof(ITHEADER)))) return 0;
  183.     if(!(poslookup=(UBYTE*)_mm_malloc(256*sizeof(UBYTE)))) return 0;
  184.     if(!(itpat=(ITNOTE*)_mm_malloc(256*64*sizeof(ITNOTE)))) return 0;
  185.     if(!(mask=(UBYTE*)_mm_malloc(64*sizeof(UBYTE)))) return 0;
  186.     if(!(last=(ITNOTE*)_mm_malloc(64*sizeof(ITNOTE)))) return 0;
  187.  
  188.     return 1;
  189. }
  190.  
  191. void IT_Cleanup(void)
  192. {
  193.     if(mh) free(mh);
  194.     if(poslookup) free(poslookup);
  195.     if(itpat) free(itpat);
  196.     if(mask) free(mask);
  197.     if(last) free(last);
  198.     if(paraptr) free(paraptr);
  199.     if(noteindex) free(noteindex);
  200.  
  201.     mh        = NULL;
  202.     poslookup = NULL;
  203.     itpat     = NULL;
  204.     mask      = NULL;
  205.     last      = NULL;
  206.     paraptr   = NULL;
  207.     noteindex = NULL;
  208. }
  209.  
  210. /* Because so many IT files have 64 channels as the set number used, but really
  211.    only use far less (usually 8 to 12 still), I had to make this function,
  212.    which determines the number of channels that are actually USED by a pattern.
  213.  
  214.    NOTE: You must first seek to the file location of the pattern before calling
  215.          this procedure.
  216.  
  217.    Returns 1 on error
  218. */
  219. BOOL IT_GetNumChannels(UWORD patrows)
  220. {
  221.     int row=0,flag,ch;
  222.  
  223.     do {
  224.         if((flag=_mm_read_UBYTE(modfp))==EOF) {
  225.             _mm_errno = MMERR_LOADING_PATTERN;
  226.             return 1;
  227.         }
  228.         if(!flag)
  229.             row++;
  230.         else {
  231.             ch=(flag-1)&63;
  232.             remap[ch]=0;
  233.             if(flag & 128) mask[ch]=_mm_read_UBYTE(modfp);
  234.             if(mask[ch]&1) _mm_read_UBYTE(modfp);
  235.             if(mask[ch]&2) _mm_read_UBYTE(modfp);
  236.             if(mask[ch]&4) _mm_read_UBYTE(modfp);
  237.             if(mask[ch]&8) { _mm_read_UBYTE(modfp);_mm_read_UBYTE(modfp); }
  238.         }
  239.     } while(row<patrows);
  240.  
  241.     return 0;
  242. }
  243.  
  244. BOOL IT_ReadPattern(UWORD patrows)
  245. {
  246.     int row=0,flag,ch,blah;
  247.     ITNOTE *itt=itpat,dummy,*n,*l;
  248.  
  249.     memset(itt,255,patrows*of.numchn*sizeof(ITNOTE));
  250.  
  251.     do {
  252.         if((flag=_mm_read_UBYTE(modfp))==EOF) {
  253.             _mm_errno = MMERR_LOADING_PATTERN;
  254.             return 0;
  255.         }
  256.         if(!flag) {
  257.             itt=&itt[of.numchn];
  258.             row++;
  259.         } else {
  260.             ch=remap[(flag-1)&63];
  261.             if(ch!=-1) {
  262.                 n=&itt[ch];
  263.                 l=&last[ch];
  264.             } else 
  265.                 n=l=&dummy;
  266.  
  267.             if(flag&128) mask[ch]=_mm_read_UBYTE(modfp);
  268.             if(mask[ch]&1)
  269.                 /* convert IT note off to internal note off */
  270.                 if((l->note=n->note=_mm_read_UBYTE(modfp))==255) 
  271.                     l->note=n->note=253;
  272.             if(mask[ch]&2)
  273.                 l->ins=n->ins=_mm_read_UBYTE(modfp);
  274.             if(mask[ch]&4)
  275.                 l->volpan=n->volpan=_mm_read_UBYTE(modfp);
  276.             if(mask[ch]&8) {
  277.                 l->cmd=n->cmd=_mm_read_UBYTE(modfp);
  278.                 l->inf=n->inf=_mm_read_UBYTE(modfp);
  279.             }
  280.             if(mask[ch]&16)
  281.                 n->note=l->note;
  282.             if(mask[ch]&32)
  283.                 n->ins=l->ins;
  284.             if(mask[ch]&64)
  285.                 n->volpan=l->volpan;
  286.             if(mask[ch]&128) {
  287.                 n->cmd=l->cmd;
  288.                 n->inf=l->inf;
  289.             }
  290.         }
  291.     } while(row<patrows);
  292.  
  293.     for(blah=0;blah<of.numchn;blah++) {
  294.         if(!(of.tracks[numtrk++]=IT_ConvertTrack(&itpat[blah],patrows)))
  295.             return 0;
  296.     }
  297.  
  298.     return 1;
  299. }
  300.  
  301. UBYTE* IT_ConvertTrack(ITNOTE* tr,UWORD numrows)
  302. {
  303.     int t;
  304.     UBYTE note,ins,volpan;
  305.  
  306.     UniReset();
  307.  
  308.     for(t=0;t<numrows;t++) {
  309.         note=tr[t*of.numchn].note;
  310.         ins=tr[t*of.numchn].ins;
  311.         volpan=tr[t*of.numchn].volpan;
  312.  
  313.         if(note!=255) {
  314.             if(note==253)
  315.                 UniWrite(UNI_KEYOFF);
  316.             else if(note==254)
  317.                 UniPTEffect(0xc,-1);    /* note cut command */
  318.             else
  319.                 UniNote(note);
  320.         }
  321.  
  322.         if((ins)&&(ins<100))
  323.             UniInstrument(ins-1);
  324.         else if(ins==253)
  325.             UniWrite(UNI_KEYOFF);
  326.         else if(ins!=255) { /* crap */
  327.             _mm_errno=MMERR_LOADING_PATTERN;
  328.             return NULL;
  329.         }
  330.  
  331.         /* process volume / panning column
  332.            volume / panning effects do NOT all share the same memory address
  333.            yet. */
  334.         if(volpan<=64) 
  335.             UniVolEffect(VOL_VOLUME,volpan);
  336.         else if(volpan<=74)    /* fine volume slide up (65-74) */
  337.             UniVolEffect(VOL_VOLSLIDE,0x0f+((volpan-65)<<4));
  338.         else if(volpan<=84)    /* fine volume slide down (75-84) */
  339.             UniVolEffect(VOL_VOLSLIDE,0xf0+(volpan-75));
  340.         else if(volpan<=94)    /* volume slide up (85-94) */
  341.             UniVolEffect(VOL_VOLSLIDE,((volpan-85)<<4));
  342.         else if(volpan<=104)/* volume slide down (95-104) */
  343.             UniVolEffect(VOL_VOLSLIDE,(volpan-95));
  344.         else if(volpan<=114)/* pitch slide down (105-114) */
  345.             UniVolEffect(VOL_PITCHSLIDEDN,((volpan-105)<<4));
  346.         else if(volpan<=124)/* pitch slide up (115-124) */
  347.             UniVolEffect(VOL_PITCHSLIDEUP,(volpan-115));
  348.         else if(volpan<=127) { /* crap */
  349.             _mm_errno=MMERR_LOADING_PATTERN;
  350.             return NULL;
  351.         } else if(volpan<=192)
  352.             UniVolEffect(VOL_PANNING,((volpan-128)==64)?255:((volpan-128)<<2));
  353.         else if(volpan<=202)/* portamento to note */
  354.             UniVolEffect(VOL_PORTAMENTO,portatable[volpan-193]);
  355.         else if(volpan<=212)/* vibrato */
  356.             UniVolEffect(VOL_VIBRATO,(volpan-203));
  357.         else if(volpan!=255) { /* crap */
  358.             _mm_errno=MMERR_LOADING_PATTERN;
  359.             return NULL;
  360.         }
  361.  
  362.         S3MIT_ProcessCmd(tr[t*of.numchn].cmd,tr[t*of.numchn].inf,old_effect);
  363.  
  364.         UniNewline();
  365.     }
  366.     return UniDup();
  367. }
  368.  
  369. void LoadMidiString(FILE* modfp,CHAR* dest)
  370. {
  371.     CHAR *cur,*last;
  372.  
  373.     _mm_read_UBYTES(dest,32,modfp);
  374.     cur=last=dest;
  375.     /* remove blanks and uppercase all */
  376.     while(*last) {
  377.         if(isalnum(*last)) *(cur++)=toupper(*last);
  378.         last++;
  379.     }
  380.     *cur=0;
  381. }
  382.  
  383. /* Load midi information for resonant filters */
  384. void IT_LoadMidiConfiguration(FILE* modfp)
  385. {
  386.     int i;
  387.  
  388.     memset(filtermacros,0,sizeof(filtermacros));
  389.     memset(filtersettings,0,sizeof(filtersettings));
  390.  
  391.     if (modfp) { /* information is embedded in file */
  392.         UWORD dat;
  393.         CHAR midiline[33];
  394.  
  395.         dat=_mm_read_I_UWORD(modfp);
  396.         _mm_fseek(modfp,8*dat+0x120,SEEK_CUR);
  397.  
  398.         /* read midi macros */
  399.         for(i=0;i<16;i++) {
  400.             LoadMidiString(modfp,midiline);
  401.             if((!strncmp(midiline,"F0F00",5))&&
  402.                ((midiline[5]=='0')||(midiline[5]=='1')))
  403.                     filtermacros[i]=(midiline[5]-'0')|0x80;
  404.         }
  405.  
  406.         /* read standalone filters */
  407.         for(i=0x80;i<0x100;i++) {
  408.             LoadMidiString(modfp,midiline);
  409.             if((!strncmp(midiline,"F0F00",5))&&
  410.                ((midiline[5]=='0')||(midiline[5]=='1'))) {
  411.                 filtersettings[i].filter=(midiline[5]-'0')|0x80;
  412.                 dat=(midiline[6])?(midiline[6]-'0'):0;
  413.                 if(midiline[7])dat=(dat<<4)|(midiline[7]-'0');
  414.                 filtersettings[i].inf=dat;
  415.             }
  416.         }
  417.     } else { /* use default information */
  418.         filtermacros[0]=FILT_CUT;
  419.         for(i=0x80;i<0x90;i++) {
  420.             filtersettings[i].filter=FILT_RESONANT;
  421.             filtersettings[i].inf=(i&0x7f)<<3;
  422.         }
  423.     }
  424.     activemacro=0;
  425.     for(i=0;i<0x80;i++) {
  426.         filtersettings[i].filter=filtermacros[0];
  427.         filtersettings[i].inf=i;
  428.     }
  429. }
  430.  
  431. int cvt_c5spd_to_finetune(ULONG c5spd,int sampnum)
  432. {
  433.     int ctmp=0,tmp,note=1,finetune=0;
  434.  
  435.     c5spd>>=1;
  436.     while((tmp=getfrequency(of.flags,getlinearperiod(note,0)))<c5spd) {
  437.         ctmp=tmp;
  438.         note++;
  439.     }
  440.  
  441.     if(tmp!=c5spd) {
  442.         if((tmp-c5spd)<(c5spd-ctmp))
  443.             while(tmp>c5spd)
  444.                 tmp=getfrequency(of.flags,getlinearperiod(note,--finetune));
  445.         else {
  446.             note--;
  447.             while(ctmp<c5spd)
  448.                 ctmp=getfrequency(of.flags,getlinearperiod(note,++finetune));
  449.         }
  450.     }
  451.  
  452.     noteindex[sampnum]=note-4*OCTAVE;
  453.     return finetune;
  454. }
  455.  
  456. BOOL IT_Load(BOOL curious)
  457. {
  458.     int t,u,lp;
  459.     INSTRUMENT *d;
  460.     SAMPLE *q;
  461.     BOOL compressed=0;
  462.  
  463.     numtrk=0;
  464.     filters=0;
  465.  
  466.     /* try to read module header */
  467.     _mm_read_I_ULONG(modfp);    /* kill the 4 byte header */
  468.     _mm_read_string(mh->songname,26,modfp);
  469.     _mm_read_UBYTES(mh->blank01,2,modfp);
  470.     mh->ordnum      =_mm_read_I_UWORD(modfp);
  471.     mh->insnum      =_mm_read_I_UWORD(modfp);
  472.     mh->smpnum      =_mm_read_I_UWORD(modfp);
  473.     mh->patnum      =_mm_read_I_UWORD(modfp);
  474.     mh->cwt         =_mm_read_I_UWORD(modfp);
  475.     mh->cmwt        =_mm_read_I_UWORD(modfp);
  476.     mh->flags       =_mm_read_I_UWORD(modfp);
  477.     mh->special     =_mm_read_I_UWORD(modfp);
  478.     mh->globvol     =_mm_read_UBYTE(modfp);
  479.     mh->mixvol      =_mm_read_UBYTE(modfp);
  480.     mh->initspeed   =_mm_read_UBYTE(modfp);
  481.     mh->inittempo   =_mm_read_UBYTE(modfp);
  482.     mh->pansep      =_mm_read_UBYTE(modfp);
  483.     mh->zerobyte    =_mm_read_UBYTE(modfp);
  484.     mh->msglength   =_mm_read_I_UWORD(modfp);
  485.     mh->msgoffset   =_mm_read_I_ULONG(modfp);
  486.     _mm_read_UBYTES(mh->blank02,4,modfp);
  487.     _mm_read_UBYTES(mh->pantable,64,modfp);
  488.     _mm_read_UBYTES(mh->voltable,64,modfp);
  489.  
  490.     if(feof(modfp)) {
  491.         _mm_errno = MMERR_LOADING_HEADER;
  492.         return 0;
  493.     }
  494.  
  495.     /* set module variables */
  496.     of.songname    = DupStr(mh->songname,26);    /* make a cstr of songname  */
  497.     of.reppos      = 0;
  498.     of.numpat      = mh->patnum;
  499.     of.numins      = mh->insnum;
  500.     of.numsmp      = mh->smpnum;
  501.     of.initspeed   = mh->initspeed;
  502.     of.inittempo   = mh->inittempo;
  503.     of.initvolume  = mh->globvol;
  504.  
  505.     /* set the module type */
  506.     /* 2.16 : IT 2.14p3 with resonant filters */
  507.     /* 2.15 : IT 2.14p3 (improved compression) */
  508.     if((mh->cwt==0x216)||(mh->cwt==0x215))
  509.         of.modtype=strdup(IT_Version[mh->cmwt<0x214?2:3]);
  510.     else {
  511.         of.modtype     = strdup(IT_Version[mh->cmwt<0x214?0:1]);
  512.         of.modtype[mh->cmwt<0x214?15:26] = (mh->cwt>>8)+'0';
  513.         of.modtype[mh->cmwt<0x214?17:28] = ((mh->cwt>>4)&0xf)+'0';
  514.         of.modtype[mh->cmwt<0x214?18:29] = ((mh->cwt)&0xf)+'0';
  515.     }
  516.  
  517.     old_effect = 0;
  518.     if(mh->flags&8) {
  519.         of.flags|=(UF_XMPERIODS | UF_LINEAR);
  520.         old_effect|=2;
  521.     }
  522.     if((mh->cwt>=0x106)&&(mh->flags&16))
  523.         old_effect|=1;
  524.  
  525.     /* set panning positions */
  526.     for(t=0;t<64;t++) {
  527.         mh->pantable[t]&=0x7f;
  528.         if(mh->pantable[t]<64)
  529.             of.panning[t]=mh->pantable[t]<<2;
  530.         else if(mh->pantable[t]==64)
  531.             of.panning[t]=255;
  532.         else if(mh->pantable[t]==100)
  533.             of.panning[t]=PAN_SURROUND;
  534.         else if(mh->pantable[t]==127)
  535.             of.panning[t]=PAN_CENTER;
  536.         else {
  537.             _mm_errno=MMERR_LOADING_HEADER;
  538.             return 0;
  539.         }
  540.     }
  541.  
  542.     /* set channel volumes */
  543.     memcpy(of.chanvol,mh->voltable,64);
  544.  
  545.     /* read the order data */
  546.     if(!AllocPositions(mh->ordnum)) return 0;
  547.  
  548.     for(t=0;t<mh->ordnum;t++) {
  549.         of.positions[t]=_mm_read_UBYTE(modfp);
  550.         if((of.positions[t]>mh->patnum)&&(of.positions[t]<254))
  551.             of.positions[t]=255;
  552.     }
  553.  
  554.     if(feof(modfp)) {
  555.         _mm_errno = MMERR_LOADING_HEADER;
  556.         return 0;
  557.     }
  558.  
  559.     of.numpos = 0;poslookupcnt=mh->ordnum;
  560.     for(t=0;t<mh->ordnum;t++) {
  561.         of.positions[of.numpos]=of.positions[t];
  562.         poslookup[t]=of.numpos;    /* bug fix for freaky S3Ms / ITs */
  563.         if(of.positions[t]<254) of.numpos++;
  564.         else
  565.           /* end of song special pattern */
  566.           if((of.positions[t]==255)&&(!curious)) break;
  567.     }
  568.  
  569.     if(!(paraptr=(ULONG*)_mm_malloc((mh->insnum+mh->smpnum+of.numpat)*
  570.                                    sizeof(ULONG)))) return 0;
  571.  
  572.     /* read the instrument, sample, and pattern parapointers */
  573.     _mm_read_I_ULONGS(paraptr,mh->insnum+mh->smpnum+of.numpat,modfp);
  574.  
  575.     /* now is a good time to check if the header was too short :) */
  576.     if(feof(modfp)) {
  577.         _mm_errno = MMERR_LOADING_HEADER;
  578.         return 0;
  579.     }
  580.  
  581.     /* Check for and load midi information for resonant filters */
  582.     if(mh->cmwt>=0x216) {
  583.         if(mh->special&8) {
  584.             IT_LoadMidiConfiguration(modfp);
  585.             if(feof(modfp)) {
  586.                 _mm_errno = MMERR_LOADING_HEADER;
  587.                 return 0;
  588.             }
  589.         } else
  590.             IT_LoadMidiConfiguration(NULL);
  591.         filters=1;
  592.     }
  593.  
  594.     /* Check for and load song comment */
  595.     if((mh->special&1)&&(mh->cwt>=0x104)&&(mh->msglength)) {
  596.         _mm_fseek(modfp,(long)(mh->msgoffset),SEEK_SET);
  597.         if(!ReadComment(mh->msglength)) return 0;
  598.     }
  599.  
  600.     if(!(mh->flags&4)) of.numins=of.numsmp;
  601.     if(!AllocSamples()) return 0;
  602.  
  603.     if(!(noteindex=(int*)_mm_malloc(mh->smpnum*sizeof(int)))) return 0;
  604.  
  605.     /* Load all samples */
  606.     q = of.samples;
  607.     for(t=0;t<mh->smpnum;t++) {
  608.         ITSAMPLE s;
  609.  
  610.         /* seek to sample position */
  611.         _mm_fseek(modfp,(long)(paraptr[mh->insnum+t]+4),SEEK_SET);
  612.  
  613.         /* load sample info */
  614.         _mm_read_string(s.filename,12,modfp);
  615.         s.zerobyte    = _mm_read_UBYTE(modfp);
  616.         s.globvol     = _mm_read_UBYTE(modfp);
  617.         s.flag        = _mm_read_UBYTE(modfp);
  618.         s.volume      = _mm_read_UBYTE(modfp);
  619.         _mm_read_string(s.sampname,26,modfp);
  620.         s.convert     = _mm_read_UBYTE(modfp);
  621.         s.panning     = _mm_read_UBYTE(modfp);
  622.         s.length      = _mm_read_I_ULONG(modfp);
  623.         s.loopbeg     = _mm_read_I_ULONG(modfp);
  624.         s.loopend     = _mm_read_I_ULONG(modfp);
  625.         s.c5spd       = _mm_read_I_ULONG(modfp);
  626.         s.susbegin    = _mm_read_I_ULONG(modfp);
  627.         s.susend      = _mm_read_I_ULONG(modfp);
  628.         s.sampoffset  = _mm_read_I_ULONG(modfp);
  629.         s.vibspeed    = _mm_read_UBYTE(modfp);
  630.         s.vibdepth    = _mm_read_UBYTE(modfp);
  631.         s.vibrate     = _mm_read_UBYTE(modfp);
  632.         s.vibwave     = _mm_read_UBYTE(modfp);
  633.  
  634.         /* Generate an error if c5spd is > 512k, or samplelength > 256 megs
  635.            (nothing would EVER be that high) */
  636.  
  637.         if(feof(modfp)||(s.c5spd>0x7ffffL)||(s.length>0xfffffffUL)||
  638.            (s.loopbeg>0xfffffffUL)||(s.loopend>0xfffffffUL)) {
  639.             _mm_errno = MMERR_LOADING_SAMPLEINFO;
  640.             return 0;
  641.         }
  642.  
  643.         q->samplename = DupStr(s.sampname,26);
  644.         q->speed      = s.c5spd / 2;
  645.         q->panning    = ((s.panning&127)==64)?255:(s.panning&127)<<2;
  646.         q->length     = s.length;
  647.         q->loopstart  = s.loopbeg;
  648.         q->loopend    = s.loopend;
  649.         q->volume     = s.volume;
  650.         q->globvol    = s.globvol;
  651.         q->seekpos    = s.sampoffset;
  652.  
  653.         /* Convert speed to XM linear finetune */
  654.         if(of.flags&UF_LINEAR)
  655.             q->speed=cvt_c5spd_to_finetune(s.c5spd,t);
  656.  
  657.         if(s.panning&128) q->flags|=SF_OWNPAN;
  658.  
  659.         if(s.vibrate) {
  660.             q->vibflags |= AV_IT;
  661.             q->vibtype   = s.vibwave;
  662.             q->vibsweep  = s.vibrate * 2;
  663.             q->vibdepth  = s.vibdepth;
  664.             q->vibrate   = s.vibspeed;
  665.         }
  666.  
  667.         if(s.flag&2) q->flags|=SF_16BITS;
  668.         if((s.flag&8)&&(mh->cwt>=0x214)) {
  669.             q->flags|=SF_ITPACKED;
  670.             compressed=1;
  671.         }
  672.         if(s.flag&16) q->flags|=SF_LOOP;
  673.         if(s.flag&64) q->flags|=SF_BIDI;
  674.  
  675.         if(mh->cwt>=0x200) {
  676.             if(s.convert&1) q->flags|=SF_SIGNED;
  677.             if(s.convert&4) q->flags|=SF_DELTA;   
  678.         }
  679.  
  680.         q++;
  681.     }
  682.  
  683.     /* Load instruments if instrument mode flag enabled */
  684.     if(mh->flags&4) {
  685.         if(!AllocInstruments()) return 0;
  686.         d=of.instruments;
  687.         of.flags|=UF_NNA|UF_INST;
  688.  
  689.         for(t=0;t<mh->insnum;t++) {
  690.             ITINSTHEADER ih;
  691.  
  692.             /* seek to instrument position */
  693.             _mm_fseek(modfp,paraptr[t]+4,SEEK_SET);
  694.  
  695.             /* load instrument info */
  696.             _mm_read_string(ih.filename,12,modfp);
  697.             ih.zerobyte  = _mm_read_UBYTE(modfp);
  698.             if(mh->cwt<0x200) {
  699.                 /* load IT 1.xx inst header */
  700.                 ih.volflg    = _mm_read_UBYTE(modfp);
  701.                 ih.volbeg    = _mm_read_UBYTE(modfp);
  702.                 ih.volend    = _mm_read_UBYTE(modfp);
  703.                 ih.volsusbeg = _mm_read_UBYTE(modfp);
  704.                 ih.volsusend = _mm_read_UBYTE(modfp);
  705.                 _mm_read_I_UWORD(modfp);
  706.                 ih.fadeout   = _mm_read_I_UWORD(modfp);
  707.                 ih.nna       = _mm_read_UBYTE(modfp);
  708.                 ih.dnc       = _mm_read_UBYTE(modfp);
  709.             } else {
  710.                 /* Read IT200+ header */
  711.                 ih.nna       = _mm_read_UBYTE(modfp);
  712.                 ih.dct       = _mm_read_UBYTE(modfp);
  713.                 ih.dca       = _mm_read_UBYTE(modfp);
  714.                 ih.fadeout   = _mm_read_I_UWORD(modfp);
  715.                 ih.ppsep     = _mm_read_UBYTE(modfp);
  716.                 ih.ppcenter  = _mm_read_UBYTE(modfp);
  717.                 ih.globvol   = _mm_read_UBYTE(modfp);
  718.                 ih.chanpan   = _mm_read_UBYTE(modfp);
  719.                 ih.rvolvar   = _mm_read_UBYTE(modfp);
  720.                 ih.rpanvar   = _mm_read_UBYTE(modfp);
  721.             }
  722.  
  723.             ih.trkvers   = _mm_read_I_UWORD(modfp);
  724.             ih.numsmp    = _mm_read_UBYTE(modfp);
  725.             _mm_read_UBYTE(modfp);
  726.             _mm_read_string(ih.name,26,modfp);
  727.             _mm_read_UBYTES(ih.blank01,6,modfp);
  728.             _mm_read_I_UWORDS(ih.samptable,ITNOTECNT,modfp);
  729.             if(mh->cwt<0x200) {
  730.                 /* load IT 1xx volume envelope */
  731.                 _mm_read_UBYTES(ih.volenv,200,modfp);
  732.                 for(lp=0;lp<ITENVCNT;lp++) {
  733.                     ih.oldvoltick[lp] = _mm_read_UBYTE(modfp);
  734.                     ih.volnode[lp]    = _mm_read_UBYTE(modfp);
  735.                 } 
  736.             } else {
  737.                     /* load IT 2xx vol & chanpan & pitch envs */
  738.                 ih.volflg    = _mm_read_UBYTE(modfp);
  739.                 ih.volpts    = _mm_read_UBYTE(modfp);
  740.                 ih.volbeg    = _mm_read_UBYTE(modfp);
  741.                 ih.volend    = _mm_read_UBYTE(modfp);
  742.                 ih.volsusbeg = _mm_read_UBYTE(modfp);
  743.                 ih.volsusend = _mm_read_UBYTE(modfp);
  744.                 for(lp=0;lp<ITENVCNT;lp++) {
  745.                     ih.volnode[lp] = _mm_read_UBYTE(modfp);
  746.                     ih.voltick[lp] = _mm_read_I_UWORD(modfp);
  747.                 }
  748.                 _mm_read_UBYTE(modfp);
  749.  
  750.                 ih.panflg    = _mm_read_UBYTE(modfp);
  751.                 ih.panpts    = _mm_read_UBYTE(modfp);
  752.                 ih.panbeg    = _mm_read_UBYTE(modfp);
  753.                 ih.panend    = _mm_read_UBYTE(modfp);
  754.                 ih.pansusbeg = _mm_read_UBYTE(modfp);
  755.                 ih.pansusend = _mm_read_UBYTE(modfp);
  756.                 for(lp=0;lp<ITENVCNT;lp++) {
  757.                     ih.pannode[lp] = _mm_read_SBYTE(modfp);
  758.                     ih.pantick[lp] = _mm_read_I_UWORD(modfp);
  759.                 }
  760.                 _mm_read_UBYTE(modfp);
  761.  
  762.                 ih.pitflg    = _mm_read_UBYTE(modfp);
  763.                 ih.pitpts    = _mm_read_UBYTE(modfp);
  764.                 ih.pitbeg    = _mm_read_UBYTE(modfp);
  765.                 ih.pitend    = _mm_read_UBYTE(modfp);
  766.                 ih.pitsusbeg = _mm_read_UBYTE(modfp);
  767.                 ih.pitsusend = _mm_read_UBYTE(modfp);
  768.                 for(lp=0;lp<ITENVCNT;lp++) {
  769.                     ih.pitnode[lp] = _mm_read_SBYTE(modfp);
  770.                     ih.pittick[lp] = _mm_read_I_UWORD(modfp);
  771.                 }
  772.                 _mm_read_UBYTE(modfp);
  773.             }
  774.  
  775.             if(feof(modfp)) {
  776.                 _mm_errno = MMERR_LOADING_SAMPLEINFO;
  777.                 return 0;
  778.             }
  779.  
  780.             d->volflg|=EF_VOLENV;
  781.             d->insname = DupStr(ih.name,26);
  782.             d->nnatype = ih.nna;
  783.  
  784.             if(mh->cwt<0x200) {
  785.                 d->volfade=ih.fadeout<< 6;
  786.                 if(ih.dnc) {
  787.                     d->dct=DCT_NOTE;
  788.                     d->dca=DCA_CUT;
  789.                 }
  790.  
  791.                 if(ih.volflg&1) d->volflg|=EF_ON;
  792.                 if(ih.volflg&2) d->volflg|=EF_LOOP;
  793.                 if(ih.volflg&4) d->volflg|=EF_SUSTAIN;      
  794.  
  795.                 /* XM conversion of IT envelope Array */
  796.                 d->volbeg    = ih.volbeg;   
  797.                 d->volend    = ih.volend;
  798.                 d->volsusbeg = ih.volsusbeg;
  799.                 d->volsusend = ih.volsusend;
  800.  
  801.                 if(ih.volflg&1) {
  802.                     for(u=0;u<ITENVCNT;u++)
  803.                         if(ih.oldvoltick[d->volpts]!=0xff) {
  804.                             d->volenv[d->volpts].val=(ih.volnode[d->volpts]<<2);
  805.                             d->volenv[d->volpts].pos=ih.oldvoltick[d->volpts];
  806.                             d->volpts++;
  807.                         } else
  808.                             break;
  809.                 }  
  810.             } else {
  811.                 d->panning=((ih.chanpan&127)==64)?255:(ih.chanpan&127)<<2;
  812.                 if(!(ih.chanpan&128)) d->flags|=IF_OWNPAN;
  813.  
  814.                 if(!(ih.ppsep & 128)) {
  815.                     d->pitpansep=ih.ppsep<<2;
  816.                     d->pitpancenter=ih.ppcenter;
  817.                     d->flags|=IF_PITCHPAN;
  818.                 }
  819.                 d->globvol=ih.globvol>>1;
  820.                 d->volfade=ih.fadeout<<5;
  821.                 d->dct    =ih.dct;
  822.                 d->dca    =ih.dca;
  823.  
  824.                 if(mh->cwt>=0x204) {
  825.                     d->rvolvar = ih.rvolvar;
  826.                     d->rpanvar = ih.rpanvar;
  827.                 }
  828.  
  829.                 if(ih.volflg&1) d->volflg|=EF_ON;
  830.                 if(ih.volflg&2) d->volflg|=EF_LOOP;
  831.                 if(ih.volflg&4) d->volflg|=EF_SUSTAIN;
  832.  
  833.                 if(ih.panflg&1) d->panflg|=EF_ON;
  834.                 if(ih.panflg&2) d->panflg|=EF_LOOP;
  835.                 if(ih.panflg&4) d->panflg|=EF_SUSTAIN;      
  836.  
  837.                 if(ih.pitflg&1) d->pitflg|=EF_ON;
  838.                 if(ih.pitflg&2) d->pitflg|=EF_LOOP;
  839.                 if(ih.pitflg&4) d->pitflg|=EF_SUSTAIN;
  840.                 if(ih.pitflg&0x80) {
  841.                     /* filter envelopes not supported yet */
  842.                     d->pitflg&=~EF_ON;
  843. #ifdef MIKMOD_DEBUG
  844.                     fputs("\rFilter envelopes not supported yet\n",stderr);
  845. #endif
  846.                 }
  847.  
  848.                 d->volpts    = ih.volpts;
  849.                 d->volbeg    = ih.volbeg;   
  850.                 d->volend    = ih.volend;
  851.                 d->volsusbeg = ih.volsusbeg;
  852.                 d->volsusend = ih.volsusend;
  853.  
  854.                 for(u=0;u<ih.volpts;u++) {
  855.                     d->volenv[u].val=(ih.volnode[u]<<2);
  856.                     d->volenv[u].pos=ih.voltick[u];
  857.                 }
  858.  
  859.                 d->panpts    = ih.panpts;
  860.                 d->panbeg    = ih.panbeg;
  861.                 d->panend    = ih.panend;
  862.                 d->pansusbeg = ih.pansusbeg;
  863.                 d->pansusend = ih.pansusend;
  864.  
  865.                 for(u=0;u<ih.panpts;u++) {
  866.                     d->panenv[u].val=ih.pannode[u]==32?255:(ih.pannode[u]+32)<<2;
  867.                     d->panenv[u].pos=ih.pantick[u];
  868.                 }
  869.  
  870.                 d->pitpts   =ih.pitpts;
  871.                 d->pitbeg   =ih.pitbeg;
  872.                 d->pitend   =ih.pitend;
  873.                 d->pitsusbeg=ih.pitsusbeg;
  874.                 d->pitsusend=ih.pitsusend;
  875.  
  876.                 for(u=0;u<ih.pitpts;u++) {
  877.                     d->pitenv[u].val=ih.pitnode[u]+32;
  878.                     d->pitenv[u].pos=ih.pittick[u];
  879.                 }
  880.             }
  881.    
  882.             for(u=0;u<ITNOTECNT;u++) {
  883.                 d->samplenote[u]=(ih.samptable[u]&255);
  884.                 d->samplenumber[u]=
  885.                   (ih.samptable[u]>>8)?((ih.samptable[u]>>8)-1):255;
  886.                 if(d->samplenumber[u]>=of.numsmp)
  887.                     d->samplenote[u]=0;
  888.                 else if (of.flags&UF_LINEAR) {
  889.                     int note=(int)d->samplenote[u]+noteindex[d->samplenumber[u]];
  890.                     d->samplenote[u]=(note<0)?0:(note>255?255:note);
  891.                 }
  892.             }
  893.  
  894.             d++;                  
  895.         }
  896.     } else if(of.flags & UF_LINEAR) {
  897.         if(!AllocInstruments()) return 0;
  898.         d=of.instruments;
  899.         of.flags|=UF_INST;
  900.  
  901.         for(t=0;t<mh->smpnum;t++,d++)
  902.             for(u=0;u<ITNOTECNT;u++)
  903.                 d->samplenote[u]+=noteindex[d->samplenumber[u]];
  904.     }
  905.  
  906.     /* Figure out how many channels this blasted song actually uses (what ever
  907.        happened to common courtesy of storing this simple value somewhere in the
  908.        damn module ?) */
  909.  
  910.     of.numchn=0;
  911.     memset(remap,-1,64*sizeof(UBYTE));
  912.     for(t=0;t<of.numpat;t++) {
  913.         UWORD packlen;
  914.  
  915.         /* seek to pattern position */
  916.         if(paraptr[mh->insnum+mh->smpnum+t]) { /* 0 -> empty 64 row pattern */
  917.             _mm_fseek(modfp,((long)paraptr[mh->insnum+mh->smpnum+t]),SEEK_SET);
  918.             _mm_read_I_UWORD(modfp);
  919.             /* read pattern length (# of rows) */
  920.             packlen = _mm_read_I_UWORD(modfp);
  921.             if((packlen<32)||(packlen>200)) {
  922.                 _mm_errno=MMERR_LOADING_PATTERN;
  923.                 return 0;
  924.             }
  925.             _mm_read_I_ULONG(modfp);
  926.             if(IT_GetNumChannels(packlen)) return 0;
  927.         }
  928.     }
  929.  
  930.     /* give each of them a different number */
  931.     for(t=0;t<64;t++) 
  932.         if(!remap[t])
  933.             remap[t]=of.numchn++;
  934.  
  935.     of.numtrk = of.numpat*of.numchn;
  936.  
  937.     if(!AllocPatterns()) return 0;
  938.     if(!AllocTracks()) return 0;
  939.  
  940.     for(t=0;t<of.numpat;t++) {
  941.         UWORD packlen;
  942.  
  943.         /* seek to pattern position */
  944.         if(!paraptr[mh->insnum+mh->smpnum+t]) { /* 0 -> empty 64 row pattern */
  945.             of.pattrows[t]=64;
  946.             for(u=0;u<of.numchn;u++) {
  947.                 int k;
  948.  
  949.                 UniReset();
  950.                 for(k=0;k<64;k++) UniNewline();
  951.                 of.tracks[numtrk++]=UniDup();
  952.             }
  953.         } else {
  954.             _mm_fseek(modfp,((long)paraptr[mh->insnum+mh->smpnum+t]),SEEK_SET);
  955.             packlen=_mm_read_I_UWORD(modfp);
  956.             of.pattrows[t]=_mm_read_I_UWORD(modfp);
  957.             _mm_read_I_ULONG(modfp);
  958.             if(!IT_ReadPattern(of.pattrows[t])) return 0;
  959.         }
  960.     }
  961.  
  962.     return 1;
  963. }
  964.  
  965. CHAR *IT_LoadTitle(void)
  966. {
  967.     CHAR s[26];
  968.  
  969.     _mm_fseek(modfp,4,SEEK_SET);
  970.     if(!fread(s,26,1,modfp)) return NULL;
  971.    
  972.     return(DupStr(s,26));
  973. }
  974.  
  975. /*========== Loader information */
  976.  
  977. MLOADER load_it={
  978.     NULL,
  979.     "IT",
  980.     "IT loader v1.0",
  981.     IT_Init,
  982.     IT_Test,
  983.     IT_Load,
  984.     IT_Cleanup,
  985.     IT_LoadTitle
  986. };
  987.