50 lines
1 KiB
Python
Executable file
50 lines
1 KiB
Python
Executable file
#!/usr/bin/python
|
|
|
|
A = 440
|
|
notes = {
|
|
'C' : 0,
|
|
'D' : 2,
|
|
'E' : 4,
|
|
'F' : 5,
|
|
'G' : 7,
|
|
'A' : 9,
|
|
'B' : 11,
|
|
}
|
|
|
|
rel = (2**(1/12.))
|
|
def note2freq(name):
|
|
sharp = name[1] == '#'
|
|
flat = name[1] == 'b'
|
|
if sharp or flat:
|
|
note = name[0:1]
|
|
octave = int(name[2])
|
|
else:
|
|
note = name[0]
|
|
octave = int(name[1])
|
|
num = notes[note]
|
|
if sharp:
|
|
num += 1
|
|
if flat:
|
|
num -= 1
|
|
fullnum = num + 12*(octave - 5)
|
|
return A*rel**(fullnum + 3)
|
|
|
|
if __name__ == '__main__':
|
|
test_fmt = '{:3} gave {: 9.2f} Hz\nexpected {: 9.2f} Hz'
|
|
def test(name, expected):
|
|
print(test_fmt.format(name, note2freq(name), expected))
|
|
|
|
test('C2' , 65.41)
|
|
test('Ab4', 415.30)
|
|
test('A4' , 440.00)
|
|
test('B4' , 493.88)
|
|
test('B#4', 523.25)
|
|
test('Cb5', 493.88)
|
|
test('C5' , 523.25)
|
|
print()
|
|
test('E4' , 329.63)
|
|
test('B3' , 246.94)
|
|
test('G3' , 196.00)
|
|
test('D3' , 146.83)
|
|
test('A2' , 110.00)
|
|
test('E2' , 82.41)
|