user123446
user123446

Reputation: 53

Finding the duration a note is held MIDI

I am reading a midi file to figure out the notes and velocity of it, but how would I go about figuring out the duration the note is held?

I know getData1 holds the key, and getData2 holds the velocity, but where would I figure out the duration it's held?

Upvotes: 3

Views: 2371

Answers (2)

MillKa
MillKa

Reputation: 443

Robert has already given the theoretical correct answer. The practical correct answer, as always, is slightly more complex.

Most likely, you won't find many NoteOff events, because the end of a note is often encoded as a NoteOn event with velocity zero. This allows to use the running status optimization, which avoids to repeat identical status bytes.

The event time is measured in ticks. In the header of the midi file, you find the resolution, which tells you how many ticks are in one quarter note. The resolution is usually a multiple of 24, to allow using integral tick values for normal, dotted and triplet notes.

This information is sufficient to calculate the note duration independent from tempo.

If you need the duration in milliseconds, you need the initial tempo from the header, plus all tempo change meta events within the midi file. Using all tempo changes, you can build a tempo map. Then you can calculate the time of every tempo change. Since the tempo is unchanged between two tempo changes, you can calculate the exact begin, end and duration of every note.

Upvotes: 4

Robert Harvey
Robert Harvey

Reputation: 180878

Find out the time for the note_on and note_off events for a particular note, and calculate the duration by taking the difference.

Upvotes: 7

Related Questions