Music Software Development

Music data structures for sequencers

Keep musical positions as integer ticks. At 480 ticks per quarter note, an eighth note is 240 ticks and a sixteenth note is 120. With that model you can edit tempo without rewriting any note positions.

A note as a record

{ tick: 240, duration: 120, pitch: 64, velocity: 90, channel: 0 }

The record represents E4 on MIDI channel 1. A track can own a sorted list of these records. When exporting, split each record into a start and a release. Sort all events by tick, putting release before a new start for the same pitch and channel at the same tick. Subtract the previous event tick to write each delta.

Time and tempo

At constant tempo, seconds equal ticks divided by ticks per quarter, multiplied by 60 divided by BPM. With tempo changes, store a sorted list of tempo events and sum the duration covered by each segment. Cache cumulative seconds per tempo segment if you need fast random access.

Compare this model with the MIDI file representation and the Web Audio playback clock.