I’m trying to read in .MID files to a Java program, and would like to separate each note/chord so as to display them on a UI of some sort. I didn’t have much luck using the Sequencer API in Java, and trying to use MidiFileReader directly didn’t work for me either. I’ll attach the code I used here, if anyone wants to see it:
package miditest;
import java.io.File;
import java.io.IOException;
import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
public class Main {
public static void main(String[] args) throws InvalidMidiDataException, IOException, MidiUnavailableException{
Sequence sequence = MidiSystem.getSequence(new File("test.mid"));
// Create a sequencer for the sequence
Sequencer sequencer = MidiSystem.getSequencer();
sequencer.open();
sequencer.setSequence(sequence);
// Start playing
sequencer.start();
}
}
I’ve never looked deeply into the MIDI support in Java, and the last time I played seriously with MIDI programming was when Commodore Amiga was king.
It looks like you may have to do quite a bit of manual work. Here’s a crude example that interprets all NOTE_ON and NOTE_OFF events, for the rest of the events it just prints the command number.
The reason it might seem trickier than one might have thought at first is because MIDI focuses on capturing the instrument events (for example, when a keyboard key was pressed, when it was released, etc), and not on sheet music notation.
This code prints out one line per event, stating with the tick (which is the timing information for the event), channel, event type, note name, key, velocity
For example the fur elise.mid I had lying around here produces something like this at the beginning:
UPDATE:
The channels are the 16 channels of the MIDI specification.
http://www.midi.org/techspecs/gm.php
And velocity is one of the attributes used to control the sounds. For example, capturing MIDI data on a keyboard it’d be the force with which you press a key. Normally it controls the volume of the sound. See here for more details: http://audio.tutsplus.com/tutorials/production/7-ways-to-use-and-edit-midi-velocity/