Sunday, March 13, 2016

20. Midi numbers and frequencies

Similar to mus11.py, the frequencies will be found for midi numbers between 0 and 127.


mus11.py used music21 to find the frequencies. It is more efficient to use mathematical formulas, rather than creating objects of various classes and then using their methods.


For every 12 midi numbers, the frequency increases by a factor of 2. Thus, for each midi number, the frequency increases by 12th power of 2, or 2**(1/12), with ** being the Python exponential operation. The frequency between two nearest midi numbers will be in variable g.


Further, the midi of 69 is 440 Hz, in music21.


The above can be used to create a lambda function.


# mus20.py
# Midi numbers and frequencies

g = 2**(1/12)

f = lambda midi: 440*g**(midi-69)

for midi in range(128):
    freq = f(midi)
    print('midi = {}, freq = {:.1f} Hz'.format(midi,freq))

This will generate this printout:


midi = 0, freq = 8.2 Hz
midi = 1, freq = 8.7 Hz
midi = 2, freq = 9.2 Hz
midi = 3, freq = 9.7 Hz
midi = 4, freq = 10.3 Hz
midi = 5, freq = 10.9 Hz
midi = 6, freq = 11.6 Hz
midi = 7, freq = 12.2 Hz
midi = 8, freq = 13.0 Hz
midi = 9, freq = 13.7 Hz
midi = 10, freq = 14.6 Hz
midi = 11, freq = 15.4 Hz
midi = 12, freq = 16.4 Hz
midi = 13, freq = 17.3 Hz
midi = 14, freq = 18.4 Hz
midi = 15, freq = 19.4 Hz
midi = 16, freq = 20.6 Hz
midi = 17, freq = 21.8 Hz
midi = 18, freq = 23.1 Hz
midi = 19, freq = 24.5 Hz
midi = 20, freq = 26.0 Hz
midi = 21, freq = 27.5 Hz
midi = 22, freq = 29.1 Hz
midi = 23, freq = 30.9 Hz
midi = 24, freq = 32.7 Hz
midi = 25, freq = 34.6 Hz
midi = 26, freq = 36.7 Hz
midi = 27, freq = 38.9 Hz
midi = 28, freq = 41.2 Hz
midi = 29, freq = 43.7 Hz
midi = 30, freq = 46.2 Hz
midi = 31, freq = 49.0 Hz
midi = 32, freq = 51.9 Hz
midi = 33, freq = 55.0 Hz
midi = 34, freq = 58.3 Hz
midi = 35, freq = 61.7 Hz
midi = 36, freq = 65.4 Hz
midi = 37, freq = 69.3 Hz
midi = 38, freq = 73.4 Hz
midi = 39, freq = 77.8 Hz
midi = 40, freq = 82.4 Hz
midi = 41, freq = 87.3 Hz
midi = 42, freq = 92.5 Hz
midi = 43, freq = 98.0 Hz
midi = 44, freq = 103.8 Hz
midi = 45, freq = 110.0 Hz
midi = 46, freq = 116.5 Hz
midi = 47, freq = 123.5 Hz
midi = 48, freq = 130.8 Hz
midi = 49, freq = 138.6 Hz
midi = 50, freq = 146.8 Hz
midi = 51, freq = 155.6 Hz
midi = 52, freq = 164.8 Hz
midi = 53, freq = 174.6 Hz
midi = 54, freq = 185.0 Hz
midi = 55, freq = 196.0 Hz
midi = 56, freq = 207.7 Hz
midi = 57, freq = 220.0 Hz
midi = 58, freq = 233.1 Hz
midi = 59, freq = 246.9 Hz
midi = 60, freq = 261.6 Hz
midi = 61, freq = 277.2 Hz
midi = 62, freq = 293.7 Hz
midi = 63, freq = 311.1 Hz
midi = 64, freq = 329.6 Hz
midi = 65, freq = 349.2 Hz
midi = 66, freq = 370.0 Hz
midi = 67, freq = 392.0 Hz
midi = 68, freq = 415.3 Hz
midi = 69, freq = 440.0 Hz
midi = 70, freq = 466.2 Hz
midi = 71, freq = 493.9 Hz
midi = 72, freq = 523.3 Hz
midi = 73, freq = 554.4 Hz
midi = 74, freq = 587.3 Hz
midi = 75, freq = 622.3 Hz
midi = 76, freq = 659.3 Hz
midi = 77, freq = 698.5 Hz
midi = 78, freq = 740.0 Hz
midi = 79, freq = 784.0 Hz
midi = 80, freq = 830.6 Hz
midi = 81, freq = 880.0 Hz
midi = 82, freq = 932.3 Hz
midi = 83, freq = 987.8 Hz
midi = 84, freq = 1046.5 Hz
midi = 85, freq = 1108.7 Hz
midi = 86, freq = 1174.7 Hz
midi = 87, freq = 1244.5 Hz
midi = 88, freq = 1318.5 Hz
midi = 89, freq = 1396.9 Hz
midi = 90, freq = 1480.0 Hz
midi = 91, freq = 1568.0 Hz
midi = 92, freq = 1661.2 Hz
midi = 93, freq = 1760.0 Hz
midi = 94, freq = 1864.7 Hz
midi = 95, freq = 1975.5 Hz
midi = 96, freq = 2093.0 Hz
midi = 97, freq = 2217.5 Hz
midi = 98, freq = 2349.3 Hz
midi = 99, freq = 2489.0 Hz
midi = 100, freq = 2637.0 Hz
midi = 101, freq = 2793.8 Hz
midi = 102, freq = 2960.0 Hz
midi = 103, freq = 3136.0 Hz
midi = 104, freq = 3322.4 Hz
midi = 105, freq = 3520.0 Hz
midi = 106, freq = 3729.3 Hz
midi = 107, freq = 3951.1 Hz
midi = 108, freq = 4186.0 Hz
midi = 109, freq = 4434.9 Hz
midi = 110, freq = 4698.6 Hz
midi = 111, freq = 4978.0 Hz
midi = 112, freq = 5274.0 Hz
midi = 113, freq = 5587.7 Hz
midi = 114, freq = 5919.9 Hz
midi = 115, freq = 6271.9 Hz
midi = 116, freq = 6644.9 Hz
midi = 117, freq = 7040.0 Hz
midi = 118, freq = 7458.6 Hz
midi = 119, freq = 7902.1 Hz
midi = 120, freq = 8372.0 Hz
midi = 121, freq = 8869.8 Hz
midi = 122, freq = 9397.3 Hz
midi = 123, freq = 9956.1 Hz
midi = 124, freq = 10548.1 Hz
midi = 125, freq = 11175.3 Hz
midi = 126, freq = 11839.8 Hz
midi = 127, freq = 12543.9 Hz

No comments:

Post a Comment