2

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?

1 Answers1

1

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.

David Koelle
  • 20,726
  • 23
  • 93
  • 130
  • This is exactly what I was looking for, but I can't get started. I have a CSVParserListener with all the method stubs. I have added my listener to MidiParser. How do I execute the whole thing and what the methods are supposed to do (apart from what the javadoc says)? Is there maybe a class that accomplishes a similar goal? There are several implementors but some of them have rotten(MusicFileRenderer and MusicXMLListener). – Rafael Bachmann Jun 11 '16 at 13:17
  • Now, call the parse(File) method in MidiParser. The parser will fire events to your listener. Finally, make sure you have something like a getCsv() or saveCsv(File) method in your CSVParserListener, and call that method just after you call midiParser.parse(File). – David Koelle Jun 13 '16 at 04:19
  • Thanks, you really helped me out. It works reasonably fine. I chose to go with the first method you suggested. I might try the second one though, since now chords aren't shown as such. They are simply parsed as separate notes, and I think there is no information about the 'start time' of a note so it is impossible to just piece a chord back together. – Rafael Bachmann Jun 13 '16 at 16:21
  • There is start time of a note in the MidiEvent that you can get to through an AuxilliaryMidiParser, But yes, chords are hard to reconstruct from MIDI because: 1) The notes might not be sent at exactly the same time, particularly if the MIDI was played by hand; 2) Notes could start or stop at any time, so if one of three notes in a chord stops early, do you still have a chord?; 3) Notes played at the same time might not comprise a known chord – David Koelle Jun 13 '16 at 18:14
  • My implementation assumes there is a chord when notes are playing simultaneously. Even just 2 notes or a chord for which the name is unknown. – Rafael Bachmann Jun 14 '16 at 19:23