Calculating note frequencies

The formula for calculating a frequency of a note is something like this:

freq=440*(2^((nIdx-57)/12))

Where the nIdx variable is the index of a note starting from 0.
Here are the frequencies of the zeroth octave:

nIdx Note Freq Freq as int
0 C 16.3516 1071618
1 C# 17.3239 1135339
2 D 18.3540 1202847
3 D# 19.4454 1274373
4 E 20.6017 1350153
5 F 21.8268 1430441
6 F# 23.1247 1515500
7 G 24.4997 1605612
8 G# 25.9565 1701085
9 A 27.5000 1802240
10 A# 29.1352 1909404
11 B 30.8677 2022945

If like me you prefer not to deal with floats and doubles, then you can multiply these numbers by a large constant… 65536 is a popular choiche for instance.

To save memory (my MCU for instance has only 4KB RAM) you can just precalculate the first (zeroth) octave frequencies and derive the frequencies of other octaves with a formula like:

freq=(1<<octave)*freqLookup[midiNoteID%12]

Where octave=midiNoteID/12 and midiNoteID is one from the following table…

 Octave C C# D D# E F F# G G# A A# B
0 0 1 2 3 4 5 6 7 8 9 10 11
1 12 13 14 15 16 17 18 19 20 21 22 23
2 24 25 26 27 28 29 30 31 32 33 34 35
3 36 37 38 39 40 41 42 43 44 45 46 47
4 48 49 50 51 52 53 54 55 56 57 58 59
5 60 61 62 63 64 65 66 67 68 69 70 71
6 72 73 74 75 76 77 78 79 80 81 82 83
7 84 85 86 87 88 89 90 91 92 93 94 95
8 96 97 98 99 100 101 102 103 104 105 106 107
9 108 109 110 111 112 113 114 115 116 117 118 119
10 120 121 122 123 124 125 126 127

Thats really it.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.