Manual: Fix part 1 of FS#11874 - touchscreen region in the manual isn't 100% correct.
[kugel-rb.git] / apps / plugins / mikmod / load_it.c
blob6ce3a1ce65690b156fd26d52bbd35fa6cc1313d3
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
18 02111-1307, USA.
21 /*==============================================================================
23 $Id: load_it.c,v 1.4 2010/01/12 03:30:32 realtech Exp $
25 Impulse tracker (IT) module loader
27 ==============================================================================*/
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
37 #include <ctype.h>
38 #include <stdio.h>
39 #ifdef HAVE_MEMORY_H
40 #include <memory.h>
41 #endif
42 #include <string.h>
44 #include "mikmod_internals.h"
46 #ifdef SUNOS
47 extern int fprintf(FILE *, const char *, ...);
48 extern int toupper(int);
49 #endif
51 /*========== Module structure */
53 /* header */
54 typedef struct ITHEADER {
55 CHAR songname[26];
56 UBYTE blank01[2];
57 UWORD ordnum;
58 UWORD insnum;
59 UWORD smpnum;
60 UWORD patnum;
61 UWORD cwt; /* Created with tracker (y.xx = 0x0yxx) */
62 UWORD cmwt; /* Compatible with tracker ver > than val. */
63 UWORD flags;
64 UWORD special; /* bit 0 set = song message attached */
65 UBYTE globvol;
66 UBYTE mixvol; /* mixing volume [ignored] */
67 UBYTE initspeed;
68 UBYTE inittempo;
69 UBYTE pansep; /* panning separation between channels */
70 UBYTE zerobyte;
71 UWORD msglength;
72 ULONG msgoffset;
73 UBYTE blank02[4];
74 UBYTE pantable[64];
75 UBYTE voltable[64];
76 } ITHEADER;
78 /* sample information */
79 typedef struct ITSAMPLE {
80 CHAR filename[12];
81 UBYTE zerobyte;
82 UBYTE globvol;
83 UBYTE flag;
84 UBYTE volume;
85 UBYTE panning;
86 CHAR sampname[28];
87 UWORD convert; /* sample conversion flag */
88 ULONG length;
89 ULONG loopbeg;
90 ULONG loopend;
91 ULONG c5spd;
92 ULONG susbegin;
93 ULONG susend;
94 ULONG sampoffset;
95 UBYTE vibspeed;
96 UBYTE vibdepth;
97 UBYTE vibrate;
98 UBYTE vibwave; /* 0=sine, 1=rampdown, 2=square, 3=random (speed ignored) */
99 } ITSAMPLE;
101 /* instrument information */
103 #define ITENVCNT 25
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) */
109 UBYTE volflg;
110 UBYTE volpts;
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) */
115 UBYTE panflg;
116 UBYTE panpts;
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) */
121 UBYTE pitflg;
122 UBYTE pitpts;
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) */
127 UWORD blank;
128 UBYTE globvol;
129 UBYTE chanpan;
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 */
142 UBYTE blank01[6];
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 */
152 } ITINSTHEADER;
154 /* unpacked note */
156 typedef struct ITNOTE {
157 UBYTE note,ins,volpan,cmd,inf;
158 } ITNOTE;
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 */
167 static int numtrk=0;
168 static unsigned int old_effect; /* if set, use S3M old-effects stuffs */
170 static CHAR* IT_Version[]={
171 "ImpulseTracker . ",
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 */
184 int IT_Test(void)
186 UBYTE id[4];
188 if(!_mm_read_UBYTES(id,4,modreader)) return 0;
189 if(!memcmp(id,"IMPM",4)) return 1;
190 return 0;
193 int IT_Init(void)
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;
201 return 1;
204 void IT_Cleanup(void)
206 FreeLinear();
208 MikMod_free(mh);
209 MikMod_free(poslookup);
210 MikMod_free(itpat);
211 MikMod_free(mask);
212 MikMod_free(last);
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
222 this procedure.
224 Returns 1 on error
226 static int IT_GetNumChannels(UWORD patrows)
228 int row=0,flag,ch;
230 do {
231 if((flag=_mm_read_UBYTE(modreader))==EOF) {
232 _mm_errno=MMERR_LOADING_PATTERN;
233 return 1;
235 if(!flag)
236 row++;
237 else {
238 ch=(flag-1)&63;
239 remap[ch]=0;
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);
248 return 0;
251 static UBYTE* IT_ConvertTrack(ITNOTE* tr,UWORD numrows)
253 int t;
254 UBYTE note,ins,volpan;
256 UniReset();
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;
263 if(note!=255) {
264 if(note==253)
265 UniWriteByte(UNI_KEYOFF);
266 else if(note==254) {
267 UniPTEffect(0xc,-1); /* note cut command */
268 volpan=255;
269 } else
270 UniNote(note);
273 if((ins)&&(ins<100))
274 UniInstrument(ins-1);
275 else if(ins==253)
276 UniWriteByte(UNI_KEYOFF);
277 else if(ins!=255) { /* crap */
278 _mm_errno=MMERR_LOADING_PATTERN;
279 return NULL;
282 /* process volume / panning column
283 volume / panning effects do NOT all share the same memory address
284 yet. */
285 if(volpan<=64)
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;
305 return NULL;
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;
314 return NULL;
317 S3MIT_ProcessCmd(tr[t*of.numchn].cmd,tr[t*of.numchn].inf,
318 old_effect|S3MIT_IT);
320 UniNewline();
322 return UniDup();
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));
332 do {
333 if((flag=_mm_read_UBYTE(modreader))==EOF) {
334 _mm_errno = MMERR_LOADING_PATTERN;
335 return 0;
337 if(!flag) {
338 itt=&itt[of.numchn];
339 row++;
340 } else {
341 ch=remap[(flag-1)&63];
342 if(ch!=-1) {
343 n=&itt[ch];
344 l=&last[ch];
345 } else
346 n=l=&dummy;
348 if(flag&128) mask[ch]=_mm_read_UBYTE(modreader);
349 if(mask[ch]&1)
350 /* convert IT note off to internal note off */
351 if((l->note=n->note=_mm_read_UBYTE(modreader))==255)
352 l->note=n->note=253;
353 if(mask[ch]&2)
354 l->ins=n->ins=_mm_read_UBYTE(modreader);
355 if(mask[ch]&4)
356 l->volpan=n->volpan=_mm_read_UBYTE(modreader);
357 if(mask[ch]&8) {
358 l->cmd=n->cmd=_mm_read_UBYTE(modreader);
359 l->inf=n->inf=_mm_read_UBYTE(modreader);
361 if(mask[ch]&16)
362 n->note=l->note;
363 if(mask[ch]&32)
364 n->ins=l->ins;
365 if(mask[ch]&64)
366 n->volpan=l->volpan;
367 if(mask[ch]&128) {
368 n->cmd=l->cmd;
369 n->inf=l->inf;
372 } while(row<patrows);
374 for(blah=0;blah<of.numchn;blah++) {
375 if(!(of.tracks[numtrk++]=IT_ConvertTrack(&itpat[blah],patrows)))
376 return 0;
379 return 1;
382 static void LoadMidiString(MREADER* modreader,CHAR* dest)
384 CHAR *cur,*last;
386 _mm_read_UBYTES(dest,32,modreader);
387 cur=last=dest;
388 /* remove blanks and uppercase all */
389 while(*last) {
390 if(isalnum((int)*last)) *(cur++)=toupper((int)*last);
391 last++;
393 *cur=0;
396 /* Load embedded midi information for resonant filters */
397 static void IT_LoadMidiConfiguration(MREADER* modreader)
399 int i;
401 memset(filtermacros,0,sizeof(filtermacros));
402 memset(filtersettings,0,sizeof(filtersettings));
404 if (modreader) { /* information is embedded in file */
405 UWORD dat;
406 CHAR midiline[33];
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;
437 activemacro=0;
438 for(i=0;i<0x80;i++) {
439 filtersettings[i].filter=filtermacros[0];
440 filtersettings[i].inf=i;
444 int IT_Load(int curious)
446 int t,u,lp;
447 INSTRUMENT *d;
448 SAMPLE *q;
449 int compressed=0;
451 numtrk=0;
452 filters=0;
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;
480 return 0;
483 /* set module variables */
484 of.songname = DupStr(mh->songname,26,0); /* make a cstr of songname */
485 of.reppos = 0;
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;
495 of.bpmlimit=32;
497 if(mh->songname[25]) {
498 of.numvoices=1+mh->songname[25];
499 #ifdef MIKMOD_DEBUG
500 fprintf(stderr,"Embedded IT limitation to %d voices\n",of.numvoices);
501 #endif
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]);
512 else {
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';
519 if(mh->flags&8)
520 of.flags |= UF_XMPERIODS | UF_LINEAR;
522 if((mh->cwt>=0x106)&&(mh->flags&16))
523 old_effect=S3MIT_OLDSTYLE;
524 else
525 old_effect=0;
527 /* set panning positions */
528 if (mh->flags & 1)
529 for(t=0;t<64;t++) {
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)
534 of.panning[t]=255;
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;
539 else {
540 _mm_errno=MMERR_LOADING_HEADER;
541 return 0;
544 else
545 for(t=0;t<64;t++)
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;
563 return 0;
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;
577 return 0;
580 /* Check for and load midi information for resonant filters */
581 if(mh->cmwt>=0x216) {
582 if(mh->special&8) {
583 IT_LoadMidiConfiguration(modreader);
584 if(_mm_eof(modreader)) {
585 _mm_errno = MMERR_LOADING_HEADER;
586 return 0;
588 } else
589 IT_LoadMidiConfiguration(NULL);
590 filters=1;
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 */
605 q = of.samples;
606 for(t=0;t<mh->smpnum;t++) {
607 ITSAMPLE s;
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;
638 return 0;
641 /* Reality check for sample loop information */
642 if((s.flag&16)&&
643 ((s.loopbeg>0xfffffffUL)||(s.loopend>0xfffffffUL))) {
644 _mm_errno = MMERR_LOADING_SAMPLEINFO;
645 return 0;
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;
664 if(s.vibrate) {
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;
675 compressed=1;
677 if(s.flag&16) q->flags|=SF_LOOP;
678 if(s.flag&64) q->flags|=SF_BIDI;
680 if(mh->cwt>=0x200) {
681 if(s.convert&1) q->flags|=SF_SIGNED;
682 if(s.convert&4) q->flags|=SF_DELTA;
684 q++;
687 /* Load instruments if instrument mode flag enabled */
688 if(mh->flags&4) {
689 if(!AllocInstruments()) return 0;
690 d=of.instruments;
691 of.flags|=UF_NNA|UF_INST;
693 for(t=0;t<mh->insnum;t++) {
694 ITINSTHEADER ih;
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);
702 if(mh->cwt<0x200) {
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);
713 } else {
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);
733 if(mh->cwt<0x200) {
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);
740 } else {
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)
755 #else
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)
768 #endif
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;
778 return 0;
781 d->volflg|=EF_VOLENV;
782 d->insname = DupStr(ih.name,26,0);
783 d->nnatype = ih.nna & NNA_MASK;
785 if(mh->cwt<0x200) {
786 d->volfade=ih.fadeout<< 6;
787 if(ih.dnc) {
788 d->dct=DCT_NOTE;
789 d->dca=DCA_CUT;
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;
802 if(ih.volflg&1) {
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];
807 d->volpts++;
808 } else
809 break;
811 } else {
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;
822 d->dct =ih.dct;
823 d->dca =ih.dca;
825 if(mh->cwt>=0x204) {
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
846 #else
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
862 #endif
864 IT_ProcessEnvelope(vol);
866 // Secunia SA37775
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++)
875 d->panenv[u].val=
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
883 if(ih.pitflg&0x80) {
884 /* filter envelopes not supported yet */
885 d->pitflg&=~EF_ON;
886 ih.pitpts=ih.pitbeg=ih.pitend=0;
887 #ifdef MIKMOD_DEBUG
889 static int warn=0;
891 if(!warn)
892 fprintf(stderr, "\rFilter envelopes not supported yet\n");
893 warn=1;
895 #endif
899 for(u=0;u<ITNOTECNT;u++) {
900 d->samplenote[u]=(ih.samptable[u]&255);
901 d->samplenumber[u]=
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);
911 d++;
913 } else if(of.flags & UF_LINEAR) {
914 if(!AllocInstruments()) return 0;
915 d=of.instruments;
916 of.flags|=UF_INST;
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;
922 else {
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 */
930 of.numchn=0;
931 memset(remap,-1,UF_MAXCHAN*sizeof(UBYTE));
932 for(t=0;t<of.numpat;t++) {
933 UWORD packlen;
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
942 rows */
943 packlen=_mm_read_I_UWORD(modreader);
944 if(packlen>256) {
945 _mm_errno=MMERR_LOADING_PATTERN;
946 return 0;
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++)
955 if(!remap[t])
956 remap[t]=of.numchn++;
958 of.numtrk = of.numpat*of.numchn;
959 if(of.numvoices)
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++) {
966 UWORD packlen;
968 /* seek to pattern position */
969 if(!paraptr[mh->insnum+mh->smpnum+t]) { /* 0 -> empty 64 row pattern */
970 of.pattrows[t]=64;
971 for(u=0;u<of.numchn;u++) {
972 int k;
974 UniReset();
975 for(k=0;k<64;k++) UniNewline();
976 of.tracks[numtrk++]=UniDup();
978 } else {
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;
987 return 1;
990 CHAR *IT_LoadTitle(void)
992 CHAR s[26];
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={
1003 NULL,
1004 "IT",
1005 "IT (Impulse Tracker)",
1006 IT_Init,
1007 IT_Test,
1008 IT_Load,
1009 IT_Cleanup,
1010 IT_LoadTitle
1013 /* ex:set ts=4: */