rkistner / arcore

MIDI-USB Support for Arduino
Other
192 stars 33 forks source link

How to use pitch bend ? #19

Closed orgicus closed 8 years ago

orgicus commented 8 years ago

Hi,

I'm new to MIDI and getting a noteOn/noteOff with arcore + Leonardo was pretty easy. This isn't an issue per se, but could please explain and point me in the direction on how to send pitch bend data (maybe on two different channels) ?

I've looked at this MIDI event table and tried sending these bytes to test:

MIDIEvent event = {0xE0,0x40,0x7E};//224, 64, 126
MIDIUSB.write(event);

but no joy :( Any tips ?

Thank you, George

rkistner commented 8 years ago

Did you get something working yet? In the MIDI-USB spec there are always 4 bytes per event. It typically adds a byte to the start of the normal MIDI message. This is just the event type, 0x0E in this case.

You can try the following code (haven't tested it myself):

// The pitch bend value is a 14-bit number (0-16383). 0x2000 (8192) is the default / middle value.

// First byte is the event type (0x0E = pitch bend change).
// Second byte is the event type, combined with the channel.
// Third byte is the 7 least significant bits of the value.
// Fourth byte is the 7 most significant bits of the value.
void pitchBendChange(byte channel, int value) {
  byte lowValue = value & 0x7F;
  byte highValue = value >> 7;
  MIDIEvent event = {0x0E, 0xE0 | channel, lowValue, highValue};
  MIDIUSB.write(event);
}
orgicus commented 8 years ago

Awesome! This worked like a charm. Should've read the specs carefully. Sorry about the late reply and thank you very much.

rkistner commented 8 years ago

Great, glad it worked!