/* * midinator.c * * (c) 2012 Thomas White * * This file is part of MIDInator. * * MIDInator is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * MIDInator is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with MIDInator. If not, see . * */ #include #include #include #include #include #include #include #include #include #include #include #include #include "fifo.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" ); } struct wargs { FIFO *f; int fd; }; static unsigned char get_midi(FIFO *f) { do { int err; char c; c = fifo_pop(f, &err); if ( err == 0 ) return c; usleep(1); } while ( 1 ); } static void *process_midi(void *pv) { struct wargs *w = pv; unsigned char c; int ch, cmd; FIFO *f; int fd; f = w->f; fd = w->fd; do { c = get_midi(f); if ( c == 0xfe ) { unsigned char sbuf[32]; sbuf[0] = 0xfe; write(fd, sbuf, 1); continue; } ch = c & 0x0f; cmd = c & 0xf0; if ( cmd == 0x90 ) { unsigned char sbuf[32]; int note, vel; note = get_midi(f); vel = get_midi(f); sbuf[0] = 0x91; sbuf[1] = note - 12; sbuf[2] = vel >> 1; write(fd, sbuf, 3); } /* Control change or mode message */ if ( cmd == 0xb0 ) { int ctrl, data; ctrl = get_midi(f); data = get_midi(f); if ( ctrl == 0x40 ) { unsigned char sbuf[32]; sbuf[0] = 0xb1; sbuf[1] = 0x40; sbuf[2] = data; write(fd, sbuf, 3); } } /* Program change */ if ( cmd == 0xc0 ) { unsigned char sbuf[32]; int prog; prog = get_midi(f); sbuf[0] = 0xc1; sbuf[1] = prog; write(fd, sbuf, 2); } /* Yamaha panel data */ if ( c == 0xf0 ) { unsigned char v; unsigned char buf[32]; int i = 0; printf("Panel data: "); buf[i++] = 0xf0; do { v = get_midi(f); buf[i++] = v; if ( v != 0xf7 ) { printf("%02hhx ", v); } } while ( v != 0xf7 ); printf("\n"); write(fd, buf, i); } } while ( 1 ); return NULL; } static void midi_readable(int fd, FIFO *f) { int rval, i; unsigned char buf[32]; rval = read(fd, buf, 32); if ( (rval == -1) || (rval == 0) ) { fprintf(stderr, "Read error.\n"); return; } for ( i=0; i