// // Programmer: Craig Stuart Sapp // Creation Date: Tue Dec 22 20:12:18 PST 1998 // Last Modified: Tue Dec 22 20:12:23 PST 1998 // Filename: insimple.c // Syntax: C; pthread // $Smake: gcc -O3 -Wall -o %b %f -lpthread && strip %b // #include #include #include #include #include /* for MIDI input interpreter thread */ #define MIDI_DEVICE "/dev/sequencer" // global variables: int seqfd; // sequencer file descriptor pthread_t midiInThread; // thread blocker for midi input // function declarations void* threadFunction(void*); // thread function for MIDI input interpreter /////////////////////////////////////////////////////////////////////////// int main(void) { int status; // for error checking // (1) first open the sequencer device for reading. // some examples also have a nonblocking options in 2nd param seqfd = open(MIDI_DEVICE, O_RDONLY); if (seqfd == -1) { printf("Error: cannot open %s\n", MIDI_DEVICE); exit(1); } // (2) now start the thread that interpreting incoming MIDI bytes: status = pthread_create(&midiInThread, NULL, threadFunction, NULL); if (status == -1) { printf("Error: unable to create MIDI input thread.\n"); exit(1); } // (3) finally, just wait around for MIDI messages to appear in the // input buffer and print them to the screen. while (1) { // do nothing, just wait around for thread function. } return 0; } /////////////////////////////////////////////////////////////////////////// ////////////////////////////// // // threadFunction -- this function is sent to the pthread library // functions for running as a separate thread. // void* threadFunction(void* x) { unsigned char inbytes[4]; // bytes from sequencer driver int status; // for error checking while (1) { status = read(seqfd, &inbytes, sizeof(inbytes)); if (status < 0) { printf("Error reading %s\n", MIDI_DEVICE); exit(1); } if (inbytes[0] == SEQ_MIDIPUTC) { printf("received MIDI byte: %d\n", inbytes[1]); } } }