Advertisement
DeaD_EyE

calc_freq_from_midi

Jul 4th, 2018
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.88 KB | None | 0 0
  1. """
  2. This request comes from a forum.
  3. He wanted to do a fft and plot instead the frequencies the notes.
  4. BTW: I have no clue about music...
  5. """
  6.  
  7. from itertools import product
  8.  
  9.  
  10. def calc_freq_from_midi(m, base_freq=440):
  11.     if m < 0 or m > 127:
  12.         raise ValueError('m must be between 0 and 127.')
  13.     notes = ('C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B')
  14.     octaves = range(-1, 10)
  15.     notes = product(octaves, notes)
  16.     notes = [note + str(band) for band, note in notes]
  17.     freq = base_freq * 2 ** ((m - 69)/12)
  18.     return freq, notes[m]
  19.  
  20. def printer(labels, even=False):
  21.     for freq, note in labels:
  22.         if not even or freq.is_integer():
  23.             print(f'{freq:>10.2f} Hz: {note}')
  24.  
  25. labels440 = [calc_freq_from_midi(m) for m in range(128)]
  26. labels432 = [calc_freq_from_midi(m, 432) for m in range(128)]
  27.  
  28. printer(labels440)
  29. printer(labels432)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement