# calculating midi pitch bend value from key numbers with fractional part

I am creating a tool for composing with midi micro-tonal message (pitch bend), and I wanted make sure I am calculating the amount of bending correctly. I would appreciate it if any one could tell me if something is wrong with my calculations.

According to MIDI specification the bending range is 2^14 = 16384, with 8192 being the center (no bend). Now if I want to find out the amount of upwards bending from a specific key number (e.g. 60.5) I would do:

1. find the fractional part (= 0.5)
2. multiply it by the amount of bending for one semitone (2^12 = 4096, assuming the standard of 2^13 corresponding to one whole tone) to obtain the bending value
3. add that amount to the no-bend value (2^13) and send that value as bend message to the right channel etc.

Update

Also while I'm asking, is it safe/recommended to always send the no-bend-value of 8192 as message after each pitch bending to reset the bending of the affected channel, kind of like sending a note off after a note on?

• Does this answer your question? How do I adjust the pitch range of the Midi pitch bend control? Jan 19, 2023 at 16:25
• @Theodore The answers there don't address this question. Jan 19, 2023 at 17:22
• btw, though related, I don't see this as a duplicate at all. Jan 19, 2023 at 17:40
• @Tetsujin But pitchbend is normally in two bytes, as the control number in normal CC messages is omitted there is room for this second byte. If it is actually used by the hardware after is of course a different matter.
– Tom
Jan 19, 2023 at 17:54
• I should have left the other half of my comment in situ… why are you trying to do this with PB when there's a perfectly good set of commands to handle micro tuning? Anything more than purely monotonic playback is not going to work. You are trying to invent the square wheel. We already have round wheels. Jan 20, 2023 at 9:37

1. There are already virtual instruments which support microtonal tuning. That might be the best solution for you. See also this link provided by @Tetsujin https://www.midi.org/midi-articles/microtuning-and-alternative-intonation-systems

2. The most common range of the pitch bend wheel is ±2 semitones – but it is not standard. Many instruments allow you to adjust this range. Therefore, there is no absolute correspondence between the MIDI command and the pitch of the note. You need to check settings of the specific instrument you use.

Now let's try to answer the question

I am not 100% sure how the virtual instrument interpret the pitch bend commands, but I would expect them to follow the logarithmic scale. Thanks to @Tom for providing this link confirming this: https://sites.uci.edu/camp2014/2014/04/30/managing-midi-pitchbend-messages/

Let's start with the cent scale, which is similar. One can calculate the number of cents between two frequencies f₁ and f₀ as:

`cents = 1200 log₂(f₁ / f₀)`

Let's look at the details of this formula.

• For a unison, `f₁=f₀` and `log₂(f₁ / f₀) = log₂(1) = 0`,
• for a semitone (in equal temperament) `f₁=2¹𝄍¹²·f₀` and `log₂(f₁ / f₀) = log₂(2¹𝄍¹²) = 1/12`,
• for a whole tone `f₁=2²𝄍¹²·f₀` and `log₂(f₁ / f₀) = 2/12`,

...

• for an octave `f₁=2¹²𝄍¹²·f₀=2f₀` and `log₂(f₁ / f₀) = 2`.

For the inverted interval, e.g. 1 semitone down `log₂(f₁ / f₀) = -1/12`, two semitones `-2/12` and so on.

Together with the multiplicative constant of 1200, the formula returns 100 cents for a semitone, 200 cents for a whole tone, and so on; an octave is 1200 cents.

In order to make this formula work for the pitch wheel, we need to change two things:

1. As the pitchbend value is not centered at 0, but at 2¹³, we need to add a constant 2¹³, so that for unison, when the logarithm part equals 0, the formula returns 2¹³.
2. The range is different, so the multiplicative constant 1200 needs to be changed to some yet unknown `C`, in order to cover the full pitchbend command range from 0 to 2¹⁴-1:

`pitchbend_value = 2¹³ + C · log₂(f₁ / f₀)`.

If the pitchbend in the instrument has a range of ±N semitones, the logarithm part will vary from `log₂(2⁻ᴺ𝄍¹²) = -N/12` to `log₂(2ᴺ𝄍¹²) = N/12`. We need to adjust C so that at the minimum the formula returns `2¹³-2¹³ = 0`, and at the maximum `2¹³+2¹³ = 2¹⁴ = 16384`. (Side note, that's 1 unit more than the maximum pitchbend value of 2¹⁴-1, so the maximum frequency will be a hair lower, but that's a very minor difference).

We obtain

`pitch_value = 2¹³ + 2¹³ · 12/N · log₂(f₁ / f₀)` ,

where N is the pitch bend range in semitones, N = 2 for the standard ±2 semitones range.

• Yup, normally this is handled in a logarithmic way (sites.uci.edu/camp2014/2014/04/30/…)
– Tom
Jan 19, 2023 at 18:05
• You are going to some magnificent effort in answering [& maintaining] this, but the whole idea still falls at the first hurdle for the OP, It works for monophonic, zero-release sounds only. Legato or polyphonic data will just be uncontrollable. Jan 20, 2023 at 17:35
• @Tetsujin I agree, that's why I suggest using an instrument that support microtonal tunings. There are quite some many to choose from now. But I think working with pitch wheel can be still useful at some occasions. Jan 20, 2023 at 17:50
• In my first comment yesterday [which I removed because of an erroneous data-point] I had this link form the midi org - midi.org/midi-articles/… with a variety of micro-tuning apps/implementations Jan 20, 2023 at 17:52
• @Tetsujin as comments are ephemeral entities, I allowed myself to include the link in the answer Jan 20, 2023 at 19:02