Last active
September 7, 2018 14:35
-
-
Save joxn/8c96e5bea8e24dd0951c272ad390913e to your computer and use it in GitHub Desktop.
Convert GB note numbers to piano notes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}¢') |
Author
joxn
commented
Sep 7, 2018
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment