/* * midinator.c * * Poke MIDI devices * * (c) 2012 Thomas White * */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include #include #include #include #include static void show_help(const char *s) { printf("Syntax: %s [options]\n\n", s); printf( "Do MIDI stuff\n" "\n" " -h, --help Display this help message.\n" ); } static void midi_readable(int fd) { int rval, i; unsigned char buf[32]; rval = read(fd, buf, 32); if ( (rval == -1) || (rval == 0) ) { fprintf(stderr, "Read error.\n"); return; } if ( buf[0] == 0xfe ) { write(fd, buf, 1); return; } for ( i=0; i> 0; write(fd, sbuf, 3); } if ( (buf[0] & 0xf0) == 0xc0 ) { unsigned char sbuf[32]; sbuf[0] = 0xc1; sbuf[1] = buf[1]; write(fd, sbuf, 2); } if ( (buf[0] & 0xf0) == 0xf0 ) { unsigned char sbuf[32]; sbuf[0] = buf[0]; sbuf[1] = buf[1]; sbuf[2] = buf[2]; write(fd, sbuf, 3); } } int main(int argc, char *argv[]) { int c; int fd; int rval; /* Long options */ const struct option longopts[] = { {"help", 0, NULL, 'h'}, {0, 0, NULL, 0} }; /* Short options */ while ((c = getopt_long(argc, argv, "h", longopts, NULL)) != -1) { switch (c) { case 'h' : show_help(argv[0]); return 0; case 0 : break; default : return 1; } } fd = open("/dev/snd/midiC1D0", O_RDWR); if ( fd == -1 ) { fprintf(stderr, "Couldn't open MIDI device.\n"); return 1; } int i; for ( i=0; i<8; i++ ) { unsigned char sbuf[32]; sbuf[0] = 0xb0 || i; sbuf[1] = 0x78; sbuf[2] = 0x00; write(fd, sbuf, 3); } do { fd_set fds; struct timeval tv; int sval; FD_ZERO(&fds); FD_SET(fd, &fds); tv.tv_sec = 1; tv.tv_usec = 0; sval = select(fd+1, &fds, NULL, NULL, &tv); rval = 0; if ( sval == -1 ) { int err = errno; fprintf(stderr, "select() failed: %s\n", strerror(err)); rval = 1; } else if ( sval != 0 ) { midi_readable(fd); } /* else timeout */ } while ( !rval ); close(fd); return 0; }