So I have this very simple MIDI file:
I simply want do define the length in milliseconds for these 2 notes. In real time the first note is less than a second and the second note is about 2 seconds.
As I iterate the MIDI events, every time I get a note-on or note-off event I fetch there delta ticks. I'm 100% sure these delta ticks are correct because I compared them with an algorithm that works properly.
As far as I understand, if the F5 key in this file hits note-on in an X time, the note-off of that event will hit after the number of deltaTicks of the note-off event. So that means that to find how long in milliseconds the length of the F5 key is, all I need to do is to convert the note-off event delta ticks to milliseconds. (Am I correct?)
I parsed the MIDI file so I have the tempo and the PPQ, and I searched a lot about this so till now I found this formula:
double bpm = (60000000 / mTempo);
deltaTimesInMilliseconds = deltaTicks * (60000 / (bpm * division));
but I'm probably doing something wrong here because the delta time in milliseconds for the F5 key note-off event is about 7000, which means 7 seconds.
Thank's for any help!
More of the file:
public class MidiParserN {
private static double mTempo;
private static short division;
...
...
private static TrackChunkEvent
parseMTrkEvent() throws Exception {
int deltaTicks = Math.abs(calculateDeltaTicks(getVariableLengthQuantity()));
double deltaTimesInMilliseconds = 0;
int eventType = parseEventType();
int eventNum = -1;
String eventName = "";
if (eventType == EventTypes.MIDI_EVENT.ordinal()) {
eventNum = parseMidiMessage();
eventName = midiEventsTypes[eventNum].toString();
} else if (eventType == EventTypes.SYSEX_EVENT.ordinal()) {
parseSysexEvent();
} else {
eventNum = parseAndExecuteMetaEvent();
eventName = metaEventsTypes[eventNum].toString();
}
if (eventName.equals("NOTE_ON_EVENT") && deltaTimes != 0 && mTempo != 0) {
double bpm = (60000000 / mTempo);
deltaTimesInMilliseconds = deltaTicks * (60000 / (bpm * division));
log("DELTA_TIMES_MILLISECONDS", Double.toString(deltaTimesInMilliseconds));
}
else if (eventName.equals("NOTE_OFF_EVENT") && deltaTimes != 0 && mTempo != 0) {
double bpm = (60000000 / mTempo);
deltaTimesInMilliseconds = deltaTicks * (60000 / (bpm * division));
log("DELTA_TIMES_MILISECONDS", Double.toString(deltaTimesInMilliseconds));
}
return new TrackChunkEvent(eventNum, eventName);
}
...
...
}
Updating tempo on tempo event:
if (firstByteOfMetaEvent == 0x51 && secondByteOfMetaEvent == 3) {
int tempo = byteArrayToInt(parseMetaEventSetTempo());
mTempo = tempo;
return MetaEventsTypes.SET_TEMPO.ordinal();
}
The MIDI file:
4d 54 68 64 00 00 00 06 00 01 00 02 01 80 4d 54 72 6b 00 00 00 13 00 ff 58 04 04 02 18 08 00 ff 51 03 08 52 ae 00 ff 2f 00 4d 54 72 6b 00 00 00 33 00 ff 03 15 45 6c 65 63 2e 20 50 69 61 6e 6f 20 28 43 6c 61 73 73 69 63 29 00 c0 00 82 20 90 3c 32 60 80 3c 00 83 00 90 41 32 8d 40 80 41 00 00 ff 2f 00