Attached is a 'port' of the
MIDI library 3.2
by Francois Best where I've split up the Midi class into a MidiInPort and a MidiOutPort. I haven't included the midi thru functionality but the rest is as much copy-paste as possible.
The MidiOutPort takes a Stream in the constructor thus allowing a much broader range of output devices (not tied to Hardware Serial anymore). The MidiInPort still uses the USE_SERIAL_PORT macro (because there is no virtual serial device base class I couldn't get rid of the call to Serial.begin(MIDI_BAUDRATE)).
Now I can receive midi on the hardware serial and send midi on the software serial and to multiple outputs.
Here is an example:
#include <SoftwareSerial.h>
#include "MidiInPort.h"
#include "MidiOutPort.h"
SoftwareSerial softSerial(2, 4);
MidiInPort midiIn;
MidiOutPort midiOut(&softSerial);
void OnNoteOn(byte channel, byte note, byte velocity)
{
if(velocity == 0)
{
// note off
midiOut.sendNoteOn(channel, note, 0);
}
else
{
velocity = map(velocity, 1, 127, 60, 127);
midiOut.sendNoteOn(note, velocity, channel);
}
}
void OnNoteOff(byte channel, byte note, byte velocity)
{
midiOut.sendNoteOff(note, velocity, channel);
}
void setup()
{
midiIn.setHandleNoteOn(OnNoteOn);
midiIn.setHandleNoteOff(OnNoteOff);
midiIn.begin(MIDI_CHANNEL_OMNI);
softSerial.begin(MIDI_BAUDRATE);
}
void loop()
{
midiIn.read();
}
PS: I could not find any license for the Midi library so I hope this is ok. If not, please delete this posting.