home *** CD-ROM | disk | FTP | other *** search
/ ftp.ee.lbl.gov / 2014.05.ftp.ee.lbl.gov.tar / ftp.ee.lbl.gov / bmd-1.0beta.tar.Z / bmd-1.0beta.tar / bmd-1.0beta / app / mtools / dx7dump.c next >
C/C++ Source or Header  |  1991-08-27  |  3KB  |  171 lines

  1. /*
  2.  * Copyright (c) 1990 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Lawrence Berkeley Laboratory,
  11.  * Berkeley, CA.  The name of the University may not be used to
  12.  * endorse or promote products derived from this software without
  13.  * specific prior written permission.
  14.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  15.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  16.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  17.  *
  18.  * dx7dump - dump a dx7 bulk patch dump to the serial port
  19.  */
  20.  
  21. #ifndef lint
  22. static char rcsid[] =
  23.     "@(#) $Header: dx7dump.c,v 1.1 91/08/27 00:44:24 mccanne Exp $ (LBL)";
  24. #endif
  25.  
  26. #include <stdio.h>
  27. #include <sys/file.h>
  28. #include <sys/types.h>
  29. #include <sys/termios.h>
  30.  
  31. /*
  32.  * You need the kernel hack to map B50 to the midi baud rate.
  33.  */
  34. #define B31250 B50
  35. #define PATCHSIZE 4096
  36.  
  37. void
  38. usage()
  39. {
  40.     fprintf(stderr, "usage: dx7dump [-d device] file\n");
  41.     exit(1);
  42. }
  43.  
  44. char *
  45. devname(cp)
  46.     char *cp;
  47. {
  48.     static char buf[256];
  49.  
  50.     if (cp[0] == '/')
  51.         return cp;
  52.     sprintf(buf, "/dev/%s", cp);
  53.     return buf;
  54. }
  55.  
  56. void
  57. setbaud(fd, rate)
  58.     int fd;
  59.     int rate;
  60. {
  61.     struct termios tios;
  62.  
  63.     if (ioctl(fd, TCGETS, &tios) < 0) {
  64.         perror("TCGETS");
  65.         exit(1);
  66.     }
  67.     tios.c_cflag = CLOCAL|CS8|rate;
  68.     if (ioctl(fd, TCSETS, &tios) < 0) {
  69.         perror("TCSETS");
  70.         exit(1);
  71.     }
  72. }
  73.  
  74. void
  75. wr(fd, cp, len)
  76.     int fd;
  77.     char *cp;
  78.     int len;
  79. {
  80.     int cc;
  81.  
  82.     while (len > 0) {
  83.         cc = write(fd, cp, len);
  84.         if (cc < 0) {
  85.             perror("write");
  86.             exit(1);
  87.         }
  88.         len -= cc;
  89.         cp += cc;
  90.     }
  91. }
  92.  
  93. void
  94. midi_dump(patch, device)
  95.     u_char *patch;
  96.     char *device;
  97. {
  98.     int i, fd;
  99.     int sum = 0;
  100.     u_long flags;
  101.     
  102.     u_char header[] = { 0xf0, 0x43, 0x00, 0x09, 0x20, 0x00 };
  103.     u_char trailer[] = { 0x00, 0xf7 };
  104.  
  105.     fd = open(device, O_WRONLY);
  106.     if (fd < 0) {
  107.         perror(device);
  108.         exit(1);
  109.     }
  110.     setbaud(fd, B31250);
  111.     wr(fd, header, sizeof(header));
  112.     wr(fd, patch, PATCHSIZE);
  113.     for (i = 0; i < PATCHSIZE; ++i)
  114.         sum -= patch[i];
  115.     trailer[0] = sum & 0x7f;
  116.     wr(fd, trailer, sizeof(trailer));
  117.     close(fd);
  118. }
  119.  
  120. void
  121. read_binary(fname, bp)
  122.     char *fname;
  123.     unsigned char *bp;
  124. {
  125.     int fd;
  126.  
  127.     fd = open(fname, O_RDONLY);
  128.     if (fd < 0) {
  129.         perror(fname);
  130.         exit(1);
  131.     }
  132.     if (read(fd, bp, PATCHSIZE) != PATCHSIZE) {
  133.         fprintf(stderr, "dx7dump: short file\n");
  134.         exit(1);
  135.     }
  136.     close(fd);
  137. }
  138.  
  139. int
  140. main(argc, argv)
  141.     int argc;
  142.     char **argv;
  143. {
  144.     int port = 1, c;
  145.     char *device = "/dev/ttyb";
  146.     u_char buffer[PATCHSIZE];
  147.  
  148.     extern char *optarg;
  149.     extern int optind;
  150.  
  151.     while ((c = getopt(argc, argv, "d:")) != -1) {
  152.         switch(c) {
  153.  
  154.         case 'd':
  155.             device = devname(optarg);
  156.             break;
  157.  
  158.         default:
  159.             usage();
  160.         }
  161.     }
  162.     if (optind != argc - 1)
  163.         usage();
  164.     
  165.     read_binary(argv[optind], (u_char *)buffer);
  166.     midi_dump((u_char *)buffer, device);
  167.  
  168.     return 0;
  169. }
  170.  
  171.