Skip to content

Using custom Settings

lathoub edited this page Jan 11, 2022 · 4 revisions

Using custom Settings

Default Settings

The library is shipped with the following default Settings (found in src/midi_Settings.h):

struct DefaultSettings
{
    /*! Running status enables short messages when sending multiple values
    of the same type and channel.\n
    Warning: does not work with some hardware, enable with caution.
    */
    static const bool UseRunningStatus = false;

    /*! NoteOn with 0 velocity should be handled as NoteOff.\n
    Set to true  to get NoteOff events when receiving null-velocity NoteOn messages.\n
    Set to false to get NoteOn  events when receiving null-velocity NoteOn messages.
    */
    static const bool HandleNullVelocityNoteOnAsNoteOff = true;

    /*! Setting this to true will make MIDI.read parse only one byte of data for each
    call when data is available. This can speed up your application if receiving
    a lot of traffic, but might induce MIDI Thru and treatment latency.
    */
    static const bool Use1ByteParsing = true;

    /*! Maximum size of SysEx receivable. Decrease to save RAM if you don't expect
    to receive SysEx, or adjust accordingly.
    */
    static const unsigned SysExMaxSize = 128;
};

Overriding Settings

Rather than changing the defaults in the library source files, you can override them locally in your sketch with your own Settings, like this:

struct MySettings : public midi::DefaultSettings
{
    static const unsigned SysExMaxSize = 1024; // Accept SysEx messages up to 1024 bytes long.
    static const bool UseRunningStatus = true; // My devices seem to be ok with it.
};

// Create a 'MIDI' object using MySettings bound to Serial2.
MIDI_CREATE_CUSTOM_INSTANCE(HardwareSerial, Serial2, MIDI, MySettings);

void setup()
{
    MIDI.begin();
}
void loop()
{
    MIDI.read();
}

Overriding the Default Serial Settings

When going Hairless (or just want to change the baudrate), override the DefaultSerialSettings. Due to the use of C++ macros, the easy to use MIDI_CREATE_DEFAULT_INSTANCE(); is replaced with more verbose declarations (exposing the great modularity of the library, allowing it to have multiple transport mechanisms that have extended the MIDI protocols over the years (USB, BLE, RTP,...))

struct CustomBaudRateSettings : public MIDI_NAMESPACE::DefaultSerialSettings {
  static const long BaudRate = 115200;
};

#if defined(ARDUINO_SAM_DUE) || defined(USBCON) || defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MKL26Z64__)
    // Leonardo, Due and other USB boards use Serial1 by default.
    MIDI_NAMESPACE::SerialMIDI<HardwareSerial, CustomBaudRateSettings> serialMIDI(Serial1);
    MIDI_NAMESPACE::MidiInterface<MIDI_NAMESPACE::SerialMIDI<HardwareSerial, CustomBaudRateSettings>> MIDI((MIDI_NAMESPACE::SerialMIDI<HardwareSerial, CustomBaudRateSettings>&)serialMIDI);
#else
    MIDI_NAMESPACE::SerialMIDI<HardwareSerial, CustomBaudRateSettings> serialMIDI(Serial);
    MIDI_NAMESPACE::MidiInterface<MIDI_NAMESPACE::SerialMIDI<HardwareSerial, CustomBaudRateSettings>> MIDI((MIDI_NAMESPACE::SerialMIDI<HardwareSerial, CustomBaudRateSettings>&)serialMIDI);
#endif

void setup()
{
    MIDI.begin();
}
void loop()
{
    MIDI.read();
}