RP2040 bad tempo value reading external MIDI clock

Hi guys,
I'm developing sort of a synth and it needs MIDI input.

I use this library GitHub - FortySevenEffects/arduino_midi_library: MIDI for Arduino that works pretty well on rp2040.

I also use the PIO (platformIO) framework for developing.

I need to monitor the tempo sent by DAWs or external gear via DIN, the MIDI works nicely on the two MIDI inputs (CC messages, notes on, notes off) but when I need to monitor the tempo, I get a constant value of 60.

I think it's because I read the Midi data in the loop and not in an interrupt, but I'm not sure.

My code :
main.cpp

#include <MidiInput.h>

MidiInput midiInput = MidiInput();

void setup(){
  midiInput.configure();
}

void loop(){
    static unsigned long prev = millis();
    unsigned long now = millis();
    if (now - prev > 4)
    {
        midiInput.read())

        prev = now;
    }
}

MidiInput.cpp

#include <MidiInput.h>

#include <Adafruit_TinyUSB.h>
#include <MIDI.h>

using namespace midi;

// USB MIDI_DIN object
Adafruit_USBD_MIDI usb_midi;
// Create a new instance of the Arduino MIDI_DIN Library,
// and attach usb_midi as the transport.
MIDI_CREATE_INSTANCE(Adafruit_USBD_MIDI, usb_midi, MIDI_USB);

MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI_DIN)

void MidiInput::configure()
{
    MIDI_USB.turnThruOff();
    MIDI_DIN.turnThruOff();

    MIDI_USB.begin(midiChannel);
    MIDI_DIN.begin(midiChannel);
}

bool MidiInput::read()
{
    bool beenUpdated = 0;

    if (mode)
    {
        if (MIDI_USB.read())
        {
        }
    }
    else
    {
        if (MIDI_DIN.read())
        {
        }
    }
    switch (mode ? MIDI_USB.getType() : MIDI_DIN.getType())
    {
    case midi::Clock:
    {
        static int ticks = 0;
        static unsigned long lastClock = 0;
        unsigned long clock = millis();

        if (ticks++ == 24)
        {
            unsigned long ppqTime = lastClock - clock;
            sequencer.incrementSeqIndex();

            bpm = 60, 000 / ppqTime;

            lastClock = clock;

            ticks = 0;
        }
    }
    break;
    }
    return beenUpdated;
}

MidiInput.h

class MidiInput
{
public:
    // 0 DIN, 1 USB
    bool mode = false;

    MidiInput();

    int midiChannel = 0;

    unsigned long bpm = 0;

    void configure();
    bool read();
}

C++ uses a decimal point, not a comma.

Google for "comma operator C++".

if (now - prev > 4)

Why?

C++ uses a decimal point, not a comma.

Oops you are right, it's better with 60'000

Why?

Because I have several other processing and it's responding better with this little delay, maybe I'm wrong to do this ?

With this code

case midi::Clock:
    {
        static int ticks = 0;
        static unsigned long lastClock = 0;
        unsigned long clock = millis();

        if (ticks++ >= 24)
        {
            unsigned long noteTime = lastClock - clock;

            bpm = 60000 / noteTime;

            Serial.println(noteTime);

            lastClock = clock;
            ticks = 0;

            sequencer.incrementSeqIndex();
        }
    }
}

I got a result noteTime(ppq) of
4294966875

It doesn't seems right

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.