Sha256: ad485a1f58e789d32b8b4f24f15a2535b4085745c87edfb27bab2311e6dfa9c1

Contents?: true

Size: 956 Bytes

Versions: 3

Compression:

Stored size: 956 Bytes

Contents

class HeadMusic::Interval
  include Comparable

  private_class_method :new

  NAMES = %w{perfect_unison minor_second major_second minor_third major_third perfect_fourth tritone perfect_fifth minor_sixth major_sixth minor_seventh major_seventh perfect_octave}

  attr_reader :semitones

  def self.get(semitones)
    @intervals ||= {}
    @intervals[semitones.to_i] ||= new(semitones.to_i)
  end

  def self.named(name)
    name = name.to_s
    get(NAMES.index(name)) if NAMES.include?(name)
  end

  def initialize(semitones)
    @semitones = semitones
  end

  def simplified
    HeadMusic::Interval.get(semitones % 12)
  end

  def simple?
    (0..12).include?(semitones)
  end

  def compound?
    semitones > 12
  end

  def to_i
    semitones
  end

  def +(value)
    HeadMusic::Interval.get(to_i + value.to_i)
  end

  def -(value)
    HeadMusic::Interval.get((to_i - value.to_i).abs)
  end

  def <=>(other)
    self.to_i <=> other.to_i
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
head_music-0.4.0 lib/head_music/interval.rb
head_music-0.3.1 lib/head_music/interval.rb
head_music-0.3.0 lib/head_music/interval.rb