|
Example 1 is not very useful unless you only want to read MIDI input and do nothing else. The line "read(seqfd, &inpacket, sizeof(inpacket));" will not continue until there is some data to be read from MIDI input.
The following figure shows some sample output from the Example 1 program when playing 5 notes on a Yamaha Disklavier hooked up to the MIDI input port on the computer:
|
In case you are not familiar with the MIDI protocol, here are some essentials to know. For the example above, 144 is a note-on command which expected two data bytes to follow. The next byte is the note-on's key number (60 = middle C), and next is the attack velocity (46), which is how hard the note was played. The fourth byte is a data byte since it is less than 128. Therefore the data is now in running status which means apply the last MIDI command (which was 144 to the data, so the key number is again 60. The next byte (0) is the attack velocity which in this case actually represents a note-off. etc.
The MIDI protocol does not specify timing, so the durations of the notes in the example above are undefined for now. Section 2 example programs show how timing is handled with MIDI input.
The standard method for handling MIDI input while doing other things in a program in UNIX is using threads. Threads are used to simulate multiple processes while only using one CPU. Example 2 shows how to incorporate threads into a simple MIDI input reader which has the same functionality as Example 1.
|
The thread functions of interest for MIDI input are: