Created
May 29, 2014 22:15
-
-
Save unclejamal/85e714c2d2ecbb4d1884 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Roots | |
def self.rootn(x,n) | |
Math.exp(Math.log(x)/n) | |
end | |
end | |
module Notes | |
def self.c accidental, octave | |
Note.new(4, octave, accidental) | |
end | |
def self.e accidental, octave | |
Note.new(8, octave, accidental) | |
end | |
end | |
Note = Struct.new(:base_pitch, :octave, :accidental) do | |
def pitch | |
simple_pitch = base_pitch + (octave-1) * 12 | |
return simple_pitch + 1 if accidental == :sharp | |
return simple_pitch - 1 if accidental == :flat | |
simple_pitch | |
end | |
def frequency | |
freq = (Roots.rootn(2, 12) ** (pitch - 49) ) * 440 | |
freq.round(4) | |
end | |
end | |
module Interval | |
def self.between first, second | |
case second.pitch - first.pitch | |
when 4 | |
return :major_third | |
when 3 | |
return :minor_third | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment