Skip to content

Conversation

dpezely
Copy link
Contributor

@dpezely dpezely commented Aug 7, 2025

The MIDI 2.0 spec documents could be better about total required number of bytes for each type of message, so it's too easy to miss where 128 bits are required.

  • Trailing portions of a MIDI message that are specified as "Reserved" must be included even when all zeros
    • e.g., Set Tempo looks like it should fit within 64 bits but requires 128 bits
  • Relevant tests are revised for appending appropriate number of zeroed bytes
  • All passed: cargo test --all-targets --all-features
  • Appears correct when viewed within MIDI 2.0 Workbench's SMF2 window
    • Albeit, haven't generated a .midi2 file with everything that has changed

Details:

  • References to individual sections below are from M2-104-UM
    • Universal MIDI Packet (UMP) Format and MIDI 2.0 Protocol spec
  • For a summary with visuals, see "Appendix F: All Defined UMP Formats" and "Appendix G: All Defined Messages"
    • Table 27 4-Byte UMP Formats for Message Type 0x1: System Common & System Real Time
    • Table 32 128 bit UMP Formats for Message Type 0xD: Flex Data Messages
    • Table 33 128 bit UMP Formats for Message Type 0xF: UMP Stream Messages
  • However, one table obscures the Reserved portion of some messages
    • Table 34 All Defined Message Formats (in 5 parts)
    • Beware of its rightmost column "Bytes 9-16 (64 bits)" because of portrait to landscape page rotation
  • section 7.1 UMP Stream Messages
    • UMP Stream Messages are addressed to the UMP Endpoint, without a Group
      or Channel assignment. All UMP Stream Messages are 128-bit messages [...]

  • section 7.4.5 MIDI 2.0 Per-Note Management Message
    • e.g., total bits must be extracted from their diagram
    • | mt=4 | group| 1111 | ch. | r+note# | flags | D | S |
      | .... | .... | .... | .... | ....... | ...... | . | . |
      | reserved |

  • section 7.5.1 Flex Data Messages General Format
    • See first bullet point:
    • A Format field is used to optionally allow a message to have a variable size data, in multiples of 128 bits.

  • Similar:
    • 7.5.3 Set Tempo Message
    • 7.5.4 Set Time Signature Message
    • 7.5.5 Set Metronome Message
    • 7.5.6 Example Set Metronome Messages
    • 7.5.7 Set Key Signature Message
  • Screenshots from midi2.dev's MIDI 2.0 Workbench SMF2 window: (see below)
    • That's how this issue was discovered
    • MIDI 2.0 Workbench - zeroed reserved bytes.png (with patch)
    • MIDI 2.0 Workbench - malformed midi2 file.png
    • Reserved bytes that should be zero engulfed other messages
    • Malformed at byte offset 20 and 36, thus SMF2 window unable to identify Header / Sequence boundary
    • Images via patched Workbench; see multiple PRs there

TODO — possibly related:

  • Table 27 4-Byte UMP Formats for Message Type 0x1: System Common & System Real Time
    • e.g., Tune Request uses 2 bytes with an additional 2 bytes reserved
    • midi2/src/system_common.rs mentions MinSizeUmp(1), MinSizeBytes(2)
    • That parameter for 2 bytes might need to become 4 but needs confirmation
#[midi2_proc::generate_message(
    Via(system_common::SystemCommon),
    FixedSize,
    MinSizeUmp(1),
    MinSizeBytes(2)
)]
struct TuneRequest { /* ... */ }
MIDI 2 0 Workbench - zeroed reserved bytes MIDI 2 0 Workbench - malformed midi2 file

@BenLeadbetter
Copy link
Collaborator

Thanks for the contribution! I'm out of office until the 18th. I'll do a proper review, hotfix, etc after I'm back 😊

@BenLeadbetter
Copy link
Collaborator

Looks good to me 👍

Did you already check the remaining messages (unchanged in this PR) against the MIDI 2.0 Workbench app ?

@dpezely
Copy link
Contributor Author

dpezely commented Aug 20, 2025

I've exercised these locally and viewed within MIDI 2.0 Workbench:

  • 7.5.3 Set Tempo Message
  • 7.5.4 Set Time Signature Message
  • 7.5.7 Set Key Signature Message

I probably did a 1-off test with Set Metronome Message but don't recall.

The item marked with TODO is a MIDI feature that I haven't used yet.

@dpezely
Copy link
Contributor Author

dpezely commented Aug 20, 2025

I'll make time to do another quick test for Metronome markings later today to be confirmed via MIDI 2.0 Workbench.

(If you don't have that set up already, it can be a little challenging. See PRs there for Vagrantfile and Dockerfile.)

@dpezely
Copy link
Contributor Author

dpezely commented Aug 20, 2025

Confirmed SetMetronome UMP message bits look correct via MIDI 2.0 Workbench. See second image.

The sequence in that second image:

  • byte 64 is Delta Clockstamp with very large value to exercise u20
  • byte 68 is Key Signature
  • Metronome (values for additional fields are bogus: 1, 2, 3, 4, 5)
  • Tempo
  • Time Signature
clip-window-01 clip-window-02

@BenLeadbetter
Copy link
Collaborator

Thanks for taking the time to test these!

@BenLeadbetter BenLeadbetter merged commit 6e224a9 into midi2-dev:develop Aug 21, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants