Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert MIDI to Java data structure (List, Hash Map, ???)

Tags:

java

midi

I want to grab a MIDI file, read it, and then store the data in some sort of data structure. Using this site I found an easy way to read the file, which works like a charm:

Reading MIDI Files

Now I need to figure out a way to grab that output and store it. A Hash Map doesn't seem ideal since keys need to be unique and a List of type Object doesn't seem great. Any ideas on what my best option might be. I suppose I might output it to a text or csv... Thoughts?

UPDATE: A bit more detail on what I already have.

Here is the output I'm getting (through System.out.println):

@0 Channel: 1 Note on, E5 key=76 velocity: 127
@192 Channel: 1 Note off, E5 key=76 velocity: 64
@192 Channel: 1 Note on, D#5 key=75 velocity: 127
@384 Channel: 1 Note off, D#5 key=75 velocity: 64
@384 Channel: 1 Note on, E5 key=76 velocity: 127

Now I just need to find the best method of storing this information. I should probably be expicit about "why" I'm trying to do this as well. I'm working with another developer who is going to take this data and use Batik (which I know nothing about) to display it on the screen.

Thanks for all the responses... I'll look closely at each of them tonight...

like image 329
user2745258 Avatar asked Oct 04 '22 01:10

user2745258


1 Answers

Reading the MIDI file specifications, I think you can start creating anything like

public class MIDIFile {
    enum FileFormat {
        single_track,
        syncronous_multiple_tracks,
        assyncronous_multiple_tracks;
    }

    FileFormat file_format;
    short numberOfTracks;
    short deltaTimeTicks;

    //Create Class for tracks, events, put some collection for storing the tracks, 
    //some collection for storing the events inside the tracks, etc

    //Collection<Integer, MIDITrack> the type of Collection depends on application

}

public class MIDITrack {
    int length;
    //Collection<MIDIEvent> the type of Collection depends on application
}

public class MIDIEvent {
    int delta_time;
    int event_type;    //Use of enum or final variables is interesting
    int key;
    int velocity;
}

If you only want to store MIDI messages (so not MIDI file), you could do a Class for the messages

public class MIDIEvent {
    int delta_time;
    int channel;
    int event_type;    //Use of enum or final variables is interesting

    //two bytes, interpret according the message type
    byte byte0;
    byte byte1;

    //or more memory consuming
    byte key;
    byte pressure;
    byte controller;
    short bend;
}

The type of Collection you use to store will be application specific, how you want to access the elements of the list, and much more.

If you just want to insert the MIDIMessages in the Collection and then read from the first to last you could use a LinkedList (that's an implementation of List). But if you want to modify the messages and access elements by an index, you would want to use an ArrayList (that's an implementation of List too).

Information of the MIDI file structure from http://faydoc.tripod.com/formats/mid.htm

like image 54
Diego C Nascimento Avatar answered Oct 07 '22 20:10

Diego C Nascimento