Calculating the Frequency of a Note

In western music theory, there are 12 distinct keys in an octave:
A, A#, B, C, C#, D, D#, E, F, F#, G and G#.

Two octaves, the 3 and 4 octaves, color coded with yellow and green respectively.


A note is a combination of a key and an octave – where a note one octave higher than another of the same key will have twice the frequency.
Given an octave and a key, how do we find the frequency of that note?

TL;DR

For those that just need to formula, let’s give it to you so you can be on your way!

\begin{aligned}
d = SemitoneDst(note, A_4) \\
Freq(note) = 440.0 * 2^{d/12.0}
\end{aligned}

d is an integer semitone distance from the A4 key (with A4 tuned to 440hz). Just plug that value in as part of the exponent, where every 12 semitones up, the note frequency doubles, and every 12 semitones down, the frequency halves.

Here’s the formula graphed in Desmos; you can test it against a table of notes that are tuned to A4 being 440hz.

Interval Distance

In order to calculate the frequencies of notes, we’re going to have to cover the concept of measuring distances between notes. The distance between one note to its closest neighboring note is called an interval.

The system of music we’re all used to involves 12 intervals. Each of those intervals is called a key and named after a letter of the alphabet. This is what is called Western Music Theory. This is what we hear all the time in rock, and pop, and rap, and polka, and western folk, flamenco, grindcore, etc – actually, everything. Especially everything.

