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 ****************************************************************************/
41 #include "samplefix.h"
55 #define TMP_SAMP "sampJ328G54AU3.tmp"
56 #define TMP_SONG "songDJ34957FAI.tmp"
58 void MSL_PrintDefinition( char* filename, u16 id, char* prefix );
60 #define SAMPLE_HEADER_SIZE (12 + (( target_system == SYSTEM_NDS ) ? 4:0))
62 void MSL_Erase( void )
66 file_delete( TMP_SAMP );
67 file_delete( TMP_SONG );
70 u16 MSL_AddSample( Sample* samp )
74 file_open_write_end( TMP_SAMP );
76 sample_length = samp->sample_length;
78 write32( ((samp->format & SAMPF_16BIT) ? sample_length*2 : sample_length ) + SAMPLE_HEADER_SIZE +4); // +4 for sample padding
79 write8 ( (target_system == SYSTEM_GBA) ? MAS_TYPE_SAMPLE_GBA : MAS_TYPE_SAMPLE_NDS );
80 write8( MAS_VERSION );
81 write8( samp->filename[0] == '#' ? 1 : 0);
82 write8( BYTESMASHER );
84 Write_SampleData(samp);
91 u16 MSL_AddSampleC( Sample* samp )
102 int fsize=file_size( TMP_SAMP );
105 return MSL_AddSample( samp );
107 F_SAMP = fopen( TMP_SAMP, "rb" );
108 fseek( F_SAMP, 0, SEEK_SET );
110 while( ftell( F_SAMP ) < fsize )
112 h_filesize = read32f( F_SAMP );
114 samp_len = read32f( F_SAMP );
115 samp_llen = read32f( F_SAMP );
116 sformat = read8f( F_SAMP ); /////// BUG! GBA DOESNLT WRITE FORMAT!?
118 if( target_system == SYSTEM_NDS )
124 if( samp->sample_length == samp_len && ( samp->loop_type ? samp->loop_end-samp->loop_start : 0xFFFFFFFF ) == samp_llen && sformat == sample_dsformat( samp ) )
126 // verify sample data
127 if( samp->format & SAMPF_16BIT )
129 for( st=0; st<samp_len; st++ )
131 if( read16f( F_SAMP ) != ((u16*)samp->data)[st] )
140 for( st=0; st<samp_len; st++ )
142 if( read8f( F_SAMP ) != ((u8*)samp->data)[st] )
156 skip8f( (h_filesize-SAMPLE_HEADER_SIZE ) - (st+1) , F_SAMP ); // +4 to skip padding
161 skip8f( h_filesize- SAMPLE_HEADER_SIZE , F_SAMP ); // +4 to skip padding
166 return MSL_AddSample( samp );
169 u16 MSL_AddModule( MAS_Module* mod )
174 for( x = 0; x < mod->samp_count; x++ )
176 samp_id = MSL_AddSampleC( &mod->samples[x] );
177 if( mod->samples[x].filename[0] == '#' )
178 MSL_PrintDefinition( mod->samples[x].filename+1, (u16)samp_id, "SFX_" );
179 mod->samples[x].msl_index = samp_id;
182 file_open_write_end( TMP_SONG );
183 Write_MAS( mod, false, true );
189 void MSL_Export( char* filename )
198 file_open_write( filename );
199 write16( MSL_NSAMPS );
200 write16( MSL_NSONGS );
210 parap_samp = (u32*)malloc( MSL_NSAMPS * sizeof( u32 ) );
211 parap_song = (u32*)malloc( MSL_NSONGS * sizeof( u32 ) );
213 // reserve space for parapointers
214 for( x = 0; x < MSL_NSAMPS; x++ )
215 write32( 0xAAAAAAAA );
216 for( x = 0; x < MSL_NSONGS; x++ )
217 write32( 0xAAAAAAAA );
219 file_open_read( TMP_SAMP );
220 for( x = 0; x < MSL_NSAMPS; x++ )
223 parap_samp[x] = file_tell_write();
224 file_size = read32();
225 write32( file_size );
226 for( y = 0; y < file_size+4; y++ )
231 file_open_read( TMP_SONG );
232 for( x = 0; x < MSL_NSONGS; x++ )
235 parap_song[x] = file_tell_write();
236 file_size = read32();
237 write32( file_size );
238 for( y = 0; y < file_size+4; y++ )
243 file_seek_write( 0x0C, SEEK_SET );
244 for( x = 0; x < MSL_NSAMPS; x++ )
245 write32( parap_samp[x] );
246 for( x= 0; x < MSL_NSONGS; x++ )
247 write32( parap_song[x] );
257 void MSL_PrintDefinition( char* filename, u16 id, char* prefix )
261 if( filename[0] == 0 ) // empty string
263 for( x = 0; x < (int)strlen( filename ); x++ )
265 if( filename[x] == '\\' || filename[x] == '/' ) s = x+1;
267 for( x = s; x < (int)strlen( filename ); x++ )
269 if( filename[x] != '.' )
271 newtitle[x-s] = toupper(filename[x]);
272 if( newtitle[x-s] >= ' ' && newtitle[x-s] <= '/' )
274 if( newtitle[x-s] >= ':' && newtitle[x-s] <= '@' )
276 if( newtitle[x-s] >= '[' && newtitle[x-s] <= '`' )
278 if( newtitle[x-s] >= '{' )
289 fprintf( F_HEADER, "#define %s%s %i\r\n", prefix, newtitle, id );
293 void MSL_LoadFile( char* filename, bool verbose )
298 if( file_open_read( filename ) )
300 printf( "Cannot open %s for reading! Skipping.\n", filename );
303 f_ext = get_ext( filename );
307 Load_MOD( &mod, verbose );
308 MSL_PrintDefinition( filename, MSL_AddModule( &mod ), "MOD_" );
309 Delete_Module( &mod );
312 Load_S3M( &mod, verbose );
313 MSL_PrintDefinition( filename, MSL_AddModule( &mod ), "MOD_" );
314 Delete_Module( &mod );
317 Load_XM( &mod, verbose );
318 MSL_PrintDefinition( filename, MSL_AddModule( &mod ), "MOD_" );
319 Delete_Module( &mod );
322 Load_IT( &mod, verbose );
323 MSL_PrintDefinition( filename, MSL_AddModule( &mod ), "MOD_" );
324 Delete_Module( &mod );
327 Load_WAV( &wav, verbose, true );
328 wav.filename[0] = '#'; // set SFX flag (for demo)
329 MSL_PrintDefinition( filename, MSL_AddSample( &wav ), "SFX_" );
333 // print error/warning
334 printf( "Unknown file %s...\n", filename );
340 int MSL_Create( char* argv[], int argc, char* output, char* header, bool verbose )
344 // bool comment=false;
353 F_HEADER = fopen( header, "wb" );
357 // return -1; // needs header file!
359 file_open_write( TMP_SAMP );
361 file_open_write( TMP_SONG );
364 for( x = 1; x < argc; x++ )
366 if( argv[x][0] == '-' )
372 MSL_LoadFile( argv[x], verbose );
376 MSL_Export( output );
380 fprintf( F_HEADER, "#define MSL_NSONGS %i\r\n", MSL_NSONGS );
381 fprintf( F_HEADER, "#define MSL_NSAMPS %i\r\n", MSL_NSAMPS );
382 fprintf( F_HEADER, "#define MSL_BANKSIZE %i\r\n", (MSL_NSAMPS+MSL_NSONGS) );
387 file_delete( TMP_SAMP );
388 file_delete( TMP_SONG );