How to loop midi file

Hi,

I want to create a plugin, that can load a midi file and play it in a loop. Based on the example of @IvanC Playing a midi file (revisited), I am currently able to play a single track of a midi file. But, it is only played once. What do I have to do, that the midi data is processed again, once it reached the end?

The way to do that is to first read the whole midi file, and store the midi events in datastructures of your own. Instead of fetching next events in the file, you iterate over the events in that data structure. And when you’re at the end, you reset the iterator to the start of the midi clip.

I have an open source plugin that does that: https://github.com/tomto66/Topiary-Beatz

Thanks @tomto66 for the sample. If I understood it correctly, everything that effects the midi output has to happen in the processBlock() method. This method is called repeatedly by the framework.

What I do not understand is: Everything that happens in that method is let’s say executed 50 times per second. This also means that the midiMessage buffer is written 50 times a second, collecting the data from my data structure (a MidiMessageSequence that contains the file content). But how does it work to play the midi notes smoothly in time?

Is the calculated result of the first execution of processBlock() passed to another thread that takes care to “perform” the midi data and ignores new input from processBlock() while running?

If this is the case, why does it not take the data again from processBlock() that is produced 50 times a second once it has finished?

Is the calculated result of the first execution of processBlock() passed to another thread that takes care to “perform” the midi data and ignores new input from processBlock() while running?

Still haven’t touched MIDI stuff, but AFAIK everything (both audio & midi) is processed in the audio thread which can’t be blocked or can’t wait for events to happen, it just runs an you must make sure that everything you put in it is fast enoguh to end before the next block is called to be processed. So you don’t wanna use locks, waits, memory allocation during processBlock() or having a shitton of processing to the point of the CPU not being able to handle it in time.
But when it comes to a midi file/audio file, yes you can use a background thread to load them and use as you wish (i.e storing midi/audio stuff from the file in a buffer) while the audio thread does it’s own stuff (so you can be loading a sound/midi file while other sounds are playing instead of stopping the audio). Then you just operate with that buffer/data structure you filled from the fille in the audio thread. For instance, you got an example about this in the tutorials.

OK, thanks first of all for your support! You keep me ongoing even if it stuck.

After some more investigation, it looks like I already did it right regarding fetching the events from a “second” source and then put it in the “real” buffer.
The second thing I experienced is that the playHead continues but I need to reset the start time for the midi events so it starts over again. This works now as well.
But starting from the second loop, the sound coming from the synth chained after the plugin is not smooth anymore but like pizzicato. This keeps on for all the following loops. Do you have any idea, what the reason could be for that?
If I observe the midi events with the with a midi logger in MainStage, it says “…console bandwidth exceeded, thinning some traces…”. Don’t know, if this gives a helpful hint.

You mean the first loop plays it well, but the second iteration/loop you play that file it sounds weird? Instead of debugging it in MainStage you could add a little component in your plugin showing the midi events in text (you got an example in the tutorials, the MIDI one I believe), or just putting them in the console with DBG messages so you can check what’s going on and how the midi messages of the next loop differ compared to the first one (if they do).

Anyway post some code so we can see what’s going on in your loop, it’s hard to guess without it since it could be many things.

Instead of debugging it in MainStage you could add a little component in your plugin showing the midi events in text …

Ah, good point. The reason, why I test it in MainStage (what is very annoying and time-consuming) is that the JUCE AudioPluginHost does not provide an AudioPlayHead that can be started to get the playTime. If you know a better solution for how to test a plugin and how I can use the debug console in XCode it would be great!

Anyway post some code so we can see what’s going on in your loop, it’s hard to guess without it since it could be many things.

I will try two more things and if this will not be successful, I will post some code.

I’m not doing a plugin but afaik, you can run it in Xcode as if it was an standalone app and test the basic functionalities that don’t require DAW related stuff, and even then you could writte a mockup function to test those (i.e instead of waiting for an external instrument to send you MIDI to see what happens, just write a bunch of midi messages by hand to see if the other parts of your plugin work).

Said so, you can either use break points and debug it, where you will see the call trace and the value of all the variables, or what I mentioned before. In Xcode for things like arrays I usually use DBG messages (but std::cout works too) to show the values which makes it more visual than debugging step by step. Is there a much more efficient/better method? Probably, maybe someone else can enlighten us.

OK, I see. I guess it makes sense to write a wrapper to finally save time and get improved debugging capabilities. Before I needed the AudioPlayHead, I used std::cout to see what’s going on. To get some more information from debugging in MainStage, I created a log file and surprise, I could figure out what the problem was with the pizzicato notes. There was a wrong variable in an if-statement that forced a sendAllNotesOff and therefore cut my notes.
Now only the first note is still missing starting from the second loop but I am quite optimistic to figure that out as well.