Reading MIDI files in Java












30















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();
}
}









share|improve this question

























  • Have you searched for MIDI on SO? There are quite a lot of questions and answers about it. Example: stackoverflow.com/questions/1182877/reading-midi-files the first answer

    – extraneon
    Oct 3 '10 at 17:06


















30















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();
}
}









share|improve this question

























  • Have you searched for MIDI on SO? There are quite a lot of questions and answers about it. Example: stackoverflow.com/questions/1182877/reading-midi-files the first answer

    – extraneon
    Oct 3 '10 at 17:06
















30












30








30


17






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();
}
}









share|improve this question
















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();
}
}






java audio midi javasound






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 29 '18 at 8:24









Andrew Thompson

153k27163339




153k27163339










asked Oct 3 '10 at 17:02









Hassan KhanHassan Khan

3582719




3582719













  • Have you searched for MIDI on SO? There are quite a lot of questions and answers about it. Example: stackoverflow.com/questions/1182877/reading-midi-files the first answer

    – extraneon
    Oct 3 '10 at 17:06





















  • Have you searched for MIDI on SO? There are quite a lot of questions and answers about it. Example: stackoverflow.com/questions/1182877/reading-midi-files the first answer

    – extraneon
    Oct 3 '10 at 17:06



















Have you searched for MIDI on SO? There are quite a lot of questions and answers about it. Example: stackoverflow.com/questions/1182877/reading-midi-files the first answer

– extraneon
Oct 3 '10 at 17:06







Have you searched for MIDI on SO? There are quite a lot of questions and answers about it. Example: stackoverflow.com/questions/1182877/reading-midi-files the first answer

– extraneon
Oct 3 '10 at 17:06














1 Answer
1






active

oldest

votes


















45














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



import java.io.File;

import javax.sound.midi.MidiEvent;
import javax.sound.midi.MidiMessage;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequence;
import javax.sound.midi.ShortMessage;
import javax.sound.midi.Track;

public class Test {
public static final int NOTE_ON = 0x90;
public static final int NOTE_OFF = 0x80;
public static final String NOTE_NAMES = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};

public static void main(String args) throws Exception {
Sequence sequence = MidiSystem.getSequence(new File("test.mid"));

int trackNumber = 0;
for (Track track : sequence.getTracks()) {
trackNumber++;
System.out.println("Track " + trackNumber + ": size = " + track.size());
System.out.println();
for (int i=0; i < track.size(); i++) {
MidiEvent event = track.get(i);
System.out.print("@" + event.getTick() + " ");
MidiMessage message = event.getMessage();
if (message instanceof ShortMessage) {
ShortMessage sm = (ShortMessage) message;
System.out.print("Channel: " + sm.getChannel() + " ");
if (sm.getCommand() == NOTE_ON) {
int key = sm.getData1();
int octave = (key / 12)-1;
int note = key % 12;
String noteName = NOTE_NAMES[note];
int velocity = sm.getData2();
System.out.println("Note on, " + noteName + octave + " key=" + key + " velocity: " + velocity);
} else if (sm.getCommand() == NOTE_OFF) {
int key = sm.getData1();
int octave = (key / 12)-1;
int note = key % 12;
String noteName = NOTE_NAMES[note];
int velocity = sm.getData2();
System.out.println("Note off, " + noteName + octave + " key=" + key + " velocity: " + velocity);
} else {
System.out.println("Command:" + sm.getCommand());
}
} else {
System.out.println("Other message: " + message.getClass());
}
}

System.out.println();
}

}
}


For example the fur elise.mid I had lying around here produces something like this at the beginning:



@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
@576 Channel: 1 Note off, E5 key=76 velocity: 64
@576 Channel: 1 Note on, D#5 key=75 velocity: 127
@768 Channel: 1 Note off, D#5 key=75 velocity: 64
@768 Channel: 1 Note on, E5 key=76 velocity: 127
@960 Channel: 1 Note off, E5 key=76 velocity: 64
@960 Channel: 1 Note on, B4 key=71 velocity: 127
@1152 Channel: 1 Note off, B4 key=71 velocity: 64
@1152 Channel: 1 Note on, D5 key=74 velocity: 127
@1344 Channel: 1 Note off, D5 key=74 velocity: 64
@1344 Channel: 1 Note on, C5 key=72 velocity: 127
@1536 Channel: 1 Note off, C5 key=72 velocity: 64
@1536 Channel: 1 Note on, A4 key=69 velocity: 127
@1920 Channel: 1 Note off, A4 key=69 velocity: 64


UPDATE:
The channels are the 16 channels of the MIDI specification.



http://www.midi.org/techspecs/gm.php




Channels: All 16 MIDI Channels are
supported. Each Channel can play a
variable number of voices (polyphony).
Each Channel can play a different
instrument (sound/patch/timbre).
Key-based percussion is always on MIDI
Channel 10.




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/






share|improve this answer


























  • I had another question, the tick is the time at which the sound plays, but what does the Channel represent? And velocity?

    – Hassan Khan
    Oct 3 '10 at 20:07











  • See the updates on my original answer.

    – Sami Koivu
    Oct 3 '10 at 21:54











  • Thank you very much for your help.

    – Hassan Khan
    Oct 3 '10 at 22:00











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3850688%2freading-midi-files-in-java%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









45














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



import java.io.File;

import javax.sound.midi.MidiEvent;
import javax.sound.midi.MidiMessage;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequence;
import javax.sound.midi.ShortMessage;
import javax.sound.midi.Track;

public class Test {
public static final int NOTE_ON = 0x90;
public static final int NOTE_OFF = 0x80;
public static final String NOTE_NAMES = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};

public static void main(String args) throws Exception {
Sequence sequence = MidiSystem.getSequence(new File("test.mid"));

int trackNumber = 0;
for (Track track : sequence.getTracks()) {
trackNumber++;
System.out.println("Track " + trackNumber + ": size = " + track.size());
System.out.println();
for (int i=0; i < track.size(); i++) {
MidiEvent event = track.get(i);
System.out.print("@" + event.getTick() + " ");
MidiMessage message = event.getMessage();
if (message instanceof ShortMessage) {
ShortMessage sm = (ShortMessage) message;
System.out.print("Channel: " + sm.getChannel() + " ");
if (sm.getCommand() == NOTE_ON) {
int key = sm.getData1();
int octave = (key / 12)-1;
int note = key % 12;
String noteName = NOTE_NAMES[note];
int velocity = sm.getData2();
System.out.println("Note on, " + noteName + octave + " key=" + key + " velocity: " + velocity);
} else if (sm.getCommand() == NOTE_OFF) {
int key = sm.getData1();
int octave = (key / 12)-1;
int note = key % 12;
String noteName = NOTE_NAMES[note];
int velocity = sm.getData2();
System.out.println("Note off, " + noteName + octave + " key=" + key + " velocity: " + velocity);
} else {
System.out.println("Command:" + sm.getCommand());
}
} else {
System.out.println("Other message: " + message.getClass());
}
}

System.out.println();
}

}
}


For example the fur elise.mid I had lying around here produces something like this at the beginning:



@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
@576 Channel: 1 Note off, E5 key=76 velocity: 64
@576 Channel: 1 Note on, D#5 key=75 velocity: 127
@768 Channel: 1 Note off, D#5 key=75 velocity: 64
@768 Channel: 1 Note on, E5 key=76 velocity: 127
@960 Channel: 1 Note off, E5 key=76 velocity: 64
@960 Channel: 1 Note on, B4 key=71 velocity: 127
@1152 Channel: 1 Note off, B4 key=71 velocity: 64
@1152 Channel: 1 Note on, D5 key=74 velocity: 127
@1344 Channel: 1 Note off, D5 key=74 velocity: 64
@1344 Channel: 1 Note on, C5 key=72 velocity: 127
@1536 Channel: 1 Note off, C5 key=72 velocity: 64
@1536 Channel: 1 Note on, A4 key=69 velocity: 127
@1920 Channel: 1 Note off, A4 key=69 velocity: 64


