Parse midi files

Hi,
Do you know how to read/parse a midi file in processing 3?
There was some librairies but to old to be use in processing 3, or I find some Java code to play a midi file “as it is”, but what I want to do is to play each event (notes or chords) one after the other when I pressed a key
Any clues?
Thanks,
A.

1 Like

Hey,

not sure if there is a library that does exactly what you need, but there is the javax.sound.midi package, that you can use to access the data from midi-files. Maybe using a sequencer would be better but here is an example that parses (one track of) a midi file and logs the note-on and note-off events.

import javax.sound.midi.*;

void setup() {
  String path = dataPath("some_midi_file.mid");
  File midiFile = new File(path);
  try {
    Sequence seq = MidiSystem.getSequence(midiFile);
    Track[] tracks = seq.getTracks();

    // how many tracks are there
    println("number of tracks: "+ tracks.length);

    // parse first track
    println("events of 1st track:");
    Track myTrack = tracks[0];
    for (int j =0; j< myTrack.size(); j++) {
      // get midi-message for every event
      if (myTrack.get(j).getMessage() instanceof ShortMessage) {
        ShortMessage m =  (ShortMessage) myTrack.get(j).getMessage();

        // log note-on or note-off events
        int cmd = m.getCommand();
        if (cmd == ShortMessage.NOTE_OFF || cmd == ShortMessage.NOTE_ON) {
          print( (cmd==ShortMessage.NOTE_ON ? "NOTE_ON" : "NOTE_OFF") + "; ");
          print("channel: " + m.getChannel() + "; ");
          print("note: " + m.getData1() + "; ");
          println("velocity: " + m.getData2());  
        }
      }
    }
    println();
  }

  catch(Exception e) {
    e.printStackTrace();
    exit();
  }
}

Maybe that helps as a starting point.

2 Likes

As another option, perhaps try manually installing TheMIDIBus (which is mainly a wrapper for a subset of javax.sound.midi, see http://www.smallbutdigital.com/projects/themidibus/) Even if it isn’t currently listed in Contributions Manager, I believe (?) it can be manually installed and works with Processing 3.

I did this some time ago to visualize midi tracks:

I wonder if it works still.

1 Like

Hi hamoid,I have tried your code and it didn’t work. Please explain how to use the glsl part, if possible just paste how to use the code in Processing.You can also paste an image of the glsl part for clarity.

thanks

Chigozie

Hi @Chigoz, I just tried and in my system it works fine. If you want to get rid of the shader part, delete the 4 lines that mention “shader” and uncomment the background() call.

In which way did it fail to work for you? Do you hear the classical music playing? What OS? Did you download the data/ folder containing the shader program and the midi file?

1 Like

I eventually ran the program but I didn’t hear the sound nor see the shader part. I am using Windows 10 64bits. I wish you can do a video and upload to the internet so I can see your results. I’m a newbie at programming,I do not understand the error messages.

Maybe someone else on Windows could try the program? Maybe the lack of sound is related to this?
https://www.oracle.com/technetwork/java/soundbanks-135798.html

I’ll make a video when I have some free time :slight_smile:

try on
++ win 10 / 64 bit /
++ processing 3.5.3
get sound and fullscreen show


and that are?

2 Likes