home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 1 / GoldFishApril1994_CD1.img / d1xx / d101 / midi / dev / example / hr.c next >
C/C++ Source or Header  |  1987-09-05  |  3KB  |  112 lines

  1. /* hex receive from midi.library */
  2. /*
  3.     display incoming MIDI messages in Hex to the console.
  4.  
  5.     Shows proper method of waiting for, receiving, processing, and disposing
  6.     of MIDI messages.
  7. */
  8.  
  9.  
  10. #include <libraries/dos.h>
  11. #include <clib/macros.h>
  12. #include <midi/midi.h>
  13. #include <functions.h>
  14.  
  15. #include <stdio.h>
  16.  
  17. void *MidiBase;
  18.  
  19. struct MDest *dest;
  20. struct MRoute *route;
  21. UBYTE *msg;            /* buffer this in case we get shut down before freeing the current message */
  22.  
  23. main(argc,argv)
  24. char **argv;
  25. {
  26.     char *sname = "MidiIn";
  27.     static struct MRouteInfo routeinfo = { -1, -1 };    /* route spec's: receive all */
  28.  
  29.     printf ("MIDI Hex Display\n");
  30.  
  31.     if (argc > 1 && *argv[1] == '?') {
  32.     printf ("usage: hr [source]\n");
  33.     exit (1);
  34.     }
  35.  
  36.     if (!(MidiBase = OpenLibrary (MIDINAME,MIDIVERSION))) {
  37.     printf ("can't open midi.library\n");
  38.     goto clean;
  39.     }
  40.  
  41.     if (argc > 1) {        /* if there's an argument, use it as an alt. source name */
  42.     sname = argv[1];
  43.     }
  44.                 /* create out dest node (private) */
  45.     if (!(dest = CreateMDest (NULL,NULL))) {
  46.     printf ("can't create Dest\n");
  47.     goto clean;
  48.     }
  49.                 /* create out route to MidiIn or whatever the user specified */
  50.     if (!(route = MRouteDest (sname, dest, &routeinfo))) {
  51.     printf ("can't create Route (can't find source \"%s\"?)\n",sname);
  52.     goto clean;
  53.     }
  54.  
  55.     process(dest);        /* process until shutdown */
  56.  
  57. clean:
  58.     cleanup();
  59. }
  60.  
  61. _abort()            /* abort routine called when CTRL-C is hit (Aztec) */
  62. {
  63.     fflush(stdout);
  64.     cleanup();
  65.     exit (1);
  66. }
  67.  
  68. cleanup()
  69. {
  70.     if (msg) FreeMidiMsg (msg);
  71.     if (route) DeleteMRoute (route);
  72.     if (dest) DeleteMDest (dest);
  73.     if (MidiBase) CloseLibrary (MidiBase);
  74. }
  75.  
  76. process (dest)
  77. struct MDest *dest;
  78. {
  79.     ULONG flags = SIGBREAKF_CTRL_C | (1L << dest->DestPort->mp_SigBit);
  80.  
  81.     while (!(Wait(flags) & SIGBREAKF_CTRL_C)) {     /* wait until we get a message or CTRL-C is hit, quit on CTRL-C */
  82.     while (msg = GetMidiMsg (dest)) {        /* get a message */
  83.         dumpmsg (msg);                /* print it */
  84.         FreeMidiMsg (msg);                /* free it */
  85.     }
  86.     }
  87. }
  88.  
  89.  
  90. dumpmsg (msg)
  91. UBYTE *msg;
  92. {
  93.     long len = MidiMsgLength (msg);        /* find out how long the message is */
  94.  
  95.     if (*msg == MS_SYSEX) {            /* if it's a System Exclusive message... */
  96.     dump (msg,MIN(len,8));                /* only print the first 8 bytes */
  97.     printf ("... (%ld bytes)\n",len);
  98.     }
  99.     else {                    /* otherwise */
  100.     dump (msg,MidiMsgLength(msg));            /* print the whole message */
  101.     printf ("\n");
  102.     }
  103. }
  104.  
  105.  
  106. dump (s,len)                    /* print len bytes in hex */
  107. UBYTE *s;
  108. long len;
  109. {
  110.     while (len--) printf ("%02x ",*s++);
  111. }
  112.