class HeadMusic::ChromaticInterval

A chromatic interval is the distance between two pitches measured in half-steps.

Constants

NAMES

TODO: include the Named module

Attributes

semitones[R]
specific_interval[R]

Public Class Methods

get(identifier) click to toggle source
# File lib/head_music/chromatic_interval.rb, line 17
def self.get(identifier)
  @intervals ||= {}
  candidate = identifier.to_s.downcase.gsub(/\W+/, '_')
  semitones = NAMES.index(candidate) || identifier.to_i
  @intervals[semitones] ||= new(semitones.to_i)
end
new(semitones) click to toggle source
# File lib/head_music/chromatic_interval.rb, line 24
def initialize(semitones)
  @semitones = semitones
end

Public Instance Methods

+(other) click to toggle source
# File lib/head_music/chromatic_interval.rb, line 51
def +(other)
  HeadMusic::ChromaticInterval.get(to_i + other.to_i)
end
-(other) click to toggle source
# File lib/head_music/chromatic_interval.rb, line 55
def -(other)
  HeadMusic::ChromaticInterval.get((to_i - other.to_i).abs)
end
<=>(other) click to toggle source
# File lib/head_music/chromatic_interval.rb, line 59
def <=>(other)
  to_i <=> other.to_i
end
compound?() click to toggle source
# File lib/head_music/chromatic_interval.rb, line 36
def compound?
  semitones > 12
end
diatonic_name() click to toggle source
# File lib/head_music/chromatic_interval.rb, line 44
def diatonic_name
  NAMES[simple.semitones].gsub(/_/, ' ')
end
simple() click to toggle source
# File lib/head_music/chromatic_interval.rb, line 28
def simple
  HeadMusic::ChromaticInterval.get(semitones % 12)
end
simple?() click to toggle source
# File lib/head_music/chromatic_interval.rb, line 32
def simple?
  (0..12).cover?(semitones)
end
to_i() click to toggle source
# File lib/head_music/chromatic_interval.rb, line 40
def to_i
  semitones
end