Here is a simple program which will print any MIDI messages it receives, and then echo any note-on/off messages with a transposition of 7 half-steps:
#include "improv.h" MidiIO midi; int main(void) { midi.setInputPort(1); midi.setOutputPort(1); midi.open(); MidiMessage message; while (1) { if (midi.getCount() > 0) { message = midi.extract(); cout << "Message received: " << "command byte: " << (int)message[0] << " p1 byte: " << (int)message[1] << " p2 byte: " << (int)message[2] << endl; if (message.p0() == 144) { midi.rawsend(144, message.p1() + 7, message.p2()); } } } return 0; }
In the example program above, the input/output ports were hardwired to 1. You may need to change this, or add code to select the correct I/O ports.
improv.h is used to include all possible header files for the improv library. You could also use the following includes:
#include "MidiIO.h" #include "MidiMessage.h"
also, you can put the event loop to sleep for a millisecond or so since MIDI input will not come much faster than that. You can use the sleep() function for this, or there is an Improv class called Idler which is non-os specific:
#include "improv.h" MidiIO midi; int main(void) { Idler idler(1); midi.setInputPort(1); midi.setOutputPort(1); midi.open(); MidiMessage message; while (1) { while (midi.getCount() > 0) { message = midi.extract(); cout << "Message received: " << "command byte: " << (int)message[0] << " p1 byte: " << (int)message[1] << " p2 byte: " << (int)message[2] << endl; if (message.p0() == 144) { midi.rawsend(144, message.p1() + 7, message.p2()); } } if (message.p0() == 144 && message.p1() == 21) { break; } idler.sleep(); } return 0; }