pimoroni / keybow-firmware

Keybow Firmware for the Raspberry Pi Zero

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

generic MIDI message sending

flauta opened this issue · comments

I'm interested in extending the MIDI function, enabling the Keybow to send all kind of MIDI messages besides the NoteOn/NoteOff.
I'm not a C programmer at all, so my attached example code could be totally unadequate: take it only as an example of how I'd like to see it working.

void sendMIDImessage(int channel, int code, int b1, int b2, int b3) {
	unsigned char buf[4];
	buf[0] = code | channel & 0x7f;
	buf[1] = b1 & 0x7f;
	buf[2] = b2 & 0x7f;
	buf[3] = b3 & 0x7f;
    write(midi_output, buf, 4);
}

void sendMIDINote(int channel, int note, int velocity, int state) {
    unsigned char code
    if(state==1){
		code = 0x90;
	}
	else
	{
		code = 0x80;
	}
	sendMIDImessage(channel, code, note, velocity, 0);
}

I'm totally onboard with this- I'm not sure why I started specifically with note-on/off rather than just a generic MIDI message (probably because I wanted to keep my POC super simple) but there's no reason why this couldn't work.

Do you know if there's actually an upper limit on the number of data bytes you can send over MIDI, or do things like PC, CC, etc, just all happen to fit into 3? I'd wager some third party products have a pretty liberal interpretation of the spec these days, unless they just run the bus fast and stuff everything into SysEx?

As I read in the original MIDI specs, the single MIDI message can be only 2, 3 or 4 bytes (comprising the first byte specifying the message type)
see [https://www.usb.org/sites/default/files/midi10.pdf] - page 16
But in the same page I read that

Most typical parsed MIDI events are two or three bytes in length. Unused bytes must be padded
with zeros (in the case of a one- or two-byte MIDI event) to preserve the 32-bit fixed length of the USB-MIDI Event.

So after all the total length will be always 4 bytes, the standard says.

I can't answer to your question about the liberal interpretation of the standards, I'm not an expert at all. For my actual purposes a generic and simple message sending will be adequate.