A semitone is an interval movement from one key to the next one in a 12 note system – either directly before or directly after. This includes moving from a white to the directly-neighboring white key (e.g., F3 to E3) or a white to the directly-neighboring black key (F3 to F#3) and vice-versa.

Illustration Below showing a distance of a semitone for various notes on a keyboard.

Arrows showing the distance of a semitone for various notes.

Two of these movements in one direction are called a tone (e.g., E3 to F#3).

Illustration Below showing the distance of a tone for various notes on a keyboard.

Arrow showing the distance of a tone for various notes. Note the distance is two semitones.

Keep in mind there are other systems. They tend to often keep the octave system called equal temperament. This is where the same key that’s one octave higher is twice the frequency, but instead of 12, they instead divide an octave by a different number of intervals. For example, quartertone music divided an octave by 24 tones. Anything more than 12 keys is called microtonal. Here is an example of a 22 tone song by microtonal artist Sevish.

Why Would I Need to Calculate This?

This article is part of a series covering the theory of a codebase for a Unity project. Therefore Unity engine code and C# will be used when it’s time to start showing code. The principles and foundations are independent of Unity or digital audio – it’s general music theory math.

So, why would anyone need to know this?

  • As a musician, you don’t.
  • As a sound technician, you probably don’t, but it’s relevant.
  • As a (non-musical) audio programmer, you probably don’t, but it’s relevant.
  • As a programmer for a DAW or synth, it’s your base principle, an important foundation.

Also, if you’re a (digital) music enthusiast just wanting to always know more technical things it’s an interesting tidbit.

Some Context

Before we get into denser theory, let’s point out a couple of things.

  • An octave starts at the C key. There’s nothing mathematical about it, but it is a popular way that people notate these notes. We will be coding to adapt to that.
  • When saying these note names or looking at them on a piano, there are these black keys that we call sharps (#) and flats (b). While they’re relevant to music theory when calculating the frequency this difference does not matter. The math doesn’t change between black and white keys.

For more discussion on why an octave starts with the letter C, here’s a recommended StackExchange article.
And an additional link on the same subject, from Ars Nova.

Psychoacoustics

As mentioned before, in each octave, the notes will have double the frequency of the previous octave. Why? Who got to decide this? What mad scientist concocted this piece of mad math in their mad basement!? Well, no one. Or, well, everyone? Unlike some cases where a dude just “invents” math, this is more about a perception all humans inherently share.

There a branch of science that deals with sound and psychology called psychoacoustics. It focuses on how living beings (specifically humans) perceive sounds and music on a physiological, psychological, and biological level. So while psychology and wave physics are involved – it’s less focused on those pure disciplines and more focused on how they meet together to describe our perception of sound.

Laaaaaaarge words!

Why is a note that’s one octave higher than it’s previous key exactly twice the frequency? Because humans can hear and recognize that pattern and find it harmonious and pleasing. Everything else in this article that complicates things with theory and math (cursed math!) is based on that.

So what’s the explanation for 12 semitones in an octave? Why is that the undisputed standard? Sorry, not sure, don’t care. That’s just something I accept. Feel free to do your own research on that. Although if I had to throw out a hypothesis, I imagine any kind of music (specifically the number of keys in an octave) is an acquired taste – and this is the brand of music we’ve been widely exposed to (since birth) and have culturally inherited on a massive and historical scale.

For more discussion on why there are twelve notes in an octave, here’s a recommended StackExchange article.

Tranposings and the A4 Pitch Standard

Recognizing the structure of music is more about relative multiplication between each of the notes rather than any exact pitch. This goes back to that study of psychoacoustics we just introduced.

Take these two tracks, they’re both the same jingle, except the left one starts on A4, and the right one starts on A#4. All I’ve done for the right side is just raise each note one semitone higher.

Demo tune.
Same tune, 1 interval higher.

You can easily notice the note change, right? But you can also tell they’re the exact same jingle, right? Even though it’s one semitone higher, which means each notes’ frequency is 1.059463 times greater than the semitone directly before it.

2^{1/12} = 1.059463

And we can start on any note and move it any number of semitones up or down (technically, it doesn’t even have to be a semitone increment, we could just multiply all note frequencies by any arbitrary constant number). What makes the music is really the relative multiplication factors between each note and their timings to each other. This ability to move a song in its frequency is called transposing. You can transpose it up or down by any amount, but you’ll usually want to place your notes in a range that’s familiar for musicians, and that isn’t too high pitched or low pitched.

So if that’s the case, where do we start? If everything is relative, is there a baseline? Yes. If anything, just so multiple instruments can synchronize frequencies and be in tune with each other. Often A4 is chosen to have a value of 440hz – and every other note is a certain multiple of that based on its semitone distance from A4. [stackex]

The Code

So to recap:

  • An octave is divided by several intervals called keys.
  • There are 12 in the Western Musical system, 7 letters (white keys), and 5 in betweens (black keys/sharps & flats).
  • A note comprised of a key and an octave. (e.g., if the key is A and the octave is 5, the note is an A5)
  • The frequency of a key in an octave is twice the frequency of its previous octave.
  • Each key in an octave is a consistent power-of-two multiple to another key that’s in another octave.
\begin{aligned}
d = IntervalDst(note, A_4) \\
st = NumIntervalsAnOctave\\
Freq(note) = 440.0 * 2^{d/st}
\end{aligned}

With st being 12 for Western Music Theory’s 12 intervals.

Here are some snippets from the utility class that calculate note frequencies based on a key and octave. It’s C# that was originally coded for use in Unity.

public static class WesternFreqUtils
{
    public enum Key
    {
        A,  As,
        B,
        C,  Cs,
        D,  Ds,
        E,
        F,  Fs,
        G,  Gs
    }
...

A big issue is an octave not starting at A – the lowest alphabetical letter. It starts at C. There are different ways we could handle this, including having the Key enum start at C (and after G, it would wrap back to A, A#, and B) – since they’re just names when you think about it until we assign it a value by ordering it in the enum. But I decided to leave the Key enum starting at A because DagNAMMIT! That’s how alphabets start!

Next, we create a function that assigns an integer semitone value to a key and octave. The exact number isn’t important. What’s important is that returned values are relatively correct – i.e., a note a that’s n semitones more than b returns a value that’s greater by n when they’re both calculated by this function. To handle the octave change in the middle, we remap the enum values so that C is 0, and then it wraps around after G# all the way to up B.

...

    public static int GetNote(Key k, int octave)
    {
        int octbase = octave * 12;

        switch (k)
        {
            case Key.C:
                return octbase + 0;

            case Key.Cs:
                return octbase + 1;

            case Key.D:
                return octbase + 2;

            case Key.Ds:
                return octbase + 3;

            case Key.E:
                return octbase + 4;

            case Key.F:
                return octbase + 5;

            case Key.Fs:
                return octbase + 6;

            case Key.G:
                return octbase + 7;

            case Key.Gs:
                return octbase + 8;

            case Key.A:
                return octbase + 9;

            case Key.As:
                return octbase + 10;

            case Key.B:
                return octbase + 11;

            default:
                return -1;
        }
    }
    
...

And here’s the function we’ve all been waiting for, GetFrequency()!

...

    public static float GetFrequency(Key k, int octave)
    {
        // Get the integer assignment of A4
        int baseline = GetNote(Key.A, 4);
        
        // The the integer assignment of our key in question
        int key = GetNote(k, octave);
        
        // Get the difference (in semitones) between A4 and our key
        int diff = key - baseline;

        const float A4Fr = 440.0f;
        return A4Fr * Mathf.Pow(2.0f, (float)diff / 12.0f);
    }
    
...

And just for completeness, a function that inverts GetNote(), and a function that calculates note frequency based on the result from GetNote().

...
    public static void GetKeyInfo(int key, out Key n, out int octave)
    {
        octave = key / 12;
        int st = key % 12;

        switch (st)
        {
            case 0:
                n = Key.C;
                break;

            case 1:
                n = Key.Cs;
                break;

            case 2:
                n = Key.D;
                break;

            case 3:
                n = Key.Ds;
                break;

            case 4:
                n = Key.E;
                break;

            case 5:
                n = Key.F;
                break;

            case 6:
                n = Key.Fs;
                break;

            case 7:
                n = Key.G;
                break;

            case 8:
                n = Key.Gs;
                break;

            case 9:
                n = Key.A;
                break;

            case 10:
                n = Key.As;
                break;

            case 11:
                n = Key.B;
                break;

            default:
                n = Key.C;
                octave = -1;
                break;
        }
    }

    public static float GetFrequency(int note)
    {
        Key k;
        int o;
        GetKeyInfo(note, out k, out o);

        return GetFrequency(k, o);
    }
...
}

Conclusion

I hope to get around using this to procedurally play notes with the AudioClip class in Unity and then move on to dynamic streaming. The next post is going to be on primitive waves used for generating tones.

So before we end the article, lets recap the major points:

  • The formula for calculating the frequency of a tone was covered.
    • Each octave is twice the frequency of the previous octave.
    • An octave in Western Music Theory is divided into 12 notes.
    • The note A4 is agreed to be 440.0hs to establish a baseline.
  • The basic concept of psychoacoustics was covered, along with how the recognizable music we hear involves the ratios between notes rather than their actual literal frequencies.
  • Some source code was covered, including dealing with octaves’ eccentricity starting at the letter C instead of A.

– Stay strong, code on. William Leu.

Explore more articles here.
Explore more audio articles here.