Ch Ryder
Ch Ryder

Reputation: 597

How do I calculate the number of ticks per measure from a MIDI file

I am trying to calculate the number of ticks per measure (bar) from a MIDI file, but I am a bit stuck.

I have a MIDI file from which I can extract the following information (provided in meta messages):

@0: Time signature: 4/4, Metronome pulse: 24 MIDI clock ticks per click, Number of 32nd notes per beat: 8

There are two tempo messages, which I'm not sure are relevant:

@0: Microseconds per quarternote: 400000, Beats per minute: 150.0

@1800: Microseconds per quarternote: 441176, Beats per minute: 136.0001450668214

From trial and error, looking at the Note On messages, and looking at the MIDI file in Garageband, I can 'guess' that the number of ticks per measure is 2100, with a quarternote 525 ticks.

My question is: can I arrive at the 2100 number using the tempo information that was provided above, and if so how? Or have I not parsed enough information from the MIDI file and is there some other control message that I need to look at?

Upvotes: 0

Views: 1137

Answers (1)

Ch Ryder
Ch Ryder

Reputation: 597

Use the following Java 11 code to extract the ticks per measure. This assumes 4 quarter notes per bar.

public MidiFile(String filename) throws Exception {

    var file = new File(filename);
    var sequence = MidiSystem.getSequence(file);
    System.out.println("Tick length: " + sequence.getTickLength());
    System.out.println("Division Type: " + sequence.getDivisionType());
    System.out.println("Resolution (PPQ if division = " + javax.sound.midi.Sequence.PPQ + "): " + sequence.getResolution());
    System.out.println("Ticks per measure: " + (4 * sequence.getResolution()));

}

Upvotes: 0

Related Questions