Skip to content

Instantly share code, notes, and snippets.

@unclejamal
Created May 29, 2014 22:15
Show Gist options
  • Save unclejamal/85e714c2d2ecbb4d1884 to your computer and use it in GitHub Desktop.
Save unclejamal/85e714c2d2ecbb4d1884 to your computer and use it in GitHub Desktop.
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