1 /****************************************************************************
3 * ____ ___ ____ __ ______ ___ ____ ____/ / *
4 * / __ `__ \/ __ `/ |/ / __ `__ \/ __ \/ __ / *
5 * / / / / / / /_/ /> </ / / / / / /_/ / /_/ / *
6 * /_/ /_/ /_/\__,_/_/|_/_/ /_/ /_/\____/\__,_/ *
8 * Copyright (c) 2008, Mukunda Johnson (mukunda@maxmod.org) *
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. *
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 ****************************************************************************/
23 // information from XM.TXT by Mr.H of Triton
34 #include "samplefix.h"
39 #define vstr_xm_samp " %2i ³ %s%s ³ %-22s ³\n"
40 #define vstr_xm_nosamp " -- ³ -- ³ %-22s ³\n"
41 #define vstr_xm_div "ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\n"
42 #define vstr_xm_patt " \x0e %2i "
43 #define vstr_xm_samp_top "ÚÄÄÄÄÄÂÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿\n"
44 #define vstr_xm_samp_header "³INDEX³SAMPLES³ENVELOPE³ NAME ³\n"
45 #define vstr_xm_samp_prefix "³%3i ³"
46 #define vstr_xm_samp_slice "ÃÄÄÄÄÄÅÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´\n"
47 #define vstr_xm_samp_bottom "ÀÄÄÄÄÄÁÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ\n"
49 #define vstr_xm_samp " %2i | %s%s | %-22s |\n"
50 #define vstr_xm_nosamp " -- | -- | %-22s |\n"
51 #define vstr_xm_div "--------------------------------------------\n"
52 #define vstr_xm_patt " Pattern %2i "
53 #define vstr_xm_samp_top ".-----------------------------------------------.\n"
54 #define vstr_xm_samp_header "|INDEX|SAMPLES|ENVELOPE| NAME |\n"
55 #define vstr_xm_samp_prefix "|%3i |"
56 #define vstr_xm_samp_slice "|-----+-------+--------+------------------------|\n"
57 #define vstr_xm_samp_bottom "`-----------------------------------------------'\n"
60 int Get_XM_Frequency( s8 relnote, s8 finetune )
67 freq = middle_c * pow( 2.0, (1.0/12.0)*rn + (1.0/(12.0*128.0))*ft );
71 int Load_XM_Instrument( Instrument* inst, MAS_Module* mas, u8* p_nextsample, bool verbose )
104 memset( inst, 0, sizeof( Instrument ) );
106 inst_headstart = file_tell_read();
107 inst_size = read32();
109 for( x = 0; x < 22; x++ )
110 inst->name[x] = read8(); // instrument name
112 // printf( " Name=\"%s\"\n", inst->name );
113 // if( read8() != 0 )
114 // return ERR_UNKNOWNINST;
115 read8(); // instrument type, SUPPOSED TO ALWAYS BE 0...
119 samp_headsize = read32();
121 for( x = 0; x < 96; x++ )
123 inst->notemap[x+12] = ((read8()+ns+1)*256) | (x+12);
125 for( x=0; x < 12; x++ )
126 inst->notemap[x] =( inst->notemap[12]&0xFF00) | x;
128 inst->notemap[x] =( inst->notemap[12]&0xFF00) | x;
129 for( x = 0; x < 12; x++ )
131 inst->envelope_volume.node_x[x] = read16();
132 inst->envelope_volume.node_y[x] = (u8)read16();
134 for( x = 0; x < 12; x++ )
136 inst->envelope_pan.node_x[x] = read16();
137 inst->envelope_pan.node_y[x] = (u8)read16();
139 inst->global_volume = 128;
140 inst->envelope_volume.node_count = read8();
141 inst->envelope_pan.node_count = read8();
142 inst->envelope_volume.sus_start = inst->envelope_volume.sus_end = read8();
143 inst->envelope_volume.loop_start = read8();
144 inst->envelope_volume.loop_end = read8();
145 inst->envelope_pan.sus_start = inst->envelope_pan.sus_end = read8();
146 inst->envelope_pan.loop_start = read8();
147 inst->envelope_pan.loop_end = read8();
152 inst->env_flags |= 1|8;
154 inst->env_flags |= 2;
157 inst->envelope_volume.sus_start=inst->envelope_volume.sus_end=255;
159 inst->envelope_pan.sus_start=inst->envelope_pan.sus_end=255;
162 inst->envelope_volume.loop_start=inst->envelope_volume.loop_end=255;
164 inst->envelope_pan.loop_start=inst->envelope_pan.loop_end=255;
167 vibsweep=32768/(read8()+1);
170 inst->fadeout = read16()/32; // apply scalar!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
171 file_seek_read( inst_headstart+inst_size, SEEK_SET );
176 printf( " Has Volume Envelope\n" );
178 printf( " Has Panning Envelope\n" );
180 printf( " Contains %i samples...\n", nsamples );
182 printf( " Loading sample\n" );
185 // read sample headers
186 for( x = 0; x < nsamples; x++ )
189 return ERR_TOOMANYSAMPLES;
190 samp_headstart = file_tell_read();
191 samp = &mas->samples[ns+x];
192 // if( verbose && nsamples != 1 )
193 // printf( " Loading sample %i...\n", x+1 );
194 memset( samp, 0, sizeof( Sample ) );
195 samp->msl_index = 0xFFFF;
196 samp->sample_length = read32();
197 samp->loop_start = read32();
198 samp->loop_end = read32()+samp->loop_start;
199 samp->default_volume = read8();
200 samp->global_volume = 64;
202 samp->vibtype = vibtype;
203 samp->vibdepth = vibdepth;
204 samp->vibspeed = vibrate;
205 samp->vibrate = vibsweep;
207 finetune = (s8)read8();
209 samp->default_panning = (read8()>>1) | 128;
210 relnote = (s8)read8();
213 for( y =0; y <22; y ++)
215 samp->name[y] = read8();
217 samp->filename[y] = samp->name[y];
220 samp->frequency = Get_XM_Frequency( relnote, finetune );
222 // samp->bit16 = loopbits & 16 ? true : false;
223 samp->format = loopbits & 16 ? SAMP_FORMAT_U16 : SAMP_FORMAT_U8;
224 if( samp->format & SAMPF_16BIT )
226 samp->sample_length /= 2;
227 samp->loop_start /= 2;
230 samp->loop_type = loopbits & 3;
231 file_seek_read( samp_headstart + samp_headsize, SEEK_SET );
235 printf( " Length........%i\n", samp->sample_length );
236 if( samp->loop_type == 0 )
237 printf( " Loop..........Disabled\n" );
238 else if( samp->loop_type == 1 )
239 printf( " Loop..........Forward %i->%i\n", samp->loop_start, samp->loop_end );
240 else if( samp->loop_type == 2 )
241 printf( " Loop..........BIDI %i->%i\n", samp->loop_start, samp->loop_end );
242 printf( " Volume........%i\n", samp->default_volume );
243 printf( " Panning.......%i\n", samp->default_panning & 127 );
244 printf( " Middle C......%ihz\n", samp->frequency );
245 printf( " 16 bit........%s\n", (samp->format & SAMPF_16BIT) ? "yes (will be converted)" : "no" );
250 for( x = 0; x < nsamples; x++ )
252 samp = &mas->samples[ns+x];
253 if( samp->sample_length == 0 )
257 if( samp->format & SAMPF_16BIT )
259 samp->data = (u16*)malloc( samp->sample_length*2 );
260 for( t = 0; t < samp->sample_length; t++ )
262 sample_old = (s16)((s16)read16() + sample_old);
263 ((u16*)samp->data)[t] = sample_old + 32768;
268 samp->data = (u8*)malloc( samp->sample_length );
269 for( t = 0; t < samp->sample_length; t++ )
271 sample_old = (s8)((s8)read8() + sample_old);
272 ((u8*)samp->data)[t] = sample_old + 128;
277 *p_nextsample = ns+nsamples;
282 printf( vstr_xm_samp, nsamples, (volbits&1)?"V":"-", (panbits&1)?"P":"-", inst->name );
287 file_seek_read( inst_headstart+inst_size, SEEK_SET );
289 printf( vstr_xm_nosamp, inst->name );
295 void CONV_XM_EFFECT( u8* fx, u8* param )
303 case 0: // 0xy arpeggio
310 case 1: // 1xx porta up
316 case 2: // 2xx porta down
322 case 3: // 3xx porta to note
326 case 4: // 4xy vibrato
330 case 5: // 5xy volslide+glissando
334 case 6: // 6xy volslide+vibrato
338 case 7: // 7xy tremolo
342 case 8: // 8xx set panning
346 case 9: // 9xx set offset
350 case 0xA: // Axy volume slide
354 case 0xB: // Bxx position jump
358 case 0xC: // Cxx set volume
359 wfx = 27; // compatibility effect
362 case 0xD: // Dxx pattern break
364 wpm = (wpm&0xF) + (wpm>>4) * 10;
365 /*if( wpm >= 0xF0 ) what the fuck is this
367 wpm = 0xE0 | (wpm&0xF);
369 if( wpm & 0xF == 0xF )
371 wpm = 0x0F | (wpm&0xF0);
375 case 0xE: // Exy extended
376 // if( (wpm & 0xF0) != 0xC0 )
380 case 1: // fine porta up
382 wpm = 0xF0 | (wpm&0xF);
385 case 2: // fine porta down
387 wpm = 0xF0 | (wpm&0xF);
390 case 3: // glissando control
391 case 5: // set finetune
397 case 4: // vibrato control
399 wpm = 0x30 | (wpm&0xF);
402 case 6: // pattern loop
404 wpm = 0xB0 | (wpm&0xF);
407 case 7: // tremolo control
409 wpm = 0x40 | (wpm&0xF);
412 case 8: // set panning
414 wpm = (wpm&0xF) * 16;
417 case 9: // old retrig
419 wpm = 0x20 | (wpm&0xF);
422 case 10: // fine volslide up
424 wpm = 0x00 | (wpm&0xF);
427 case 11: // fine volslide down
429 wpm = 0x10 | (wpm&0xF);
434 wpm = 0xC0 | (wpm&0xF);
437 case 13: // note delay
439 wpm = 0xD0 | (wpm&0xF);
442 case 14: // pattern delay
444 wpm = 0xE0 | (wpm&0xF);
450 case 0: // set filter
457 case 0xF: // Fxx set speed
464 case 16: // Gxx set global volume
469 case 17: // Hxx global volume slide
473 case 18: // Ixx unused
474 case 19: // Jxx unused
475 case 22: // Mxx unused
476 case 23: // Nxx unused
477 case 24: // Oxx unused
478 case 26: // Qxx unused
479 case 28: // Sxx unused
480 case 30: // Uxx unused
481 case 31: // Vxx unused
482 case 32: // Wxx unused
483 case 34: // Yxx unused
484 case 35: // Zxx unused
489 case 20: // Kxx key off
493 case 21: // Lxx set envelope position
497 case 25: // Pxx panning slide
503 case 27: // Rxx retrigger note
507 case 29: // Txx tremor
511 case 33: // Xxx extra fine slide
515 wpm = 0xE0 | (wpm & 0xF);
517 else if( (wpm>>4) == 2 )
520 wpm = 0xE0 | (wpm & 0xF);
533 int Load_XM_Pattern( Pattern* patt, u32 nchannels, bool verbose )
543 headstart = file_tell_read();
547 return ERR_UNKNOWNPATTERN;
549 memset( patt, 0, sizeof( Pattern ) );
551 patt->nrows = read16();
555 printf( "- %i rows, %.2f KB\n", patt->nrows, (float)(clength)/1000 );
558 for( row = 0; row < patt->nrows*MAX_CHANNELS; row++ )
560 patt->data[row].note = 250;
561 patt->data[row].vol = 0;
564 file_seek_read( headstart+headsize, SEEK_SET );
573 for( row = 0; row < patt->nrows; row++ )
575 for( col = 0; col < nchannels; col++ )
577 e = row*MAX_CHANNELS+col;
579 if( b & 128 ) // packed
581 if( b & 1 ) // bit 0 set: Note follows
583 patt->data[e].note = read8(); // (byte) Note (1-96, 1 = C-0)
584 if( patt->data[e].note == 97 )
585 patt->data[e].note = 255;
587 patt->data[e].note += 12-1;
589 if( b & 2 ) // 1 set: Instrument follows
591 patt->data[e].inst = read8(); // (byte) Instrument (1-128)
593 if( b & 4 ) // 2 set: Volume column byte follows
595 patt->data[e].vol = read8(); // (byte) Volume column byte
597 if( b & 8 ) // 3 set: Effect type follows
598 fx = read8(); // (byte) Effect type
601 if( b & 16 ) // 4 set: Guess what!
602 param=read8(); // (byte) Effect parameter
606 if( fx != 0 || param != 0 )
608 CONV_XM_EFFECT( &fx, ¶m ); // convert effect
609 patt->data[e].fx = fx;
610 patt->data[e].param = param;
615 patt->data[e].note = b; // (byte) Note (1-96, 1 = C-0)
616 if( patt->data[e].note == 97 )
617 patt->data[e].note = 255;
619 patt->data[e].note += 12-1;
620 patt->data[e].inst = read8(); // (byte) Instrument (1-128)
621 patt->data[e].vol = read8(); // (byte) Volume column byte (see below)
622 fx = read8(); // (byte) Effect type
623 param=read8(); // (byte) Effect parameter
624 CONV_XM_EFFECT( &fx, ¶m ); // convert effect
625 patt->data[e].fx = fx;
626 patt->data[e].param = param;
633 int Load_XM( MAS_Module* mod, bool verbose )
641 memset( mod, 0, sizeof( MAS_Module ) );
643 mod->old_effects=true;
645 mod->global_volume=64;
648 if( read32() != 'etxE' || read32() != 'dedn' || read32() != 'doM ' || read32() != ':elu' || read8() != ' ' )
649 return ERR_INVALID_MODULE;
650 for( x = 0; x < 20; x++ )
651 mod->title[x] = read8();
654 printf( vstr_xm_div );
655 printf( "Loading XM, \"%s\"\n", mod->title );
657 if( read8() != 0x1a )
658 return ERR_INVALID_MODULE;
659 skip8( 20 ); // tracker name
660 xm_version = read16();
661 xm_headsize = read32();
662 mod->order_count = (u8)read16();
663 mod->restart_pos = (u8)read16();
664 xm_nchannels = read16();
665 mod->patt_count = (u8)read16();
666 mod->inst_count = (u8)read16();
667 mod->freq_mode = read16() & 1 ? true : false; // flags
668 mod->initial_speed = (u8)read16();
669 mod->initial_tempo = (u8)read16();
673 printf( "Version....%i.%i\n", xm_version>>8 & 0xFF, xm_version & 0xFF );
674 printf( "Length.....%i\n", mod->order_count );
675 printf( "Restart....%i\n", mod->restart_pos );
676 printf( "Channels...%i\n", xm_nchannels );
677 printf( "#Patterns..%i\n", mod->patt_count );
678 printf( "#Instr.....%i\n", mod->inst_count );
679 printf( "Freq Mode..%s\n", mod->freq_mode ? "Linear" : "Amiga" );
680 printf( "Speed......%i\n", mod->initial_speed );
681 printf( "Tempo......%i\n", mod->initial_tempo );
684 for( x = 0; x < 32; x++ )
686 mod->channel_volume[x] = 64;
687 mod->channel_panning[x] = 128;
691 printf( vstr_xm_div );
692 printf( "Reading sequence...\n" );
694 for( x = 0; x < 200; x++ ) // read order table
696 if( x < mod->order_count )
697 mod->orders[x] = read8();
701 mod->orders[x] = 255;
705 for( ; x < 256; x++ ) // skip 200->255
707 file_seek_read( 60+xm_headsize, SEEK_SET ); // or maybe 60..
711 printf( vstr_xm_div );
712 printf( "Loading patterns...\n" );
713 printf( vstr_xm_div );
716 mod->patterns = (Pattern*)malloc( mod->patt_count * sizeof( Pattern ) );
717 for( x = 0; x < mod->patt_count; x++ )
720 printf( vstr_xm_patt, x+1 );
721 Load_XM_Pattern( &mod->patterns[x], xm_nchannels, verbose );
724 mod->instruments = (Instrument*)malloc( mod->inst_count * sizeof( Instrument ) );
725 mod->samples = (Sample*)malloc( 256 * sizeof( Sample ) );
731 printf( vstr_xm_div );
732 printf( "Loading instruments...\n" );
733 printf( vstr_xm_samp_top );
734 printf( vstr_xm_samp_header );
735 printf( vstr_xm_samp_slice );
738 for( x = 0; x < mod->inst_count; x++ )
741 // printf( "Reading Instrument %i...\n", x+1 );
743 printf( vstr_xm_samp_prefix, x+1 );
744 Load_XM_Instrument( &mod->instruments[x], mod, &next_sample, verbose );
749 printf( vstr_xm_samp_bottom );
752 mod->samp_count = next_sample;