/* testKSM() */ /* Rip_KSM() */ /* Depack_KSM() */ #include "globals.h" #include "extern.h" int16_t testKSM ( void ) { PW_Start_Address = PW_i; if ( (PW_Start_Address + 1536) > PW_in_size) return BAD; /* test "a" */ if ( in_data[PW_Start_Address+15] != 'a' ) return BAD; /* test volumes */ for ( PW_k=0 ; PW_k<15 ; PW_k++ ) if ( in_data[PW_Start_Address+54+PW_k*32] > 0x40 ) return BAD; /* test tracks data */ /* first, get the highest track number .. */ PW_j = 0; for ( PW_k=0 ; PW_k<1024 ; PW_k ++ ) { if ( in_data[PW_Start_Address+PW_k+512] == 0xFF ) break; if ( in_data[PW_Start_Address+PW_k+512] > PW_j ) PW_j = in_data[PW_Start_Address+PW_k+512]; } if ( PW_k == 1024 ) { /*printf ( "#5 Start:%ld\n" , PW_Start_Address );*/ return BAD; } if ( PW_j == 0 ) { /*printf ( "#6 Start:%ld\n" , PW_Start_Address );*/ return BAD; } /* so, now, PW_j is the highest track number (first is 00h !!) */ /* real test on tracks data starts now */ /* first, test if we don't get out of the file */ if ( (PW_Start_Address + 1536 + PW_j*192 + 64*3) > PW_in_size ) return BAD; /* now testing tracks */ for ( PW_k = 0 ; PW_k <= PW_j ; PW_k++ ) for ( PW_l=0 ; PW_l < 64 ; PW_l++ ) if ( in_data[PW_Start_Address+1536+PW_k*192+PW_l*3] > 0x24 ) return BAD; /* PW_j is still the highest track number */ return GOOD; } void Rip_KSM ( void ) { /* PW_j is the highest track number */ PW_WholeSampleSize = 0; for ( PW_k=0 ; PW_k<15 ; PW_k++ ) PW_WholeSampleSize += ((in_data[PW_Start_Address+52+PW_k*32]*256)+in_data[PW_Start_Address+53+PW_k*32]); OutputSize = ((PW_j+1)*192) + PW_WholeSampleSize + 1536; CONVERT = GOOD; Save_Rip ( "Kefrens Sound Machine module", KSM ); if ( Save_Status == GOOD ) PW_i += 2; /* -1 should do but call it "just to be sure" :) */ } /* * Kefrens_Sound_Machine.c 1997 (c) Sylvain "Asle" Chipaux * * Depacks musics in the Kefrens Sound Machine format and saves in ptk. * * Last revision : 26/11/1999 * - reduced to only one FREAD. * - Speed-up, Clean-up and Binary smaller. * Another Update : 28/11/1999 * - removed fopen() speed up and SIZE !. * Another Update : 05 may 2001 * - added transciption for sample names * Another Update : 26 nov 2003 * - used htonl() so that use of addy is now portable on 68k archs * update 30/08/10 * - patternlist with only one pattern fixed * - conversion to STK instead of PTK */ #define ON 1 #define OFF 2 void Depack_KSM ( void ) { uint8_t *Whatever; uint8_t c1=0x00,c2=0x00,c5; uint8_t Track_Numbers[128][4]; uint8_t Track_Numbers_Real[128][4]; uint8_t Track_Datas[4][192]; uint8_t Max=0x00; uint8_t poss[37][2]; uint8_t PatPos; uint8_t Status=ON; uint8_t transco[]={'a','b','c','d','e','f','g','h','i','j' ,'k','l','m','n','o','p','q','r','s','t' ,'u','v','w','x','y','z' ,'-',':','!','~','1','2','3','4','5','6' ,'7','8','9','0',' ',';'}; int32_t Where=PW_Start_Address; int32_t WholeSampleSize=0; uint32_t i=0,j=0,k=0,l; FILE *out; if ( Save_Status == BAD ) return; BZERO ( Track_Numbers , 128*4 ); BZERO ( Track_Numbers_Real , 128*4 ); fillPTKtable(poss); sprintf ( Depacked_OutName , "%d.mod" , Cpt_Filename-1 ); out = PW_fopen ( Depacked_OutName , "w+b" ); /* title */ Whatever = (uint8_t *) malloc ( 1024 ); BZERO ( Whatever , 1024 ); fwrite ( &in_data[Where+2] , 13 , 1 , out ); fwrite ( Whatever , 7 , 1 , out ); /* fill-up there */ /* read and write whole header */ /*printf ( "Converting sample headers ... " );*/ Where += 32; for ( i=0 ; i<15 ; i++ ) { /* write name */ for ( k=0 ; k<15 ; k++ ) Whatever[230+k] = transco[in_data[Where+k]]; fwrite ( &Whatever[230] , 22 , 1 , out ); /* size */ c1 = in_data[Where+20]; c2 = in_data[Where+21]; k = (in_data[Where+20] * 256) + in_data[Where+21]; WholeSampleSize += k; c2 /= 2; if ( (c1/2)*2 != c1 ) { if ( c2 < 0x80 ) c2 += 0x80; else { c2 -= 0x80; c1 += 0x01; } } c1 /= 2; fwrite ( &c1 , 1 , 1 , out ); fwrite ( &c2 , 1 , 1 , out ); /* finetune */ fwrite ( Whatever , 1 , 1 , out ); /* volume */ fwrite ( &in_data[Where+22] , 1 , 1 , out); /* loop start */ c1 = in_data[Where+24]; c2 = in_data[Where+25]; j = k - ((c1*256)+c2); c2 /= 2; if ( (c1/2)*2 != c1 ) { if ( c2 < 0x80 ) c2 += 0x80; else { c2 -= 0x80; c1 += 0x01; } } c1 /= 2; fwrite ( &c1 , 1 , 1 , out ); fwrite ( &c2 , 1 , 1 , out ); if ( j != k ) { /* write loop size */ /* use of htonl() suggested by Xigh !.*/ j/=2; l = htonl(j); c1 = *((uint8_t *)&l+2); c2 = *((uint8_t *)&l+3); fwrite ( &c1 , 1 , 1 , out ); fwrite ( &c2 , 1 , 1 , out ); } else { c1 = 0x00; c2 = 0x01; fwrite ( &c1 , 1 , 1 , out ); fwrite ( &c2 , 1 , 1 , out ); } Where += 32; } /*printf ( "ok\n" );*/ /* pattern list */ /*printf ( "creating the pattern list ... " );*/ Where = PW_Start_Address+512; for ( PatPos=0x00 ; PatPos<128 ; PatPos++ ) { Track_Numbers[PatPos][0] = in_data[Where+PatPos*4]; Track_Numbers[PatPos][1] = in_data[Where+PatPos*4+1]; Track_Numbers[PatPos][2] = in_data[Where+PatPos*4+2]; Track_Numbers[PatPos][3] = in_data[Where+PatPos*4+3]; if ( Track_Numbers[PatPos][0] == 0xFF ) break; if ( Track_Numbers[PatPos][0] > Max ) Max = Track_Numbers[PatPos][0]; if ( Track_Numbers[PatPos][1] > Max ) Max = Track_Numbers[PatPos][1]; if ( Track_Numbers[PatPos][2] > Max ) Max = Track_Numbers[PatPos][2]; if ( Track_Numbers[PatPos][3] > Max ) Max = Track_Numbers[PatPos][3]; } /* write patpos */ fwrite ( &PatPos , 1 , 1 , out ); /* ntk byte */ c1 = 0x7f; fwrite ( &c1 , 1 , 1 , out ); /* sort tracks numbers */ c5 = 0x00; for ( i=0 ; i