From e0ef1d1b20df2ff5f656902f1f95cddc83ec3a81 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Sun, 12 Feb 2012 01:34:05 +0100 Subject: Initial import --- .gitignore | 2 + Makefile | 22 +++++++++ midinator.c | 161 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 185 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 midinator.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2ae0ea9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.o +midinator diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..835a701 --- /dev/null +++ b/Makefile @@ -0,0 +1,22 @@ +PROGS = midinator + +CFLAGS = -O2 -Wall -g ${INCLUDES} +CAIRO_CFLAGS=${CFLAGS} `pkg-config --cflags cairo` +GTK_CFLAGS=${CAIRO_CFLAGS} `pkg-config --cflags gtk+-2.0` + +LIBS = +CAIRO_LIBS=${LIBS} `pkg-config --libs cairo` +GTK_LIBS=${CAIRO_LIBS} `pkg-config --libs gtk+-2.0` + +all: ${PROGS} + +midinator: midinator.o + gcc midinator.o -o midinator ${LIBS} + +midinator.o: midinator.c + gcc -c midinator.c -o midinator.o ${CFLAGS} + +clean: + rm -f ${PROGS} *.o + +.PHONEY: all diff --git a/midinator.c b/midinator.c new file mode 100644 index 0000000..40b9c1d --- /dev/null +++ b/midinator.c @@ -0,0 +1,161 @@ +/* + * 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; +} -- cgit v1.2.3