Source code: | switch1.cpp |
switch1 reads MIDI note messages coming from a synthesizer keyboard and echos to MIDI output another MIDI message which switches the key number and attack velocity parameters from the incoming MIDI note message.
A MIDI note message consists of three byte. The first byte indicates to the synthesizer (or computer) connected to the MIDI cable that a note message is being sent. In hexadecimal notation the first byte would start with the digit "9", the following digit in the byte would indicate which MIDI channel the incoming note is to be played on -- a "0" would indicate channel 1, and a "F" would indicate channel 16. After a note-on MIDI message, a synthesizer would expect exactly two more bytes which serve as parameters to the MIDI note-on message. Parameter byte #1 would be interpreted as the key number to play (60 = middle-C, for example), and Parameter byte #2 would be interpreted as the attack velocity (1 = soft to 127 = very loud). Both of the parameter bytes for the key number and the attack velocity are 7-bit numbers which means that they are in the range from 0 to 127 (or 00 to 7f hexadecimal). An attack velocity of 0 is interpreted as a note-off message.
Here is an example of what the switch1 program does:
Here is the main code from the switch1 program which does the byte swaping:
while (synth.getNoteCount() > 0) { noteMessage = synth.extractNote(); if (noteMessage.p2() == 0) { synth.play(0, lastnote, 0); } else { synth.play(0, noteMessage.p2(), noteMessage.p1()); lastnote = noteMessage.p2(); } }
The algorithm for the above code is:
while there are incoming MIDI note messages coming from the synthesizer: (1) extract a note message from the incoming buffer of notes. (2) if the velocity of the message is 0, then send out an note-off message to the last note sent out by the computer. (3) if the incoming note message velocity is not 0, then switch parameters 1 and 2 of the incoming note message.