build-a-midi-parser / lesson-25.md
Lesson 25 · Assembling the timed song

Absolute ticks

Delta-times are gaps between events; to place events on a timeline you add them up into absolute ticks from the start of the track. Today you make that running total.

The goal

Convert a track's delta-times into absolute tick positions.

Start here - the target
TO DO
Scenario: Delta-times accumulate into absolute tick positions
Givena track whose events have delta-times 0, 96, and 96 in order
Whenthe deltas are accumulated into absolute ticks
Thenthe absolute tick positions are 0, 96, and 192
Andthe first event is always at tick 0 regardless of later deltas
Background

A delta-time is relative - it says “wait this many ticks after the previous event.” That is compact for storage but useless for asking “what happens at tick 192?” So the first step toward timing is a running total: walk the events in order, keep a sum, and each event’s absolute tick is the sum after adding its own delta. Deltas 0, 96, 96 become absolute ticks 0, 96, 192.

Add an AbsTick field to each timed event and fill it in this one pass. Absolute ticks are the common currency for everything left in the project: pairing a note-on with the note-off that comes later, collecting tempo changes at the ticks where they happen, and converting any tick to seconds. It is a tiny computation with a big payoff - the whole track now lives on one shared timeline.

Make it work
// running sum: each event sits at the previous total plus its own delta
var tick uint32
for i := range events {
tick += events[i].Delta
events[i].AbsTick = tick // add this field to TrackEvent
}
CheckpointDONE
You can place each event at an absolute tick. Commit and stop here.