Audio callback blocks midi processing (via callback)?

In the arduino IDE I wrote a patch for Daisy Seed in which I set some midi call backs in the setup() routine:

MIDI.setHandleNoteOn(handleNoteOn);
MIDI.setHandleNoteOff(handleNoteOff);
MIDI.setHandleControlChange(handleControlChange);
MIDI.begin(MIDI_CHANNEL_OMNI); 

For test purposes in handleMidiOn() I switch on an LED which I switch off in handleNoteOff(). As soon as I start sending midi notes to the Daisy, the led starts to flash according to the notes I send. Nice!

However, when I add a callback for audio processing to setup(),

DAISY.begin(AudioCallback);

the midi notes are not processed anymore, or at least I do not see the LEDs flashing (the audio is processed though). Is this to be expected or do I have to handle the MIDI.begin and DAISY.begin differently?

If I change the sequence of commands, so start the audio callback before the midi callback, then only the audio callback is running. Do I need to chain them in some sort of way?

I would suggest that for questions like this, it is best to post a complete program demonstrating your problem. Reduce the program to the minimum needed.

In fact, on the Teensy forum, this is a strict rule. It saves a lot of unnecessary back and forth dialog.

The program is much too large (and has a lot of code files that need to be linked) to be posted even in a reduced size. Therefore I ended with the question whether the callbacks need to be chained or not? Basically I’m wondering how the callback mechanism works. E.g. what will happen if the AudioCallBack takes too long to do its thing between consecutive samples / buffers. Will it be interrupted by the next start of the callback routine? And does the same go for the midi call back routine?

I notice the DaisyExamples/pod/midi/Midi.cpp doesn’t use a callback for MIDI.

Are you using a generic Arduino MIDI library?

The code is based on the Pod midi example. I’m not sure which midi library the arduino ide links to the code.

[Edit] This is what the IDE says: “Using library MIDI at version 4.3.1 in folder: /home/jos/projects/arduino/libraries/MIDI”
The MIDI.h says:

  • @file MIDI.h
  • Project Arduino MIDI Library
  • @brief MIDI Library for the Arduino
  • @author Francois Best
  • @date 24/02/11
  • @license MIT - Copyright © 2015 Francois Best

Interesting. That example also doesn’t use a MIDI callback.

Well, it says:

MIDI.setHandleNoteOn(handleNoteOn);
MIDI.begin(MIDI_CHANNEL_OMNI);

which I think together work as a callback similar to:

DAISY.begin(AudioCallback);

Sorry, I misread it, my mistake.

I’ll update Daisyduino and try to figure it out.

Now I’m more confused. DaisyDuino/examples/Pod/Midi/Midi.ino works for me, and it uses Audio callback , and MIDI.setHandleNoteOn(handleNoteOn). Then I added a call to set a NoteOff handler, and it works correctly.

So I’m back to wondering, what is different in your program? Are you calling MIDI.read() in the loop()?

Yes, I am. But I’m getting a feeling that my AudioCallback routine takes up too much time.
I have the same processing code without the midi part running just fine when I compile it without the Arduino IDE using a Makefile, so I’m wondering whether compiling in the IDE produces code that runs less efficient.

The code is not exactly the same as I’m having trouble adding the MIDI.h to the Makefile code and I am not using the “DaisyDuino.h” because I do not seem to need it. So I’ve not been able to run the midi code using the Makefile setup. But the audio processing works well and is free of distortion when I’m running that code.