UPDATE:
The channels are the 16 channels of the MIDI specification.



http://www.midi.org/techspecs/gm.php




Channels: All 16 MIDI Channels are
supported. Each Channel can play a
variable number of voices (polyphony).
Each Channel can play a different
instrument (sound/patch/timbre).
Key-based percussion is always on MIDI
Channel 10.




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/






share|improve this answer


























  • I had another question, the tick is the time at which the sound plays, but what does the Channel represent? And velocity?

    – Hassan Khan
    Oct 3 '10 at 20:07











  • See the updates on my original answer.

    – Sami Koivu
    Oct 3 '10 at 21:54











  • Thank you very much for your help.

    – Hassan Khan
    Oct 3 '10 at 22:00
















45














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



import java.io.File;

import javax.sound.midi.MidiEvent;
import javax.sound.midi.MidiMessage;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequence;
import javax.sound.midi.ShortMessage;
import javax.sound.midi.Track;

public class Test {
public static final int NOTE_ON = 0x90;
public static final int NOTE_OFF = 0x80;
public static final String NOTE_NAMES = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};

public static void main(String args) throws Exception {
Sequence sequence = MidiSystem.getSequence(new File("test.mid"));

int trackNumber = 0;
for (Track track : sequence.getTracks()) {
trackNumber++;
System.out.println("Track " + trackNumber + ": size = " + track.size());
System.out.println();
for (int i=0; i < track.size(); i++) {
MidiEvent event = track.get(i);
System.out.print("@" + event.getTick() + " ");
MidiMessage message = event.getMessage();
if (message instanceof ShortMessage) {
ShortMessage sm = (ShortMessage) message;
System.out.print("Channel: " + sm.getChannel() + " ");
if (sm.getCommand() == NOTE_ON) {
int key = sm.getData1();
int octave = (key / 12)-1;
int note = key % 12;
String noteName = NOTE_NAMES[note];
int velocity = sm.getData2();
System.out.println("Note on, " + noteName + octave + " key=" + key + " velocity: " + velocity);
} else if (sm.getCommand() == NOTE_OFF) {
int key = sm.getData1();
int octave = (key / 12)-1;
int note = key % 12;
String noteName = NOTE_NAMES[note];
int velocity = sm.getData2();
System.out.println("Note off, " + noteName + octave + " key=" + key + " velocity: " + velocity);
} else {
System.out.println("Command:" + sm.getCommand());
}
} else {
System.out.println("Other message: " + message.getClass());
}
}

System.out.println();
}

}
}


For example the fur elise.mid I had lying around here produces something like this at the beginning:



@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
@576 Channel: 1 Note off, E5 key=76 velocity: 64
@576 Channel: 1 Note on, D#5 key=75 velocity: 127
@768 Channel: 1 Note off, D#5 key=75 velocity: 64
@768 Channel: 1 Note on, E5 key=76 velocity: 127
@960 Channel: 1 Note off, E5 key=76 velocity: 64
@960 Channel: 1 Note on, B4 key=71 velocity: 127
@1152 Channel: 1 Note off, B4 key=71 velocity: 64
@1152 Channel: 1 Note on, D5 key=74 velocity: 127
@1344 Channel: 1 Note off, D5 key=74 velocity: 64
@1344 Channel: 1 Note on, C5 key=72 velocity: 127
@1536 Channel: 1 Note off, C5 key=72 velocity: 64
@1536 Channel: 1 Note on, A4 key=69 velocity: 127
@1920 Channel: 1 Note off, A4 key=69 velocity: 64


UPDATE:
The channels are the 16 channels of the MIDI specification.



http://www.midi.org/techspecs/gm.php




Channels: All 16 MIDI Channels are
supported. Each Channel can play a
variable number of voices (polyphony).
Each Channel can play a different
instrument (sound/patch/timbre).
Key-based percussion is always on MIDI
Channel 10.




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/






share|improve this answer


























  • I had another question, the tick is the time at which the sound plays, but what does the Channel represent? And velocity?

    – Hassan Khan
    Oct 3 '10 at 20:07











  • See the updates on my original answer.

    – Sami Koivu
    Oct 3 '10 at 21:54











  • Thank you very much for your help.

    – Hassan Khan
    Oct 3 '10 at 22:00














45












45








45







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



import java.io.File;

import javax.sound.midi.MidiEvent;
import javax.sound.midi.MidiMessage;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequence;
import javax.sound.midi.ShortMessage;
import javax.sound.midi.Track;

public class Test {
public static final int NOTE_ON = 0x90;
public static final int NOTE_OFF = 0x80;
public static final String NOTE_NAMES = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};

public static void main(String args) throws Exception {
Sequence sequence = MidiSystem.getSequence(new File("test.mid"));

int trackNumber = 0;
for (Track track : sequence.getTracks()) {
trackNumber++;
System.out.println("Track " + trackNumber + ": size = " + track.size());
System.out.println();
for (int i=0; i < track.size(); i++) {
MidiEvent event = track.get(i);
System.out.print("@" + event.getTick() + " ");
MidiMessage message = event.getMessage();
if (message instanceof ShortMessage) {
ShortMessage sm = (ShortMessage) message;
System.out.print("Channel: " + sm.getChannel() + " ");
if (sm.getCommand() == NOTE_ON) {
int key = sm.getData1();
int octave = (key / 12)-1;
int note = key % 12;
String noteName = NOTE_NAMES[note];
int velocity = sm.getData2();
System.out.println("Note on, " + noteName + octave + " key=" + key + " velocity: " + velocity);
} else if (sm.getCommand() == NOTE_OFF) {
int key = sm.getData1();
int octave = (key / 12)-1;
int note = key % 12;
String noteName = NOTE_NAMES[note];
int velocity = sm.getData2();
System.out.println("Note off, " + noteName + octave + " key=" + key + " velocity: " + velocity);
} else {
System.out.println("Command:" + sm.getCommand());
}
} else {
System.out.println("Other message: " + message.getClass());
}
}

System.out.println();
}

}
}


For example the fur elise.mid I had lying around here produces something like this at the beginning:



@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
@576 Channel: 1 Note off, E5 key=76 velocity: 64
@576 Channel: 1 Note on, D#5 key=75 velocity: 127
@768 Channel: 1 Note off, D#5 key=75 velocity: 64
@768 Channel: 1 Note on, E5 key=76 velocity: 127
@960 Channel: 1 Note off, E5 key=76 velocity: 64
@960 Channel: 1 Note on, B4 key=71 velocity: 127
@1152 Channel: 1 Note off, B4 key=71 velocity: 64
@1152 Channel: 1 Note on, D5 key=74 velocity: 127
@1344 Channel: 1 Note off, D5 key=74 velocity: 64
@1344 Channel: 1 Note on, C5 key=72 velocity: 127
@1536 Channel: 1 Note off, C5 key=72 velocity: 64
@1536 Channel: 1 Note on, A4 key=69 velocity: 127
@1920 Channel: 1 Note off, A4 key=69 velocity: 64


UPDATE:
The channels are the 16 channels of the MIDI specification.



http://www.midi.org/techspecs/gm.php




Channels: All 16 MIDI Channels are
supported. Each Channel can play a
variable number of voices (polyphony).
Each Channel can play a different
instrument (sound/patch/timbre).
Key-based percussion is always on MIDI
Channel 10.




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/






share|improve this answer















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



import java.io.File;

import javax.sound.midi.MidiEvent;
import javax.sound.midi.MidiMessage;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequence;
import javax.sound.midi.ShortMessage;
import javax.sound.midi.Track;

public class Test {
public static final int NOTE_ON = 0x90;
public static final int NOTE_OFF = 0x80;
public static final String NOTE_NAMES = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};

