Playing a MIDI file (revisited)

When I was a total JUCE noob, back in 2005, I created a topic to get some information about MIDI file playing :

At the end of that topic, I shared a quite bad source code example in a zip file containing something that didn’t really work as expected but might give some insight to other people who want to play a MIDI file in an app as well.

The funny thing about this topic is that, over the last 13 years (wow I got some new white hair just by typing this number) I had I don’t know how many requests to make that sample code available again !

So, I created a new project and tried to come up quickly with something not too bad to replace that sample code and to stop finally that loooooong chain of requests :slight_smile:

PluginEditor.cpp (2.6 KB)
PluginEditor.h (1.8 KB)
PluginProcessor.cpp (9.6 KB)
PluginProcessor.h (4.0 KB)

That sample code is for a plug-in project. It allows the user to load a MIDI file from the hard drive, to select a track among the available ones, and then playing the MIDI file track content for any virtual instrument put after in the DAW at the original tempo, when playing is enabled in the transport bar. That code can probably be improved a lot, but at least it does what it is supposed to do, and provides some additional “note off events” when necessary.

Have fun !

21 Likes

Just wanted to say thank you !

2 Likes

Whoahow!

It’s back!!! Awesome div by zero!!! thancc.

Hmmm, why it only plays 1 track though? MIDI type 1 has multiple track. Ah alright, lemme fix it. … hang in there… there it is!

tada! I have improved it. Now with checking Entire Tracks we can play all tracks in that MIDI file yay!!!

Uh, um still need help though. idk how to make playhead override.

wow, 4 years later.

One of the most useful post for sure

1 Like

The browser does not launch for me when I click ‘Load midi file’. I made the following changes for new JUCE version :

std::unique_ptr<juce::FileChooser> theFileChooser=std::make_unique<juce::FileChooser> ("Select a Wave file to play...",
                                                       juce::File{},
                                                       "*.mid");
        auto chooserFlags = juce::FileBrowserComponent::openMode |
        juce::FileBrowserComponent::canSelectFiles; 
        
        theFileChooser->launchAsync (chooserFlags, [this] (const juce::FileChooser& fc)           // [8]
        {
            auto file = fc.getResult();
         
            processor.loadMIDIFile(file);

in the buttonClicked listener.

I’m just getting started with JUCE. Mostly interested in MIDI stuff so really interested by this code! Just wondered: what is the processLock needed for?