Skip to content

How to calculate notes and frequencies?

arbitrary hexcode edited this page Dec 29, 2022 · 44 revisions

12-tone System Definition

There seems to be much terminology to refer to the same scale: (Western scale, 12-tone scale, 12-tet, Chromatic scale, equal-temperament).

The following calculations and formulas aim to conform to Scientific Pitch Notation (SPN) / International Pitch Notation (IPN). It adheres to these definitions and reference frequencies:

  • An octave number increases by 1 upon an ascension from B to C.
  • 12 intervals: semitones per octave.
  • 1 semitone = 100 cents. (1 semitone is subdivided into 100 cents)
  • Frequencies calculated using "twelfth root of two" rule: 2^(1/12)
  • 1 cent = 2^(1/12) = 1.05946
  • 1 octave = 1200 cents. (12 * 100). A frequency ratio of 2:1
  • 1 Hz = 1 cycle / second.
  • 1 wholetone = 1 step
  • 1 semitone = 1/2 step
  • Reference notes:
    • Middle C = C4
    • A4 = MIDI Note 69
    • A440 = 440 Hz

A semitone is a musical interval that corresponds to the smallest step on a standard piano keyboard. It is the interval between one key and the next adjacent key on the keyboard, whether white or black. In Western music, a semitone is equal to half of a whole tone, which is the difference between two adjacent keys.

  • For example, the interval between the notes C and C# is a semitone, as is the interval between B and C.

On a guitar, each fret is a semitone.

Code snippets are python, but the formulas and definitions should be simple enough to understand and implement in any language.

Defining notes

We can define 12 notes from A to G#. Here it is alphabetically:

notes = ('A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#')

However, I prefer to start at C instead of A here because piano nomenclature & music theory is typically represented as "starting on C". (This also makes some calculations simple as C will be used as a reference point in later calculations.)

notes = ('C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B')

Note: use of tuple instead of an array to keep this definition immutable / read-only.

todo: handle sharps and flats definition appropriately

A♯ = B♭
C♯ = D♭ 
D♯ = E♭
F♯ = G♭
G♯ = A♭

Calculating pitch for note using the Twelfth Root Of Two

Pitch of a note is typically expressed in terms of its frequency in hertz (Hz).

frequency = reference frequency * 2^(steps from reference note / intervals)

The reference frequency is the pitch of the reference note, which is typically A4 (also known as A440) with a frequency of 440 Hz. There are 12 semitones in equal temperament. A semitone has 100 cents.

1 cent = 2^(1/12)

The steps from reference note is the number of steps or intervals the note is from the reference note on the musical scale.

  • intervals = 12
  • A4 = 440.0Hz
  • A = 9

f = 440 * 2^((n-9)/12)

('C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B').index('A')
>>> 9

For example, if you want to calculate the pitch of C4 (middle C), you would use the following calculation:

pitch = 440 * (2^((0-9) / 12)) = 261.6255653005986

Frequency of C4 = 261.62 Hz. Where reference note = A440 & C = 0:

Mapping MIDI key ID's to frequencies

Using A4 as a reference note. The MIDI note for A4 is 69. steps from reference note = (midi_note - a4_key_midi). Given MIDI note m:

f = 440 * 2^((m-69)/12)

pitch = 440 * (2^((60-69) / 12)) = 261.6255653005986

def midi_note_to_frequency(midi_note):
    a4_freq = 440.0
    a4_key_midi = 69    
    return a4_freq * math.pow(2, (midi_note - a4_key_midi) / 12)

Converting frequency to cents

A cent is a unit of measure used to describe the difference in pitch between two notes. It is equal to 1/100th of a semitone, which is the smallest interval commonly used in Western 12-tone music. For example are 100 cents between notes C4 and C#4.

To convert the frequency of a note to cents, you can use the following formula:

cents = 1200 * log2(frequency / reference frequency)

1200 = 12 notes * 100 cents. The reference frequency is a pitch that is considered to be the "zero" point for the scale, against which all other pitches are measured. For example, in equal temperament, the reference pitch is typically the note A4 (also known as A440), which has a frequency of 440 Hz.

Therefore, to calculate the number of cents for a given frequency in equal temperament, you can use the following formula:

cents = 1200 * log2(frequency / 440)

For example, if you want to calculate the number of cents for a frequency of 220 Hz (which is the pitch of A3), you would use the following calculation:

cents = 1200 * log2(220 / 440) = -1200

This means that the pitch of A3 is 1200 cents lower than the reference pitch of A4. A3 is 12 semitones, or 1 octave less than A4.

Another method using C2 as reference frequency:

def frequency_to_cents(freq):
    c2_freq = 65.40639  # C2 used as base "Low C"
    return frequency_to_cents(freq, c2_freq)


def frequency_to_cents(freq, base_freq):
    return (math.log(freq) - math.log(base_freq)) * 1200.0 * math.log2(math.e)

What are all the scales?

define scale: A Scale is a group of 2 or more semitones. A minor third is 3 semitones, a major third is 4 semitones, and perfect fifth is 7 semitones.

2048 scales.

def todo

A432 and other tuning

def todo  

Pythagorean Tuning

def todo  

just give me data

Note and pitch from octave -1 to 13 (C-1 | 8.175799 | 0 to B13 | 252868.250243 | 179 ) and a reference tuning of A4@440Hz:

Note Pitch (Hz) MIDI Note
C-1 8.175799 0
C#-1 8.661957 1
D-1 9.177024 2
D#-1 9.722718 3
E-1 10.300861 4
F-1 10.913382 5
F#-1 11.562326 6
G-1 12.249857 7
G#-1 12.978272 8
A-1 13.75 9
A#-1 14.567618 10
B-1 15.433853 11
C0 16.351598 12
C#0 17.323914 13
D0 18.354048 14
D#0 19.445436 15
E0 20.601722 16
F0 21.826764 17
F#0 23.124651 18
G0 24.499715 19
G#0 25.956544 20
A0 27.5 21
A#0 29.135235 22
B0 30.867706 23
C1 32.703196 24
C#1 34.647829 25
D1 36.708096 26
D#1 38.890873 27
E1 41.203445 28
F1 43.653529 29
F#1 46.249303 30
G1 48.999429 31
G#1 51.913087 32
A1 55.0 33
A#1 58.27047 34
B1 61.735413 35
C2 65.406391 36
C#2 69.295658 37
D2 73.416192 38
D#2 77.781746 39
E2 82.406889 40
F2 87.307058 41
F#2 92.498606 42
G2 97.998859 43
G#2 103.826174 44
A2 110.0 45
A#2 116.54094 46
B2 123.470825 47
C3 130.812783 48
C#3 138.591315 49
D3 146.832384 50
D#3 155.563492 51
E3 164.813778 52
F3 174.614116 53
F#3 184.997211 54
G3 195.997718 55
G#3 207.652349 56
A3 220.0 57
A#3 233.081881 58
B3 246.941651 59
C4 261.625565 60
C#4 277.182631 61
D4 293.664768 62
D#4 311.126984 63
E4 329.627557 64
F4 349.228231 65
F#4 369.994423 66
G4 391.995436 67
G#4 415.304698 68
A4 440.0 69
A#4 466.163762 70
B4 493.883301 71
C5 523.251131 72
C#5 554.365262 73
D5 587.329536 74
D#5 622.253967 75
E5 659.255114 76
F5 698.456463 77
F#5 739.988845 78
G5 783.990872 79
G#5 830.609395 80
A5 880.0 81
A#5 932.327523 82
B5 987.766603 83
C6 1046.502261 84
C#6 1108.730524 85
D6 1174.659072 86
D#6 1244.507935 87
E6 1318.510228 88
F6 1396.912926 89
F#6 1479.977691 90
G6 1567.981744 91
G#6 1661.21879 92
A6 1760.0 93
A#6 1864.655046 94
B6 1975.533205 95
C7 2093.004522 96
C#7 2217.461048 97
D7 2349.318143 98
D#7 2489.01587 99
E7 2637.020455 100
F7 2793.825851 101
F#7 2959.955382 102
G7 3135.963488 103
G#7 3322.437581 104
A7 3520.0 105
A#7 3729.310092 106
B7 3951.06641 107
C8 4186.009045 108
C#8 4434.922096 109
D8 4698.636287 110
D#8 4978.03174 111
E8 5274.040911 112
F8 5587.651703 113
F#8 5919.910763 114
G8 6271.926976 115
G#8 6644.875161 116
A8 7040.0 117
A#8 7458.620184 118
B8 7902.13282 119
C9 8372.01809 120
C#9 8869.844191 121
D9 9397.272573 122
D#9 9956.063479 123
E9 10548.081821 124
F9 11175.303406 125
F#9 11839.821527 126
G9 12543.853951 127
G#9 13289.750323 128
A9 14080.0 129
A#9 14917.240369 130
B9 15804.26564 131
C10 16744.036179 132
C#10 17739.688383 133
D10 18794.545147 134
D#10 19912.126958 135
E10 21096.163642 136
F10 22350.606812 137
F#10 23679.643054 138
G10 25087.707903 139
G#10 26579.500645 140
A10 28160.0 141
A#10 29834.480737 142
B10 31608.53128 143
C11 33488.072358 144
C#11 35479.376765 145
D11 37589.090293 146
D#11 39824.253916 147
E11 42192.327285 148
F11 44701.213623 149
F#11 47359.286107 150
G11 50175.415806 151
G#11 53159.00129 152
A11 56320.0 153
A#11 59668.961474 154
B11 63217.062561 155
C12 66976.144717 156
C#12 70958.75353 157
D12 75178.180587 158
D#12 79648.507833 159
E12 84384.65457 160
F12 89402.427247 161
F#12 94718.572214 162
G12 100350.831611 163
G#12 106318.00258 164
A12 112640.0 165
A#12 119337.922949 166
B12 126434.125122 167
C13 133952.289434 168
C#13 141917.50706 169
D13 150356.361174 170
D#13 159297.015666 171
E13 168769.309139 172
F13 178804.854494 173
F#13 189437.144428 174
G13 200701.663223 175
G#13 212636.005161 176
A13 225280.0 177
A#13 238675.845897 178
B13 252868.250243 179

Typical range of human hearing is 20 Hz to 20,000 kHz. So you're unlikely to hear much of anything below octave 0 (though you may feel it) and anything above octave 10.

Shut up, I want sub octave data

These notes aren't within hearing, and the frequency is so low its not really sound and becomes meaningless. But if you really needed to know what frequency E-14 would be; it's 0.001257Hz and the MIDI note would be -152. You're welcome.

TODO: double check floating-point division, I don't trust the accuracy of these low calculations

Note and pitch from octave -14 to -2 (C-14 | 0.000998 | -156 to B-2 | 7.716927 | -1) and a reference tuning of A4@440 Hz:

Note Pitch (Hz) MIDI Note
C-14 0.000998 -156
C#-14 0.001057 -155
D-14 0.00112 -154
D#-14 0.001187 -153
E-14 0.001257 -152
F-14 0.001332 -151
F#-14 0.001411 -150
G-14 0.001495 -149
G#-14 0.001584 -148
A-14 0.001678 -147
A#-14 0.001778 -146
B-14 0.001884 -145
C-13 0.001996 -144
C#-13 0.002115 -143
D-13 0.00224 -142
D#-13 0.002374 -141
E-13 0.002515 -140
F-13 0.002664 -139
F#-13 0.002823 -138
G-13 0.002991 -137
G#-13 0.003169 -136
A-13 0.003357 -135
A#-13 0.003557 -134
B-13 0.003768 -133
C-12 0.003992 -132
C#-12 0.004229 -131
D-12 0.004481 -130
D#-12 0.004747 -129
E-12 0.00503 -128
F-12 0.005329 -127
F#-12 0.005646 -126
G-12 0.005981 -125
G#-12 0.006337 -124
A-12 0.006714 -123
A#-12 0.007113 -122
B-12 0.007536 -121
C-11 0.007984 -120
C#-11 0.008459 -119
D-11 0.008962 -118
D#-11 0.009495 -117
E-11 0.010059 -116
F-11 0.010658 -115
F#-11 0.011291 -114
G-11 0.011963 -113
G#-11 0.012674 -112
A-11 0.013428 -111
A#-11 0.014226 -110
B-11 0.015072 -109
C-10 0.015968 -108
C#-10 0.016918 -107
D-10 0.017924 -106
D#-10 0.01899 -105
E-10 0.020119 -104
F-10 0.021315 -103
F#-10 0.022583 -102
G-10 0.023926 -101
G#-10 0.025348 -100
A-10 0.026855 -99
A#-10 0.028452 -98
B-10 0.030144 -97
C-9 0.031937 -96
C#-9 0.033836 -95
D-9 0.035848 -94
D#-9 0.037979 -93
E-9 0.040238 -92
F-9 0.04263 -91
F#-9 0.045165 -90
G-9 0.047851 -89
G#-9 0.050696 -88
A-9 0.053711 -87
A#-9 0.056905 -86
B-9 0.060288 -85
C-8 0.063873 -84
C#-8 0.067672 -83
D-8 0.071695 -82
D#-8 0.075959 -81
E-8 0.080475 -80
F-8 0.085261 -79
F#-8 0.090331 -78
G-8 0.095702 -77
G#-8 0.101393 -76
A-8 0.107422 -75
A#-8 0.11381 -74
B-8 0.120577 -73
C-7 0.127747 -72
C#-7 0.135343 -71
D-7 0.143391 -70
D#-7 0.151917 -69
E-7 0.160951 -68
F-7 0.170522 -67
F#-7 0.180661 -66
G-7 0.191404 -65
G#-7 0.202785 -64
A-7 0.214844 -63
A#-7 0.227619 -62
B-7 0.241154 -61
C-6 0.255494 -60
C#-6 0.270686 -59
D-6 0.286782 -58
D#-6 0.303835 -57
E-6 0.321902 -56
F-6 0.341043 -55
F#-6 0.361323 -54
G-6 0.382808 -53
G#-6 0.405571 -52
A-6 0.429688 -51
A#-6 0.455238 -50
B-6 0.482308 -49
C-5 0.510987 -48
C#-5 0.541372 -47
D-5 0.573564 -46
D#-5 0.60767 -45
E-5 0.643804 -44
F-5 0.682086 -43
F#-5 0.722645 -42
G-5 0.765616 -41
G#-5 0.811142 -40
A-5 0.859375 -39
A#-5 0.910476 -38
B-5 0.964616 -37
C-4 1.021975 -36
C#-4 1.082745 -35
D-4 1.147128 -34
D#-4 1.21534 -33
E-4 1.287608 -32
F-4 1.364173 -31
F#-4 1.445291 -30
G-4 1.531232 -29
G#-4 1.622284 -28
A-4 1.71875 -27
A#-4 1.820952 -26
B-4 1.929232 -25
C-3 2.04395 -24
C#-3 2.165489 -23
D-3 2.294256 -22
D#-3 2.43068 -21
E-3 2.575215 -20
F-3 2.728346 -19
F#-3 2.890581 -18
G-3 3.062464 -17
G#-3 3.244568 -16
A-3 3.4375 -15
A#-3 3.641904 -14
B-3 3.858463 -13
C-2 4.087899 -12
C#-2 4.330979 -11
D-2 4.588512 -10
D#-2 4.861359 -9
E-2 5.150431 -8
F-2 5.456691 -7
F#-2 5.781163 -6
G-2 6.124929 -5
G#-2 6.489136 -4
A-2 6.875 -3
A#-2 7.283809 -2
B-2 7.716927 -1

The sounds of blackholes?

"In musical terms, the pitch of the sound generated by the black hole translates into the note of B flat. But, a human would have no chance of hearing this cosmic performance because the note is 57 octaves lower than middle-C." - https://science.nasa.gov/science-news/science-at-nasa/2003/09sep_blackholesounds/

`middle-C = C4. C4 - 57 = C-53'

B♭-53 =

Chandra X-ray Observatory observed the waves of pressure fronts propagating away from a black hole, their one oscillation every 10 million years:

1 Hz = 1 cycle / second
f = 1/10,000,000 years


w = angular frequency in radians per second = 2 * pi * frequency
frequency = (radians per second / (2 * pi)

bflat == a#

B♭−53 = 3.235fHz?

BTW: its midi note -??? to play C-53

Note Pitch (Hz) MIDI Note
-53