home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / midifos2.zip / MFTEXT.C < prev    next >
C/C++ Source or Header  |  1993-06-14  |  5KB  |  260 lines

  1. /*
  2.  * mftext
  3.  * 
  4.  * Convert a MIDI file to verbose text.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <ctype.h>
  9. #include <os2.h>
  10.  
  11. #include "midifile.h"
  12.  
  13. static FILE *F;
  14. VOID prtime(VOID);
  15. VOID initfuncs(VOID);
  16.  
  17. int filegetc(VOID)
  18. {
  19.     return(getc(F));
  20. }
  21.  
  22. main(int argc, char **argv)
  23. {
  24.     FILE *efopen();
  25.  
  26.     if ( argc > 1 )
  27.         F = efopen(argv[1],"r");
  28.     else
  29.         F = stdin;
  30.  
  31.     initfuncs();
  32.     Mf_getc = filegetc;
  33.     midifile();
  34.     fclose(F);
  35.     exit(0);
  36. }
  37.  
  38. FILE *efopen(char *name, char *mode)
  39. {
  40.     FILE *f;
  41.     extern int errno;
  42. #ifdef OS2
  43.     CHAR buf[1024];
  44.     ULONG cbBuf;
  45. #else
  46.     extern char *sys_errlist[];
  47.     extern int sys_nerr;
  48.     char *errmess;
  49. #endif
  50.  
  51.     if ( (f=fopen(name,mode)) == NULL )
  52.     {
  53. #ifdef OS2
  54.         if (DosGetMessage(NULL, 0, buf, 1024, errno, "oso001.msg", &cbBuf))
  55.             fprintf(stderr, "SYS%04d: error text unavailable\n", errno);
  56.         else
  57.         {
  58.             buf[cbBuf] = (char)0;
  59.             fprintf(stderr,"%s\n",buf);
  60.         }
  61. #else
  62.         fprintf(stderr,"*** ERROR *** Cannot open '%s'!\n",name);
  63.         if ( errno <= sys_nerr )
  64.             errmess = sys_errlist[errno];
  65.         else
  66.             errmess = "Unknown error!";
  67.         fprintf(stderr,"************* Reason: %s\n",errmess);
  68. #endif
  69.         exit(1);
  70.     }
  71.     return(f);
  72. }
  73.  
  74. VOID error(char *s)
  75. {
  76.     fprintf(stderr,"Error: %s\n",s);
  77. }
  78.  
  79. VOID txt_header(int format, int ntrks, int division)
  80. {
  81.     printf("Header format=%d ntrks=%d division=%d\n",format,ntrks,division);
  82. }
  83.  
  84. VOID txt_trackstart(VOID)
  85. {
  86.     printf("Track start\n");
  87. }
  88.  
  89. VOID txt_trackend(VOID)
  90. {
  91.     printf("Track end\n");
  92. }
  93.  
  94. VOID txt_noteon(int chan, int pitch, int vol)
  95. {
  96.     prtime();
  97.     printf("Note on, chan=%d pitch=%d vol=%d\n",chan+1,pitch,vol);
  98. }
  99.  
  100. VOID txt_noteoff(int chan, int pitch, int vol)
  101. {
  102.     prtime();
  103.     printf("Note off, chan=%d pitch=%d vol=%d\n",chan+1,pitch,vol);
  104. }
  105.  
  106. VOID txt_pressure(int chan, int pitch,int press)
  107. {
  108.     prtime();
  109.     printf("Pressure, chan=%d pitch=%d press=%d\n",chan+1,pitch,press);
  110. }
  111.  
  112. VOID txt_parameter(int chan, int control, int value)
  113. {
  114.     prtime();
  115.     printf("Parameter, chan=%d c1=%d c2=%d\n",chan+1,control,value);
  116. }
  117.  
  118. VOID txt_pitchbend(int chan, int msb, int lsb)
  119. {
  120.     prtime();
  121.     printf("Pitchbend, chan=%d msb=%d lsb=%d\n",chan+1,msb,lsb);
  122. }
  123.  
  124. VOID txt_program(int chan, int program)
  125. {
  126.     prtime();
  127.     printf("Program, chan=%d program=%d\n",chan+1,program);
  128. }
  129.  
  130. VOID txt_chanpressure(int chan, int press)
  131. {
  132.     prtime();
  133.     printf("Channel pressure, chan=%d pressure=%d\n",chan+1,press);
  134. }
  135.  
  136. VOID txt_sysex(int leng, char *mess)
  137. {
  138.     prtime();
  139.     printf("Sysex, leng=%d\n",leng);
  140. }
  141.  
  142. VOID txt_metamisc(int type, int leng, char *mess)
  143. {
  144.     prtime();
  145.     printf("Meta event, unrecognized, type=0x%02x leng=%d\n",type,leng);
  146. }
  147.  
  148. VOID txt_metaspecial(int type, char *mess)
  149. {
  150.     prtime();
  151.     printf("Meta event, sequencer-specific, type=0x%02x",type);
  152. }
  153.  
  154. VOID txt_metatext(int type, int leng, char *mess)
  155. {
  156.     static char *ttype[] = {
  157.         NULL,
  158.         "Text Event",        /* type=0x01 */
  159.         "Copyright Notice",    /* type=0x02 */
  160.         "Sequence/Track Name",
  161.         "Instrument Name",    /* ...       */
  162.         "Lyric",
  163.         "Marker",
  164.         "Cue Point",        /* type=0x07 */
  165.         "Unrecognized"
  166.     };
  167.     int unrecognized = (sizeof(ttype)/sizeof(char *)) - 1;
  168.     int n, c;
  169.     char *p = mess;
  170.  
  171.     if ( type < 1 || type > unrecognized )
  172.         type = unrecognized;
  173.     prtime();
  174.     printf("Meta Text, type=0x%02x (%s)  leng=%d\n",type,ttype[type],leng);
  175.     printf("     Text = <");
  176.     for ( n=0; n<leng; n++ ) {
  177.         c = *p++;
  178.         printf( (isprint(c)||isspace(c)) ? "%c" : "\\0x%02x" , c);
  179.     }
  180.     printf(">\n");
  181. }
  182.  
  183. VOID txt_metaseq(int num)
  184. {
  185.     prtime();
  186.     printf("Meta event, sequence number = %d\n",num);
  187. }
  188.  
  189. VOID txt_metaeot(VOID)
  190. {
  191.     prtime();
  192.     printf("Meta event, end of track\n");
  193. }
  194.  
  195. VOID txt_keysig(int sf, int mi)
  196. {
  197.     prtime();
  198.     printf("Key signature, sharp/flats=%d  minor=%d\n",sf,mi);
  199. }
  200.  
  201. VOID txt_tempo(int tempo)
  202. {
  203.     prtime();
  204.     printf("Tempo, microseconds-per-MIDI-quarter-note=%d\n",tempo);
  205. }
  206.  
  207. VOID txt_timesig(int nn, int dd, int cc, int bb)
  208. {
  209.     int denom = 1;
  210.     while ( dd-- > 0 )
  211.         denom *= 2;
  212.     prtime();
  213.     printf("Time signature=%d/%d  MIDI-clocks/click=%d  32nd-notes/24-MIDI-clocks=%d\n",
  214.         nn,denom,cc,bb);
  215. }
  216.  
  217. VOID txt_smpte(int hr, int mn, int se, int fr, int ff)
  218. {
  219.     prtime();
  220.     printf("SMPTE, hour=%d minute=%d second=%d frame=%d fract-frame=%d\n",
  221.         hr,mn,se,fr,ff);
  222. }
  223.  
  224. VOID txt_arbitrary(int leng,char *mess)
  225. {
  226.     prtime();
  227.     printf("Arbitrary bytes, leng=%d\n",leng);
  228. }
  229.  
  230. VOID prtime(VOID)
  231. {
  232.     printf("Time=%ld  ",Mf_currtime);
  233. }
  234.  
  235. VOID initfuncs(VOID)
  236. {
  237.     Mf_error = error;
  238.     Mf_header =  txt_header;
  239.     Mf_starttrack =  txt_trackstart;
  240.     Mf_endtrack =  txt_trackend;
  241.     Mf_on =  txt_noteon;
  242.     Mf_off =  txt_noteoff;
  243.     Mf_pressure =  txt_pressure;
  244.     Mf_controller =  txt_parameter;
  245.     Mf_pitchbend =  txt_pitchbend;
  246.     Mf_program =  txt_program;
  247.     Mf_chanpressure =  txt_chanpressure;
  248.     Mf_sysex =  txt_sysex;
  249.     Mf_metamisc =  txt_metamisc;
  250.     Mf_seqnum =  txt_metaseq;
  251.     Mf_eot =  txt_metaeot;
  252.     Mf_timesig =  txt_timesig;
  253.     Mf_smpte =  txt_smpte;
  254.     Mf_tempo =  txt_tempo;
  255.     Mf_keysig =  txt_keysig;
  256.     Mf_sqspecific =  txt_metaspecial;
  257.     Mf_text =  txt_metatext;
  258.     Mf_arbitrary =  txt_arbitrary;
  259. }
  260.