Instructables

Send and Receive MIDI with Arduino

Featured

Step 9: Pitchbend and Arduino

Now that you know how to control note on and note off, you can try pitchbend.

Pitchbend information is stored in 2 data bytes, and most significant byte (MSB) and a least significant byte (LSB).  Each of these bytes contains only 7 bits of information.  This means that all pitchbend information is stored in 14 bits, with the most significant 7 bits stored in the MSB and the least significant 7 bits stored in the LSB.

For most applications you will only find yourself changing pitchbend via the MSB and just setting the LSB to 0.  In this case you have 7 bits of resolution for pitchbend (128 steps).  In MIDI protocol pitchbend = 64 is no pitchbend, pitchbend greater than 64 is pitchbends the frequency up, and less than 64 pitchbends the frequency down.

In the example below a note played and held, then played again while the pitchbend increments from 64 to its max value of 127, then played a third time while the pitchbend increments from 64 to its min value of 0.  This sequence is looped forever.  The images above show the output in ableton from this Arduino sketch.

Below is a video demonstration of the code above.  For this piece of code, pitchbend will be most noticeable in instruments with a long sustain, such as a string instrument, keep that in mind when testing the code for yourself.


You will most likely be fine using only 128 steps of pitchbend resolution, but in case you must use all 16384 steps, see the code below.  Basically what I've done here is defined a variable called pitchbendVal, which varies from 0 to 16383.  As I said below the "zero" pitchbend value is msb = 64 and lsb = 0.  In binary this is:

MSB = 64 = 01000000
LSB = 0 = 0000000

(remember MSB and LSB are 7 bit numbers)

putting these values together we get:

1000000 0000000
    MSB      LSB


or

10000000000000
which translates to 8192 in decimal

so now the "zero" pitchbend value is 8192.

You'll also notice I had to break the variable pitchbendVal into two 7 bit parts to send out via MIDI message:
MIDImessage(pitchbend, (pitchbendVal&127), (pitchbendVal>>7));
the first part, pitchbendVal&127, returns the least significant 7 bits of pitchbendVal
the second part, pitchbendVal>>7, returns the most significant 7 bits of pitchbendVal
see & and >> on the Arduino reference page for more info.
 
Remove these adsRemove these ads by Signing Up