Search code examples
javacsvmidijfugue

JFugue to parse midi file and output .csv?


I want to convert a midi file to a .csv representation like this:

Notename, startTick, duration, velocity

also including entire chords(sets of notes starting at the same tick).

Now, JFugue has a MidiParser class which looks like it might be able to do that. My own implementation is a bit crude and doesn't detect note off events or breaks or chords.

So, is there functionality in JFugue that can help me accomplish this?


Solution

  • Create your own ParserListener (maybe a "CsvParserListener"), add it as a listener to the MidiParser, and have your CsvParserListener know how to create your CSV file.

    ParserListener is an interface in JFugue. You can also extend ParserListenerAdapater instead of implementing ParserListener.

    There is also a second way. You can also create a class that implements AuxilliaryMidiParser and overrides parseHandledEvent(MidiEvent e). You'll get direct access to the MIDI Event, which might work better for you because you'll have easy access to the tick when a note is played. Add your AuxilliaryMidiParser to a regular MidiParser, then call midiParser.parse(File), and finally a method on your auxiliary parser that you write to save the CSV file.