Creating a game or an application in Unity that involves MIDI, such as a rhythm game or tutorial, often requires knowing the real-time value of each note. This post can be a valuable resource.
Be sure to read the chapter “How Maestro MPTK could help you” as it offers significant insights. The initial chapter, while somewhat intricate, is beneficial for those seeking to comprehend MIDI.
The MIDI Standard
As you may be aware, the timing of MIDI events is determined by the number of ticks since the previous event on the same track. Consequently, it is a relative value, not tied to the tempo, and indeed, it is independent of real time.
Calculation
The MPTK_DeltaTicksPerQuarterNote which is a fixed value for each MIDI, indicate the duration time in “ticks” constitute a beat.
Typically, a beat corresponds to a quarter note, but this can vary depending on the time signature. For instance, in a 4/4 time signature, the beat is a quarter note, while in a 4/2 time signature, it is a half note. Additionally, it’s important to remember that the time signature may change during the course of a MIDI sequence. More details here.
For example, if the Division of Ticks Per Quarter Note (DTPQN) is 96, then the tick value for an eighth note in a 4/4 time signature would be 48 (since an eighth note is half a quarter note). With a tempo of 120 Beats Per Minute (BPM), the duration of this eighth note would be 0.25 seconds (as 120 BPM equals 120 quarters per minute, or 2 quarters per second, which translates to 4 eighth notes per second).
Here a video of MIDI duration calculator and a calculator app.
Duration
Determining the actual duration of an event, such as a MIDI Note-On, can be intricate due to its reliance on the interval of ticks between the Note-On event and subsequent Note-Off event, rather than a specified duration within the event itself.
You’re always there? Excellent! Now for the good news. Fortunately, Maestro MPTK automatically performs all these calculations when the MIDI is loaded.
How Maestro MPTK could help you?
Timing
In Maestro MPTK, each MIDI event is represented by an instance of the class MPTKEvent class, which includes several useful attributes:
- Tick: The absolute time in ticks of an event, from the start of a MIDI file playback, is independent of the tempo.
- RealTime: time in milliseconds of this MIDI Event from the beginning of the MIDI, accounting for the changes in tempo.
- Duration: The duration of a note in milliseconds related to changes in tempo and time signature.
- Beat and Measure of each event are determined by the time signature change event.
Normally, you should not need to convert tick values to real-world time values yourself.
Other interesting attributes and classes include:
- MPTK_TempoMap: list of all tempo changes (MPTKTempo) found in the MIDI.
- MPTK_SignMap: list of all signature changes (MPTKSignature) found in the MIDI.
You can access to these attributes when the MIDI is loaded. Look at the demo source code TestMidiFilePlayerScripting.cs
and search for the method MaestroOnEventStartPlayMidi()
2 comments on “MIDI ticks, duration and real-time with MPTK”
Wallet Up
1.12.2024 at 04:21Built a nice alternative just for what I was looking for MIDI Duration Calculator
https://walletup.app/midi/
Thierry Bachmann
2.12.2024 at 08:14Nice! I will propose your link.