At the core of MIDI is a representation of music as discrete note events, each of those having a static pitch. This is perfect for representing music as played on keyboard instruments. You can convert any frequency corresponding to a note on the tempered scale into a MIDI note number, using:
$69 + 12 \times \log_2 \frac{frequency}{440}$
Under the assumption that the MIDI receiver is calibrated for A4 = 440 Hz.
This representation is alright for piano music, but the problem is how to represent pitches which are not mapped to the tempered scale (non-western music, non musical sounds), and how to represent pitch variations over the duration of a note (glissando, vibrato).
This is done in MIDI by using "pitch bend messages" which instruct the synthesizer to shift the pitch of the currently played note by a small interval. Most synthesizers are calibrated by default for +/- 2 semitones over the course of the pitch bend message range (0 .. 16383). 8192 corresponds to no pitch bending - the emitted pitch is exactly that of the note value. The mapping between the pitch bend value and the frequency shifting ratio is given by:
$\frac{f_{emitted\_note}}{f_{note\_message}} = 2 ^ {\frac{pitchbend - 8192}{4096 \times 12}}$
You can thus get the frequency of a note played by a syntheszier from the following formula:
$440 \times 2^{\frac{note - 69}{12.0} + \frac{pitchbend - 8192}{4096 \times 12}}$
Where note is the 7-bits MIDI note number of the last received Note On message ; and pitchbend is the 14-bits value of the last received Pitch bend message. A synthesizer starts with its pitch bend register set to 8192, and this value is also reset during the reception of a "Reset all controllers" message.
Let us take the following example. You want to express a flute trill with the following frequency trajectory: 500 Hz, 510 Hz, 500 Hz, as MIDI messages.
The base note number is:
$round(69 + 12 \times \log_2(500 / 440)) = 71$.
So you send a "note on" message with note# equal to 71. This is equivalent to a pitch of:
$440 \times 2 ^ {(71 - 69) / 12} = 493.88$
Which is the nearest pitch on the tempered scale. You need to send a pitch bend message to raise the pitch by a factor of:
$\frac{500}{493.88} = 1.0124$
And get your 500 Hz. The corresponding pitch bend value is:
$round(8192 + 4096 \times 12 \times log_2 1.0124) = 9065$
To get your 510 Hz, the pitch bend value would be:
$round(8192 + 4096 \times 12 \times log_2 \frac{510}{493.88}) = 10469$
So your sequence of MIDI messages for 500, 510, 500 Hz would be:
- NOTE 71
- PITCH BEND 9065
- ...
- PITCH BEND 10469
- ...
- PITCH BEND 9065
You can think of the MIDI note number as the "integral" part of the pitch ; and the pitch bend as a redundant "fractional" part of the pitch.