home *** CD-ROM | disk | FTP | other *** search
/ PC Loisirs 18 / cd.iso / sharewar / mikm202 / source / mikcvt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-18  |  5.6 KB  |  315 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <dir.h>
  4. #include <string.h>
  5.  
  6. #include "mtypes.h"
  7. #include "wildfile.h"
  8.  
  9. #include "mloader.h"
  10. #include "munitrk.h"
  11.  
  12. /*
  13.     Declare external loaders:
  14. */
  15.  
  16. extern LOADER mtmload,s3mload,ultload,modload,dsmload,medload,
  17.               farload,s69load,uniload,xmload,stmload,m15load;
  18.  
  19.  
  20. FILE *fpi,*fpo;
  21.  
  22.  
  23. UWORD numsamples;
  24. ULONG samplepos[128];
  25. ULONG samplesize[128];
  26. UBYTE buf[8000];
  27.  
  28. static char path[MAXPATH];
  29. static char drive[MAXDRIVE];
  30. static char dir[MAXDIR];
  31. static char name[MAXFILE];
  32. static char ext[MAXEXT];
  33.  
  34.  
  35. int CopyData(FILE *fpi,FILE *fpo,ULONG len)
  36. {
  37.     ULONG todo;
  38.  
  39.     while(len){
  40.         todo=(len>8000)?8000:len;
  41.         if(!fread(buf,todo,1,fpi)) return 0;
  42.         fwrite(buf,todo,1,fpo);
  43.         len-=todo;
  44.     }
  45.     return 1;
  46. }
  47.  
  48.  
  49. /***************************************************************************
  50. ****************************************************************************
  51. ***************************************************************************/
  52.  
  53.  
  54. int TrkCmp(UBYTE *t1,UBYTE *t2)
  55. {
  56.     int l1,l2;
  57.  
  58.     if(t1==NULL || t2==NULL) return 0;
  59.  
  60.     l1=TrkLen(t1);
  61.     l2=TrkLen(t2);
  62.  
  63.     if(l1!=l2) return 0;
  64.  
  65.     return(MyCmp(t1,t2,l1));
  66. }
  67.  
  68.  
  69.  
  70. void ReplaceTrack(UNIMOD *mf,int t1,int t2)
  71. {
  72.     int t;
  73.  
  74.     for(t=0;t<mf->numpat*mf->numchn;t++){
  75.         if(mf->patterns[t]==t1) mf->patterns[t]=t2;
  76.     }
  77. }
  78.  
  79.  
  80.  
  81. void Optimize(UNIMOD *mf)
  82. /*
  83.     Optimizes the number of tracks in a modfile by removing tracks with
  84.     identical contents.
  85. */
  86. {
  87.     int t,u,done=0,same,newcnt=0;
  88.     UBYTE *ta;
  89.     UBYTE **newtrk;
  90.  
  91.     if(!(newtrk=malloc(mf->numtrk*sizeof(UBYTE *)))) return;
  92.  
  93.     for(t=0;t<mf->numtrk;t++){
  94.  
  95.         // ta is track to examine
  96.  
  97.         ta=mf->tracks[t];
  98.  
  99.         // does ta look familiar ?
  100.  
  101.         for(same=u=0;u<newcnt;u++){
  102.             if(TrkCmp(ta,newtrk[u])){
  103.                 same=1;
  104.                 break;
  105.             }
  106.         }
  107.  
  108.         if(same){
  109.             ReplaceTrack(mf,t,u);
  110.             done++;
  111.         }
  112.         else{
  113.             ReplaceTrack(mf,t,newcnt);
  114.             newtrk[newcnt++]=ta;
  115.         }
  116.  
  117.         printf("\rOptimizing: %d\%",(t*100L)/mf->numtrk);
  118.     }
  119.  
  120.     printf("\rOptimized : %d tracks\n",done);
  121.  
  122.     free(mf->tracks);
  123.     mf->tracks=newtrk;
  124.     mf->numtrk=newcnt;
  125. }
  126.  
  127. /***************************************************************************
  128. ****************************************************************************
  129. ***************************************************************************/
  130.  
  131.  
  132. WORD MD_SampleLoad(FILE *fp,ULONG length,ULONG loopstart,ULONG loopend,UWORD flags)
  133. {
  134.     // record position of sample
  135.  
  136.     samplepos[numsamples]=ftell(fp);
  137.  
  138.     // determine it's bytesize
  139.  
  140.     if(flags&SF_16BITS) length<<=1;
  141.  
  142.     // record bytesize and skip the sample
  143.  
  144.     samplesize[numsamples++]=length;
  145.     fseek(fp,length,SEEK_CUR);
  146.     return 1;
  147. }
  148.  
  149.  
  150. void MD_SampleUnLoad(WORD handle)
  151. {
  152. }
  153.  
  154.  
  155. void StrWrite(char *s)
  156. /*
  157.     Writes a null-terminated string as a pascal string to fpo.
  158. */
  159. {
  160.     int len;
  161.  
  162.     len=(s!=NULL) ? strlen(s) : 0;
  163.     fwrite(&len,sizeof(int),1,fpo);
  164.     if(len) fwrite(s,len,1,fpo);
  165. }
  166.  
  167.  
  168. void TrkWrite(UBYTE *t)
  169. /*
  170.     Writes a track to fpo.
  171. */
  172. {
  173.     UWORD len;
  174.     if(t==NULL) printf("NULL track");
  175.     len=TrkLen(t);
  176.     fwrite(&len,sizeof(int),1,fpo);
  177.     fwrite(t,len,1,fpo);
  178. }
  179.  
  180.  
  181. int main(int argc,char *argv[])
  182. {
  183.     int t,v,w;
  184.  
  185.     puts(mikbanner);
  186.  
  187.     //     Expand wildcards on commandline (only neccesary for MSDOS):
  188.  
  189.     MyGlob(&argc,&argv,0);
  190.  
  191.     /*
  192.         Register the loaders we want to use..
  193.     */
  194.  
  195.     ML_RegisterLoader(&m15load);
  196.     ML_RegisterLoader(&modload);
  197.     ML_RegisterLoader(&mtmload);
  198.     ML_RegisterLoader(&farload);
  199.     ML_RegisterLoader(&s69load);
  200.     ML_RegisterLoader(&s3mload);
  201.     ML_RegisterLoader(&stmload);
  202.     ML_RegisterLoader(&dsmload);
  203.     ML_RegisterLoader(&medload);
  204.     ML_RegisterLoader(&ultload);
  205.     ML_RegisterLoader(&uniload);
  206.     ML_RegisterLoader(&xmload);
  207.  
  208.     if(argc==1 || argv[1][0]=='/'){
  209.  
  210.         // display a usage message
  211.  
  212.         puts("Usage: MIKCVT <fletch.mod> ... ");
  213.         puts("Converts your modules to .UNI modules\n");
  214.         exit(-1);
  215.     }
  216.  
  217.     for(t=1; t<argc; t++){
  218.  
  219.         UNIMOD *mf;
  220.  
  221.         printf("In file : %s\n",argv[t]);
  222.  
  223.         numsamples=0;
  224.  
  225.         if((fpi=fopen(argv[t],"rb"))==NULL){
  226.             printf("MikCvt Error: Error opening input file\n");
  227.             break;
  228.         }
  229.  
  230.         fnsplit(argv[t],drive,dir,name,ext);
  231.         fnmerge(path,NULL,NULL,name,".UNI");
  232.  
  233.         printf("Out file: %s\n",path);
  234.  
  235.         if((fpo=fopen(path,"wb"))==NULL){
  236.             printf("MikCvt Error: Error opening output file\n");
  237.             break;
  238.         }
  239.  
  240.         mf=ML_LoadFP(fpi);
  241.  
  242.         //    didn't work -> exit with error
  243.  
  244.         if(mf==NULL){
  245.             printf("MikCvt Error: %s\n",myerr);
  246.             fclose(fpi);
  247.             break;
  248.         }
  249.  
  250.         printf( "Songname: %s\n"
  251.                 "Modtype : %s\n",
  252.                 mf->songname,
  253.                 mf->modtype);
  254.  
  255.         // Optimize the tracks
  256.  
  257.         Optimize(mf);
  258.  
  259.         // Write UNI header
  260.  
  261.         fwrite("UN04",4,1,fpo);
  262.         fwrite(mf,sizeof(UNIHEADER),1,fpo);
  263.         StrWrite(mf->songname);
  264.         StrWrite(mf->modtype);
  265.         StrWrite(mf->comment);
  266.  
  267.         // Write instruments
  268.  
  269.         for(v=0;v<mf->numins;v++){
  270.  
  271.             INSTRUMENT *i=&mf->instruments[v];
  272.  
  273.             fwrite(i,sizeof(UNIINSTRUMENT),1,fpo);
  274.             StrWrite(i->insname);
  275.  
  276.             for(w=0;w<i->numsmp;w++){
  277.  
  278.                 SAMPLE *s=&i->samples[w];
  279.  
  280.                 fwrite(s,sizeof(UNISAMPLE),1,fpo);
  281.                 StrWrite(s->samplename);
  282.             }
  283.         }
  284.  
  285.         // Write patterns
  286.  
  287.         fwrite(mf->pattrows,sizeof(UWORD),mf->numpat,fpo);
  288.         fwrite(mf->patterns,sizeof(UWORD),mf->numpat*mf->numchn,fpo);
  289.  
  290.         // Write tracks
  291.  
  292.         for(v=0;v<mf->numtrk;v++){
  293.             TrkWrite(mf->tracks[v]);
  294.         }
  295.  
  296.         printf("Writing samples.. ");
  297.  
  298.         // Write sample-data
  299.  
  300.         for(v=0;v<numsamples;v++){
  301.             fseek(fpi,samplepos[v],SEEK_SET);
  302.             CopyData(fpi,fpo,samplesize[v]);
  303.         }
  304.  
  305.         puts("Done.");
  306.  
  307.         // and clean up
  308.  
  309.         fclose(fpo);
  310.         fclose(fpi);
  311.         ML_Free(mf);
  312.     }
  313.     return 0;
  314. }
  315.