MIDI running status: why a clock byte is not a lost note
A MIDI keyboard works with the clock off. With sync enabled, a raw serial bridge starts dropping notes. Before blaming timing jitter, check whether its decoder forgets which note it was reading when it sees a Timing Clock byte.
In a MIDI 1.0 byte stream, F8 is a complete Timing Clock message. It may arrive between the pitch and velocity of a note. Report it separately: it neither fills the note’s velocity slot nor clears the channel status. The MIDI Association message overview describes this interleaving; its message summary lists the status bytes.
Decode a seven-byte example by hand
Start with 90 3C F8 64 40 F8 64.
-
90 3C: Note On, channel 1; note number 60, velocity pending. -
F8: emit a clock, leaving the partial note alone. -
64: velocity 100; now emit the first note. -
40: note number 64. The earlier90is still in effect, so this starts another Note On without a new status byte. -
F8 64: emit the clock, then use the last byte as the second note’s velocity.
The result in completion order is Clock, Note 60, Clock, Note 64. The velocity byte 64 is hexadecimal for decimal 100.
Keep two pieces of parser state
Running status remembers the most recent applicable channel status. Pending data holds the incomplete payload. A clock touches neither. When a note completes, clear its pending bytes but retain the status. Likewise, don't clear either variable just because one serial read returned fewer bytes than the next.
This exercise handles only Note On, Note Off and Timing Clock. It raises on unsupported statuses instead of silently interpreting them as notes. A complete parser must also implement other channel statuses, System Common, SysEx and other real-time messages.
def note_stream(): running = None pending = [] def feed(chunk): nonlocal running for b in chunk: if b == 0xF8: yield ("clock",) elif 0x80 <= b <= 0x9F: pending.clear() running = b elif b >= 0x80: raise ValueError("Unsupported status: expected Note On, Note Off, or Clock") elif running is not None: pending.append(b) if len(pending) == 2: note, velocity = pending pending.clear() yield ("note", running, note, velocity) return feed Test the same bytes at every split
Feed 90 3C, F8, then 64 40 F8 64 through the same decoder. Appending F8 3C 00 40 00 adds another clock and two zero-velocity Note On encodings of releases. The decoder reports the wire form without converting them to Note Off. Consume each generator before feeding the next chunk.
feed = note_stream() actual = [] for hex_chunk in ("90 3C", "F8", "64 40 F8 64", "F8 3C 00 40 00"): actual.extend(feed(bytes.fromhex(hex_chunk))) assert actual == [ ("clock",), ("note", 0x90, 60, 100), ("clock",), ("note", 0x90, 64, 100), ("clock",), ("note", 0x90, 60, 0), ("note", 0x90, 64, 0), ] The offline fixture checked all 2,048 ways to partition those 12 bytes into nonempty reads. It also moved one clock through all ten positions around and within the nine non-clock bytes; the parsed notes didn't change. If your library already delivers complete events, start with those instead of decoding raw bytes again. Standard MIDI Files and MIDI 2.0 UMP have different framing.
From Sonari Studio: Sonari for Mac is a MIDI performance input. This models a downstream raw-byte bridge, not a capture of the app or a claim about its low-level encoding.
AI-generated Sonari Studio tutorial. The decoder fixture was tested offline without hardware.
原文:https://www.a8q9.cn/en/article/midi-running-status-why-a-clock-byte-is-not-a-lost-note-3d35fb.html