dropped aas, moved to maxmod
[gbajam21] / tools / mmutil / it.c
1 /****************************************************************************
2  *                                                          __              *
3  *                ____ ___  ____ __  ______ ___  ____  ____/ /              *
4  *               / __ `__ \/ __ `/ |/ / __ `__ \/ __ \/ __  /               *
5  *              / / / / / / /_/ />  </ / / / / / /_/ / /_/ /                *
6  *             /_/ /_/ /_/\__,_/_/|_/_/ /_/ /_/\____/\__,_/                 *
7  *                                                                          *
8  *         Copyright (c) 2008, Mukunda Johnson (mukunda@maxmod.org)         *
9  *                                                                          *
10  * Permission to use, copy, modify, and/or distribute this software for any *
11  * purpose with or without fee is hereby granted, provided that the above   *
12  * copyright notice and this permission notice appear in all copies.        *
13  *                                                                          *
14  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES *
15  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF         *
16  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR  *
17  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES   *
18  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN    *
19  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF  *
20  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.           *
21  ****************************************************************************/
22
23 // information from ITTECH.TXT
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include "defs.h"
28 #include "mas.h"
29 #include "it.h"
30 #include "files.h"
31 #include "simple.h"
32 #include "errors.h"
33 #include "samplefix.h"
34
35 #ifdef SUPER_ASCII
36 #define vstr_it_div "ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\n"
37 #define vstr_it_instr_top   "ÚÄÄÄÄÄÂÄÄÄÄÄÄÂÄÄÄÄÄÂÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿\n"
38 #define vstr_it_instr_head  "³INDEX³VOLUME³ NNA ³ ENV ³            NAME           ³\n"
39 #define vstr_it_instr_slice "ÃÄÄÄÄÄÅÄÄÄÄÄÄÅÄÄÄÄÄÅÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´\n"
40 #define vstr_it_instr           "³%3i  ³ %3i%% ³ %3s ³ %s%s%s ³ %-26s³\n"
41 #define vstr_it_instr_bottom "ÀÄÄÄÄÄÁÄÄÄÄÄÄÁÄÄÄÄÄÁÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ\n"
42
43 #define vstr_it_samp_top    "ÚÄÄÄÄÄÂÄÄÄÄÄÄÂÄÄÄÄÄÄÄÂÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿\n"
44 #define vstr_it_samp_head   "³INDEX³VOLUME³DVOLUME³ LOOP ³  MID-C  ³            NAME           ³\n"
45 #define vstr_it_samp_slice  "ÃÄÄÄÄÄÅÄÄÄÄÄÄÅÄÄÄÄÄÄÄÅÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´\n"
46 #define vstr_it_samp            "³%3i  ³ %3i%% ³ %3i%%  ³ %4s ³%6ihz ³ %-26s³\n"
47 #define vstr_it_samp_bottom "ÀÄÄÄÄÄÁÄÄÄÄÄÄÁÄÄÄÄÄÄÄÁÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ\n"
48
49 #define vstr_it_pattern " \x0e %2i"
50 #else
51 #define vstr_it_div "--------------------------------------------\n"
52 #define vstr_it_instr_top   vstr_it_div
53 #define vstr_it_instr_head  " INDEX VOLUME  NNA   ENV   NAME\n"
54 //#define vstr_it_instr_slice ""
55 #define vstr_it_instr           " %-3i   %3i%%    %3s   %s%s%s   %-26s \n"
56 #define vstr_it_instr_bottom vstr_it_div
57
58 #define vstr_it_samp_top    vstr_it_div
59 #define vstr_it_samp_head   " INDEX VOLUME DVOLUME LOOP   MID-C     NAME            \n"
60 //#define vstr_it_samp_slice  ""
61 #define vstr_it_samp            " %-3i   %3i%%   %3i%%    %4s  %6ihz   %-26s \n"
62 #define vstr_it_samp_bottom vstr_it_div
63
64 #define vstr_it_pattern " * %2i"
65 #endif
66
67 bool Load_IT_Envelope( Instrument_Envelope* env, bool unsign )
68 {
69         // read envelopes
70         u8 a;
71         u8 node_count;
72         int x;
73         
74         bool env_loop=false;
75         bool env_sus=false;
76         bool env_enabled=false;
77         bool env_filter=false;
78
79         memset( env, 0, sizeof( Instrument_Envelope ) );
80
81         a=read8();
82         
83         if( a & 1 )
84                 env_enabled = true;
85         if( !(a & 2) )
86         {
87                 env->loop_start=255;
88                 env->loop_end  =255;
89         }
90         else
91                 env_loop=true;
92         if( !(a & 4) )
93         {
94                 env->sus_start=255;
95                 env->sus_end=255;
96         }
97         else
98                 env_sus=true;
99
100         if( a & 128 )
101         {
102                 unsign=false;
103                 env_filter=true;
104                 env->env_filter=env_filter;
105         }
106         
107         node_count=read8();
108         if( node_count != 0 )
109                 env->env_valid=true;
110         
111         env->node_count = node_count;
112         if( env_loop )
113         {
114                 env->loop_start=read8();
115                 env->loop_end=read8();
116         }
117         else
118         {
119                 skip8( 2 );
120         }
121         if( env_sus )
122         {
123                 env->sus_start=read8();
124                 env->sus_end=read8();
125         }
126         else
127         {
128                 skip8( 2 );
129         }
130         for( x = 0; x < 25; x++ )
131         {
132                 env->node_y[x] = read8();
133                 if( unsign )
134                         env->node_y[x] += 32;
135                 env->node_x[x] = read16();
136                 
137         }
138         read8(); // unused byte
139         env->env_enabled = env_enabled;
140         return env_enabled;
141 }
142
143 int Load_IT_Instrument( Instrument* inst, bool verbose, int index )
144 {
145         u16 a;
146         int x;
147
148         memset( inst, 0, sizeof( Instrument ) );
149
150         skip8( 17 );
151         inst->nna               =read8();
152         inst->dct               =read8();
153         inst->dca               =read8();
154         a=read16(); if( a > 255 ) a = 255;
155         inst->fadeout   =(u8)a;
156         skip8( 2 );
157         inst->global_volume             =read8();
158         a= read8();
159         a = (a&128) | ((a&127)*2 > 127 ? 127 : (a&127)*2);
160         inst->setpan                    =a^128;
161         inst->random_volume             =read8();
162         skip8( 5 );
163         for( x = 0; x < 26; x++ )
164                 inst->name[x] = read8();
165         skip8( 6 );
166         
167         for( x = 0; x < 120; x++ )
168                 inst->notemap[x] = read16();
169         
170         inst->env_flags=0;
171         
172         Load_IT_Envelope( &inst->envelope_volume, false );
173         inst->env_flags |= inst->envelope_volume.env_valid ? 1 : 0;
174         inst->env_flags |= inst->envelope_volume.env_enabled ? 8 : 0;
175
176         Load_IT_Envelope( &inst->envelope_pan, true );
177         inst->env_flags |= inst->envelope_pan.env_enabled ? 2 : 0;
178
179         Load_IT_Envelope( &inst->envelope_pitch, true );
180         inst->env_flags |= inst->envelope_pitch.env_enabled ? 4 : 0;
181
182         if( verbose )
183         {
184                 printf( vstr_it_instr, \
185                                 index+1, \
186                                 (inst->global_volume*100)/128, \
187                                 ((inst->nna==0)?"CUT":((inst->nna==1)?"CON":((inst->nna==2)?"OFF":((inst->nna==3)?"FAD":"???")))), \
188                                 (inst->env_flags&8)?"V":"-", \
189                                 (inst->env_flags&2)?"P":"-", \
190                                 (inst->env_flags&4)?"T":"-", \
191                                 inst->name );
192
193         /*      printf( "%i%%   ", (inst->global_volume*100) / 128 );
194                 switch( inst->nna )
195                 {
196                 case 0:
197                         printf( "%s     ", "CUT" ); break;
198                 case 1:
199                         printf( "%s     ", "OFF" ); break;
200                 case 2:
201                         printf( "%s     ", "CONT" ); break;
202                 case 3:
203                         printf( "%s     ", "FADE" ); break;
204                 }
205                 if( (!(inst->env_flags & 2)) && (!(inst->env_flags & 4)) && (!(inst->env_flags & 8)) )
206                 {
207                         printf( "-      " );
208                 }
209                 else
210                 {
211                         if( inst->env_flags & 8)
212                                 printf( "V" );
213                         if( inst->env_flags & 2)
214                                 printf( "P" );
215                         if( inst->env_flags & 4)
216                                 printf( "S" );
217                         printf( "       " );
218                 }
219                 printf( "%s\n", inst->name );*/
220         }
221
222         skip8( 7 );
223         return 0;
224 }
225
226 void Create_IT_Instrument( Instrument* inst, int sample )
227 {
228         int x;
229
230         memset( inst, 0, sizeof( Instrument ) );
231
232         inst->global_volume             =128;
233         
234         for( x = 0; x < 120; x++ )
235                 inst->notemap[x] = x+sample*256;
236 }
237
238 int Load_IT_Sample( Sample* samp )
239 {
240         bool bit16;
241         bool hasloop;
242         bool pingpong;
243         bool samp_unsigned=false;
244         u8 a;
245         u32 samp_length;
246         u32 loop_start;
247         u32 loop_end;
248         u32 c5spd;
249         u32 data_address;
250         int x;
251         
252         memset( samp, 0, sizeof( Sample ) );
253         samp->msl_index = 0xFFFF;
254
255         if( read32() != 'SPMI' )
256                 return ERR_UNKNOWNSAMPLE;
257         for( x = 0; x < 12; x++ )       // dos filename
258                 samp->filename[x] = read8();
259         if( read8() != 0 )
260                 return ERR_UNKNOWNSAMPLE;
261         samp->global_volume = read8();
262         a = read8();
263         samp->it_compression = a & 8 ? 1 : 0;
264         bit16 = a & 2;
265         hasloop = a & 16;
266         pingpong = a & 64;
267         samp->default_volume = read8();
268         for( x = 0; x < 26; x++ )
269                 samp->name[x] = read8();
270         a=read8();
271         samp->default_panning = read8();
272         samp->default_panning = (((samp->default_panning&127) == 64) ? 127 : (samp->default_panning<<1)) | (samp->default_panning&128);
273         if( !(a & 1) )
274                 samp_unsigned=true;
275         samp_length = read32();
276         loop_start=read32();
277         loop_end=read32();
278         c5spd=read32();
279         
280         samp->frequency                 = c5spd;
281         samp->sample_length             = samp_length;
282         samp->loop_start                = loop_start;
283         samp->loop_end                  = loop_end;
284         
285         skip8( 8 ); // susloop start/end
286         data_address = read32();
287         samp->vibspeed = read8();
288         samp->vibdepth = read8();
289         samp->vibrate = read8();
290         samp->vibtype = read8();
291         samp->datapointer = data_address;
292         if( hasloop )
293         {
294                 if( pingpong )
295                         samp->loop_type = 2;
296                 else
297                         samp->loop_type = 1;
298                 samp->loop_start = loop_start;
299                 samp->loop_end = loop_end;
300         }
301         else
302         {
303                 samp->loop_type = 0;
304         }
305         samp->format = (bit16 ? SAMPF_16BIT : 0) | (samp_unsigned ? 0 : SAMPF_SIGNED );
306         if( samp->sample_length == 0 ) samp->loop_type = 0;
307         return 0;
308 }
309
310 int Load_IT_Sample_CMP( u8 *p_dest_buffer, int samp_len, u16 cmwt, bool bit16 );
311 int Load_IT_SampleData( Sample* samp, u16 cwmt )
312 {
313         u32 x;
314         int a;
315         if( samp->sample_length == 0 ) return 0;
316         if( samp->format & SAMPF_16BIT )
317                 samp->data = (u16*)malloc( (u32)(samp->sample_length)*2 );
318         else
319                 samp->data = (u8*)malloc( (u32)(samp->sample_length) );
320
321         if( !samp->it_compression )
322         {
323         
324                 for( x = 0; x < samp->sample_length; x++ )
325                 {
326                         if( samp->format & SAMPF_16BIT )
327                         {
328                                 if( !(samp->format & SAMPF_SIGNED) )
329                                 {
330                                         a = (unsigned short)read16();
331                                 }
332                                 else
333                                 {
334                                         a = (signed short)read16();
335                                         a += 32768;
336                                 }
337                                 ((u16*)samp->data)[x] = (u16)a;
338                         }
339                         else
340                         {
341                                 if( !(samp->format & SAMPF_SIGNED) )
342                                 {
343                                         a = (unsigned char)read8();
344                                 }
345                                 else
346                                 {
347                                         a = (signed char)read8();
348                                         a += 128;
349                                 }
350                                 ((u8*)samp->data)[x] = (u8)a;
351                         }
352                         
353                 }
354         }
355         else
356         {
357                 Load_IT_Sample_CMP( samp->data, samp->sample_length, cwmt, (bool)(samp->format & SAMPF_16BIT) );
358         }
359         FixSample( samp );
360         return 0;
361 }
362
363 int Empty_IT_Pattern( Pattern *patt ) {
364
365         int x;
366         memset( patt, 0, sizeof( Pattern ) );
367         patt->nrows = 64;
368         for( x = 0; x < patt->nrows*MAX_CHANNELS; x++ ) {
369                 patt->data[x].note = 250; // special clears for vol&note
370                 patt->data[x].vol = 255;
371         }
372         return ERR_NONE;
373 }
374
375 int Load_IT_Pattern( Pattern* patt )
376 {
377         int x;
378         int clength;
379         u8 chanvar;
380         u8 chan;
381         u8 maskvar;
382
383         u8 old_maskvar[MAX_CHANNELS];
384         u8 old_note[MAX_CHANNELS];
385         u8 old_inst[MAX_CHANNELS];
386         u8 old_vol[MAX_CHANNELS];
387         u8 old_fx[MAX_CHANNELS];
388         u8 old_param[MAX_CHANNELS];
389         
390         memset( patt, 0, sizeof( Pattern ) );
391         
392         clength = read16();
393         patt->nrows = read16();
394         skip8(4);
395
396         patt->clength = clength;
397
398         for( x = 0; x < patt->nrows*MAX_CHANNELS; x++ )
399         {
400                 patt->data[x].note = 250; // special clears for vol&note
401                 patt->data[x].vol = 255;
402         }
403         
404         
405         // DECOMPRESS IT PATTERN
406         
407         for( x = 0; x < patt->nrows; x++ )
408         {
409 GetNextChannelMarker:
410                 chanvar = read8();                                      // Read byte into channelvariable.
411                 if( chanvar == 0 )                                      // if(channelvariable = 0) then end of row
412                         continue;                                       
413                 
414                 chan = (chanvar-1) & 63;                        // Channel = (channelvariable-1) & 63
415                 if( chan >= MAX_CHANNELS )
416                         return ERR_MANYCHANNELS;
417                 
418                 if( chanvar & 128 )                                     // if(channelvariable & 128) then read byte into maskvariable
419                         old_maskvar[chan] = read8();
420                 
421                 maskvar = old_maskvar[chan];
422                 
423                 if( maskvar & 1 )                                       // if(maskvariable & 1), then read note. (byte value)
424                 {
425                         old_note[chan] = read8();
426                         patt->data[x*MAX_CHANNELS+chan].note = old_note[chan];
427                 }
428                 
429                 if( maskvar & 2 )                                       // if(maskvariable & 2), then read instrument (byte value)
430                 {
431                         old_inst[chan] = read8();
432                         patt->data[x*MAX_CHANNELS+chan].inst = old_inst[chan];
433                 }
434                 
435                 if( maskvar & 4 )                                       // if(maskvariable & 4), then read volume/panning (byte value)
436                 {
437                         old_vol[chan] = read8();
438                         patt->data[x*MAX_CHANNELS+chan].vol = old_vol[chan];
439                 }
440
441                 if( maskvar & 8 )                                       // if(maskvariable & 8), then read command (byte value) and commandvalue
442                 {
443                         old_fx[chan] = read8();
444                         patt->data[x*MAX_CHANNELS+chan].fx = old_fx[chan];
445                         old_param[chan] = read8();
446                         patt->data[x*MAX_CHANNELS+chan].param = old_param[chan];
447                 }
448
449                 if( maskvar & 16 )                                                                                      // if(maskvariable & 16), then note = lastnote for channel
450                         patt->data[x*MAX_CHANNELS+chan].note = old_note[chan];
451                 if( maskvar & 32 )                                                                                      // if(maskvariable & 32), then instrument = lastinstrument for channel
452                         patt->data[x*MAX_CHANNELS+chan].inst = old_inst[chan];
453                 if( maskvar & 64 )                                                                                      // if(maskvariable & 64), then volume/pan = lastvolume/pan for channel
454                         patt->data[x*MAX_CHANNELS+chan].vol = old_vol[chan];
455                 if( maskvar & 128 )                                                                                     // if(maskvariable & 128), then {
456                 {
457                         patt->data[x*MAX_CHANNELS+chan].fx = old_fx[chan];              // command = lastcommand for channel and
458                         patt->data[x*MAX_CHANNELS+chan].param = old_param[chan];// commandvalue = lastcommandvalue for channel
459                 }
460                 goto GetNextChannelMarker;
461         }
462         
463         return ERR_NONE;
464 }
465
466 int Load_IT( MAS_Module* itm, bool verbose )
467 {
468         u8 b;
469         u16 w;
470         int x;
471         int cc;
472
473         u16 cwt;
474         u16 cmwt;
475         
476         u32* parap_inst;
477         u32* parap_samp;
478         u32* parap_patt;
479
480         bool instr_mode;
481         
482         memset( itm, 0, sizeof( MAS_Module ) );
483
484         if( read32() != 'MPMI' )
485                 return ERR_INVALID_MODULE;
486         for( x = 0; x < 28; x++ )
487                 itm->title[x] = read8();
488         itm->order_count        = (u16)read16();
489         itm->inst_count         = (u8)read16();
490         itm->samp_count         = (u8)read16();
491         itm->patt_count         = (u8)read16();
492         cwt = read16();
493         cmwt = read16(); // upward compatible
494         //skip8( 4 );   // created with tracker / upward compatible
495         w = read16(); // flags
496         itm->stereo = w & 1;
497         itm->inst_mode = instr_mode = w & 4;
498         itm->freq_mode = w & 8;
499         itm->old_effects = w & 16;
500         itm->link_gxx = w & 32;
501         skip8( 2 ); // special
502         itm->global_volume = read8();
503         skip8( 1 ); // mix volume
504         itm->initial_speed = read8();
505         itm->initial_tempo = read8();
506
507         if( verbose )
508         {
509                 printf( vstr_it_div );
510                 printf( "Loading IT, \"%s\"\n", itm->title );
511                 printf( vstr_it_div );
512                 printf( "#Orders......%i\n", itm->order_count );
513                 printf( "#Instr.......%i\n", itm->inst_count );
514                 printf( "#Samples.....%i\n", itm->samp_count );
515                 printf( "#Patterns....%i\n", itm->patt_count );
516                 printf( "Stereo.......%s\n", itm->stereo ? "Yes" : "No" );
517                 printf( "Slides.......%s\n", itm->freq_mode ? "Linear" : "Amiga" );
518                 printf( "Old Effects..%s\n", itm->old_effects ? "Yes" : "No" );
519                 printf( "Global Vol...%i%%\n", (itm->global_volume*100)/128 );
520                 printf( "Speed........%i\n", itm->initial_speed );
521                 printf( "Tempo........%i\n", itm->initial_tempo );
522                 printf( "Instruments..%s\n", instr_mode ? "Yes" : "Will be supplied" );
523                 printf( vstr_it_div );
524         }
525         skip8( 12 ); // SEP, PWD, MSGLENGTH, MESSAGE OFFSET, [RESERVED]
526         for( x = 0; x < 64; x++ )
527         {
528                 b = read8();
529                 if( x < MAX_CHANNELS )
530                         itm->channel_panning[x] = b*4 > 255 ? 255 : b*4;        // map 0->64 to 0->255
531         }
532         for( x = 0; x < 64; x++ )
533         {
534                 b = read8();
535                 if( x < MAX_CHANNELS )
536                         itm->channel_volume[x] = b;
537         }
538         for( x = 0; x < itm->order_count; x++ )
539                 itm->orders[x] = read8();
540         
541         parap_inst = (u32*)malloc( itm->inst_count * sizeof( u32 ) );
542         parap_samp = (u32*)malloc( itm->samp_count * sizeof( u32 ) );
543         parap_patt = (u32*)malloc( itm->patt_count * sizeof( u32 ) );
544         
545         for( x = 0; x < itm->inst_count; x++ )
546                 parap_inst[x] = read32();
547         for( x = 0; x < itm->samp_count; x++ )
548                 parap_samp[x] = read32();
549         for( x = 0; x < itm->patt_count; x++ )
550                 parap_patt[x] = read32();
551         
552         itm->samples = (Sample*)malloc( itm->samp_count * sizeof( Sample ) );
553         itm->patterns = (Pattern*)malloc( itm->patt_count * sizeof( Pattern ) );
554
555         if( instr_mode )
556         {
557                 itm->instruments = (Instrument*)malloc( itm->inst_count * sizeof( Instrument ) );
558                 if( verbose )
559                 {
560                         printf( "Loading Instruments...\n" );
561                         printf( vstr_it_instr_top );
562                         printf( vstr_it_instr_head );
563 #ifdef vstr_it_instr_slice
564                         printf( vstr_it_instr_slice );
565 #endif
566                         //printf( "INDEX        VOLUME  NNA     ENV     NAME\n" );
567                 }
568                 
569                 // read instruments
570                 for( x = 0; x < itm->inst_count; x++ )
571                 {
572                 //      if( verbose )
573                 //              printf( "%i     ", x+1 );
574                         file_seek_read( parap_inst[x], SEEK_SET );
575                         Load_IT_Instrument( &itm->instruments[x], verbose, x );
576                 }
577
578                 if( verbose )
579                 {
580                         printf( vstr_it_instr_bottom );
581                 }
582         }
583
584         if( verbose )
585         {
586                 printf( "Loading Samples...\n" );
587                 printf( vstr_it_samp_top );
588                 printf( vstr_it_samp_head );
589 #ifdef vstr_it_samp_slice
590                 printf( vstr_it_samp_slice );
591 #endif
592                 //printf( "INDEX        VOLUME  DVOLUME LOOP    MID-C   NAME\n" );
593         }
594         
595         // read samples
596         for( x = 0; x < itm->samp_count; x++ )
597         {
598                 file_seek_read( parap_samp[x], SEEK_SET );
599                 Load_IT_Sample( &itm->samples[x] );
600                 if( verbose )
601                 {
602                         printf( vstr_it_samp, x+1, (itm->samples[x].global_volume * 100) / 64, (itm->samples[x].default_volume * 100) / 64, itm->samples[x].loop_type == 0 ? "None" : (itm->samples[x].loop_type == 1 ? "Forw" : "BIDI"), itm->samples[x].frequency, itm->samples[x].name );
603                         //printf( "%i   %i%%    %i%%    %s      %ihz    %s\n", x+1, (itm->samples[x].global_volume*100) / 64, (itm->samples[x].default_volume*100) / 64, itm->samples[x].loop_type == 0 ? "None" : (itm->samples[x].loop_type == 1 ? "Yes" : "BIDI"), itm->samples[x].frequency, itm->samples[x].name );
604                 }
605         }
606
607         if( verbose )
608         {
609                 printf( vstr_it_samp_bottom );
610         }
611
612         if( !instr_mode )
613         {
614                 if( verbose )
615                 {
616                         printf( "Adding Instrument Templates...\n" );
617                         printf( vstr_it_div );
618                 }
619                 itm->inst_count = itm->samp_count;
620                 itm->instruments = (Instrument*)malloc( itm->inst_count * sizeof( Instrument ) );
621                 cc=0;
622                 for( x = 0; x < itm->samp_count; x++ )
623                 {
624                         if( verbose )
625                         {
626                                 printf( " * %2i", x+1 );
627                                 cc++;
628                                 if( cc == 15)
629                                 {
630                                         cc=0;
631                                         printf("\n");
632                                 }
633                         }
634                         Create_IT_Instrument( &itm->instruments[x], x+1 );
635                 }
636                 if( verbose )
637                 {
638                         if( cc != 0 )
639                                 printf( (((x+1)%15)==0)?"":"\n" );
640                         printf( vstr_it_div );
641                 }
642         }
643
644         if(verbose)
645         {
646                 printf( "Reading Patterns...\n" );
647                 printf( vstr_it_div );
648         }
649
650         // read patterns
651         cc=0;
652         for( x = 0; x < itm->patt_count; x++ )
653         {
654                 file_seek_read( parap_patt[x], SEEK_SET );
655                 if( parap_patt[x] != 0 )
656                 {
657                         if( verbose )
658                         {
659                                 printf( vstr_it_pattern, x+1 );
660                                 cc++;
661                                 if( cc == 15 )
662                                 {
663                                         cc=0;
664                                         printf("\n");
665                                 }
666                         }
667                         Load_IT_Pattern( &itm->patterns[x] );
668                         
669                 }
670                 else
671                 {
672                         Empty_IT_Pattern( &itm->patterns[x] );
673                         //memset( &itm->patterns[x], 0, sizeof( Pattern ) );
674                 }
675         }
676         
677         
678         if( verbose )
679         {
680                 if( cc != 0 )
681                         printf( "\n" );
682                 printf( vstr_it_div );
683                 printf( "Loading Sample Data...\n" );
684         }
685         // read sample data
686         for( x = 0; x < itm->samp_count; x++ )
687         {
688                 file_seek_read( itm->samples[x].datapointer, SEEK_SET );
689                 Load_IT_SampleData( &itm->samples[x], cmwt );
690         }
691
692         if( verbose )
693         {
694                 printf( vstr_it_div );
695         }
696
697         free( parap_inst );
698         free( parap_samp );
699         free( parap_patt );
700         
701         return ERR_NONE;
702 }
703
704 /* * NOTICE * NOTICE * NOTICE * NOTICE * NOTICE * NOTICE * NOTICE * NOTICE * NOTICE * NOTICE * NOTICE
705
706  -The following sample decompression code is based on CHIBITRACKER's code (http://chibitracker.berlios.de) which is based on xmp's code.(http://xmp.helllabs.org) which is based in openCP code.
707
708 * NOTICE * NOTICE * NOTICE * NOTICE * NOTICE * NOTICE * NOTICE * NOTICE * NOTICE * NOTICE * NOTICE */
709
710 int Load_IT_CompressedSampleBlock( u8** buffer )
711 {
712         u32 size;
713         u32 x;
714         size = read16();
715         (*buffer) = (u8*)malloc( size+4 );
716         (*buffer)[size+0]=0;
717         (*buffer)[size+1]=0;
718         (*buffer)[size+2]=0;
719         (*buffer)[size+3]=0;
720         for( x = 0; x < size; x++ )
721                 (*buffer)[x] = read8();
722         return ERR_NONE;
723 }
724
725 int Load_IT_Sample_CMP( u8 *p_dest_buffer, int samp_len, u16 cmwt, bool bit16 )
726 {
727         u8*     c_buffer=NULL;
728         u16 block_length;               // length of compressed data block in samples
729         u16 block_position;             // position in block 
730         u8 bit_width;                   // actual "bit width" 
731         u32 aux_value;                  // value read from file to be processed 
732         s16 d1, d2;             // integrator buffers (d2 for it2.15) 
733         s8 d18, d28;
734         s8 v8;                  // sample value 
735         s16 v16;                // sample value 16 bit
736         bool it215; // is this an it215 module?
737         u16 border;
738         u8 tmp_shift;
739         u32 bit_readpos=0;
740         int i;
741         
742         u32 nbits, dsize;
743
744         u8 *dest8_write = (u8 *)p_dest_buffer;
745         u16 *dest16_write = (u16 *)p_dest_buffer;
746
747         nbits = bit16 ? 16 : 8;
748         dsize = bit16 ? 4 : 3;
749         for (i=0;i<samp_len;i++)
750                 p_dest_buffer[i]=128;
751         
752
753
754         it215=(cmwt==0x215);
755
756         // now unpack data till the dest buffer is full 
757
758         while( samp_len )
759         {
760         // read a new block of compressed data and reset variables 
761                 Load_IT_CompressedSampleBlock( &c_buffer );
762                 bit_readpos=0;
763                 if( bit16 )
764                         block_length = (samp_len < 0x4000) ? samp_len : 0x4000;
765                 else
766                         block_length = (samp_len < 0x8000) ? samp_len : 0x8000;
767                 block_position = 0;
768                 bit_width = nbits+1;            // start with width of 9 bits 
769                 d1 = d2 = d18=d28=0;            // reset integrator buffers 
770                 
771
772                 // now uncompress the data block 
773                 while ( block_position < block_length ) {
774
775                         aux_value = readbits( c_buffer, bit_readpos, bit_width );                       // read bits 
776                         bit_readpos+=bit_width;
777                         
778                         if ( bit_width < 7 ) { // method 1 (1-6 bits) 
779
780                                 if( bit16 )
781                                 {
782                                         if ( (signed)aux_value == (1 << (bit_width - 1)) ) { // check for "100..." 
783
784                                                 aux_value = readbits( c_buffer, bit_readpos, dsize )+1; //read_n_bits_from_IT_compressed_block(3) + 1; // yes -> read new width; 
785                                                 bit_readpos += dsize;
786                                         bit_width = (aux_value < bit_width) ? aux_value : aux_value + 1;
787                                                                 // and expand it 
788                                                 continue; // ... next value 
789                                         }
790                                 }
791                                 else
792                                 {
793                                         if ( aux_value == ((u32)1 << ((u32)bit_width - 1)) ) { // check for "100..." 
794
795                                                 aux_value = readbits( c_buffer, bit_readpos, dsize )+1; //read_n_bits_from_IT_compressed_block(3) + 1; // yes -> read new width; 
796                                                 bit_readpos += dsize;
797                                         bit_width = (aux_value < bit_width) ? aux_value : aux_value + 1;
798                                                                 // and expand it 
799                                                 continue; // ... next value 
800                                         }
801                                 }
802
803                         } else if ( bit_width < nbits + 1 ) { // method 2 (7-8 bits) 
804
805                                 if( bit16 )
806                                 {
807                                         border = (0xFFFF >> ((nbits+1) - bit_width)) - (nbits/2);
808                                                                 // lower border for width chg 
809
810                                         if ( (int)aux_value > (int)border && (int)aux_value <= ((int)border + nbits) ) {
811
812                                                 aux_value -= border; // convert width to 1-8 
813                                                 bit_width = (aux_value < bit_width) ? aux_value : aux_value + 1;
814                                                                 // and expand it 
815                                                 continue; // ... next value 
816                                         }       
817                                 }
818                                 else
819                                 {
820                                         border = (0xFF >> ((nbits+1) - bit_width)) - (nbits/2);
821                                                                 // lower border for width chg 
822
823                                         if ( aux_value > border && aux_value <= (border + nbits) ) {
824
825                                                 aux_value -= border; // convert width to 1-8 
826                                                 bit_width = (aux_value < bit_width) ? aux_value : aux_value + 1;
827                                                                 // and expand it 
828                                                 continue; // ... next value 
829                                         }       
830                                 }
831
832                         } else if ( bit_width == nbits+1 ) { // method 3 (9 bits) 
833
834                                 if ( aux_value & (1<<nbits) ) {                 // bit 8 set? 
835
836                                         bit_width = (aux_value + 1) & 0xff;             // new width... 
837                                         continue;                               // ... and next value 
838                                 }
839
840                         } else { // illegal width, abort 
841
842                                 if( c_buffer ) {
843                                         free( c_buffer ); c_buffer=NULL; }
844                                 return ERR_UNKNOWNSAMPLE;
845                         }
846
847                         // now expand value to signed byte 
848                         if ( bit_width < nbits ) {
849
850                                 tmp_shift = nbits - bit_width;
851                                 if( bit16 )
852                                 {
853                                         v16=(aux_value << tmp_shift);
854                                         v16>>=tmp_shift;
855                                 }
856                                 else
857                                 {
858                                         v8=(aux_value << tmp_shift);
859                                         v8>>=tmp_shift;
860                                 }
861
862                         }
863                         else
864                         {
865                                 if( bit16 )
866                                         v16 = (s16) aux_value;
867                                 else
868                                         v8 = (s8) aux_value;
869                         }
870                         
871                         if( bit16 )
872                         {
873                                 // integrate upon the sample values 
874                                 d1 += v16;
875                         d2 += d1;
876
877                                 // ... and store it into the buffer
878                                 *(dest16_write++) = (it215 ? d2+32768 : d1+32768);
879                         }
880                         else
881                         {
882                                 // integrate upon the sample values 
883                                 d18 += v8;
884                         d28 += d18;
885                                 
886                                 // ... and store it into the buffer
887                                 *(dest8_write)++ = (it215 ? (int)d28+128 : (int)d18+128);
888                         }
889                                 block_position++;
890
891                 }
892
893                 // now subtract block lenght from total length and go on 
894                 if( c_buffer ) {
895                         free( c_buffer ); c_buffer=NULL; }
896                 samp_len -= block_length;
897         }
898         
899         return ERR_NONE;
900 }