Mapping frequencies to midi notes

Hello, again!

I would like to find a way to map frequencies into midi notes. I don’t know the best method to do this, since the frequency domain is continuous and the midi notes domain are discrete (note 1, note 2, note 3, etc…).

So I have the following problems:

  1. How can I get the closest midi note relative to the given frequency.

  2. The second problem is to play that midi note a little shifted from its assigned frequency. Thus I would get the exact pitch which the input frequency is.

In respect to the problem (1), does anyone have a C++ array which I can map frequencies do midi notes? And about (2), Is there a way to send a “noteOn” midi message that we can send a shift-note information together?


Just reverse the formula used in MidiMessage::getMidiNoteInHertz…

With 440 Hz at middle A (midi note 69) you get:

Hertz = 440.0 * pow(2.0, (midi note - 69)/12);


midi note = log(Hertz/440.0)/log(2) * 12 + 69;

If you want to play the correct frequency from that, you need to pitch bend from the midi note. Say you want 430.3 Hz when having +/- 200 cents pitch bend:

1 cent = 1/1200 octave

log(430.3/440)/log(2) * 1200 ~= -39 cents

with +8191/-8192 resolution of pitch bend that would be:

pitch bend value = 8192 * -39/200 = -1597

Hope that helps…