Consider this function:
void noteOn(uint8_t pitch) {
midiEventPacket_t noteOn = {0x09, 0x90 | 9, pitch, 127};
MidiUSB.sendMIDI(noteOn);
}
midiEventPacket_t
is defined as:
typedef struct
{
uint8_t header;
uint8_t byte1;
uint8_t byte2;
uint8_t byte3;
}midiEventPacket_t;
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?
EDIT
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);
}