public static void main(String args) throws Exception {
Sequence sequence = MidiSystem.getSequence(new File("test.mid"));

int trackNumber = 0;
for (Track track : sequence.getTracks()) {
trackNumber++;
System.out.println("Track " + trackNumber + ": size = " + track.size());
System.out.println();
for (int i=0; i < track.size(); i++) {
MidiEvent event = track.get(i);
System.out.print("@" + event.getTick() + " ");
MidiMessage message = event.getMessage();
if (message instanceof ShortMessage) {
ShortMessage sm = (ShortMessage) message;
System.out.print("Channel: " + sm.getChannel() + " ");
if (sm.getCommand() == NOTE_ON) {
int key = sm.getData1();
int octave = (key / 12)-1;
int note = key % 12;
String noteName = NOTE_NAMES[note];
int velocity = sm.getData2();
System.out.println("Note on, " + noteName + octave + " key=" + key + " velocity: " + velocity);
} else if (sm.getCommand() == NOTE_OFF) {
int key = sm.getData1();
int octave = (key / 12)-1;
int note = key % 12;
String noteName = NOTE_NAMES[note];
int velocity = sm.getData2();
System.out.println("Note off, " + noteName + octave + " key=" + key + " velocity: " + velocity);
} else {
System.out.println("Command:" + sm.getCommand());
}
} else {
System.out.println("Other message: " + message.getClass());
}
}

System.out.println();
}

}
}


For example the fur elise.mid I had lying around here produces something like this at the beginning:



@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
@576 Channel: 1 Note off, E5 key=76 velocity: 64
@576 Channel: 1 Note on, D#5 key=75 velocity: 127
@768 Channel: 1 Note off, D#5 key=75 velocity: 64
@768 Channel: 1 Note on, E5 key=76 velocity: 127
@960 Channel: 1 Note off, E5 key=76 velocity: 64
@960 Channel: 1 Note on, B4 key=71 velocity: 127
@1152 Channel: 1 Note off, B4 key=71 velocity: 64
@1152 Channel: 1 Note on, D5 key=74 velocity: 127
@1344 Channel: 1 Note off, D5 key=74 velocity: 64
@1344 Channel: 1 Note on, C5 key=72 velocity: 127
@1536 Channel: 1 Note off, C5 key=72 velocity: 64
@1536 Channel: 1 Note on, A4 key=69 velocity: 127
@1920 Channel: 1 Note off, A4 key=69 velocity: 64


UPDATE:
The channels are the 16 channels of the MIDI specification.



http://www.midi.org/techspecs/gm.php




Channels: All 16 MIDI Channels are
supported. Each Channel can play a
variable number of voices (polyphony).
Each Channel can play a different
instrument (sound/patch/timbre).
Key-based percussion is always on MIDI
Channel 10.




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/







share|improve this answer














share|improve this answer



share|improve this answer








edited Oct 3 '10 at 21:53

























answered Oct 3 '10 at 17:56









Sami KoivuSami Koivu

3,42031920




3,42031920













  • I had another question, the tick is the time at which the sound plays, but what does the Channel represent? And velocity?

    – Hassan Khan
    Oct 3 '10 at 20:07











  • See the updates on my original answer.

    – Sami Koivu
    Oct 3 '10 at 21:54











  • Thank you very much for your help.

    – Hassan Khan
    Oct 3 '10 at 22:00



















  • I had another question, the tick is the time at which the sound plays, but what does the Channel represent? And velocity?

    – Hassan Khan
    Oct 3 '10 at 20:07











  • See the updates on my original answer.

    – Sami Koivu
    Oct 3 '10 at 21:54











  • Thank you very much for your help.

    – Hassan Khan
    Oct 3 '10 at 22:00

















I had another question, the tick is the time at which the sound plays, but what does the Channel represent? And velocity?

– Hassan Khan
Oct 3 '10 at 20:07





I had another question, the tick is the time at which the sound plays, but what does the Channel represent? And velocity?

– Hassan Khan
Oct 3 '10 at 20:07













See the updates on my original answer.

– Sami Koivu
Oct 3 '10 at 21:54





See the updates on my original answer.

– Sami Koivu
Oct 3 '10 at 21:54













Thank you very much for your help.

– Hassan Khan
Oct 3 '10 at 22:00





Thank you very much for your help.

– Hassan Khan
Oct 3 '10 at 22:00


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3850688%2freading-midi-files-in-java%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas