I am trying to extract the tempo of a melody from the first track of a midi file and apply it to the rest of the tracks containing the note events.
Bascically I've been trying to replace the Thread.sleep()
method after the noteOn()
message which is playing a note for a fixed time interval everytime. Hence I'm losing the tempo of the entire track.
I was successful in extracting the tempo information from the first track from a previously asked question How does Midi TEMPO message apply to other tracks? but am unable to apply it to the rest of the tracks.
Is there a particular function that does that? I tried searching but couldn't find one.
Here is my code for reference.
int trackNumber = 0;
for (Track track : sequence.getTracks()) {
trackNumber++;
System.out.println();
for (int i=0; i < track.size(); i++) {
MidiEvent event = track.get(i);
MidiMessage message = event.getMessage();
if (message instanceof MetaMessage) {
MetaMessage mm = (MetaMessage) message;
if(mm.getType()==SET_TEMPO){
byte[] data = mm.getData();
int tempo = (data[0] & 0xff) << 16 | (data[1] & 0xff) << 8 | (data[2] & 0xff);
int bpm = 60000000 / tempo;
}
}
if (message instanceof ShortMessage) {
ShortMessage sm = (ShortMessage) message;
if (sm.getCommand() == NOTE_ON) {
int key = sm.getData1();
int velocity = sm.getData2();
channels[0].noteOn(key,velocity);
Thread.sleep(280);//Pays all the note for fixed duration
}
else if (sm.getCommand() == NOTE_OFF) {
int key = sm.getData1();
int velocity = sm.getData2();
channels[0].noteOff(key);
}
}
}
System.out.println();
}
}