chord/z
2024-05-14 12:58:17 -05:00

68 lines
1.1 KiB
Ruby

#!/usr/bin/ruby
# Prototype for converting note names to
# frequencies
def raise_semitone(freq,nsemitones)
semitone_constant=2**(1/12.0) #1.059463
freq*semitone_constant**nsemitones
end
def getfreq(name)
a0=27.5
semi=0
semi_name={'a'=>0,'b'=>2,'c'=>3,'d'=>5,'e'=>7,'f'=>8,'g'=>10}
str_pos=1
if name[1]=='#'
semi+=1
str_pos=2
elsif name[1]=='b'
semi-=1
str_pos=2
end
oct=name[str_pos..].to_i
if name[0]!='a'&&name[0]!='b'
oct=(oct>0)?(oct-1):(0)
end
semi+=semi_name[name[0]]+oct*12
freq=raise_semitone(a0,semi)
freq
end
def test name,correct
freq=getfreq name
if freq.to_i==correct
s=""
else
s="\t[FAIL: #{correct}]"
end
print "'#{name}':\t#{freq.to_i}#{s}\n"
end
def main
test 'a0',27
test 'a1',55
test 'a2',110
test 'a3',220
test 'a4',440
test 'a#4',466
test 'bb4',466
test 'a5',880
test 'a6',1760
test 'c3',130
test 'g6',1567
test 'e1',41
test 'e2',82
test 'e3',164
test 'e4',329
test 'e5',659
test 'e6',1318
test 'f#4',369
test 'd7',2349
# test 'a10',99999
end
main