Skip to content

Instantly share code, notes, and snippets.

@maddievision
Last active December 26, 2015 10:29
Show Gist options
  • Save maddievision/7137451 to your computer and use it in GitHub Desktop.
Save maddievision/7137451 to your computer and use it in GitHub Desktop.

SMF and Real Time Events

channel messages

x = channel (first byte is called the status byte)

8x nn vv -note off
9x nn vv -note on
Ax nn pp -note pressure
Bx cc vv -control change
Cx pp    -program change
Dx pp    -channel pressure
Ex ll hh -pitch wheel

Running status

If the status byte is the same for subsequent events (including the channel part) it can be omitted for the next event (save it as Running Status)

Running status is not affected by non-channel commands.

eg multiple notes:

90 3C 7F
   3F 7F
   42 7F
   
80 3C 00
   3F 00
   42 00

sysex

F0 xx .. .. .. .. F7 - sysex
F7 xx .. .. .. .. F7 - sysex continuation

SMF only:

FF xx ll .. .. .. .. - meta event, ll = length
FF 2F 00             - end of track meta event

##Real Time only:

###system messages

F1 - who cares etc.
F2
F3
F4
F5
F6
F7

###real time (1 byte each, can appear anywhere, even between messages)

F8 - midi clock
F9
FA
FB
FC
FD 
FE - active sense
FF - reset

SMF Stuff

Timestamps

Variable length quantity, each byte represents 7-bits from in order from MSB to LSB All bytes except the last have the 8th bit set. (bit 8 clear means last byte) Used to specify delta time in between events

examples:

08       = 08
81 00    = 80
81 80 00 = 4000

Header and tracks

Header chunk

"MThd"      - header
ll ll ll ll - length of chunk (always 6)
ff ff       - format (0, 1 or 2)
tt tt       - number of tracks
pp pp       - PPQN (also SMTPE if negative)

After this, tracks follow.

Track chunk

  • Format 0 - only 1 track, events address all channels
  • Format 1 - t number of tracks, 1 tempo track + track for each channel
  • Format 2 - t number of tracks, several tracks which are played sequentially (like multiple format 0 tracks), rarely used

For each track:

"MTrk"      - header
ll ll ll ll - length of track (all the bytes that follow)
vlq ...     - for each event (vlq = variable length quantity delta time in ticks)
vlq FF 2F 00 - end of track meta event
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment