summaryrefslogtreecommitdiff
path: root/midinator.c
diff options
context:
space:
mode:
Diffstat (limited to 'midinator.c')
-rw-r--r--midinator.c161
1 files changed, 161 insertions, 0 deletions
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 <taw@bitwiz.org.uk>
+ *
+ */
+
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdarg.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <getopt.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+
+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<rval; i++ ) {
+ if ( i != 0 ) printf(" : ");
+ printf("%hhx", buf[i]);
+ }
+ printf("\n");
+
+ if ( (buf[0] & 0xf0) == 0x90 ) {
+ unsigned char sbuf[32];
+ sbuf[0] = 0x91;
+ sbuf[1] = buf[1] + 12;
+ sbuf[2] = buf[2] >> 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;
+}