/* * Programmer: Dominik Schmidt-Philipp * Filename: main.c * * Dedicated to seleomlivet * */ #include #include /* for threading */ // function declarations: void alsa_error (const char *format, ...); void* midiinfunction (void * arg); void midi_in_dispatch (void * arg, unsigned char * m); typedef struct { snd_rawmidi_t* midiin; snd_rawmidi_t* midiout; unsigned char hui_in_zone; } housicIO; int main(int argc, char *argv[]) { int status; int mode = SND_RAWMIDI_SYNC; pthread_t midiinthread; housicIO IOs = {NULL,NULL,0}; // snd_rawmidi_t* midiin = NULL; const char* portname = "virtual"; if ((status = snd_rawmidi_open(&IOs.midiin, NULL, portname, mode)) < 0) { alsa_error("Problem opening MIDI input: %s", snd_strerror(status)); exit(1); } status = pthread_create(&midiinthread, NULL, midiinfunction, &IOs); pthread_join(midiinthread, NULL); //snd_rawmidi_close(midiin); //midiin=NULL; return 0; } ////////////////////////////// // // midiinfunction -- Thread function which waits around until a MIDI // input byte arrives and then react correspondingly // void *midiinfunction(void *arg) { housicIO* IOs = (housicIO*)arg; snd_rawmidi_t* midiin = IOs->midiin; int status; int i = 0; char buffer[3]; unsigned char message[3]; while (1) { if ((status = snd_rawmidi_read(midiin, buffer, 3)) < 0) { alsa_error("Problem reading MIDI input: %s", snd_strerror(status)); } // in case of MIDI running status, value bytes need to not override status byte for (i=1; i<=status;i++) { message[3-i] = (unsigned char) buffer[status-i]; } midi_in_dispatch(IOs,message); fflush(stdout); } } ////////////////////////////// // // midi_in_dispatch -- Thread function which waits around until a MIDI // input byte arrives and then react correspondingly // void midi_in_dispatch(void *arg, unsigned char *m) { housicIO* IOs = (housicIO*)arg; if (m[0] == 0xb0) { // received a CC message on MIDIchannel 1 if (m[1] == 0x0f) { // received a zone select IOs->hui_in_zone = m[2]; } } } ////////////////////////////// // // error -- print error message // void alsa_error(const char *format, ...) { va_list ap; va_start(ap, format); vfprintf(stderr, format, ap); va_end(ap); putc('\n', stderr); }