Skip to content

Instantly share code, notes, and snippets.

@victormurcia
Created September 3, 2022 03:05
Show Gist options
  • Save victormurcia/abefa6eec9e940e3a4c5873e75310572 to your computer and use it in GitHub Desktop.
Save victormurcia/abefa6eec9e940e3a4c5873e75310572 to your computer and use it in GitHub Desktop.
Piano notes to frequencies converter
def get_piano_notes():
# White keys are in Uppercase and black keys (sharps) are in lowercase
octave = ['C', 'c', 'D', 'd', 'E', 'F', 'f', 'G', 'g', 'A', 'a', 'B']
base_freq = 440 #Frequency of Note A4
keys = np.array([x+str(y) for y in range(0,9) for x in octave])
# Trim to standard 88 keys
start = np.where(keys == 'A0')[0][0]
end = np.where(keys == 'C8')[0][0]
keys = keys[start:end+1]
note_freqs = dict(zip(keys, [2**((n+1-49)/12)*base_freq for n in range(len(keys))]))
note_freqs[''] = 0.0 # stop
return note_freqs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment