Write a Standard MIDI File
A format 0 file has one track. Its MThd header gives the format, track count and time division. Its MTrk chunk starts with the byte length of the track data. Store chunk lengths and fixed-width header fields in big-endian order.
A one-beat middle C
4D 54 68 64 00 00 00 06 00 00 00 01 01 E0
4D 54 72 6B 00 00 00 14
00 FF 51 03 07 A1 20 Tempo 500,000 µs/quarter (120 BPM)
00 90 3C 64 Note On
83 60 80 3C 00 Note Off after 480 ticks
00 FF 2F 00 End of TrackTick division 480 means one quarter note spans 480 ticks. A delta time measures ticks since the last event. The two bytes 83 60 encode 480 as (3 × 128) + 96. The high bit signals a following byte.
To add a sixteenth-note rest before the first note, replace its delta 00 with 78 (120 ticks). Both deltas occupy one byte, so the track length stays the same. If an edit adds bytes, recalculate the track chunk length before saving.
Export a working file from the sequencer, then compare its event bytes with this example. Musical time in the exported file is separate from browser playback time.