Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- This request comes from a forum.
- He wanted to do a fft and plot instead the frequencies the notes.
- BTW: I have no clue about music...
- """
- from itertools import product
- def calc_freq_from_midi(m, base_freq=440):
- if m < 0 or m > 127:
- raise ValueError('m must be between 0 and 127.')
- notes = ('C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B')
- octaves = range(-1, 10)
- notes = product(octaves, notes)
- notes = [note + str(band) for band, note in notes]
- freq = base_freq * 2 ** ((m - 69)/12)
- return freq, notes[m]
- def printer(labels, even=False):
- for freq, note in labels:
- if not even or freq.is_integer():
- print(f'{freq:>10.2f} Hz: {note}')
- labels440 = [calc_freq_from_midi(m) for m in range(128)]
- labels432 = [calc_freq_from_midi(m, 432) for m in range(128)]
- printer(labels440)
- printer(labels432)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement