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

  1. /* simplified receive system exclusive */
  2. /*    receives next sys/ex message and save it to a named file */
  3.  
  4. /*
  5.     shows usage of GetMidiMsg(), MidiMsgLength(), FreeMidiMsg() and msg
  6.     filtering with routes.
  7. */
  8.  
  9. #include <libraries/dos.h>
  10. #include <midi/midi.h>
  11. #include <functions.h>
  12. #include <fcntl.h>
  13.  
  14. void *MidiBase;
  15.  
  16. main(argc,argv)
  17. char **argv;
  18. {
  19.     extern int Enable_Abort;
  20.     int fo;                /* output file descriptor */
  21.     UBYTE *msg=0, *getmsg();
  22.     struct MDest *dest=0;
  23.     struct MRoute *route=0;
  24.     static struct MRouteInfo routeinfo = { MMF_SYSEX };     /* receive only sys/ex msg's */
  25.     char *fname;
  26.  
  27.     printf ("Receive Sys/Ex\n");
  28.     Enable_Abort = 0;            /* disable auto CTRL-C handling */
  29.  
  30.     if (argc < 2) {
  31.     printf ("usage: rsx <file>\n");
  32.     exit (1);
  33.     }
  34.  
  35.     fname = argv[1];            /* get file name */
  36.  
  37.     if (!(MidiBase = OpenLibrary (MIDINAME,MIDIVERSION))) {
  38.     printf ("can't open midi.library\n");
  39.     goto clean;
  40.     }
  41.                     /* create our dest node (private) */
  42.     if (!(dest = CreateMDest (NULL,NULL))) {
  43.     printf ("can't create Dest\n");
  44.     goto clean;
  45.     }
  46.                     /* create our route to MidiIn */
  47.     if (!(route = MRouteDest ("MidiIn", dest, &routeinfo))) {
  48.     printf ("can't create Route (can't find MidiIn?)\n");
  49.     goto clean;
  50.     }
  51.                     /* create our output file */
  52.     if ( (fo=creat(fname,0666)) == -1) {
  53.     printf ("can't open %s\n",fname);
  54.     goto clean;
  55.     }
  56.                     /* wait for the next message, save it or skip if terminated */
  57.     if (msg = getmsg(dest)) {
  58.     long len = MidiMsgLength(msg);        /* get message length */
  59.  
  60.     if (write (fo,msg,(int)len) != len ||    /* won't work for msg's bigger than 64K */
  61.         close(fo) == -1) {
  62.         printf ("error writing file.\n");
  63.     }
  64.     printf ("sys/ex file \"%s\" saved. (%ld bytes)\n",fname,len);
  65.     }
  66.     else {
  67.     printf ("save aborted.\n");
  68.     close(fo);
  69.     unlink(fname);
  70.     }
  71.  
  72. clean:
  73.     if (msg) FreeMidiMsg (msg);
  74.     if (route) DeleteMRoute (route);
  75.     if (dest) DeleteMDest (dest);
  76.     if (MidiBase) CloseLibrary (MidiBase);
  77. }
  78.  
  79.  
  80. /*
  81.    waits for next msg (will be sys/ex because that's all that our route lets
  82.    through).  Returns pointer to message.  If CTRL-C is hit before a message
  83.    arrives, returns NULL
  84. */
  85.  
  86. UBYTE *getmsg (dest)
  87. struct MDest *dest;
  88. {
  89.     ULONG flags = SIGBREAKF_CTRL_C | (1L << dest->DestPort->mp_SigBit);
  90.  
  91.     if (Wait(flags) & SIGBREAKF_CTRL_C) return 0;
  92.     return GetMidiMsg (dest);
  93. }
  94.