Consider this function:

void noteOn(uint8_t pitch) {
  midiEventPacket_t noteOn = {0x09, 0x90 | 9, pitch, 127};

midiEventPacket_t is defined as:

typedef struct
    uint8_t header;
    uint8_t byte1;
    uint8_t byte2;
    uint8_t byte3;

The first nibble of the first byte of a MIDI message is the command: 0x90 ["note on"] and the second nibble is the MIDI channel: 9, so what's sent over the wire should be 0x99 meaning "Note On, channel 9", with the two more bytes representing the pitch and the velocity.

My question is, why do all of the examples start with: {0x09, 0x90 | 9,? I'm not understanding the use of the struct here. Intuitively, I thought I would need: {0x90 | 9, pitch, 127, 0}. Why do all of the examples have that seemingly strange syntax?


After getting a great explanation below, here's my new function!

void noteOn(uint8_t pitch) { midiEventPacket_t noteOnPacket; noteOnPacket.header = USB_CABLE_NUMBER | (MIDI_NOTE_ON >> 4); noteOnPacket.byte1 = MIDI_NOTE_ON | CHANNEL; noteOnPacket.byte2 = pitch; noteOnPacket.byte3 = MIDI_BUTTON_ON_VELOCITY; MidiUSB.sendMIDI(noteOnPacket); }


From the specs:

The first byte in each 32-bit USB-MIDI Event Packet is a Packet Header contains a Cable Number (4 bits) followed by a Code Index Number (4 bits). [...]


The Cable Number (CN) is a value ranging from 0x0 to 0xF indicating the number assignment of the Embedded MIDI Jack associated with the endpoint that is transferring the data. The Code Index Number (CIN) indicates the classification of the bytes in the MIDI_x fields. The following table summarizes these classifications.

CIN | MIDI_x Size | Description


0x9 | 3 | Note-on


No rationale is given why the CIN is required if the full message is also included, but it could be used for quicker routing of the event to the routine that will process it.

|improve this answer|||||
  • OH, LIGHTBULB moment. midiEventPacket_t represents the USB packet, not the MIDI packet – Jonathan S. Fisher Jul 17 '17 at 4:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.