Skip to content

Instantly share code, notes, and snippets.

@joxn
Last active September 7, 2018 14:35
Show Gist options
  • Save joxn/8c96e5bea8e24dd0951c272ad390913e to your computer and use it in GitHub Desktop.
Save joxn/8c96e5bea8e24dd0951c272ad390913e to your computer and use it in GitHub Desktop.
Convert GB note numbers to piano notes
import math, itertools
def keyno(f):
return 12 * math.log2(f/440) + 49
def gbfreq(n):
return 131072/(2048-n)
def keyfreq(k):
return math.pow(2, (k - 49)/12) * 440
def temper_cents(freq, actual):
return 1200 * math.log2(freq/actual)
scale = itertools.cycle('C C♯ D D♯ E F F♯ G G♯ A A♯ B'.split())
oct = '₂ ₃ ₄ ₅ ₆ ₇ ₈'.split()
keys = {}
def update_keys(n):
freq = gbfreq(n)
key = round(keyno(freq))
actual = keyfreq(key)
if not (1 <= key <= 88):
return
temper = temper_cents(freq, actual)
cn, cf, ca, ct = keys.setdefault(key, (n, freq, actual, temper))
if abs(temper) < abs(ct):
keys[key] = (n, freq, actual, temper)
for n in range(2048):
update_keys(n)
head = '\nnote gb_n key# Hz actual err'
cur_o = None
for (k, (n, freq, actual, temper)), note in zip(keys.items(), scale):
o = oct[(k-16)//12]
if o != cur_o:
print(head)
cur_o = o
print(f'{note+o:4s} {n:4d} {k:3d} {round(freq):4d} {round(actual):4d} {round(temper): >+3d}¢')
@joxn
Copy link
Author

joxn commented Sep 7, 2018

note gb_n  key#   Hz actual  err
C₂     44    16   65     65  +0¢
C♯₂   157    17   69     69  +0¢
D₂    263    18   73     73  +0¢
D♯₂   363    19   78     78  +0¢
E₂    457    20   82     82  +0¢
F₂    547    21   87     87  +0¢
F♯₂   631    22   92     92  +0¢
G₂    711    23   98     98  +1¢
G♯₂   786    24  104    104  +1¢
A₂    856    25  110    110  -1¢
A♯₂   923    26  117    117  +0¢
B₂    986    27  123    123  -1¢

note gb_n  key#   Hz actual  err
C₃   1046    28  131    131  +0¢
C♯₃  1102    29  139    139  +0¢
D₃   1155    30  147    147  -1¢
D♯₃  1205    31  155    156  -1¢
E₃   1253    32  165    165  +1¢
F₃   1297    33  175    175  -1¢
F♯₃  1339    34  185    185  -1¢
G₃   1379    35  196    196  -1¢
G♯₃  1417    36  208    208  +1¢
A₃   1452    37  220    220  -1¢
A♯₃  1486    38  233    233  +1¢
B₃   1517    39  247    247  -1¢

note gb_n  key#   Hz actual  err
C₄   1547    40  262    262  +0¢
C♯₄  1575    41  277    277  +0¢
D₄   1602    42  294    294  +1¢
D♯₄  1627    43  311    311  +1¢
E₄   1650    44  329    330  -2¢
F₄   1673    45  350    349  +1¢
F♯₄  1694    46  370    370  +1¢
G₄   1714    47  392    392  +2¢
G♯₄  1732    48  415    415  -2¢
A₄   1750    49  440    440  -1¢
A♯₄  1767    50  466    466  +1¢
B₄   1783    51  495    494  +3¢

note gb_n  key#   Hz actual  err
C₅   1798    52  524    523  +3¢
C♯₅  1812    53  555    554  +3¢
D₅   1825    54  588    587  +1¢
D♯₅  1837    55  621    622  -3¢
E₅   1849    56  659    659  -2¢
F₅   1860    57  697    698  -3¢
F♯₅  1871    58  741    740  +1¢
G₅   1881    59  785    784  +2¢
G♯₅  1890    60  830    831  -2¢
A₅   1899    61  880    880  -1¢
A♯₅  1907    62  930    932  -5¢
B₅   1915    63  986    988  -4¢

note gb_n  key#   Hz actual  err
C₆   1923    64 1049   1047  +3¢
C♯₆  1930    65 1111   1109  +3¢
D₆   1936    66 1170   1175  -6¢
D♯₆  1943    67 1248   1245  +5¢
E₆   1949    68 1324   1319  +7¢
F₆   1954    69 1394   1397  -3¢
F♯₆  1959    70 1473   1480  -9¢
G₆   1964    71 1560   1568  -8¢
G♯₆  1969    72 1659   1661  -2¢
A₆   1974    73 1771   1760 +11¢
A♯₆  1978    74 1872   1865  +7¢
B₆   1982    75 1986   1976  +9¢

note gb_n  key#   Hz actual  err
C₇   1985    76 2081   2093 -10¢
C♯₇  1989    77 2222   2217  +3¢
D₇   1992    78 2341   2349  -6¢
D♯₇  1995    79 2473   2489 -11¢
E₇   1998    80 2621   2637 -10¢
F₇   2001    81 2789   2794  -3¢
F♯₇  2004    82 2979   2960 +11¢
G₇   2006    83 3121   3136  -8¢
G♯₇  2009    84 3361   3322 +20¢
A₇   2011    85 3542   3520 +11¢
A♯₇  2013    86 3745   3729  +7¢
B₇   2015    87 3972   3951  +9¢

note gb_n  key#   Hz actual  err
C₈   2017    88 4228   4186 +17¢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment