[MIDI] How to use the library.

Hello,

I have found sometime ago a MIDI library working fine, i used it like this:

#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();

Unfortunately, I don’t remember much more… Does somebody knows were it is located on the disk? I failed to find it, I would like to read it to know how to use it. If somebody knows something about a documentation, it will be much appreciated.

Regards,

Pfeuh

I think this is the source code and docs for the MIDI library.

Thanks a lot! That's it! Much appreciated!

Hello,

I have a problem using the library, due I think to the few cpp knowledge that I have. After defining an interface with MIDI_CREATE_INSTANCE I use output methods like sendNoteOn and sendNoteOff, it works fine. But methods like sendRealTime use an enum as parameter. It is defined in midi_Defs.h:

/*! Enumeration of MIDI types */
enum MidiType
{
    InvalidType           = 0x00,    ///< For notifying errors
    NoteOff               = 0x80,    ///< Note Off
    NoteOn                = 0x90,    ///< Note On
    AfterTouchPoly        = 0xA0,    ///< Polyphonic AfterTouch
    ControlChange         = 0xB0,    ///< Control Change / Channel Mode
    ProgramChange         = 0xC0,    ///< Program Change
    AfterTouchChannel     = 0xD0,    ///< Channel (monophonic) AfterTouch
    PitchBend             = 0xE0,    ///< Pitch Bend
    SystemExclusive       = 0xF0,    ///< System Exclusive
    TimeCodeQuarterFrame  = 0xF1,    ///< System Common - MIDI Time Code Quarter Frame
    SongPosition          = 0xF2,    ///< System Common - Song Position Pointer
    SongSelect            = 0xF3,    ///< System Common - Song Select
    TuneRequest           = 0xF6,    ///< System Common - Tune Request
    Clock                 = 0xF8,    ///< System Real Time - Timing Clock
    Start                 = 0xFA,    ///< System Real Time - Start
    Continue              = 0xFB,    ///< System Real Time - Continue
    Stop                  = 0xFC,    ///< System Real Time - Stop
    ActiveSensing         = 0xFE,    ///< System Real Time - Active Sensing
    SystemReset           = 0xFF,    ///< System Real Time - System Reset
};

Does somebody know how to use it? This doesn’t work:

MIDI.sendRealTime(0xff)

I have now a sample code to show the problem I have. It concerns the method sendRealTime of MIDI library. This code works but generates a warning. By the way I don"t think that the developer wrote some enums only for its private usage.

#include <MIDI.h>

/* MIDI INTERFACE */
MIDI_CREATE_INSTANCE(HardwareSerial, Serial, MIDI);

#define TICK_DURATION 500

bool ledState;
unsigned long milestone;

void tickSequencer()
{
    midi::MidiType value = 0xfe;
    MIDI.sendRealTime(value);

    milestone += TICK_DURATION;
    ledState ^= 1;
    digitalWrite(LED_BUILTIN, ledState);
}

void setup()
{
    MIDI.begin(MIDI_CHANNEL_OMNI);
    milestone = millis();   
    pinMode(LED_BUILTIN, OUTPUT);
}

void loop()
{
    MIDI.read();
    
    if(millis() >= milestone)
        tickSequencer();
}

I understand the warning, but I dont understand how to correctly use the MidiType enun. A little bit of help should be appreciated. Here are some failed tries:

MIDI.sendRealTime(MIDI::MidiType.ActiveSensing);
MIDI.sendRealTime(midi::MidiType.ActiveSensing);
MIDI.sendRealTime(MidiType.ActiveSensing);
MIDI.sendRealTime(MIDI.MidiType.ActiveSensing);
MIDI.sendRealTime(MIDI.MidiType.ActiveSensing);
MIDI.sendRealTime(MIDI::MidiType->ActiveSensing);
MIDI.sendRealTime(midi::MidiType->ActiveSensing);
MIDI.sendRealTime(MidiType->ActiveSensing);
MIDI.sendRealTime(MIDI.MidiType->ActiveSensing);
MIDI.sendRealTime(MIDI.MidiType->ActiveSensing);
MIDI.sendRealTime(midi::MidiType::ActiveSensing);
MIDI.sendRealTime(midi::ActiveSensing);

Main takeaway: use an IDE that automatically completes what you type. I use Visual Studio Code with the C/C++ extension, and it works great. You can also use PlatformIO and abandon the Arduino "IDE" entirely.

Pieter

Yes, it could be a workaround... But I like to understand what I write. :wink:

I think using auto completion actually helps understanding what you write.
It goes looking for the definitions and declarations for you, so you don’t have to go look up the source code on GitHub or on some folder on your computer.
When you hover over a piece of code in VSCode, it shows information about the function, and it allows you to click through to the source file for it.

It is a point of view. I wrote my first assembler program for 6502's asm in the early 80's. When I say I wrote, I wrote... All instructions on a paper (a 2ko binary ), then translate addresses, mnemonics and data in a byte list, and program this list as data for a basic program.

On the other hand, I've worked professionally (I'm in retirement now) ten years every day with eclipse for an arm32, I'm aware about auto completion and programs that work for you. I've seen in my team good developpers, excellent for clicking, but they understand nothing about a dummy binary operation, how the stack is working or even what job is behind a #include. For me, auto completion solves some small problems just in time, but on years, it makes more bad that good.

I understand that autocompletion it is the easiest answer to give. But I just want to DIY a little bit ans understand what I do. Installing eclipse or VS and fighting with libraries and configurations should not be mandatory to do that. Maybe I'm wrong and the documentation and examples for MIDI library are just perfect for an experienced cpp programmer... That doityourselfers are not.

pfeuh:
Maybe I'm wrong and the documentation and examples for MIDI library are just perfect for an experienced cpp programmer... That doityourselfers are not.

The examples and documentation of most Arduino libraries (including, and especially the core libraries) are useless.
When I need an answer, I open the source code.
What I mean is that it's easier to have the IDE open the source files for you, instead of having to find the files yourself.

I agree with you that using only autocomplete isn't always a good thing, and that understanding the source code is important.
I just think that a good IDE makes it easier to take a look at the source code.
When you type a function, you think to yourself "I wonder if this function does what I want", you CTRL+Click the function name, and the IDE opens up its implementation.
Without the IDE, you might have to go search for it on GitHub or in your libraries folder, which is more work, and might discourage many people, so they don't look at the source code at all.

But that's just my point of view :slight_smile:

PieterP:
When you type a function, you think to yourself "I wonder if this function does what I want"

In this case, it's different. I know exactly what the fonction does and what parameter I have to give. It's only a question of cpp knowledge that I haven't. Perhaps this forum is not the good place for helping on this kind of question.

The function is public but inline. Perhaps it has not been tested and not been used for now? I have use this fonction in some other contexts and other midi libraries, but never with an enum typedef, this blocks me.

I have found the right syntax to use inside a sketch, it's
MIDI.sendRealTime(midi::ActiveSensing)

And why the hell is the second MIDI in lowercase? Because the enum is defined in a namespace included with the file midi_Namespace.h
I've found this define:
#define MIDI_NAMESPACE midi

I think that is definitely out of reach for a doityourselfer, the target of this forum should be to help on this kind of problem. I've find by myself without installing VS or Eclipse, I guess this topic can help now some beginners.

the MIDI library is a good one and musicians like to use it as arduino beginners. But this kind of problem can definitely stop a project.