Created
June 12, 2022 08:49
-
-
Save Cynthia7979/0978bae8d929efbecc037c88d529e571 to your computer and use it in GitHub Desktop.
Convert a series of notes into frequency
This file contains hidden or 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
""" | |
Note To Frequency Converter | |
MIT License 2022 | |
Inspired by stuartmemo's JavaScript version | |
See https://gist.github.com/stuartmemo/3766449 for more info and this gist in more languages | |
""" | |
NOTES = ('a', 'a#', 'b', 'c', 'c#', 'd', 'd#', 'e', 'f', 'f#', 'g', 'g#') | |
def freq(note): | |
if len(note) == 3: | |
octave = int(note[2]) | |
else: | |
octave = int(note[1]) | |
key_no = NOTES.index(note[:-1]) | |
if key_no < 3: | |
key_no = key_no + 12 + ((octave - 1) * 12) + 1 | |
else: | |
key_no = key_no + ((octave - 1) * 12) + 1 | |
return 440 * (2 ** ((key_no - 49) / 12)) | |
# Test | |
print(freq('a4')) | |
# Actual - change the BPM to alter duration, or assign duration values yourself. The time shown below is in seconds. | |
bpm = 90.0 | |
quad_dur = round(60.0 / bpm, 3) | |
eighth_dur = round(quad_dur / 2.0, 3) | |
sixteenth_dur = round(eighth_dur / 2.0, 3) | |
# If using Assembly, this may also come in handy. | |
# eighth_dur = 2 | |
# sixteenth_dur = 1 | |
# Transcribe your notes in here | |
sheet = [ | |
('d#5', eighth_dur), | |
('d5', eighth_dur), | |
('a#5', eighth_dur), | |
('a5', sixteenth_dur), | |
('g5', sixteenth_dur), | |
('f#5', eighth_dur), | |
('g5', eighth_dur), | |
('a5', eighth_dur), | |
('c5', eighth_dur), | |
('d#5', eighth_dur), | |
('d5', eighth_dur), | |
('c6', eighth_dur), | |
('a#5', sixteenth_dur), | |
('a5', sixteenth_dur), | |
('g5', eighth_dur), | |
('a5', eighth_dur), | |
('a#5', eighth_dur), | |
('e5', eighth_dur), | |
('g5', eighth_dur), | |
('a5', eighth_dur), | |
('a#5', eighth_dur), | |
('e5', eighth_dur), | |
('g5', eighth_dur), | |
('a5', eighth_dur), | |
('a#5', eighth_dur), | |
('e6', eighth_dur), | |
('d6', eighth_dur), | |
('d6', eighth_dur), | |
('c6', eighth_dur), | |
('c6', eighth_dur), | |
('a#5', eighth_dur), | |
('a#5', eighth_dur), | |
('a5', eighth_dur), | |
('a#4', eighth_dur), | |
('d#5', eighth_dur), | |
('d5', eighth_dur), | |
('a#5', eighth_dur), | |
('a5', sixteenth_dur), | |
('g5', sixteenth_dur), | |
('f#5', eighth_dur), | |
('g5', eighth_dur), | |
('a5', eighth_dur), | |
('c5', eighth_dur), | |
('d#5', eighth_dur), | |
('d5', eighth_dur), | |
('c6', eighth_dur), | |
('a#5', sixteenth_dur), | |
('a5', sixteenth_dur), | |
('g5', eighth_dur), | |
('a5', eighth_dur), | |
('a#5', eighth_dur), | |
('e5', eighth_dur), | |
('g5', eighth_dur), | |
('a5', eighth_dur), | |
('a#5', eighth_dur), | |
('e5', eighth_dur), | |
('f5', eighth_dur), | |
('f#5', eighth_dur), | |
('g5', eighth_dur), | |
('e6', eighth_dur), | |
('d6', eighth_dur), | |
('d6', eighth_dur), | |
('c6', eighth_dur), | |
('c6', eighth_dur), | |
('a#5', eighth_dur), | |
('a#5', eighth_dur), | |
('a5', eighth_dur), | |
('a#4', eighth_dur) | |
] | |
# This export format is for Assembly specifially. You may write your own version if you need. | |
def export(sheet): | |
lines = ['FREQ_L\tDW\t'] | |
first_note = True | |
for note, dur in sheet: | |
f = freq(note) | |
if len(lines[-1] + f', {int(f)}, {dur}') > 80: | |
lines.append(f'\tDW\t{int(f)}, {dur}') | |
else: | |
if not first_note: | |
lines[-1] += ', ' | |
else: | |
first_note = False | |
lines[-1] += f'{int(f)}, {dur}' | |
return '\n'.join(lines) | |
if __name__ == '__main__': | |
export(sheet) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment