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 ST3's TECH.DOC
33 #include "samplefix.h"
35 #define S3M_NOTE(a) (((a&15)+(a>>4)*12)+12)
38 #define vstr_s3m_samp " %5i ³ %-4s³ %3i%% ³%5ihz³ %-28s³\n"
39 #define vstr_s3m_sampe " ----- ³ --- ³ ---- ³ ----- ³ %-28s³\n"
40 #define vstr_s3m_div "ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\n"
41 #define vstr_s3m_sampt_top "ÚÄÄÄÄÄÂÄÄÄÄÄÄÄÂÄÄÄÄÄÂÄÄÄÄÄÄÂÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿\n"
42 #define vstr_s3m_sampt_mid "³INDEX³LENGTH ³LOOP ³VOLUME³ MID-C ³ NAME ³\n"
43 #define vstr_s3m_sampt_slice "ÃÄÄÄÄÄÅÄÄÄÄÄÄÄÅÄÄÄÄÄÅÄÄÄÄÄÄÅÄÄÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´\n"
44 #define vstr_s3m_sampt_index "³ %2i ³"
45 #define vstr_s3m_sampt_bottom "ÀÄÄÄÄÄÁÄÄÄÄÄÄÄÁÄÄÄÄÄÁÄÄÄÄÄÄÁÄÄÄÄÄÄÄÁÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ\n"
46 #define vstr_s3m_pattern " \x0e %2i%s"
48 #define vstr_s3m_samp "%-5i %-3s %3i%% %5ihz %-28s \n"
49 #define vstr_s3m_sampe "----- --- ---- ------- %-28s\n"
50 #define vstr_s3m_div "--------------------------------------------\n"
51 #define vstr_s3m_sampt_top vstr_s3m_div
52 #define vstr_s3m_sampt_mid " INDEX LENGTH LOOP VOLUME MID-C NAME\n"
53 //#define vstr_s3m_sampt_slice ""
54 #define vstr_s3m_sampt_index " %-2i "
55 #define vstr_s3m_sampt_bottom vstr_s3m_div
56 #define vstr_s3m_pattern " * %2i%s"
59 int Load_S3M_SampleData( Sample* samp, u8 ffi )
63 if( samp->sample_length == 0 )
65 if( samp->format & SAMPF_16BIT )
66 samp->data = (u16*)malloc( samp->sample_length*2 );
68 samp->data = (u8*)malloc( samp->sample_length );
71 // signed samples [VERY OLD]
72 for( x = 0; x < samp->sample_length; x++ )
74 if( samp->format & SAMPF_16BIT )
78 ((u16*)samp->data)[x] = (u16)a;
84 ((u8*)samp->data)[x] = (u8)a;
91 for( x = 0; x < samp->sample_length; x++ )
93 if( samp->format & SAMPF_16BIT )
96 ((u16*)samp->data)[x] = (u16)a;
101 ((u8*)samp->data)[x] = (u8)a;
107 return ERR_UNKNOWNSAMPLE;
113 int Load_S3M_Sample( Sample* samp, bool verbose )
117 memset( samp, 0, sizeof( Sample ) );
118 samp->msl_index = 0xFFFF;
119 if( read8() == 1 ) // type, 1 = sample
121 for( x = 0; x < 12; x++ )
122 samp->filename[x] = read8();
123 samp->datapointer = (read8()*65536+read16())*16;//read24();
124 samp->sample_length = read32();
125 samp->loop_start = read32();
126 samp->loop_end = read32();
127 samp->default_volume = read8();
128 samp->global_volume = 64;
130 if( read8() != 0 ) // packing, 0 = unpacked
131 return ERR_UNKNOWNSAMPLE;
133 samp->loop_type = flags&1 ? 1 : 0;
135 return ERR_UNKNOWNSAMPLE;
136 //samp->bit16 = flags&4 ? true : false;
137 samp->format = flags&4 ? SAMP_FORMAT_U16 : SAMP_FORMAT_U8;
138 samp->frequency = read32();
139 read32(); // reserved
140 skip8( 8 ); // internal variables
141 for( x =0 ; x < 28; x++ )
142 samp->name[x] = read8();
143 if( read32() != 'SRCS' )
144 return ERR_UNKNOWNSAMPLE;
148 // printf( "ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ\n" );
149 // printf( "Loading Samples...\n" );
150 // printf( "ÚÄÄÄÄÄÂÄÄÄÄÄÄÂÄÄÄÄÂÄÄÄÄÄÄÂÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿\n" );
151 // printf( "³LENGTH³LOOP³VOLUME³ MID-C ³ NAME ³\n");
152 // printf( "ÅÄÄÄÄÄÄÅÄÄÄÄÅÄÄÄÄÄÄÅÄÄÄÄÄÅÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ´\n" );
153 printf( vstr_s3m_samp, samp->sample_length, samp->loop_type ? "Yes" : "No", (samp->default_volume*100) / 64, samp->frequency, samp->name );
154 /*printf( " Name......%s\n", samp->name );
155 printf( " Length....%i\n", samp->sample_length );
156 if( samp->loop_type )
157 printf( " Loop......%i->%i\n", samp->loop_start, samp->loop_end );
159 printf( " Loop......Disabled\n" );
160 printf( " Volume....%i\n", samp->default_volume );
161 printf( " Middle C..%ihz\n", samp->frequency );
163 printf( " 16 bit....yes\n" );*/
170 printf( vstr_s3m_sampe, samp->name );
176 int Load_S3M_Pattern( Pattern* patt )
186 memset( patt, 0, sizeof( Pattern ) );
188 patt->clength = clength;
191 for( row = 0; row < 64*MAX_CHANNELS; row++ )
193 patt->data[row].note = 250;
194 patt->data[row].vol = 255;
197 for( row = 0; row < 64; row++ )
199 while( (what = read8()) != 0 ) // BYTE:what / 0=end of row
201 col = what & 31; // &31=channel
203 z = row*MAX_CHANNELS+col;
205 if( what & 32 ) // &32=follows; BYTE:note, BYTE:instrument
207 patt->data[z].note = read8();
208 if( patt->data[z].note == 255 )
209 patt->data[z].note = 250;
210 else if( patt->data[z].note == 254 )
211 patt->data[z].note = 254;
213 patt->data[z].note = S3M_NOTE( patt->data[z].note );
214 patt->data[z].inst = read8();
217 if( what & 64 ) // &64=follows; BYTE:volume
219 patt->data[z].vol = read8();
222 if( what & 128 ) // &128=follows; BYTE:command, BYTE:info
224 patt->data[z].fx = read8();
225 patt->data[z].param = read8();
226 if( patt->data[z].fx == 3 ) // convert pattern break to hexadecimal
228 patt->data[z].param = (patt->data[z].param&0xF) + (patt->data[z].param/16)*10;
230 if( patt->data[z].fx == 'X'-64 )
232 patt->data[z].param *= 2; // multiply volume scale by 2
234 if( patt->data[z].fx == 'V'-64 )
236 patt->data[z].param *= 2; // multiply volume scale by 2
239 if( patt->data[z].fx == 255 )
241 patt->data[z].fx = 0;
242 patt->data[z].param = 0;
249 int Load_S3M( MAS_Module* mod, bool verbose )
259 bool chan_enabled[32];
268 memset( mod, 0, sizeof( MAS_Module ) );
269 for( x = 0; x < 28; x++ )
270 mod->title[x] = read8(); // read song name
272 if( read8() != 0x1A );
273 // return ERR_INVALID_MODULE;
275 return ERR_INVALID_MODULE;
278 printf( vstr_s3m_div );
281 printf( "Loading S3M, \"%s\"\n", mod->title );
283 skip8( 2 ); // reserved space
284 mod->order_count = (u8)read16();
285 mod->inst_count = (u8)read16();
286 mod->samp_count = mod->inst_count;
287 mod->patt_count = (u8)read16();
289 for( x = 0; x < 32; x++ )
290 mod->channel_volume[x] = 64;
292 mod->freq_mode = 0; // amiga frequencies
293 mod->old_effects=true; // old effects (maybe not?)
294 mod->link_gxx=false; // dont link gxx memory
295 mod->restart_pos = 0; // restart from beginning
298 s3m_flags = read16();
301 if( read32() != 'MRCS' ) // "SCRM" mark
302 return ERR_INVALID_MODULE;
303 mod->global_volume = read8()*2;
304 mod->initial_speed = read8();
305 mod->initial_tempo = read8();
306 stereo = read8() >> 7; // master volume
307 read8(); // ultra click removal
308 dp = read8(); // default pan positions (when 252)
309 skip8( 8+2 ); // reserved space + special pointer
310 for( x = 0; x < 32; x++ )
313 chan_enabled[x] = chn >> 7;
316 if( (chn&127) < 8 ) // left channel
317 mod->channel_panning[x] = clamp_u8( 128 - (PANNING_SEP/2) );
318 else // right channel
319 mod->channel_panning[x] = clamp_u8( 128 + (PANNING_SEP/2) );
323 mod->channel_panning[x] = 128;
326 for( x = 0; x < mod->order_count; x++ )
328 mod->orders[x] = read8();
330 parap_inst = (u16*)malloc( mod->inst_count * sizeof( u16 ) );
331 parap_patt = (u16*)malloc( mod->patt_count * sizeof( u16 ) );
333 for( x = 0; x < mod->inst_count; x++ )
334 parap_inst[x] = read16();
335 for( x = 0; x < mod->patt_count; x++ )
336 parap_patt[x] = read16();
340 for( x = 0; x < 32; x++ )
345 mod->channel_panning[x] = (a&15)*16 > 255 ? 255 : (a&15)*16;
354 mod->channel_panning[x] = clamp_u8( 128 - (PANNING_SEP/2) );
358 mod->channel_panning[x] = clamp_u8( 128 + (PANNING_SEP/2) );
363 mod->channel_panning[x] = 128;
370 for( x = 0; x < 32; x++ )
373 mod->channel_panning[x] = x & 1 ? clamp_u8( 128 - (PANNING_SEP/2) ) : clamp_u8( 128 + (PANNING_SEP/2) );
375 mod->channel_panning[x] = 128;
379 mod->instruments = (Instrument*)malloc( mod->inst_count * sizeof( Instrument ) );
380 mod->samples = (Sample*)malloc( mod->samp_count * sizeof( Sample ) );
381 mod->patterns = (Pattern*)malloc( mod->patt_count * sizeof( Pattern ) );
385 printf( vstr_s3m_div );
386 printf( "Loading Samples...\n" );
387 printf( vstr_s3m_sampt_top );
388 printf( vstr_s3m_sampt_mid );
389 #ifdef vstr_s3m_sampt_slice
390 printf( vstr_s3m_sampt_slice );
394 for( x = 0; x < mod->inst_count; x++ )
398 printf( vstr_s3m_sampt_index, x+1 );
399 //printf( "Sample %i\n", x+1 );
401 // create instrument for sample
402 memset( &mod->instruments[x], 0, sizeof( Instrument ) );
403 mod->instruments[x].global_volume = 128;
405 for( y = 0; y < 120; y++ )
406 mod->instruments[x].notemap[y] = y | ((x+1) << 8);
409 file_seek_read( parap_inst[x]*16, SEEK_SET );
410 if( Load_S3M_Sample( &mod->samples[x], verbose ) )
412 printf( "Error loading sample!\n" );
413 return ERR_UNKNOWNSAMPLE;
420 printf( vstr_s3m_sampt_bottom );
421 printf( "Loading Patterns...\n" );
422 printf( vstr_s3m_div );
424 for( x = 0; x < mod->patt_count; x++ )
428 printf( vstr_s3m_pattern, x+1, ((x+1)%15)?"":"\n" );
430 //printf( "%i...", x+1 );
431 file_seek_read( parap_patt[x]*16, SEEK_SET );
432 Load_S3M_Pattern( &mod->patterns[x] );
438 printf( vstr_s3m_div );
439 printf( "Loading Sample Data...\n" );
441 for( x = 0; x < mod->samp_count; x++ )
443 file_seek_read( mod->samples[x].datapointer, SEEK_SET );
444 Load_S3M_SampleData( &mod->samples[x], (u8)ffi );
448 printf( vstr_s3m_div );