 # Midi note in hertz

For the built in function of getting the midi note pressed in hertz, could someone explain to me what is happening here?

``````double MidiMessage::getMidiNoteInHertz (const int noteNumber, const double frequencyOfA) noexcept
{
return frequencyOfA * std::pow (2.0, (noteNumber - 69) / 12.0);
}
``````

Thank you

In equal temperament, the ratio between two frequencies as a function of pitch is

``````F(n) = F2/F1 = 2^(n /12)
``````

Where `F2` is the frequency of the second pitch, `F1` is the frequency of the first (in hertz) and `n` is the difference between the two pitches, in semitones (1/12 of an octave).

So to compute the frequency of one pitch, you need the frequency of another. In this case they use A4 as the reference pitch, which is (normally) tuned to 440Hz.

The difference in semitones is the difference in MIDI note number, since there is one note number per semitone. So `noteNumber - 69` is the difference in semitones between the pitch and A4 (midi note #69).

1 Like

Note, that 440Hz is by far not undisputed https://en.wikipedia.org/wiki/Concert_pitch#Current_concert_pitches

Nearly all modern symphony orchestras in Germany and Austria and many in other countries in continental Europe (such as Russia, Sweden and Spain) tune to A = 443 Hz.

So good to keep it configurable…

Additionally, for example, 1 octave down would be:
frequencyOfA times 2 to the power of -1, i.e. 1/2.

2 to the power of 1/12, times itself 12 times, gives you 2; and doubling the frequency of a sound raises it 1 octave, so that is 12 equal divisions of an octave.
I know this is super obvious to some, but maybe the OP could use more clarity!

So good to keep it configurable

Not a bad idea to keep temperament configurable, either. It’s a cool feature that should be more common.

2 Likes

I agree completely. It seems absurd for frequency to be limited to 7 bits when most software is using 64 bits internally for audio calculations, and, e.g., 120 ticks per quarter note is not uncommon. The fineness of both DSP and rhythm are treated with great precision, but the actual pitches of notes are considered comparatively inconsequential.