amf2mod.c

Go to the documentation of this file.
00001 // AMF->MOD converter
00002 // made in 1995 by Mr. P / Powersource
00003 // mrp@fish.share.net, ac054@sfn.saskatoon.sk.ca
00004 // This program is hereby placed in the public domain.
00005 
00006 #include <stdio.h>
00007 #include <stdlib.h>
00008 #include <string.h>
00009 
00010 main(int ac, char *av[])
00011 {
00012         char *inpt,*outpt;
00013         FILE *inf,*outf;
00014         int j;
00015         unsigned char *workbuf;
00016         char orders[256];
00017         int workptr;
00018         unsigned long i,numpats,k;
00019         unsigned long length,repstart,replen;
00020         char insstruc[30];
00021         int numorders;
00022         unsigned int period,ins,effect;
00023         long percentile;
00024 
00025         int periodtable[]={6848,6464,6096,5760,5424,5120,4832,4560,4304,
00026                            4064,3840,3628,3424,3232,3048,2880,2712,2560,
00027                            2416,2280,2152,2032,1920,1814,1712,1616,1524,
00028                            1440,1356,1280,1208,1140,1076,1016, 960, 907,
00029                             856, 808, 762, 720, 678, 640, 604, 570, 538,
00030                             508, 480, 453, 428, 404, 381, 360, 339, 320,
00031                             302, 285, 269, 254, 240, 226, 214, 202, 190,
00032                             180, 170, 160, 151, 143, 135, 127, 120, 113,
00033                             107, 101,  95,  90,  85,  80,  75,  71,  67,
00034                              63,  60,  56,  53,  50,  47,  45,  42,  40,
00035                              37,  35,  33,  31,  30,  28};
00036 
00037         if (ac<3) {
00038                 printf("syntax: AMF2MOD inputfile outputfile\n");
00039                 exit(1);
00040         }
00041 
00042         inpt=av[1];
00043         outpt=av[2];
00044 
00045         inf=fopen(inpt,"rb");
00046         outf=fopen(outpt,"wb");
00047 
00048         if (!inf) {
00049                 printf ("Unable to open file for reading '%s'\n", inpt);
00050                 exit(-1);
00051         }
00052         else if (!outf) {
00053                 printf ("Unable to open file for writing '%s'\n", outpt);
00054                 exit(-1);
00055         }
00056 
00057         // AMF conversion will take place now.
00058         printf("Converting %s from AMF to MOD, %s as output\n\n",inpt,outpt);
00059         printf("Translating header:     [ ]");
00060 
00061         workbuf = (unsigned char *)malloc(64*37);
00062         // no title for MOD
00063         memset(workbuf,0,64*37);
00064         fwrite(workbuf,1,20,outf);
00065 
00066         // Skip past ASYLUM Music Format v1.0
00067         fseek(inf,32,0);
00068 
00069         // load valuable data
00070         fread(workbuf,1,6,inf);
00071         numorders=workbuf[4];
00072         numpats=workbuf[3];
00073 
00074         // load orders
00075         memset(orders,0,256);
00076         fread(orders,1,numorders,inf);
00077         fseek(inf,294,0);
00078 
00079         printf("\b\bX]\n");
00080         printf("Translating instruments [ ]");
00081 
00082         // load instruments
00083         fread(workbuf,37,64,inf);
00084 
00085         workptr=0;
00086 
00087         for (i=0;i<31;i++) {
00088                 // status report
00089                 printf("\b\b|]");
00090                 // now copy name
00091                 strcpy(insstruc,(char*) &(workbuf[workptr]));
00092                 // read in instrument data
00093                 insstruc[24]=workbuf[workptr+22];
00094                 // copy the rest
00095                 insstruc[25]=workbuf[workptr+23];
00096                 length=((workbuf[workptr+25])     |
00097                        ((workbuf[workptr+26])<<8) |
00098                        ((workbuf[workptr+27])<<16)|
00099                        ((workbuf[workptr+28])<<24))/2;
00100 
00101                 insstruc[22]=((length>>8)&0xff);
00102                 insstruc[23]=(length&0xff);
00103                 printf("\b\b/]");
00104 
00105                 repstart=((workbuf[workptr+29]&0xff)     |
00106                          ((workbuf[workptr+30]&0xff)<<8) |
00107                          ((workbuf[workptr+31]&0xff)<<16)|
00108                          ((workbuf[workptr+32]&0xff)<<24))/2;
00109 
00110                 insstruc[26]=((repstart>>8)&0xff);
00111                 insstruc[27]=(repstart&0xff);
00112                 printf("\b\b-]");
00113 
00114                 replen=((workbuf[workptr+33]&0xff)     |
00115                        ((workbuf[workptr+34]&0xff)<<8) |
00116                        ((workbuf[workptr+35]&0xff)<<16)|
00117                        ((workbuf[workptr+36]&0xff)<<24))/2;
00118 
00119                 insstruc[28]=((replen>>8)&0xff);
00120                 insstruc[29]=(replen&0xff);
00121                 printf("\b\b\\]");
00122 
00123                 workptr+=37;
00124                 // now write
00125                 fwrite(insstruc,1,30,outf);
00126         }
00127         printf("\b\bX]\n");
00128 
00129         printf("Translating orders      [ ]");
00130         // now, misc data
00131         insstruc[0]=numorders;
00132         insstruc[1]=127;
00133         fwrite(insstruc,1,2,outf);
00134         // and orders
00135         fwrite(orders,1,128,outf);
00136         // finally, 8CHN
00137         insstruc[0]='8';
00138         insstruc[1]='C';
00139         insstruc[2]='H';
00140         insstruc[3]='N';
00141         fwrite(insstruc,1,4,outf);
00142 //      gotoxy(26,wherey());
00143         printf("\b\bX]\n");
00144         printf("Translating patterns    00%%");
00145 
00146         // now tackle patterns
00147         for(i=0;i<numpats;i++) {
00148                 // load a pattern
00149                 fread(workbuf,64,32,inf);
00150                 // conversion
00151                 for(j=0;j<512;j++) {
00152                         // convert period->note;
00153                         if (workbuf[j*4] != 0) {
00154                                 // lookup
00155                                 period=periodtable[workbuf[j*4]];
00156                         } else
00157                                 period=0;
00158                         ins=workbuf[j*4+1];
00159                         // asm is the only way
00160                         effect=(workbuf[j*4+2] * 256)|workbuf[j*4+3];
00161                         // encode it
00162                         insstruc[0]=(ins&0xf0)|((period&0xf00)>>8);
00163                         insstruc[1]=(period&0xff);
00164                         insstruc[2]=((ins&0xf)<<4)|((effect&0xf00)>>8);
00165                         insstruc[3]=(effect&0xff);
00166                         // write it
00167                         fwrite(insstruc,1,4,outf);
00168                 }
00169                 // status display
00170                 percentile=((100*(i*2048))/(2048*numpats));
00171                 printf("\b\b\b%2ld%%",percentile);
00172         }
00173         printf("\b\b\b[X]\n");
00174 
00175         printf("Copying sample data     00%%");
00176         i=2662+(2048*numpats);
00177         // first, find our limit
00178         fseek(inf,0,2);
00179         k=ftell(inf);
00180         fseek(inf,i,0);
00181         // read 4096-byte blocks until there is no more room
00182         free(workbuf);
00183         workbuf=(unsigned char *)malloc(31*1024);
00184         while((k-i)>=31*1024) {
00185                 fread(workbuf,31*1024,1,inf);
00186                 fwrite(workbuf,31*1024,1,outf);
00187                 i+=31*1024;
00188 
00189                 percentile=((100*i)/k);
00190                 printf("\b\b\b%2ld%%",percentile);
00191         }
00192         // finish it off
00193         if ((k-i) != 0) {
00194                 fread(workbuf,k-i,1,inf);
00195                 fwrite(workbuf,k-i,1,outf);
00196         }
00197 
00198         free(workbuf);
00199         printf("\b\b\b[X]\n");
00200 
00201         fclose(inf);
00202         fclose(outf);
00203         printf("\nConversion successful.\n");
00204         return 0;
00205 }
00206 

Generated on Fri Jul 27 22:27:09 2007 for pentagram by  doxygen 1.4.7