Question

I'm trying the second day to send a midi signal. I'm using following code:

int pitchValue = 8191 //or -8192;
int msb = ?;
int lsb = ?;
UInt8 midiData[]  = { 0xe0, msb,  lsb};
[midi sendBytes:midiData size:sizeof(midiData)];

I don't understand how to calculate msb and lsb. I tried pitchValue << 8. But it's working incorrect, When I'm looking to events using midi tool I see min -8192 and +8064 max. I want to get -8192 and +8191.

Sorry if question is simple.

Was it helpful?

Solution

Pitch bend data is offset to avoid any sign bit concerns. The maximum negative deviation is sent as a value of zero, not -8192, so you have to compensate for that, something like this Python code:

def EncodePitchBend(value):
    ''' return a 2-tuple containing (msb, lsb) '''
    if (value < -8192) or (value > 8191):
        raise ValueError
    value += 8192
    return (((value >> 7) & 0x7F), (value & 0x7f))

OTHER TIPS

Since MIDI data bytes are limited to 7 bits, you need to split pitchValue into two 7-bit values:

int msb = (pitchValue + 8192) >> 7 & 0x7F;
int lsb = (pitchValue + 8192) & 0x7F;

Edit: as @bgporter pointed out, pitch wheel values are offset by 8192 so that "zero" (i.e. the center position) is at 8192 (0x2000) so I edited my answer to offset pitchValue by 8192.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top